@@ -54,54 +54,71 @@ typedef struct InfoSyncBulkInst{
54
54
DYNAMIXEL::InfoSyncBulkInst_t sr_present_pos;
55
55
const uint16_t user_pkt_buf_cap = 128 ;
56
56
uint8_t user_pkt_buf[user_pkt_buf_cap];
57
- const uint16_t recv_buf_cap = 128 ;
58
- uint8_t recv_buf[recv_buf_cap];
59
- int32_t goal_vel = 0 ;
57
+
58
+ const uint8_t DXL_ID_CNT = 2 ;
59
+ const uint16_t SR_START_ADDR = 126 ;
60
+ const uint16_t SR_ADDR_LEN = 10 ; // 2+4+4
61
+ const uint16_t SW_START_ADDR = 104 ; // Goal velocity
62
+ const uint16_t SW_ADDR_LEN = 4 ;
63
+ typedef struct sr_data {
64
+ int16_t present_current;
65
+ int32_t present_velocity;
66
+ int32_t present_position;
67
+ } sr_data_t ;
68
+ uint8_t id_list[DXL_ID_CNT] = {1 , 3 };
69
+ sr_data_t present_values[DXL_ID_CNT];
70
+ int32_t goal_velocity[DXL_ID_CNT] = {100 , 300 };
60
71
61
72
Dynamixel2Arduino dxl (DXL_SERIAL, DXL_DIR_PIN);
62
73
63
74
void setup () {
64
75
// put your setup code here, to run once:
76
+ uint8_t i;
65
77
DEBUG_SERIAL.begin (115200 );
66
- dxl.begin (1000000 );
67
- dxl.scan ();
78
+ dxl.begin (57600 );
68
79
69
- dxl. torqueOff ( 1 );
70
- dxl.setOperatingMode ( 1 , OP_VELOCITY );
71
- dxl.torqueOn ( 1 );
72
-
73
- dxl. torqueOff ( 3 );
74
- dxl.setOperatingMode ( 3 , OP_VELOCITY );
75
- dxl. torqueOn ( 3 );
80
+ for (i= 0 ; i<DXL_ID_CNT; i++){
81
+ dxl.torqueOff (id_list[i] );
82
+ dxl.setOperatingMode (id_list[i], OP_VELOCITY );
83
+ }
84
+ for (i= 0 ; i<DXL_ID_CNT; i++){
85
+ dxl.torqueOn (id_list[i] );
86
+ }
76
87
77
- // Using user buffer
88
+ // Generate SyncRead packet using user buffer
78
89
sr_present_pos.p_packet_buf = user_pkt_buf;
79
90
sr_present_pos.packet_buf_capacity = user_pkt_buf_cap;
91
+ dxl.beginSyncRead (SR_START_ADDR, SR_ADDR_LEN, &sr_present_pos);
92
+ for (i=0 ; i<DXL_ID_CNT; i++){
93
+ if (i<(DXL_ID_CNT-1 )){
94
+ dxl.addSyncReadID (id_list[i], &sr_present_pos);
95
+ }else {
96
+ // 3rd parameter is a flag to finish adding parameter (ID) and create packet.(default is false)
97
+ dxl.addSyncReadID (id_list[i], &sr_present_pos, true );
98
+ }
99
+ }
80
100
81
- dxl.beginSyncRead (132 , 4 , &sr_present_pos);
82
- dxl.addSyncReadID (1 , &sr_present_pos);
83
- dxl.addSyncReadID (3 , &sr_present_pos, true );
84
-
85
- // Using class default buffer
86
- dxl.beginSyncWrite (104 , 4 );
87
- goal_vel = 100 ;
88
- dxl.addSyncWriteData (1 , (uint8_t *)&goal_vel);
89
- goal_vel = 200 ;
90
- dxl.addSyncWriteData (3 , (uint8_t *)&goal_vel);
101
+ // Generate SyncRead packet using class default buffer
102
+ dxl.beginSyncWrite (SW_START_ADDR, SW_ADDR_LEN);
103
+ for (i=0 ; i<DXL_ID_CNT; i++){
104
+ dxl.addSyncWriteData (id_list[i], (uint8_t *)&goal_velocity[i]);
105
+ }
91
106
dxl.sendSyncWrite ();
92
107
}
93
108
94
109
void loop () {
95
110
// put your main code here, to run repeatedly:
96
- uint16_t recv_len ;
111
+ uint8_t recv_cnt ;
97
112
98
- recv_len = dxl.sendSyncRead (recv_buf, recv_buf_cap, &sr_present_pos);
99
- if (recv_len > 0 ){
100
- DEBUG_SERIAL.print (" \t Receive Len : " );
101
- DEBUG_SERIAL.print (recv_len);
102
- DEBUG_SERIAL.println ();
103
- for (uint16_t i=0 ; i<recv_len; i++){
104
- DEBUG_SERIAL.print (recv_buf[i], HEX);DEBUG_SERIAL.print (" " );
113
+ recv_cnt = dxl.sendSyncRead ((uint8_t *)&present_values, sizeof (present_values), &sr_present_pos);
114
+ if (recv_cnt > 0 ){
115
+ DEBUG_SERIAL.print (" Received ID Count: " );
116
+ DEBUG_SERIAL.println (recv_cnt);
117
+ for (uint16_t i=0 ; i<recv_cnt; i++){
118
+ DEBUG_SERIAL.print (" ID: " );DEBUG_SERIAL.println (id_list[i]);
119
+ DEBUG_SERIAL.print (" \t Present Current: " );DEBUG_SERIAL.println (present_values[i].present_current );
120
+ DEBUG_SERIAL.print (" \t Present Velocity: " );DEBUG_SERIAL.println (present_values[i].present_velocity );
121
+ DEBUG_SERIAL.print (" \t Present Position: " );DEBUG_SERIAL.println (present_values[i].present_position );
105
122
}
106
123
DEBUG_SERIAL.println ();
107
124
}else {
0 commit comments