1 #ifndef _HKXCOMMUNICATION_H
2 #define _HKXCOMMUNICATION_H
73 HKX_CMD_ROM_WRITE = 0x01,
74 HKX_CMD_ROM_READ = 0x02,
75 HKX_CMD_RAM_WRITE = 0x03,
76 HKX_CMD_RAM_READ = 0x04,
80 HKX_CMD_ROLLBACK = 0x08,
81 HKX_CMD_REBOOT = 0x09,
128 HKX_TORQUE_FREE = 0x00,
129 HKX_TORQUE_BREAK = 0x40,
130 HKX_TORQUE_ON = 0x60,
148 HKX_LED_GREEN = 0x01,
152 HKX_LED_YELLOW = 0x05,
154 HKX_LED_WHITE = 0x07,
165 HKX_CTRL_POSITION = 0x00,
166 HKX_CTRL_ROTATION = 0x01,
171 static const int16_t temperatureConversion[] = {-7947,-7178,-6320,-5781,-5380,-5058,-4786,-4549,-4340,-4151,-3979,-3820,-3673,-3535,
172 -3406,-3283,-3167,-3057,-2951,-2850,-2753,-2659,-2569,-2482,-2397,-2315,-2236,-2159,-2083,-2010,-1938,-1868,-1800,-1733,-1667,-1603,
173 -1539,-1477,-1417,-1357,-1298,-1240,-1183,-1126,-1071,-1016,-962,-909,-856,-804,-753,-702,-652,-602,-553,-504,-456,-408,-361,-314,
174 -267,-221,-175,-129,-84,-39,5,49,93,137,181,224,267,310,352,394,437,478,520,562,603,644,686,727,767,808,849,889,929,970,1010,1050,
175 1090,1130,1170,1209,1249,1289,1328,1368,1407,1447,1486,1526,1565,1605,1644,1684,1723,1762,1802,1841,1881,1920,1960,1999,2039,2079,
176 2119,2158,2198,2238,2278,2318,2359,2399,2439,2480,2520,2561,2602,2643,2684,2725,2766,2808,2850,2891,2933,2976,3018,3060,3103,3146,
177 3189,3232,3276,3320,3364,3408,3453,3497,3542,3588,3633,3679,3725,3772,3818,3866,3913,3961,4009,4057,4106,4156,4205,4256,4306,4357,
178 4409,4461,4513,4566,4619,4673,4728,4783,4839,4895,4952,5009,5068,5127,5186,5247,5308,5370,5433,5496,5561,5626,5693,5760,5828,5898,
179 5968,6040,6113,6187,6263,6339,6417,6497,6578,6861,6746,6832,6920,7010,7102,7196,7292,7391,7492,7596,7703,7812,7925,8041,8160,8284,
180 8411,8542,8679,8820,8966,9118,9276,9441,9613,9793,9982,10181,10390,10611,10845,11093,11359,11643,11949,12280,12641,13036,13472,
181 13959,14509,15139,15873,16750,17829,19218,21132,24101,30091
201 template <
typename T>
231 HkxMaybe(T value) : _empty(false), _value(new T(value)){};
238 HkxMaybe(T* addressValue) : _empty(false), _value(addressValue){};
269 void setValue(T value) {
if(!_empty){*_value = value;}};
299 serialPrint.begin(baudrate);
300 infoPrint(F(
"start > Start the communication for printing"));
316 serialPrint.begin(baudrate);
317 infoPrint(F(
"start > Start the communication for printing"));
321 #if defined (__AVR_ATmega32U4__)
332 serialPrint.begin(baudrate);
333 infoPrint(F(
"start > Start the communication for printing"));
349 serialPrint.begin(baudrate);
350 infoPrint(F(
"start > Start the communication for printing"));
463 uint8_t temperatureRaw = 128;
466 while((temperatureConversion[temperatureRaw] != temperatureValue) && ((sup - inf) > 1) ){
467 if(temperatureConversion[temperatureRaw] > temperatureValue) {sup = temperatureRaw;}
468 else {inf = temperatureRaw;}
469 temperatureRaw = (sup + inf)/2;
471 return temperatureRaw;
481 static int16_t
angleRawToValue(int16_t angleRaw){
return ((int32_t)angleRaw)*326/100;};
490 static int16_t
angleValueToRaw(int16_t angleValue){
return ((int32_t)angleValue*100/326)+(angleValue<0?-1:(angleValue>0?1:0));};
526 static int16_t
velocityValueRaw(int16_t velocityValue){
return ((int32_t)velocityValue)*100/2909+(velocityValue<0?-1:(velocityValue>0?1:0));};
535 static uint16_t
timeRawToValue(uint8_t timeRaw){
return ((uint16_t)timeRaw*112)/10;};
544 static uint8_t
timeValueToRaw(uint16_t timeValue){
return (uint8_t)((timeValue*10)/112)+1;};
557 static uint16_t
slopeRawToValue(uint16_t slopeRaw){
return ((uint32_t)(slopeRaw?slopeRaw:32768)*1000)/835;};
571 uint16_t slopeRaw = (((uint32_t)slopeValue*835)/1000) + 1;
572 return slopeRaw>32767?0:slopeRaw;
591 switch (baudrateRaw){
592 case 0x02 :
return HKX_666666;
593 case 0x03 :
return HKX_500000;
594 case 0x04 :
return HKX_400000;
595 case 0x07 :
return HKX_250000;
596 case 0x09 :
return HKX_200000;
597 case 0x10 :
return HKX_115200;
598 case 0x22 :
return HKX_57600; }
618 case HKX_666666 :
return 0x02;
619 case HKX_500000 :
return 0x03;
620 case HKX_400000 :
return 0x04;
621 case HKX_250000 :
return 0x07;
622 case HKX_200000 :
return 0x09;
623 case HKX_115200 :
return 0x10;
624 case HKX_57600 :
return 0x22; }
724 uint8_t
checkSum1(uint8_t packetSize, uint8_t ID,
hkxCommand CMD,
const uint8_t data[], uint8_t dataLength){
725 uint8_t cks = packetSize ^ ID ^ CMD;
726 for(
int i = 0 ; i < dataLength ; i++){
868 uint8_t
iJogRequest(uint8_t length,
const uint16_t jog[],
const uint8_t set[],
const uint8_t ID[],
const uint8_t playTime[]);
888 uint8_t
sJogRequest(uint8_t playTime, uint8_t length,
const uint16_t jog[],
const uint8_t set[],
const uint8_t ID[]);
904 _serialServos.begin(newBaudrate);
905 infoPrint(F(
"start > Start the communication with the servos"));
913 this->_serialServos.end();
914 infoPrint(F(
"stop > Stop the communication with the servos"));
927 errorPrint(F(
"rebootRequest > input values not correct"));
947 errorPrint(F(
"rollbackRequest > input values not correct"));
950 uint8_t data[] = {IDskip?(uint8_t)1:(uint8_t)0 , bandSkip?(uint8_t)1:(uint8_t)0};
965 #endif // _HKXCOMMUNICATION_H
const uint8_t HKX_MAX_ID
Definition: HkxCommunication.h:29
Stream *const _serialPrint
Definition: HkxCommunication.h:417
uint8_t rebootRequest(uint8_t ID)
Reboot request.
Definition: HkxCommunication.h:925
const String _className
Definition: HkxCommunication.h:644
HkxPrint(HardwareSerial &serialPrint, long baudrate)
Active constructor with default values.
Definition: HkxCommunication.h:298
HkxMaybe(hkxMaybeHasNoValue noValue)
Empty constructor.
Definition: HkxCommunication.h:224
void errorPrint(const String &message)
Print error.
Definition: HkxCommunication.h:759
void warningPrint(const String &message) const
Print warning.
Definition: HkxCommunication.h:394
uint8_t readPacket(uint8_t &ID, hkxCommand &CMD, uint8_t data[], uint8_t dataLength, HkxMaybe< HkxStatus > statusED)
Read the received packet.
Definition: HkxCommunication.cpp:155
static uint8_t timeValueToRaw(uint16_t timeValue)
Time from value to raw.
Definition: HkxCommunication.h:544
HkxMaybe(T value)
Copy value constructor.
Definition: HkxCommunication.h:231
HkxCommunication(hkxBaudrate baudrate, HardwareSerial &serialServos, HkxPrint &print)
Constructor.
Definition: HkxCommunication.cpp:186
const uint8_t HKX_SERIAL_BUFFER
Definition: HkxCommunication.h:26
void errorPrint(const String &message) const
Print error.
Definition: HkxCommunication.h:381
void infoPrint(const String &message)
Print info.
Definition: HkxCommunication.h:773
const uint16_t HKX_MAX_CONTROL_PARAM
Definition: HkxCommunication.h:53
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.
Definition: HkxCommunication.cpp:382
Class to manage optional parameters.
Definition: HkxCommunication.h:202
HkxMaybe()
Default constructor.
Definition: HkxCommunication.h:214
HkxPrint()
Default constructor.
Definition: HkxCommunication.h:287
static int16_t velocityValueRaw(int16_t velocityValue)
Velocity from value to raw.
Definition: HkxCommunication.h:526
const uint16_t HKX_MAX_PWM
Definition: HkxCommunication.h:49
const uint8_t HKX_MAX_TEMPERATURE
Definition: HkxCommunication.h:35
bool isEmpty() const
Check if empty.
Definition: HkxCommunication.h:253
Status management of the Arduino library for HerkuleX servo (DRS-0101 and DRS-0201 models) ...
T *const _value
Definition: HkxCommunication.h:207
hkxBaudrate
Baud rate.
Definition: HkxCommunication.h:109
const boolean _infoMessages
Definition: HkxCommunication.h:415
const uint8_t HKX_MAX_CHECK_PERIOD
Definition: HkxCommunication.h:55
hkxTorqueControl
Torque control.
Definition: HkxCommunication.h:127
HardwareSerial & getSerial()
Get the Serial.
Definition: HkxCommunication.h:960
hkxCommand
Available commands.
Definition: HkxCommunication.h:72
const uint8_t HKX_MAX_ACCELERATION_RATIO
Definition: HkxCommunication.h:39
const uint8_t HKX_MAX_SATURATOR_OFFSET
Definition: HkxCommunication.h:45
Class to manage the communication.
Definition: HkxCommunication.h:634
Class to manage the status of the servos.
Definition: HkxStatus.h:55
uint8_t readRequest(uint8_t ID, hkxMemory mem, uint8_t start, uint8_t length, uint8_t returnData[], HkxMaybe< HkxStatus > statusED)
Read request.
Definition: HkxCommunication.cpp:194
static int16_t angleRawToValue(int16_t angleRaw)
Angle from raw to value.
Definition: HkxCommunication.h:481
static int16_t velocityRawToValue(int16_t velocityRaw)
Velocity from raw to value.
Definition: HkxCommunication.h:517
uint8_t sJogRequest(uint8_t playTime, uint8_t length, const uint16_t jog[], const uint8_t set[], const uint8_t ID[])
S_JOG request.
Definition: HkxCommunication.cpp:411
Class to manage unit conversions.
Definition: HkxCommunication.h:428
void setValue(T value)
Set the value.
Definition: HkxCommunication.h:269
HkxPrint(HardwareSerial &serialPrint, long baudrate, bool errorMessages, bool warningMessages, bool infoMessages)
Active constructor.
Definition: HkxCommunication.h:315
void stop()
Stop communication.
Definition: HkxCommunication.h:912
HkxPrint & _print
Definition: HkxCommunication.h:642
static int16_t angleValueToRaw(int16_t angleValue)
Angle from value to raw.
Definition: HkxCommunication.h:490
const uint8_t HKX_ID_ALL
Definition: HkxCommunication.h:31
const uint8_t HKX_NUMBER_CONNECT_TRIALS
Definition: HkxCommunication.h:24
const uint8_t HKX_MAX_POLICY
Definition: HkxCommunication.h:33
uint8_t receivePacket(uint8_t &dataLength)
Receive a packet.
Definition: HkxCommunication.cpp:51
static uint8_t baudrateValueToRaw(hkxBaudrate baudrate)
Baud rate from value to raw.
Definition: HkxCommunication.h:616
~HkxMaybe()
Default destructor.
Definition: HkxCommunication.h:244
hkxLEDControl
LED control.
Definition: HkxCommunication.h:146
const boolean _warningMessages
Definition: HkxCommunication.h:413
uint8_t _packetRead[HKX_SERIAL_BUFFER]
Definition: HkxCommunication.h:638
void start(hkxBaudrate newBaudrate)
Start communication.
Definition: HkxCommunication.h:903
uint8_t checkSum1(uint8_t packetSize, uint8_t ID, hkxCommand CMD, const uint8_t data[], uint8_t dataLength)
Checksum1.
Definition: HkxCommunication.h:724
T & getValue() const
Get the value.
Definition: HkxCommunication.h:262
static int16_t voltageRawToValue(uint8_t voltageRaw)
Voltage from raw to value.
Definition: HkxCommunication.h:437
const bool HKX_DEV
Definition: HkxCommunication.h:18
hkxMemory
Memory type.
Definition: HkxCommunication.h:91
const uint8_t HKX_WAIT_TIME
Definition: HkxCommunication.h:20
static uint16_t positionValueToRaw(int16_t positionValue)
Position from value to raw.
Definition: HkxCommunication.h:508
void cleanSerialBuffer()
Clean the buffer.
Definition: HkxCommunication.h:752
HkxMaybe(T *addressValue)
Pointer value constructor.
Definition: HkxCommunication.h:238
static int16_t temperatureRawToValue(uint8_t temperatureRaw)
Temperature from raw to value.
Definition: HkxCommunication.h:454
Class to manage the print of the messages.
Definition: HkxCommunication.h:278
HardwareSerial & _serialServos
Definition: HkxCommunication.h:640
uint8_t writeRequest(uint8_t ID, hkxMemory mem, uint8_t start, uint8_t length, const uint8_t writeData[])
Write request.
Definition: HkxCommunication.cpp:280
const uint8_t HKX_MAX_MIN_PWM
Definition: HkxCommunication.h:47
static uint16_t timeRawToValue(uint8_t timeRaw)
Time from raw to value.
Definition: HkxCommunication.h:535
void infoPrint(const String &message) const
Print info.
Definition: HkxCommunication.h:407
void reinitPacketRead()
Reinitialization of _packetRead.
Definition: HkxCommunication.h:746
static uint8_t temperatureValueToRaw(int16_t temperatureValue)
Temperature from value to raw.
Definition: HkxCommunication.h:462
const uint8_t HKX_MAX_TIME
Definition: HkxCommunication.h:41
static uint16_t slopeRawToValue(uint16_t slopeRaw)
Slope from raw to value.
Definition: HkxCommunication.h:557
static int16_t positionRawToValue(uint16_t positionRaw)
Position from raw to value.
Definition: HkxCommunication.h:499
uint8_t statusRequest(uint8_t ID, HkxStatus &statusED)
Status request.
Definition: HkxCommunication.cpp:310
const bool _empty
Definition: HkxCommunication.h:205
hkxControlMode
Control mode.
Definition: HkxCommunication.h:164
uint8_t checkSum2(uint8_t checkSum1)
Checksum2.
Definition: HkxCommunication.h:740
const uint8_t HKX_MAX_POSITIONNING
Definition: HkxCommunication.h:43
uint8_t rollbackRequest(uint8_t ID, boolean IDskip, boolean bandSkip)
Roll-back request.
Definition: HkxCommunication.h:945
const uint8_t HKX_NUMBER_SEND_TRIALS
Definition: HkxCommunication.h:22
hkxMaybeHasNoValue
No value.
Definition: HkxCommunication.h:191
static uint16_t slopeValueToRaw(uint16_t slopeValue)
Slope from value to raw.
Definition: HkxCommunication.h:570
const boolean _errorMessages
Definition: HkxCommunication.h:407
void sendPacket(uint8_t ID, hkxCommand CMD, const uint8_t data[], uint8_t dataLength)
Send a packet.
Definition: HkxCommunication.cpp:17
static uint8_t voltageValueToRaw(int16_t voltageValue)
Voltage from value to raw.
Definition: HkxCommunication.h:446
void warningPrint(const String &message)
Print warning.
Definition: HkxCommunication.h:766
static hkxBaudrate baudrateRawToValue(uint8_t baudrateRaw)
Baud rate from raw to value.
Definition: HkxCommunication.h:590
const uint8_t HKX_MAX_VOLTAGE
Definition: HkxCommunication.h:37
const uint16_t HKX_MAX_POSITION
Definition: HkxCommunication.h:51
HkxPrint(HkxPrint hkxPrint, bool errorMessages, bool warningMessages, bool infoMessages)
Copy constructor.
Definition: HkxCommunication.h:368