-
Notifications
You must be signed in to change notification settings - Fork 410
/
protocol1_packet_handler.cpp
745 lines (615 loc) · 22.7 KB
/
protocol1_packet_handler.cpp
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*******************************************************************************
* Copyright 2017 ROBOTIS CO., LTD.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
/* Author: zerom, Ryu Woon Jung (Leon) */
#if defined(__linux__)
#include "protocol1_packet_handler.h"
#elif defined(__APPLE__)
#include "protocol1_packet_handler.h"
#elif defined(_WIN32) || defined(_WIN64)
#define WINDLLEXPORT
#include "protocol1_packet_handler.h"
#elif defined(ARDUINO) || defined(__OPENCR__) || defined(__OPENCM904__)
#include "../../include/dynamixel_sdk/protocol1_packet_handler.h"
#endif
#include <string.h>
#include <stdlib.h>
#define TXPACKET_MAX_LEN (250)
#define RXPACKET_MAX_LEN (250)
///////////////// for Protocol 1.0 Packet /////////////////
#define PKT_HEADER0 0
#define PKT_HEADER1 1
#define PKT_ID 2
#define PKT_LENGTH 3
#define PKT_INSTRUCTION 4
#define PKT_ERROR 4
#define PKT_PARAMETER0 5
///////////////// Protocol 1.0 Error bit /////////////////
#define ERRBIT_VOLTAGE 1 // Supplied voltage is out of the range (operating volatage set in the control table)
#define ERRBIT_ANGLE 2 // Goal position is written out of the range (from CW angle limit to CCW angle limit)
#define ERRBIT_OVERHEAT 4 // Temperature is out of the range (operating temperature set in the control table)
#define ERRBIT_RANGE 8 // Command(setting value) is out of the range for use.
#define ERRBIT_CHECKSUM 16 // Instruction packet checksum is incorrect.
#define ERRBIT_OVERLOAD 32 // The current load cannot be controlled by the set torque.
#define ERRBIT_INSTRUCTION 64 // Undefined instruction or delivering the action command without the reg_write command.
using namespace dynamixel;
Protocol1PacketHandler *Protocol1PacketHandler::unique_instance_ = new Protocol1PacketHandler();
Protocol1PacketHandler::Protocol1PacketHandler() { }
const char *Protocol1PacketHandler::getTxRxResult(int result)
{
switch(result)
{
case COMM_SUCCESS:
return "[TxRxResult] Communication success.";
case COMM_PORT_BUSY:
return "[TxRxResult] Port is in use!";
case COMM_TX_FAIL:
return "[TxRxResult] Failed transmit instruction packet!";
case COMM_RX_FAIL:
return "[TxRxResult] Failed get status packet from device!";
case COMM_TX_ERROR:
return "[TxRxResult] Incorrect instruction packet!";
case COMM_RX_WAITING:
return "[TxRxResult] Now recieving status packet!";
case COMM_RX_TIMEOUT:
return "[TxRxResult] There is no status packet!";
case COMM_RX_CORRUPT:
return "[TxRxResult] Incorrect status packet!";
case COMM_NOT_AVAILABLE:
return "[TxRxResult] Protocol does not support This function!";
default:
return "";
}
}
void Protocol1PacketHandler::printTxRxResult(int result)
{
#if defined(ARDUINO) || defined(__OPENCR__) || defined(__OPENCM904__)
Serial.println("This function is deprecated. Use 'Serial.print()' and 'getRxPacketError()' instead");
Serial.println(getTxRxResult(result));
#else
printf("This function is deprecated. Use 'printf()' and 'getRxPacketError()' instead\n");
printf("%s\n", getTxRxResult(result));
#endif
}
const char *Protocol1PacketHandler::getRxPacketError(uint8_t error)
{
if (error & ERRBIT_VOLTAGE)
return "[RxPacketError] Input voltage error!";
if (error & ERRBIT_ANGLE)
return "[RxPacketError] Angle limit error!";
if (error & ERRBIT_OVERHEAT)
return "[RxPacketError] Overheat error!";
if (error & ERRBIT_RANGE)
return "[RxPacketError] Out of range error!";
if (error & ERRBIT_CHECKSUM)
return "[RxPacketError] Checksum error!";
if (error & ERRBIT_OVERLOAD)
return "[RxPacketError] Overload error!";
if (error & ERRBIT_INSTRUCTION)
return "[RxPacketError] Instruction code error!";
return "";
}
void Protocol1PacketHandler::printRxPacketError(uint8_t error)
{
#if defined(ARDUINO) || defined(__OPENCR__) || defined(__OPENCM904__)
Serial.println("This function is deprecated. Use 'Serial.print()' and 'getRxPacketError()' instead");
Serial.println(getRxPacketError(error));
#else
printf("This function is deprecated. Use 'printf()' and 'getRxPacketError()' instead\n");
printf("%s\n", getRxPacketError(error));
#endif
}
int Protocol1PacketHandler::txPacket(PortHandler *port, uint8_t *txpacket)
{
uint8_t checksum = 0;
uint8_t total_packet_length = txpacket[PKT_LENGTH] + 4; // 4: HEADER0 HEADER1 ID LENGTH
uint8_t written_packet_length = 0;
if (port->is_using_)
return COMM_PORT_BUSY;
port->is_using_ = true;
// check max packet length
if (total_packet_length > TXPACKET_MAX_LEN)
{
port->is_using_ = false;
return COMM_TX_ERROR;
}
// make packet header
txpacket[PKT_HEADER0] = 0xFF;
txpacket[PKT_HEADER1] = 0xFF;
// add a checksum to the packet
for (uint16_t idx = 2; idx < total_packet_length - 1; idx++) // except header, checksum
checksum += txpacket[idx];
txpacket[total_packet_length - 1] = ~checksum;
// tx packet
port->clearPort();
written_packet_length = port->writePort(txpacket, total_packet_length);
if (total_packet_length != written_packet_length)
{
port->is_using_ = false;
return COMM_TX_FAIL;
}
return COMM_SUCCESS;
}
int Protocol1PacketHandler::rxPacket(PortHandler *port, uint8_t *rxpacket)
{
int result = COMM_TX_FAIL;
uint8_t checksum = 0;
uint8_t rx_length = 0;
uint8_t wait_length = 6; // minimum length (HEADER0 HEADER1 ID LENGTH ERROR CHKSUM)
while(true)
{
rx_length += port->readPort(&rxpacket[rx_length], wait_length - rx_length);
if (rx_length >= wait_length)
{
uint8_t idx = 0;
// find packet header
for (idx = 0; idx < (rx_length - 1); idx++)
{
if (rxpacket[idx] == 0xFF && rxpacket[idx+1] == 0xFF)
break;
}
if (idx == 0) // found at the beginning of the packet
{
if (rxpacket[PKT_ID] > 0xFD || // unavailable ID
rxpacket[PKT_LENGTH] > RXPACKET_MAX_LEN || // unavailable Length
rxpacket[PKT_ERROR] > 0x7F) // unavailable Error
{
// remove the first byte in the packet
for (uint16_t s = 0; s < rx_length - 1; s++)
rxpacket[s] = rxpacket[1 + s];
//memcpy(&rxpacket[0], &rxpacket[idx], rx_length - idx);
rx_length -= 1;
continue;
}
// re-calculate the exact length of the rx packet
if (wait_length != rxpacket[PKT_LENGTH] + PKT_LENGTH + 1)
{
wait_length = rxpacket[PKT_LENGTH] + PKT_LENGTH + 1;
continue;
}
if (rx_length < wait_length)
{
// check timeout
if (port->isPacketTimeout() == true)
{
if (rx_length == 0)
{
result = COMM_RX_TIMEOUT;
}
else
{
result = COMM_RX_CORRUPT;
}
break;
}
else
{
continue;
}
}
// calculate checksum
for (uint16_t i = 2; i < wait_length - 1; i++) // except header, checksum
checksum += rxpacket[i];
checksum = ~checksum;
// verify checksum
if (rxpacket[wait_length - 1] == checksum)
{
result = COMM_SUCCESS;
}
else
{
result = COMM_RX_CORRUPT;
}
break;
}
else
{
// remove unnecessary packets
for (uint16_t s = 0; s < rx_length - idx; s++)
rxpacket[s] = rxpacket[idx + s];
//memcpy(&rxpacket[0], &rxpacket[idx], rx_length - idx);
rx_length -= idx;
}
}
else
{
// check timeout
if (port->isPacketTimeout() == true)
{
if (rx_length == 0)
{
result = COMM_RX_TIMEOUT;
}
else
{
result = COMM_RX_CORRUPT;
}
break;
}
}
}
port->is_using_ = false;
return result;
}
// NOT for BulkRead instruction
int Protocol1PacketHandler::txRxPacket(PortHandler *port, uint8_t *txpacket, uint8_t *rxpacket, uint8_t *error)
{
int result = COMM_TX_FAIL;
// tx packet
result = txPacket(port, txpacket);
if (result != COMM_SUCCESS)
return result;
// (Instruction == BulkRead) == this function is not available.
if(txpacket[PKT_INSTRUCTION] == INST_BULK_READ)
result = COMM_NOT_AVAILABLE;
// (ID == Broadcast ID) == no need to wait for status packet or not available
// (Instruction == action) == no need to wait for status packet
if (txpacket[PKT_ID] == BROADCAST_ID || txpacket[PKT_INSTRUCTION] == INST_ACTION)
{
port->is_using_ = false;
return result;
}
// set packet timeout
if (txpacket[PKT_INSTRUCTION] == INST_READ)
{
port->setPacketTimeout((uint16_t)(txpacket[PKT_PARAMETER0+1] + 6));
}
else
{
port->setPacketTimeout((uint16_t)6); // HEADER0 HEADER1 ID LENGTH ERROR CHECKSUM
}
// rx packet
do {
result = rxPacket(port, rxpacket);
} while (result == COMM_SUCCESS && txpacket[PKT_ID] != rxpacket[PKT_ID]);
if (result == COMM_SUCCESS && txpacket[PKT_ID] == rxpacket[PKT_ID])
{
if (error != 0)
*error = (uint8_t)rxpacket[PKT_ERROR];
}
return result;
}
int Protocol1PacketHandler::ping(PortHandler *port, uint8_t id, uint8_t *error)
{
return ping(port, id, 0, error);
}
int Protocol1PacketHandler::ping(PortHandler *port, uint8_t id, uint16_t *model_number, uint8_t *error)
{
int result = COMM_TX_FAIL;
uint8_t txpacket[6] = {0};
uint8_t rxpacket[6] = {0};
if (id >= BROADCAST_ID)
return COMM_NOT_AVAILABLE;
txpacket[PKT_ID] = id;
txpacket[PKT_LENGTH] = 2;
txpacket[PKT_INSTRUCTION] = INST_PING;
result = txRxPacket(port, txpacket, rxpacket, error);
if (result == COMM_SUCCESS && model_number != 0)
{
uint8_t data_read[2] = {0};
result = readTxRx(port, id, 0, 2, data_read); // Address 0 : Model Number
if (result == COMM_SUCCESS) *model_number = DXL_MAKEWORD(data_read[0], data_read[1]);
}
return result;
}
int Protocol1PacketHandler::broadcastPing(PortHandler *port, std::vector<uint8_t> &id_list)
{
return COMM_NOT_AVAILABLE;
}
int Protocol1PacketHandler::action(PortHandler *port, uint8_t id)
{
uint8_t txpacket[6] = {0};
txpacket[PKT_ID] = id;
txpacket[PKT_LENGTH] = 2;
txpacket[PKT_INSTRUCTION] = INST_ACTION;
return txRxPacket(port, txpacket, 0);
}
int Protocol1PacketHandler::reboot(PortHandler *port, uint8_t id, uint8_t *error)
{
return COMM_NOT_AVAILABLE;
}
int Protocol1PacketHandler::factoryReset(PortHandler *port, uint8_t id, uint8_t option, uint8_t *error)
{
uint8_t txpacket[6] = {0};
uint8_t rxpacket[6] = {0};
txpacket[PKT_ID] = id;
txpacket[PKT_LENGTH] = 2;
txpacket[PKT_INSTRUCTION] = INST_FACTORY_RESET;
return txRxPacket(port, txpacket, rxpacket, error);
}
int Protocol1PacketHandler::readTx(PortHandler *port, uint8_t id, uint16_t address, uint16_t length)
{
int result = COMM_TX_FAIL;
uint8_t txpacket[8] = {0};
if (id >= BROADCAST_ID)
return COMM_NOT_AVAILABLE;
txpacket[PKT_ID] = id;
txpacket[PKT_LENGTH] = 4;
txpacket[PKT_INSTRUCTION] = INST_READ;
txpacket[PKT_PARAMETER0+0] = (uint8_t)address;
txpacket[PKT_PARAMETER0+1] = (uint8_t)length;
result = txPacket(port, txpacket);
// set packet timeout
if (result == COMM_SUCCESS)
port->setPacketTimeout((uint16_t)(length+6));
return result;
}
int Protocol1PacketHandler::readRx(PortHandler *port, uint8_t id, uint16_t length, uint8_t *data, uint8_t *error)
{
int result = COMM_TX_FAIL;
uint8_t *rxpacket = (uint8_t *)malloc(RXPACKET_MAX_LEN); //(length+6);
//uint8_t *rxpacket = new uint8_t[length+6];
do {
result = rxPacket(port, rxpacket);
} while (result == COMM_SUCCESS && rxpacket[PKT_ID] != id);
if (result == COMM_SUCCESS && rxpacket[PKT_ID] == id)
{
if (error != 0)
{
*error = (uint8_t)rxpacket[PKT_ERROR];
}
for (uint16_t s = 0; s < length; s++)
{
data[s] = rxpacket[PKT_PARAMETER0 + s];
}
//memcpy(data, &rxpacket[PKT_PARAMETER0], length);
}
free(rxpacket);
//delete[] rxpacket;
return result;
}
int Protocol1PacketHandler::readTxRx(PortHandler *port, uint8_t id, uint16_t address, uint16_t length, uint8_t *data, uint8_t *error)
{
int result = COMM_TX_FAIL;
uint8_t txpacket[8] = {0};
uint8_t *rxpacket = (uint8_t *)malloc(RXPACKET_MAX_LEN);//(length+6);
if (id >= BROADCAST_ID)
return COMM_NOT_AVAILABLE;
txpacket[PKT_ID] = id;
txpacket[PKT_LENGTH] = 4;
txpacket[PKT_INSTRUCTION] = INST_READ;
txpacket[PKT_PARAMETER0+0] = (uint8_t)address;
txpacket[PKT_PARAMETER0+1] = (uint8_t)length;
result = txRxPacket(port, txpacket, rxpacket, error);
if (result == COMM_SUCCESS)
{
if (error != 0)
{
*error = (uint8_t)rxpacket[PKT_ERROR];
}
for (uint16_t s = 0; s < length; s++)
{
data[s] = rxpacket[PKT_PARAMETER0 + s];
}
//memcpy(data, &rxpacket[PKT_PARAMETER0], length);
}
free(rxpacket);
//delete[] rxpacket;
return result;
}
int Protocol1PacketHandler::read1ByteTx(PortHandler *port, uint8_t id, uint16_t address)
{
return readTx(port, id, address, 1);
}
int Protocol1PacketHandler::read1ByteRx(PortHandler *port, uint8_t id, uint8_t *data, uint8_t *error)
{
uint8_t data_read[1] = {0};
int result = readRx(port, id, 1, data_read, error);
if (result == COMM_SUCCESS)
*data = data_read[0];
return result;
}
int Protocol1PacketHandler::read1ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint8_t *data, uint8_t *error)
{
uint8_t data_read[1] = {0};
int result = readTxRx(port, id, address, 1, data_read, error);
if (result == COMM_SUCCESS)
*data = data_read[0];
return result;
}
int Protocol1PacketHandler::read2ByteTx(PortHandler *port, uint8_t id, uint16_t address)
{
return readTx(port, id, address, 2);
}
int Protocol1PacketHandler::read2ByteRx(PortHandler *port, uint8_t id, uint16_t *data, uint8_t *error)
{
uint8_t data_read[2] = {0};
int result = readRx(port, id, 2, data_read, error);
if (result == COMM_SUCCESS)
*data = DXL_MAKEWORD(data_read[0], data_read[1]);
return result;
}
int Protocol1PacketHandler::read2ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint16_t *data, uint8_t *error)
{
uint8_t data_read[2] = {0};
int result = readTxRx(port, id, address, 2, data_read, error);
if (result == COMM_SUCCESS)
*data = DXL_MAKEWORD(data_read[0], data_read[1]);
return result;
}
int Protocol1PacketHandler::read4ByteTx(PortHandler *port, uint8_t id, uint16_t address)
{
return readTx(port, id, address, 4);
}
int Protocol1PacketHandler::read4ByteRx(PortHandler *port, uint8_t id, uint32_t *data, uint8_t *error)
{
uint8_t data_read[4] = {0};
int result = readRx(port, id, 4, data_read, error);
if (result == COMM_SUCCESS)
*data = DXL_MAKEDWORD(DXL_MAKEWORD(data_read[0], data_read[1]), DXL_MAKEWORD(data_read[2], data_read[3]));
return result;
}
int Protocol1PacketHandler::read4ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint32_t *data, uint8_t *error)
{
uint8_t data_read[4] = {0};
int result = readTxRx(port, id, address, 4, data_read, error);
if (result == COMM_SUCCESS)
*data = DXL_MAKEDWORD(DXL_MAKEWORD(data_read[0], data_read[1]), DXL_MAKEWORD(data_read[2], data_read[3]));
return result;
}
int Protocol1PacketHandler::writeTxOnly(PortHandler *port, uint8_t id, uint16_t address, uint16_t length, uint8_t *data)
{
int result = COMM_TX_FAIL;
uint8_t *txpacket = (uint8_t *)malloc(length+7);
//uint8_t *txpacket = new uint8_t[length+7];
txpacket[PKT_ID] = id;
txpacket[PKT_LENGTH] = length+3;
txpacket[PKT_INSTRUCTION] = INST_WRITE;
txpacket[PKT_PARAMETER0] = (uint8_t)address;
for (uint16_t s = 0; s < length; s++)
txpacket[PKT_PARAMETER0+1+s] = data[s];
//memcpy(&txpacket[PKT_PARAMETER0+1], data, length);
result = txPacket(port, txpacket);
port->is_using_ = false;
free(txpacket);
//delete[] txpacket;
return result;
}
int Protocol1PacketHandler::writeTxRx(PortHandler *port, uint8_t id, uint16_t address, uint16_t length, uint8_t *data, uint8_t *error)
{
int result = COMM_TX_FAIL;
uint8_t *txpacket = (uint8_t *)malloc(length+7); //#6->7
//uint8_t *txpacket = new uint8_t[length+7];
uint8_t rxpacket[6] = {0};
txpacket[PKT_ID] = id;
txpacket[PKT_LENGTH] = length+3;
txpacket[PKT_INSTRUCTION] = INST_WRITE;
txpacket[PKT_PARAMETER0] = (uint8_t)address;
for (uint16_t s = 0; s < length; s++)
txpacket[PKT_PARAMETER0+1+s] = data[s];
//memcpy(&txpacket[PKT_PARAMETER0+1], data, length);
result = txRxPacket(port, txpacket, rxpacket, error);
free(txpacket);
//delete[] txpacket;
return result;
}
int Protocol1PacketHandler::write1ByteTxOnly(PortHandler *port, uint8_t id, uint16_t address, uint8_t data)
{
uint8_t data_write[1] = { data };
return writeTxOnly(port, id, address, 1, data_write);
}
int Protocol1PacketHandler::write1ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint8_t data, uint8_t *error)
{
uint8_t data_write[1] = { data };
return writeTxRx(port, id, address, 1, data_write, error);
}
int Protocol1PacketHandler::write2ByteTxOnly(PortHandler *port, uint8_t id, uint16_t address, uint16_t data)
{
uint8_t data_write[2] = { DXL_LOBYTE(data), DXL_HIBYTE(data) };
return writeTxOnly(port, id, address, 2, data_write);
}
int Protocol1PacketHandler::write2ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint16_t data, uint8_t *error)
{
uint8_t data_write[2] = { DXL_LOBYTE(data), DXL_HIBYTE(data) };
return writeTxRx(port, id, address, 2, data_write, error);
}
int Protocol1PacketHandler::write4ByteTxOnly(PortHandler *port, uint8_t id, uint16_t address, uint32_t data)
{
uint8_t data_write[4] = { DXL_LOBYTE(DXL_LOWORD(data)), DXL_HIBYTE(DXL_LOWORD(data)), DXL_LOBYTE(DXL_HIWORD(data)), DXL_HIBYTE(DXL_HIWORD(data)) };
return writeTxOnly(port, id, address, 4, data_write);
}
int Protocol1PacketHandler::write4ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint32_t data, uint8_t *error)
{
uint8_t data_write[4] = { DXL_LOBYTE(DXL_LOWORD(data)), DXL_HIBYTE(DXL_LOWORD(data)), DXL_LOBYTE(DXL_HIWORD(data)), DXL_HIBYTE(DXL_HIWORD(data)) };
return writeTxRx(port, id, address, 4, data_write, error);
}
int Protocol1PacketHandler::regWriteTxOnly(PortHandler *port, uint8_t id, uint16_t address, uint16_t length, uint8_t *data)
{
int result = COMM_TX_FAIL;
uint8_t *txpacket = (uint8_t *)malloc(length+6);
//uint8_t *txpacket = new uint8_t[length+6];
txpacket[PKT_ID] = id;
txpacket[PKT_LENGTH] = length+3;
txpacket[PKT_INSTRUCTION] = INST_REG_WRITE;
txpacket[PKT_PARAMETER0] = (uint8_t)address;
for (uint16_t s = 0; s < length; s++)
txpacket[PKT_PARAMETER0+1+s] = data[s];
//memcpy(&txpacket[PKT_PARAMETER0+1], data, length);
result = txPacket(port, txpacket);
port->is_using_ = false;
free(txpacket);
//delete[] txpacket;
return result;
}
int Protocol1PacketHandler::regWriteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint16_t length, uint8_t *data, uint8_t *error)
{
int result = COMM_TX_FAIL;
uint8_t *txpacket = (uint8_t *)malloc(length+6);
//uint8_t *txpacket = new uint8_t[length+6];
uint8_t rxpacket[6] = {0};
txpacket[PKT_ID] = id;
txpacket[PKT_LENGTH] = length+3;
txpacket[PKT_INSTRUCTION] = INST_REG_WRITE;
txpacket[PKT_PARAMETER0] = (uint8_t)address;
for (uint16_t s = 0; s < length; s++)
txpacket[PKT_PARAMETER0+1+s] = data[s];
//memcpy(&txpacket[PKT_PARAMETER0+1], data, length);
result = txRxPacket(port, txpacket, rxpacket, error);
free(txpacket);
//delete[] txpacket;
return result;
}
int Protocol1PacketHandler::syncReadTx(PortHandler *port, uint16_t start_address, uint16_t data_length, uint8_t *param, uint16_t param_length)
{
return COMM_NOT_AVAILABLE;
}
int Protocol1PacketHandler::syncWriteTxOnly(PortHandler *port, uint16_t start_address, uint16_t data_length, uint8_t *param, uint16_t param_length)
{
int result = COMM_TX_FAIL;
uint8_t *txpacket = (uint8_t *)malloc(param_length+8);
// 8: HEADER0 HEADER1 ID LEN INST START_ADDR DATA_LEN ... CHKSUM
//uint8_t *txpacket = new uint8_t[param_length + 8];
txpacket[PKT_ID] = BROADCAST_ID;
txpacket[PKT_LENGTH] = param_length + 4; // 4: INST START_ADDR DATA_LEN ... CHKSUM
txpacket[PKT_INSTRUCTION] = INST_SYNC_WRITE;
txpacket[PKT_PARAMETER0+0] = start_address;
txpacket[PKT_PARAMETER0+1] = data_length;
for (uint16_t s = 0; s < param_length; s++)
txpacket[PKT_PARAMETER0+2+s] = param[s];
//memcpy(&txpacket[PKT_PARAMETER0+2], param, param_length);
result = txRxPacket(port, txpacket, 0, 0);
free(txpacket);
//delete[] txpacket;
return result;
}
int Protocol1PacketHandler::bulkReadTx(PortHandler *port, uint8_t *param, uint16_t param_length)
{
int result = COMM_TX_FAIL;
uint8_t *txpacket = (uint8_t *)malloc(param_length+7);
// 7: HEADER0 HEADER1 ID LEN INST 0x00 ... CHKSUM
//uint8_t *txpacket = new uint8_t[param_length + 7];
txpacket[PKT_ID] = BROADCAST_ID;
txpacket[PKT_LENGTH] = param_length + 3; // 3: INST 0x00 ... CHKSUM
txpacket[PKT_INSTRUCTION] = INST_BULK_READ;
txpacket[PKT_PARAMETER0+0] = 0x00;
for (uint16_t s = 0; s < param_length; s++)
txpacket[PKT_PARAMETER0+1+s] = param[s];
//memcpy(&txpacket[PKT_PARAMETER0+1], param, param_length);
result = txPacket(port, txpacket);
if (result == COMM_SUCCESS)
{
int wait_length = 0;
for (uint16_t i = 0; i < param_length; i += 3)
wait_length += param[i] + 7;
port->setPacketTimeout((uint16_t)wait_length);
}
free(txpacket);
//delete[] txpacket;
return result;
}
int Protocol1PacketHandler::bulkWriteTxOnly(PortHandler *port, uint8_t *param, uint16_t param_length)
{
return COMM_NOT_AVAILABLE;
}