HerkuleXLib  1.1
Arduino library to drive HerkuleX DRS-0101 and DRS-0201
Public Member Functions | Private Member Functions | Private Attributes | List of all members
HkxCommunication Class Reference

Class to manage the communication. More...

#include <HkxCommunication.h>

Public Member Functions

 HkxCommunication (hkxBaudrate baudrate, HardwareSerial &serialServos, HkxPrint &print)
 Constructor. More...
 
uint8_t readRequest (uint8_t ID, hkxMemory mem, uint8_t start, uint8_t length, uint8_t returnData[], HkxMaybe< HkxStatus > statusED)
 Read request. More...
 
uint8_t writeRequest (uint8_t ID, hkxMemory mem, uint8_t start, uint8_t length, const uint8_t writeData[])
 Write request. More...
 
uint8_t statusRequest (uint8_t ID, HkxStatus &statusED)
 Status request. More...
 
uint8_t iJogRequest (uint8_t length, const uint16_t jog[], const uint8_t set[], const uint8_t ID[], const uint8_t playTime[])
 I_JOG request. More...
 
uint8_t sJogRequest (uint8_t playTime, uint8_t length, const uint16_t jog[], const uint8_t set[], const uint8_t ID[])
 S_JOG request. More...
 
void start (hkxBaudrate newBaudrate)
 Start communication. More...
 
void stop ()
 Stop communication. More...
 
uint8_t rebootRequest (uint8_t ID)
 Reboot request. More...
 
uint8_t rollbackRequest (uint8_t ID, boolean IDskip, boolean bandSkip)
 Roll-back request. More...
 
HardwareSerial & getSerial ()
 Get the Serial. More...
 

Private Member Functions

void sendPacket (uint8_t ID, hkxCommand CMD, const uint8_t data[], uint8_t dataLength)
 Send a packet. More...
 
uint8_t receivePacket (uint8_t &dataLength)
 Receive a packet. More...
 
uint8_t readPacket (uint8_t &ID, hkxCommand &CMD, uint8_t data[], uint8_t dataLength, HkxMaybe< HkxStatus > statusED)
 Read the received packet. More...
 
uint8_t checkSum1 (uint8_t packetSize, uint8_t ID, hkxCommand CMD, const uint8_t data[], uint8_t dataLength)
 Checksum1. More...
 
uint8_t checkSum2 (uint8_t checkSum1)
 Checksum2. More...
 
void reinitPacketRead ()
 Reinitialization of _packetRead. More...
 
void cleanSerialBuffer ()
 Clean the buffer. More...
 
void errorPrint (const String &message)
 Print error. More...
 
void warningPrint (const String &message)
 Print warning. More...
 
void infoPrint (const String &message)
 Print info. More...
 

Private Attributes

uint8_t _packetRead [HKX_SERIAL_BUFFER]
 
HardwareSerial & _serialServos
 
HkxPrint_print
 
const String _className
 

Detailed Description

Class to manage the communication.

This class manages the communications with the servos. It manages some possible communications problems and proposes simple functions to send requests to the servos.

Constructor & Destructor Documentation

HkxCommunication::HkxCommunication ( hkxBaudrate  baudrate,
HardwareSerial &  serialServos,
HkxPrint print 
)

Constructor.

The constructor of HkxCommunication.

Parameters
[in]baudrate: Value of the baud rate to communicate with the servos. The same value shall be setup in the servos. See below the available values
---------------------------------------------------------------------------------------------------------
| hkxBaudrate | HKX_57600 | HKX_115200 | HKX_200000 | HKX_250000 | HKX_400000 | HKX_500000 | HKX_666666 |
---------------------------------------------------------------------------------------------------------
|  baud rate  |   57600   |   115200   |   200000   |   250000   |   400000   |   500000   |   666666   |
--------------------------------------------------------------------------------------------------------- 
Warning
From experience, the baud rate 57600 does NOT work properly. Please avoid using it.
Parameters
[in]serialServos: Serial port to use to communicate with the servos. It could be Serial, Serial1, Serial2 or Serial3 depending on the Arduino board and the wiring.
[in]print: Communication to print messages

Example:

HkxPrint print = HkxPrint(Serial, 9600);
HkxCommunication communication = HkxCommunication(HKX_115200, Serial1, print);

Member Function Documentation

uint8_t HkxCommunication::checkSum1 ( uint8_t  packetSize,
uint8_t  ID,
hkxCommand  CMD,
const uint8_t  data[],
uint8_t  dataLength 
)
inlineprivate

Checksum1.

Calculation of the Checksum1 for the packet communication integrity check.

\[ checksum1 = (packetSize \wedge ID \wedge CMD \wedge data[0] \wedge data[1] \wedge ... \wedge data[n]) \& 0xFE \]

$ A \wedge B $ is the bit exclusive OR operator (XOR).

Parameters
[in]packetSize: size of the packet.
[in]ID: id of the servo who sent the packet. The value shall be [0 ; 254] (254 means all servos).
[in]CMD: Instruction of the packet:
  • HKX_CMD_ROM_WRITE: write registers in the ROM (non-volatile),
  • HKX_CMD_ROM_READ: read registers in the ROM (non-volatile),
  • HKX_CMD_RAM_WRITE: write registers in the RAM (volatile),
  • HKX_CMD_RAM_READ: read registers in the RAM (volatile),
  • HKX_CMD_I_JOG: move the servos with individual playtime for each (asynchronous),
  • HKX_CMD_S_JOG: move the servos with the same playtime for all (synchronous),
  • HKX_CMD_STAT: read the status,
  • HKX_CMD_ROLLBACK: reset to factory default,
  • HKX_CMD_REBOOT: reboot.
[in]data[]: Array of bytes containing data.
[in]dataLength: Length of the data array.
Returns
the checksum1 value
uint8_t HkxCommunication::checkSum2 ( uint8_t  checkSum1)
inlineprivate

Checksum2.

Calculation of the Checksum2 for the packet communication integrity check.

\[ checksum2 = (~checksum1) \& 0xFE \]

$ ~A $ is the NOT operator.

Parameters
[in]checkSum1: result obtained with checkSum1().
Returns
the checksum2 value
void HkxCommunication::cleanSerialBuffer ( )
inlineprivate

Clean the buffer.

Clean the buffer of the serial to avoid asynchronised packets.

void HkxCommunication::errorPrint ( const String &  message)
inlineprivate

Print error.

Print an error message if setup.

Parameters
[in]message: content to print as an error.
HardwareSerial& HkxCommunication::getSerial ( )
inline

Get the Serial.

Return a reference to the Serial port used for the communication with the servos.

Returns
reference to the Serial port used for the communication with the servos.
uint8_t HkxCommunication::iJogRequest ( uint8_t  length,
const uint16_t  jog[],
const uint8_t  set[],
const uint8_t  ID[],
const uint8_t  playTime[] 
)

I_JOG request.

Individual JOG: move the servos with individual playtime for each (asynchronous).

Parameters
[in]length: Number of servos to move.
[in]jog[]: Array with size of length of jogs for each servo:
  • if mode is position control: jog = goal position [0 ; 1023],
  • if mode is continuous rotation: jog = PWM [0 ; 1023] + bit 14 for the sign.
[in]set[]: array with size of length of sets for each servo:
[in]ID[]: Array with size of length of ids of the servo to address the request. The values shall be [0 ; 253].
[in]playTime[]: Array with size of length of the playtime of each servo.
Returns
0 = OK
1 = bad input parameter
void HkxCommunication::infoPrint ( const String &  message)
inlineprivate

Print info.

Print an info message if setup.

Parameters
[in]message: content to print as an info.
uint8_t HkxCommunication::readPacket ( uint8_t &  ID,
hkxCommand CMD,
uint8_t  data[],
uint8_t  dataLength,
HkxMaybe< HkxStatus statusED 
)
private

Read the received packet.

Read the received packet from the servo communication port.

Warning
Prior using this function, the function ReceivePacket() shall be executed
Parameters
[out]ID: id of the servo who sent the packet.
[out]CMD: Instruction that is answered:
  • HKX_CMD_ROM_WRITE: write registers in the ROM (non-volatile),
  • HKX_CMD_ROM_READ: read registers in the ROM (non-volatile),
  • HKX_CMD_RAM_WRITE: write registers in the RAM (volatile),
  • HKX_CMD_RAM_READ: read registers in the RAM (volatile),
  • HKX_CMD_I_JOG: move the servos with individual playtime for each (asynchronous),
  • HKX_CMD_S_JOG: move the servos with the same playtime for all (synchronous),
  • HKX_CMD_STAT: read the status,
  • HKX_CMD_ROLLBACK: reset to factory default,
  • HKX_CMD_REBOOT: reboot.
[out]data[]: Array of bytes containing data.
[out]dataLength: Length of the data array.
[out]statusED: Current status of the servo. This parameter is optional either set the address of a HkxStatus variable to get the status, or set HKX_NO_VALUE to ignore it.
Returns
0 if the process went well
1 if dataLength is not consistent with the packet size
2 if no data received
uint8_t HkxCommunication::readRequest ( uint8_t  ID,
hkxMemory  mem,
uint8_t  start,
uint8_t  length,
uint8_t  returnData[],
HkxMaybe< HkxStatus statusED 
)

Read request.

Read the registers of the servo

Parameters
[in]ID: id of the servo to address the request. The value shall be [0 ; 253].
[in]mem: Memory type:
  • ROM: non-volatile memory
  • RAM: volatile memory
[in]start: Address of the register to read.
[in]length: Length (number of) registers to read.
[out]returnData[]: Array with size of length to return the read registers.
[out]statusED: Current status of the servo. This parameter is optional, either set the address of a HkxStatus variable to get the status, or set HKX_NO_VALUE to ignore it.
Returns
0 = OK
1 = bad input parameter
2 = no data received
3 = received data are not consistent
uint8_t HkxCommunication::rebootRequest ( uint8_t  ID)
inline

Reboot request.

Reboot the servo

Parameters
[in]ID: id of the servo to address the request. The value shall be [0 ; 254] (254 means all servos).
Returns
0 = OK
1 = bad input parameter
uint8_t HkxCommunication::receivePacket ( uint8_t &  dataLength)
private

Receive a packet.

Receive a packet from the servo communication port.

Parameters
[out]dataLength: Length of the data array of the received packet.
Returns
0 if the process went well
1 if headers not found
2 if packet size not found
3 if not enough data available
4 if checksum error
void HkxCommunication::reinitPacketRead ( )
inlineprivate

Reinitialization of _packetRead.

Reinitialization of the _packetRead array. Set all the values to 0;

uint8_t HkxCommunication::rollbackRequest ( uint8_t  ID,
boolean  IDskip,
boolean  bandSkip 
)
inline

Roll-back request.

Reset the servo to factory default.

Parameters
[in]ID: id of the servo to address the request. The value shall be [0 ; 254] (254 means all servos).
[in]IDskip: Set false to reset also ID to factory.
[in]bandSkip: Set false if reset also baud rate to factory.
Returns
0 = OK
1 = bad input parameter
void HkxCommunication::sendPacket ( uint8_t  ID,
hkxCommand  CMD,
const uint8_t  data[],
uint8_t  dataLength 
)
private

Send a packet.

Send a packet through the servo communication port.

Parameters
[in]ID: id of the servo to who the packet is addressed. The value should be [0 ; 254] (254 means all servos).
[in]CMD: Instruction command:
  • HKX_CMD_ROM_WRITE: write registers in the ROM (non-volatile),
  • HKX_CMD_ROM_READ: read registers in the ROM (non-volatile),
  • HKX_CMD_RAM_WRITE: write registers in the RAM (volatile),
  • HKX_CMD_RAM_READ: read registers in the RAM (volatile),
  • HKX_CMD_I_JOG: move the servos with individual playtime for each (asynchronous),
  • HKX_CMD_S_JOG: move the servos with the same playtime for all (synchronous),
  • HKX_CMD_STAT: read the status,
  • HKX_CMD_ROLLBACK: reset to factory default,
  • HKX_CMD_REBOOT: reboot.
[in]data[]: Array of bytes containing data according to the CMD.
[in]dataLength: Length of the data array.
uint8_t HkxCommunication::sJogRequest ( uint8_t  playTime,
uint8_t  length,
const uint16_t  jog[],
const uint8_t  set[],
const uint8_t  ID[] 
)

S_JOG request.

Several JOG: move the servos with the same playtime for all (synchronous).

Parameters
[in]playTime: Playtime for all servos.
[in]length: Number of servos to move.
[in]jog[]: Array with size of length of jogs for each servo:
  • if mode is position control: jog = goal position [0 ; 1023],
  • if mode is continuous rotation: jog = PWM [0 ; 1023] + bit 14 for the sign.
[in]set[]: array with size of length of sets for each servo:
[in]ID[]: Array with size of length of ids of the servo to address the request. The values shall be [0 ; 253].
Returns
0 = OK
1 = bad input parameter
void HkxCommunication::start ( hkxBaudrate  newBaudrate)
inline

Start communication.

Start the communication with the servos.

Parameters
[in]newBaudrate: Value of the baud rate to communicate with the servos. The same value shall be setup in the servos. See below the available values
---------------------------------------------------------------------------------------------------------
| hkxBaudrate | HKX_57600 | HKX_115200 | HKX_200000 | HKX_250000 | HKX_400000 | HKX_500000 | HKX_666666 |
---------------------------------------------------------------------------------------------------------
|  baud rate  |   57600   |   115200   |   200000   |   250000   |   400000   |   500000   |   666666   |
--------------------------------------------------------------------------------------------------------- 
Warning
From experience, the baud rate 57600 does NOT work properly. Please avoid using it.
uint8_t HkxCommunication::statusRequest ( uint8_t  ID,
HkxStatus statusED 
)

Status request.

Read the status of the servo

Parameters
[in]ID: id of the servo to address the request. The value shall be [0 ; 253].
[out]statusED: Current status of the servo.
Returns
0 = OK
1 = bad input parameter
2 = no data received
3 = received data are not consistent
void HkxCommunication::stop ( )
inline

Stop communication.

Stop the communication with the servos.

void HkxCommunication::warningPrint ( const String &  message)
inlineprivate

Print warning.

Print a warning message if setup.

Parameters
[in]message: content to print as a warning.
uint8_t HkxCommunication::writeRequest ( uint8_t  ID,
hkxMemory  mem,
uint8_t  start,
uint8_t  length,
const uint8_t  writeData[] 
)

Write request.

Write the registers of the servo

Parameters
[in]ID: id of the servo to address the request. The value shall be [0 ; 254] (254 means all servos).
[in]mem: Memory type:
  • ROM: non-volatile memory
  • RAM: volatile memory
[in]start: Address of the register to read.
[in]length: Length (number of) registers to read.
[in]writeData[]: Array with size of length to write registers.
Returns
0 = OK
1 = bad input parameter

Member Data Documentation

const String HkxCommunication::_className
private

Class name to print the messages

uint8_t HkxCommunication::_packetRead[HKX_SERIAL_BUFFER]
private

Allocate longest possible size of packet to be read

HkxPrint& HkxCommunication::_print
private

Communication to print messages

HardwareSerial& HkxCommunication::_serialServos
private

Serial port to communicate with the servos


The documentation for this class was generated from the following files: