-
Notifications
You must be signed in to change notification settings - Fork 0
/
SavageDynamixelSerial_Upgraded.h
464 lines (415 loc) · 17.6 KB
/
SavageDynamixelSerial_Upgraded.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
Dynamixel.cpp - Ax-12+ Half Duplex USART Comunication
Copyright (c) 2011 Savage Electronics.
Created by Savage on 27/01/11.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************
Modifications:
25/07/2011 - Eliminado la modificacion serial para ser modificada dentro del mismo Hardware Serial.
25/07/2011 - Modificado la funcion setBD() para aceptar todas la velocidades sin PDF.
25/07/2011 - Agregada la funcion de Rotacion Continua.
26/07/2011 - Agregada la funcion begin sin seteo de Direction_Pin.
25/07/2011 - Agregada la funcion Reset.
26/07/2011 - Agregada la funcion Reg_Write en move y moveSpeed.
26/07/2011 - Agregada la funcion Action.
13/12/2011 - Arreglado el manejo y envio de variables.
22/12/2011 - Compatible con la actualizacion Arduino 1.0.
10/01/2012 - Utilizacion de Macros y eliminacion codigo no necesario.
11/01/2012 - Agregadas las funciones:
int setTempLimit(unsigned char ID, unsigned char Temperature);
int setAngleLimit(unsigned char ID, int CWLimit, int CCWLimit);
int setVoltageLimit(unsigned char ID, unsigned char DVoltage, unsigned char UVoltage);
int setMaxTorque(unsigned char ID, int MaxTorque);
int setSRL(unsigned char ID, unsigned char SRL);
int setRDT(unsigned char ID, unsigned char RDT);
int setLEDAlarm(unsigned char ID, unsigned char LEDAlarm);
int setShutdownAlarm(unsigned char ID, unsigned char SALARM);
int setCMargin(unsigned char ID, unsigned char CWCMargin, unsigned char CCWCMargin);
int setCSlope(unsigned char ID, unsigned char CWCSlope, unsigned char CCWCSlope);
15/01/2012 - Agregadas las funciones:
int setPunch(unsigned char ID, int Punch);
int moving(unsigned char ID);
int lockRegister(unsigned char ID);
int RWStatus(unsigned char ID);
int readSpeed(unsigned char ID);
int readLoad(unsigned char ID);
TODO:
FUNCION SYNCWRITE.
*****************************************************************************
Contact: savageelectronics@gmail.com
Web: http://savageelectrtonics.blogspot.com/
Autor: Josue Alejandro Savage
*****************************************************************************
Modified by FCaminade
* 14/10/2017 :Extension for protocol 2.0. Add of DynamixelXClass
*/
#ifndef DynamixelSerial_h
#define DynamixelSerial_h
//////////////////////////PROTOCOL 1.0//////////////////////////////////////
// EEPROM AREA ///////////////////////////////////////////////////////////
#define AX_MODEL_NUMBER_L 0
#define AX_MODEL_NUMBER_H 1
#define AX_VERSION 2
#define AX_ID 3
#define AX_BAUD_RATE 4
#define AX_RETURN_DELAY_TIME 5
#define AX_CW_ANGLE_LIMIT_L 6
#define AX_CW_ANGLE_LIMIT_H 7
#define AX_CCW_ANGLE_LIMIT_L 8
#define AX_CCW_ANGLE_LIMIT_H 9
#define AX_SYSTEM_DATA2 10
#define AX_LIMIT_TEMPERATURE 11
#define AX_DOWN_LIMIT_VOLTAGE 12
#define AX_UP_LIMIT_VOLTAGE 13
#define AX_MAX_TORQUE_L 14
#define AX_MAX_TORQUE_H 15
#define AX_RETURN_LEVEL 16
#define AX_ALARM_LED 17
#define AX_ALARM_SHUTDOWN 18
#define AX_OPERATING_MODE 19
#define AX_DOWN_CALIBRATION_L 20
#define AX_DOWN_CALIBRATION_H 21
#define AX_UP_CALIBRATION_L 22
#define AX_UP_CALIBRATION_H 23
// RAM AREA //////////////////////////////////////////////////////////////
#define AX_TORQUE_ENABLE 24
#define AX_LED 25
#define AX_CW_COMPLIANCE_MARGIN 26
#define AX_CCW_COMPLIANCE_MARGIN 27
#define AX_CW_COMPLIANCE_SLOPE 28
#define AX_CCW_COMPLIANCE_SLOPE 29
#define AX_GOAL_POSITION_L 30
#define AX_GOAL_POSITION_H 31
#define AX_GOAL_SPEED_L 32
#define AX_GOAL_SPEED_H 33
#define AX_TORQUE_LIMIT_L 34
#define AX_TORQUE_LIMIT_H 35
#define AX_PRESENT_POSITION_L 36
#define AX_PRESENT_POSITION_H 37
#define AX_PRESENT_SPEED_L 38
#define AX_PRESENT_SPEED_H 39
#define AX_PRESENT_LOAD_L 40
#define AX_PRESENT_LOAD_H 41
#define AX_PRESENT_VOLTAGE 42
#define AX_PRESENT_TEMPERATURE 43
#define AX_REGISTERED_INSTRUCTION 44
#define AX_PAUSE_TIME 45
#define AX_MOVING 46
#define AX_LOCK 47
#define AX_PUNCH_L 48
#define AX_PUNCH_H 49
// Status Return Levels ///////////////////////////////////////////////////////////////
#define AX_RETURN_NONE 0
#define AX_RETURN_READ 1
#define AX_RETURN_ALL 2
// Instruction Set ///////////////////////////////////////////////////////////////
#define AX_PING 1
#define AX_READ_DATA 2
#define AX_WRITE_DATA 3
#define AX_REG_WRITE 4
#define AX_ACTION 5
#define AX_RESET 6
#define AX_SYNC_WRITE 131
// Specials ///////////////////////////////////////////////////////////////
#define OFF 0
#define ON 1
#define LEFT 0
#define RIGTH 1
#define AX_BYTE_READ 1
#define AX_BYTE_READ_POS 2
#define AX_RESET_LENGTH 2
#define AX_ACTION_LENGTH 2
#define AX_ID_LENGTH 4
#define AX_LR_LENGTH 4
#define AX_SRL_LENGTH 4
#define AX_RDT_LENGTH 4
#define AX_LEDALARM_LENGTH 4
#define AX_SALARM_LENGTH 4
#define AX_TL_LENGTH 4
#define AX_VL_LENGTH 6
#define AX_CM_LENGTH 6
#define AX_CS_LENGTH 6
#define AX_CCW_CW_LENGTH 8
#define AX_BD_LENGTH 4
#define AX_TEM_LENGTH 4
#define AX_MOVING_LENGTH 4
#define AX_RWS_LENGTH 4
#define AX_VOLT_LENGTH 4
#define AX_LED_LENGTH 4
#define AX_TORQUE_LENGTH 4
#define AX_POS_LENGTH 4
#define AX_GOAL_LENGTH 5
#define AX_MT_LENGTH 5
#define AX_PUNCH_LENGTH 5
#define AX_SPEED_LENGTH 5
#define AX_GOAL_SP_LENGTH 7
#define AX_ACTION_CHECKSUM 250
#define BROADCAST_ID 254
#define AX_START 255
#define AX_CCW_AL_L 255
#define AX_CCW_AL_H 3
#define TIME_OUT 200 //500000 // Este parametro depende de la velocidad de transmision
#define TX_DELAY_TIME 1 //1us // Este parametro depende de la velocidad de transmision - pero pueden ser cambiados para mayor velocidad.
#define Tx_MODE 1
#define Rx_MODE 0
#define LOCK 1
//////////////////////////PROTOCOL 2.0//////////////////////////////////////
// EEPROM AREA ///////////////////////////////////////////////////////////
#define XM_BAUD_RATE 8
#define XM_LED 65
// RAM AREA //////////////////////////////////////////////////////////////
#define XM_RETURN_DELAY_TIME 9
#define XM_TORQUE_ENABLE 64
#define XM_PROFILE_ACC 108
#define XM_PROFILE_VEL 112
#define XM_GOAL_POSITION 116
#define XM_PRESENT_CURRENT 126
#define XM_PRESENT_POSITION 132
// Status Return Levels ///////////////////////////////////////////////////////////////
// Instruction Set ///////////////////////////////////////////////////////////////
#define XM_PING 1
#define XM_READ 2
#define XM_WRITE 3
#define XM_SYNC_WRITE 131
#define XM_SYNC_READ 130
// Specials ///////////////////////////////////////////////////////////////
#define XM_START_FF 255
#define XM_START_FD 253
#define XM_0 0
#define XM_RDT_LENGTH 6
#define XM_BD_LENGTH 6
#define XM_TORQUE_LENGTH 6
#define XM_GOAL_LENGTH 9
#define XM_SYNC_WRITE_LENGTH 4
#define XM_TX_DELAY_TIME 1
#define LENGTH_2 2
#define LENGTH_3 3
#define LENGTH_4 4
#define LENGTH_6 6 //number of parameters+3
#define LENGTH_7 7
#define LENGTH_9 9
#define LENGTH_17 17
#include <inttypes.h>
class DynamixelClass
{
private:
unsigned char Checksum;
unsigned char Direction_Pin;
unsigned int Time_Counter;
unsigned char Incoming_Byte;
unsigned char Position_High_Byte;
unsigned char Position_Low_Byte;
unsigned char Speed_High_Byte;
unsigned char Speed_Low_Byte;
unsigned char Load_High_Byte;
unsigned char Load_Low_Byte;
int Moving_Byte;
int RWS_Byte;
int Speed_Long_Byte;
int Load_Long_Byte;
int Position_Long_Byte;
int Temperature_Byte;
int Voltage_Byte;
int Error_Byte;
int read_error(void);
Stream &serial_;
public:
DynamixelClass(Stream &serial);
void SetDirPin(unsigned char directionPin);
void begin(long baud, unsigned char directionPin); //depreciated
void begin(long baud); //depreciated
void end(void);
int reset(unsigned char ID);
int ping(unsigned char ID);
int setID(unsigned char ID, unsigned char newID);
int setBD(unsigned char ID, long baud);
int move(unsigned char ID, int Position);
int moveSpeed(unsigned char ID, int Position, int Speed);
int setEndless(unsigned char ID, bool Status);
int turn(unsigned char ID, bool SIDE, int Speed);
int moveRW(unsigned char ID, int Position);
int moveSpeedRW(unsigned char ID, int Position, int Speed);
void action(void);
int setTempLimit(unsigned char ID, unsigned char Temperature);
int setAngleLimit(unsigned char ID, int CWLimit, int CCWLimit);
int setVoltageLimit(unsigned char ID, unsigned char DVoltage, unsigned char UVoltage);
int setMaxTorque(unsigned char ID, int MaxTorque);
int setSRL(unsigned char ID, unsigned char SRL);
int setRDT(unsigned char ID, unsigned char RDT);
int setLEDAlarm(unsigned char ID, unsigned char LEDAlarm);
int setShutdownAlarm(unsigned char ID, unsigned char SALARM);
int setCMargin(unsigned char ID, unsigned char CWCMargin, unsigned char CCWCMargin);
int setCSlope(unsigned char ID, unsigned char CWCSlope, unsigned char CCWCSlope);
int setPunch(unsigned char ID, int Punch);
int moving(unsigned char ID);
int lockRegister(unsigned char ID);
int RWStatus(unsigned char ID);
int readTemperature(unsigned char ID);
int readVoltage(unsigned char ID);
int readPosition(unsigned char ID);
int readPosition(unsigned char ID, int *Pos_Long_Byte);
int readSpeed(unsigned char ID);
int readLoad(unsigned char ID);
int torqueStatus(unsigned char ID, bool Status);
int ledStatus(unsigned char ID, bool Status);
int synWritePos(unsigned char ID1, int Position1, unsigned char ID2, int Position2);
};
class DynamixelXClass
{
private:
unsigned char Direction_Pin;
unsigned int Time_Counter;
unsigned char Incoming_Byte;
unsigned char Position_High_Byte;
unsigned char Position_Low_Byte;
unsigned char Speed_High_Byte;
unsigned char Speed_Low_Byte;
unsigned char Load_High_Byte;
unsigned char Load_Low_Byte;
unsigned char detectID;
unsigned char Model_Low_Byte;
unsigned char Model_High_Byte;
unsigned char Fw_ver;
int Moving_Byte;
int RWS_Byte;
int Speed_Long_Byte;
int Load_Long_Byte;
int Position_Long_Byte;
int Temperature_Byte;
int Voltage_Byte;
int Error_Byte;
int Model_Long_Byte;
int read_error(void);
unsigned short update_crc(unsigned short crc_accum, unsigned char *data_blk_ptr, unsigned short data_blk_size);
Stream &serialx_;
public:
DynamixelXClass(Stream &serialx);
void SetDirPin(unsigned char directionPin);
void begin(long baud, unsigned char directionPin); //depreciated
int move(unsigned char ID, int Position);
int setRDT(unsigned char ID, unsigned char RDT); // SET return delay time
int ledStatus(unsigned char ID, bool Status);
int setBD(unsigned char ID, int baud); //0:9600, 1:57600(default), 2:115200, 3:1M, 4:2M, 5:3M, 6:4M, 7:4.5M
int setTorque(unsigned char ID, bool torque);
int synWritePos(unsigned char ID1, int Position1, unsigned char ID2, int Position2);
int setProfileAcc(unsigned char ID, int acc_range);
int setProfileVel(unsigned char ID, int vel_range);
int readPosition(unsigned char ID);
int readPosition(unsigned char ID, int *Pos_Long_Byte);
struct ReturnPacket2 ping(unsigned char ID);
int syncReadPos(unsigned char ID1, int *Pos_Long_Byte1, unsigned char ID2, int *Pos_Long_Byte2); //Sync Read of the position of 2 motors
int syncReadCur(unsigned char ID1, unsigned char ID2); //Sync Read of the current of 2 motors
};
extern DynamixelClass Dynamixel;
extern DynamixelXClass DynamixelX;
//Structures
struct ReturnPacket2
{
uint16_t value1;
uint16_t value2;
};
struct ReturnPacket3
{
uint16_t value1;
uint16_t value2;
uint16_t value3;
};
struct structest
{
uint16_t a;
uint16_t b;
uint16_t c;
};
struct PacketSize0
{ //All data Packets with 3 parameters
unsigned char header[3] = {XM_START_FF, XM_START_FF, XM_START_FD}; //defined
unsigned char reserved = XM_0; //defined
unsigned char id; //parameter
unsigned char packetlenght_L = LENGTH_3; //defined
unsigned char packetlenght_H = XM_0; //defined
unsigned char instruction; //parameter
unsigned char crc_L; //parameter
unsigned char crc_H; //parameter
};
struct PacketSize3
{ //All data Packets with 3 parameters
unsigned char header[3] = {XM_START_FF, XM_START_FF, XM_START_FD}; //defined
unsigned char reserved = XM_0; //defined
unsigned char id; //parameter
unsigned char packetlenght_L = LENGTH_6; //defined
unsigned char packetlenght_H = XM_0; //defined
unsigned char instruction; //parameter
unsigned char param1; //adress Low
unsigned char param2 = XM_0; //adress High
unsigned char param3; //value
unsigned char crc_L; //parameter
unsigned char crc_H; //parameter
};
struct PacketSize4
{ //All data Packets with 4 parameters
unsigned char header[3] = {XM_START_FF, XM_START_FF, XM_START_FD}; //defined
unsigned char reserved = XM_0; //defined
unsigned char id; //parameter
unsigned char packetlenght_L = LENGTH_7; //defined
unsigned char packetlenght_H = XM_0; //defined
unsigned char instruction; //parameter
unsigned char param1; //adress Low
unsigned char param2 = XM_0; //adress High
unsigned char param3; //value Low
unsigned char param4; //value High
unsigned char crc_L; //parameter
unsigned char crc_H; //parameter
};
struct PacketSize6
{ //All data Packets with 6 parameters
unsigned char header[3] = {XM_START_FF, XM_START_FF, XM_START_FD}; //defined
unsigned char reserved = XM_0; //defined
unsigned char id; //parameter
unsigned char packetlenght_L = LENGTH_9; //defined
unsigned char packetlenght_H = XM_0; //defined
unsigned char instruction; //parameter
unsigned char param1; //adress Low
unsigned char param2 = XM_0; //adress High
unsigned char param3; //value Low
unsigned char param4; //value High
unsigned char param5; //value
unsigned char param6; //value
unsigned char crc_L; //parameter
unsigned char crc_H; //parameter
};
struct PacketSync14
{ //Sync for 2 motors and 14 parameters
unsigned char header[3] = {XM_START_FF, XM_START_FF, XM_START_FD}; //defined
unsigned char reserved = XM_0; //defined
unsigned char id = BROADCAST_ID;
unsigned char packetlenght_L = LENGTH_17; //defined
unsigned char packetlenght_H = XM_0; //defined
unsigned char instruction = XM_SYNC_WRITE; //defined
unsigned char param1; //adress Low
unsigned char param2 = XM_0; //adress High
unsigned char param3; //datalength_L
unsigned char param4; //datalength_H
unsigned char param5; //Parameter 5
unsigned char param6; //Parameter 6
unsigned char param7; //Parameter 7
unsigned char param8; //Parameter 8
unsigned char param9; //Parameter 9
unsigned char param10; //Parameter 10
unsigned char param11; //Parameter 11
unsigned char param12; //Parameter 12
unsigned char param13; //Parameter 13
unsigned char param14; //Parameter 14
unsigned char crc_L; //parameter
unsigned char crc_H; //parameter
};
#endif