|
23 | 23 | #define DXL_SERIAL Serial
|
24 | 24 | #define DEBUG_SERIAL soft_serial
|
25 | 25 | const uint8_t DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN
|
26 |
| -#elif ARDUINO_OpenCM904 // Official ROBOTIS board with DXL circuit. |
| 26 | +#elif defined(ARDUINO_SAM_DUE) // When using DynamixelShield |
| 27 | + #define DXL_SERIAL Serial |
| 28 | + #define DEBUG_SERIAL SerialUSB |
| 29 | + const uint8_t DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN |
| 30 | +#elif defined(ARDUINO_SAM_ZERO) // When using DynamixelShield |
| 31 | + #define DXL_SERIAL Serial1 |
| 32 | + #define DEBUG_SERIAL SerialUSB |
| 33 | + const uint8_t DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN |
| 34 | +#elif defined(ARDUINO_OpenCM904) // When using official ROBOTIS board with DXL circuit. |
27 | 35 | #define DXL_SERIAL Serial3 //OpenCM9.04 EXP Board's DXL port Serial. (Serial1 for the DXL port on the OpenCM 9.04 board)
|
28 | 36 | #define DEBUG_SERIAL Serial
|
29 | 37 | const uint8_t DXL_DIR_PIN = 22; //OpenCM9.04 EXP Board's DIR PIN. (28 for the DXL port on the OpenCM 9.04 board)
|
30 |
| -#elif ARDUINO_OpenCR // Official ROBOTIS board with DXL circuit. |
| 38 | +#elif defined(ARDUINO_OpenCR) // When using official ROBOTIS board with DXL circuit. |
31 | 39 | // For OpenCR, there is a DXL Power Enable pin, so you must initialize and control it.
|
32 | 40 | // Reference link : https://github.com/ROBOTIS-GIT/OpenCR/blob/master/arduino/opencr_arduino/opencr/libraries/DynamixelSDK/src/dynamixel_sdk/port_handler_arduino.cpp#L78
|
33 | 41 | #define DXL_SERIAL Serial3
|
34 | 42 | #define DEBUG_SERIAL Serial
|
35 | 43 | const uint8_t DXL_DIR_PIN = 84; // OpenCR Board's DIR PIN.
|
36 |
| -#else // When using DynamixelShield |
| 44 | +#else // Other boards when using DynamixelShield |
37 | 45 | #define DXL_SERIAL Serial1
|
38 | 46 | #define DEBUG_SERIAL Serial
|
39 | 47 | const uint8_t DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN
|
40 | 48 | #endif
|
| 49 | + |
41 | 50 |
|
42 |
| -/* XelInfoForBulkReadParam_t |
43 |
| - A structure that contains the information needed for the parameters of the 'bulkRead packet'. |
44 |
| -
|
45 |
| - typedef struct ParamForBulkReadInst{ |
46 |
| - uint8_t id_count; |
47 |
| - XelInfoForBulkReadParam_t xel[DXL_MAX_NODE]; //refer to below. |
48 |
| - } ParamForBulkReadInst_t; |
| 51 | +/* bulkRead |
| 52 | + Structures containing the necessary information to process the 'bulkRead' packet. |
49 | 53 |
|
50 |
| - typedef struct XelInfoForBulkReadParam{ |
51 |
| - uint8_t id; |
| 54 | + typedef struct XELInfoBulkRead{ |
52 | 55 | uint16_t addr;
|
53 |
| - uint16_t length; |
54 |
| - } XelInfoForBulkReadParam_t; |
55 |
| -*/ |
56 |
| - |
57 |
| -/* XelInfoForBulkWriteParam_t |
58 |
| - A structure that contains the information needed for the parameters of the 'bulkWrite packet'. |
59 |
| -
|
60 |
| - typedef struct ParamForBulkWriteInst{ |
61 |
| - uint8_t id_count; |
62 |
| - XelInfoForBulkWriteParam_t xel[DXL_MAX_NODE]; //refer to below. |
63 |
| - } ParamForBulkWriteInst_t; |
64 |
| -
|
65 |
| - typedef struct XelInfoForBulkWriteParam{ |
| 56 | + uint16_t addr_length; |
| 57 | + uint8_t *p_recv_buf; |
66 | 58 | uint8_t id;
|
67 |
| - uint16_t addr; |
68 |
| - uint16_t length; |
69 |
| - uint8_t data[DXL_MAX_NODE_BUFFER_SIZE]; |
70 |
| - } XelInfoForBulkWriteParam_t; |
| 59 | + uint8_t error; |
| 60 | + } __attribute__((packed)) XELInfoBulkRead_t; |
| 61 | +
|
| 62 | + typedef struct InfoBulkReadInst{ |
| 63 | + XELInfoBulkRead_t* p_xels; |
| 64 | + uint8_t xel_count; |
| 65 | + bool is_info_changed; |
| 66 | + InfoSyncBulkBuffer_t packet; |
| 67 | + } __attribute__((packed)) InfoBulkReadInst_t; |
71 | 68 | */
|
72 | 69 |
|
73 |
| -/* RecvInfoFromStatusInst_t |
74 |
| - A structure used to receive data from multiple XELs. |
75 |
| -
|
76 |
| - typedef struct RecvInfoFromStatusInst{ |
77 |
| - uint8_t id_count; |
78 |
| - XelInfoForStatusInst_t xel[DXL_MAX_NODE]; //refer to below. |
79 |
| - } RecvInfoFromStatusInst_t; |
| 70 | +/* bulkWrite |
| 71 | + Structures containing the necessary information to process the 'bulkWrite' packet. |
80 | 72 |
|
81 |
| - typedef struct XelInfoForStatusInst{ |
| 73 | + typedef struct XELInfoBulkWrite{ |
| 74 | + uint16_t addr; |
| 75 | + uint16_t addr_length; |
| 76 | + uint8_t* p_data; |
82 | 77 | uint8_t id;
|
83 |
| - uint16_t length; |
84 |
| - uint8_t error; |
85 |
| - uint8_t data[DXL_MAX_NODE_BUFFER_SIZE]; |
86 |
| - } XelInfoForStatusInst_t; |
| 78 | + } __attribute__((packed)) XELInfoBulkWrite_t; |
| 79 | +
|
| 80 | + typedef struct InfoBulkWriteInst{ |
| 81 | + XELInfoBulkWrite_t* p_xels; |
| 82 | + uint8_t xel_count; |
| 83 | + bool is_info_changed; |
| 84 | + InfoSyncBulkBuffer_t packet; |
| 85 | + } __attribute__((packed)) InfoBulkWriteInst_t; |
87 | 86 | */
|
88 | 87 |
|
89 |
| -ParamForBulkReadInst_t bulk_read_param; |
90 |
| -ParamForBulkWriteInst_t bulk_write_param; |
91 |
| -RecvInfoFromStatusInst_t read_result; |
| 88 | +const uint8_t DXL_1_ID = 1; |
| 89 | +const uint8_t DXL_2_ID = 2; |
| 90 | +const uint8_t DXL_ID_CNT = 2; |
| 91 | +const uint16_t user_pkt_buf_cap = 128; |
| 92 | +uint8_t user_pkt_buf[user_pkt_buf_cap]; |
| 93 | + |
| 94 | +struct br_data_xel_1{ |
| 95 | + int16_t present_current; |
| 96 | + int32_t present_velocity; |
| 97 | +} __attribute__((packed)); |
| 98 | +struct br_data_xel_2{ |
| 99 | + int32_t present_position; |
| 100 | +} __attribute__((packed)); |
| 101 | +struct bw_data_xel_1{ |
| 102 | + int32_t goal_velocity; |
| 103 | +} __attribute__((packed)); |
| 104 | +struct bw_data_xel_2{ |
| 105 | + int32_t goal_position; |
| 106 | +} __attribute__((packed)); |
| 107 | + |
| 108 | +struct br_data_xel_1 br_data_xel_1; |
| 109 | +struct br_data_xel_2 br_data_xel_2; |
| 110 | +DYNAMIXEL::InfoBulkReadInst_t br_infos; |
| 111 | +DYNAMIXEL::XELInfoBulkRead_t info_xels_br[DXL_ID_CNT]; |
| 112 | + |
| 113 | +struct bw_data_xel_1 bw_data_xel_1; |
| 114 | +struct bw_data_xel_2 bw_data_xel_2; |
| 115 | +DYNAMIXEL::InfoBulkWriteInst_t bw_infos; |
| 116 | +DYNAMIXEL::XELInfoBulkWrite_t info_xels_bw[DXL_ID_CNT]; |
92 | 117 |
|
93 | 118 | Dynamixel2Arduino dxl(DXL_SERIAL, DXL_DIR_PIN);
|
94 | 119 |
|
95 | 120 | void setup() {
|
96 | 121 | // put your setup code here, to run once:
|
| 122 | + pinMode(LED_BUILTIN, OUTPUT); |
97 | 123 | DEBUG_SERIAL.begin(115200);
|
98 | 124 | dxl.begin(57600);
|
99 |
| - dxl.scan(); |
100 |
| - |
101 |
| - // fill the members of structure for bulkWrite |
102 |
| - bulk_write_param.xel[0].id = 1; |
103 |
| - bulk_write_param.xel[1].id = 3; |
104 |
| - bulk_write_param.xel[0].addr = 116; //Goal Position on X serise |
105 |
| - bulk_write_param.xel[1].addr = 104; //Goal Velocity on X serise |
106 |
| - bulk_write_param.xel[0].length = 4; |
107 |
| - bulk_write_param.xel[1].length = 4; |
108 |
| - bulk_write_param.id_count = 2; |
109 |
| - |
110 |
| - // fill the members of structure for bulkRead |
111 |
| - bulk_read_param.xel[0].id = 1; |
112 |
| - bulk_read_param.xel[1].id = 3; |
113 |
| - bulk_read_param.xel[0].addr = 132; //Present Position on X serise |
114 |
| - bulk_read_param.xel[1].addr = 128; //Present Velocity on X serise |
115 |
| - bulk_read_param.xel[0].length = 4; |
116 |
| - bulk_read_param.xel[1].length = 4; |
117 |
| - bulk_read_param.id_count = 2; |
118 |
| - |
119 |
| - dxl.torqueOff(1); |
120 |
| - dxl.setOperatingMode(1, OP_POSITION); |
121 |
| - dxl.torqueOn(1); |
122 |
| - |
123 |
| - dxl.torqueOff(3); |
124 |
| - dxl.setOperatingMode(3, OP_VELOCITY); |
125 |
| - dxl.torqueOn(3); |
| 125 | + |
| 126 | + dxl.torqueOff(DXL_1_ID); |
| 127 | + dxl.torqueOff(DXL_2_ID); |
| 128 | + dxl.setOperatingMode(DXL_1_ID, OP_VELOCITY); |
| 129 | + dxl.setOperatingMode(DXL_2_ID, OP_POSITION); |
| 130 | + dxl.torqueOn(DXL_1_ID); |
| 131 | + dxl.torqueOn(DXL_2_ID); |
| 132 | + |
| 133 | + // fill the members of structure for bulkRead using external user packet buffer |
| 134 | + br_infos.packet.p_buf = user_pkt_buf; |
| 135 | + br_infos.packet.buf_capacity = user_pkt_buf_cap; |
| 136 | + br_infos.packet.is_completed = false; |
| 137 | + br_infos.p_xels = info_xels_br; |
| 138 | + br_infos.xel_count = 0; |
| 139 | + |
| 140 | + info_xels_br[0].id = DXL_1_ID; |
| 141 | + info_xels_br[0].addr = 126; // Present Current of X serise. |
| 142 | + info_xels_br[0].addr_length = 2+4; // Present Current + Present Velocity |
| 143 | + info_xels_br[0].p_recv_buf = (uint8_t*)&br_data_xel_1; |
| 144 | + br_infos.xel_count++; |
| 145 | + |
| 146 | + info_xels_br[1].id = DXL_2_ID; |
| 147 | + info_xels_br[1].addr = 132; // Present Position of X serise. |
| 148 | + info_xels_br[1].addr_length = 4; // Present Position + Present Velocity |
| 149 | + info_xels_br[1].p_recv_buf = (uint8_t*)&br_data_xel_2; |
| 150 | + br_infos.xel_count++; |
| 151 | + |
| 152 | + br_infos.is_info_changed = true; |
| 153 | + |
| 154 | + |
| 155 | + // Fill the members of structure for bulkWrite using internal packet buffer |
| 156 | + br_infos.packet.p_buf = nullptr; |
| 157 | + br_infos.packet.is_completed = false; |
| 158 | + bw_infos.p_xels = info_xels_bw; |
| 159 | + bw_infos.xel_count = 0; |
| 160 | + |
| 161 | + bw_data_xel_1.goal_velocity = 0; |
| 162 | + info_xels_bw[0].id = DXL_1_ID; |
| 163 | + info_xels_bw[0].addr = 104; // Goal Velocity of X serise. |
| 164 | + info_xels_bw[0].addr_length = 4; // Goal Velocity |
| 165 | + info_xels_bw[0].p_data = (uint8_t*)&bw_data_xel_1; |
| 166 | + bw_infos.xel_count++; |
| 167 | + |
| 168 | + bw_data_xel_2.goal_position = 0; |
| 169 | + info_xels_bw[1].id = DXL_2_ID; |
| 170 | + info_xels_bw[1].addr = 116; // Goal Position of X serise. |
| 171 | + info_xels_bw[1].addr_length = 4; // Goal Position |
| 172 | + info_xels_bw[1].p_data = (uint8_t*)&bw_data_xel_2; |
| 173 | + bw_infos.xel_count++; |
| 174 | + |
| 175 | + bw_infos.is_info_changed = true; |
126 | 176 | }
|
127 | 177 |
|
128 | 178 | void loop() {
|
129 | 179 | // put your main code here, to run repeatedly:
|
130 |
| - static int32_t position = 0, velocity = 0; |
131 |
| - int32_t recv_position = 0, recv_velocity = 0; |
132 |
| - |
133 |
| - // set value to data buffer for bulkWrite |
134 |
| - position = position >= 4095 ? 0 : position+409; |
135 |
| - memcpy(bulk_write_param.xel[0].data, &position, sizeof(position)); |
136 |
| - velocity = velocity >= 200 ? -200 : velocity+10; |
137 |
| - memcpy(bulk_write_param.xel[1].data, &velocity, sizeof(velocity)); |
138 |
| - |
139 |
| - // send command using bulkWrite |
140 |
| - dxl.bulkWrite(bulk_write_param); |
141 |
| - delay(100); |
142 |
| - |
143 |
| - // Print the read data using bulkRead |
144 |
| - dxl.bulkRead(bulk_read_param, read_result); |
145 |
| - DEBUG_SERIAL.println(F("======= Bulk Read ========")); |
146 |
| - memcpy(&recv_position, read_result.xel[0].data, read_result.xel[0].length); |
147 |
| - memcpy(&recv_velocity, read_result.xel[1].data, read_result.xel[1].length); |
148 |
| - DEBUG_SERIAL.print(F("ID: "));DEBUG_SERIAL.print(read_result.xel[0].id);DEBUG_SERIAL.print(" "); |
149 |
| - DEBUG_SERIAL.print(F(", Present Position: "));DEBUG_SERIAL.print(recv_position);DEBUG_SERIAL.print(" "); |
150 |
| - DEBUG_SERIAL.print(F(", Packet Error: "));DEBUG_SERIAL.print(read_result.xel[0].error);DEBUG_SERIAL.print(" "); |
151 |
| - DEBUG_SERIAL.print(F(", Param Length: "));DEBUG_SERIAL.print(read_result.xel[0].length);DEBUG_SERIAL.print(" "); |
152 |
| - DEBUG_SERIAL.println(); |
153 |
| - DEBUG_SERIAL.print(F("ID: "));DEBUG_SERIAL.print(read_result.xel[1].id);DEBUG_SERIAL.print(" "); |
154 |
| - DEBUG_SERIAL.print(F(", Present Velocity: "));DEBUG_SERIAL.print(recv_velocity);DEBUG_SERIAL.print(" "); |
155 |
| - DEBUG_SERIAL.print(F(", Packet Error: "));DEBUG_SERIAL.print(read_result.xel[1].error);DEBUG_SERIAL.print(" "); |
156 |
| - DEBUG_SERIAL.print(F(", Param Length: "));DEBUG_SERIAL.print(read_result.xel[1].length);DEBUG_SERIAL.print(" "); |
157 |
| - DEBUG_SERIAL.println(); |
| 180 | + static uint32_t try_count = 0; |
| 181 | + uint8_t recv_cnt; |
| 182 | + |
| 183 | + bw_data_xel_1.goal_velocity+=5; |
| 184 | + if(bw_data_xel_1.goal_velocity >= 200){ |
| 185 | + bw_data_xel_1.goal_velocity = 0; |
| 186 | + } |
| 187 | + bw_data_xel_2.goal_position+=255; |
| 188 | + if(bw_data_xel_2.goal_position >= 1023){ |
| 189 | + bw_data_xel_2.goal_position = 0; |
| 190 | + } |
| 191 | + bw_infos.is_info_changed = true; |
| 192 | + |
| 193 | + DEBUG_SERIAL.print("\n>>>>>> Bulk Instruction Test : "); |
| 194 | + DEBUG_SERIAL.println(try_count++); |
| 195 | + if(dxl.bulkWrite(&bw_infos) == true){ |
| 196 | + DEBUG_SERIAL.println("[BulkWrite] Success"); |
| 197 | + DEBUG_SERIAL.print(" ID: ");DEBUG_SERIAL.print(bw_infos.p_xels[0].id); |
| 198 | + DEBUG_SERIAL.print("\t Goal Velocity: ");DEBUG_SERIAL.println(bw_data_xel_1.goal_velocity); |
| 199 | + |
| 200 | + DEBUG_SERIAL.print(" ID: ");DEBUG_SERIAL.print(bw_infos.p_xels[1].id); |
| 201 | + DEBUG_SERIAL.print("\t Goal Position: ");DEBUG_SERIAL.println(bw_data_xel_2.goal_position); |
| 202 | + }else{ |
| 203 | + DEBUG_SERIAL.print("[BulkWrite] Fail, Lib error code: "); |
| 204 | + DEBUG_SERIAL.print(dxl.getLastLibErrCode()); |
| 205 | + } |
158 | 206 | DEBUG_SERIAL.println();
|
159 |
| - delay(100); |
160 |
| -} |
161 | 207 |
|
| 208 | + delay(250); |
| 209 | + |
| 210 | + recv_cnt = dxl.bulkRead(&br_infos); |
| 211 | + if(recv_cnt > 0){ |
| 212 | + DEBUG_SERIAL.print("[BulkRead] Success, Received ID Count: "); |
| 213 | + DEBUG_SERIAL.println(recv_cnt); |
| 214 | + |
| 215 | + DEBUG_SERIAL.print(" ID: ");DEBUG_SERIAL.print(br_infos.p_xels[0].id); |
| 216 | + DEBUG_SERIAL.print(", Error: ");DEBUG_SERIAL.println(br_infos.p_xels[0].error); |
| 217 | + DEBUG_SERIAL.print("\t Present Current: ");DEBUG_SERIAL.println(br_data_xel_1.present_current); |
| 218 | + DEBUG_SERIAL.print("\t Present Velocity: ");DEBUG_SERIAL.println(br_data_xel_1.present_velocity); |
| 219 | + |
| 220 | + DEBUG_SERIAL.print(" ID: ");DEBUG_SERIAL.print(br_infos.p_xels[1].id); |
| 221 | + DEBUG_SERIAL.print(", Error: ");DEBUG_SERIAL.println(br_infos.p_xels[1].error); |
| 222 | + DEBUG_SERIAL.print("\t Present Position: ");DEBUG_SERIAL.println(br_data_xel_2.present_position); |
| 223 | + }else{ |
| 224 | + DEBUG_SERIAL.print("[BulkRead] Fail, Lib error code: "); |
| 225 | + DEBUG_SERIAL.print(dxl.getLastLibErrCode()); |
| 226 | + } |
| 227 | + DEBUG_SERIAL.println("======================================================="); |
| 228 | + |
| 229 | + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); |
| 230 | + delay(750); |
| 231 | +} |
162 | 232 |
|
0 commit comments