/
sync_read_write_velocity.ino
226 lines (197 loc) · 8.28 KB
/
sync_read_write_velocity.ino
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
/*******************************************************************************
* Copyright 2016 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.
*******************************************************************************/
#include <Dynamixel2Arduino.h>
// Please modify it to suit your hardware.
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_MEGA2560) // When using DynamixelShield
#include <SoftwareSerial.h>
SoftwareSerial soft_serial(7, 8); // DYNAMIXELShield UART RX/TX
#define DXL_SERIAL Serial
#define DEBUG_SERIAL soft_serial
const int DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN
#elif defined(ARDUINO_SAM_DUE) // When using DynamixelShield
#define DXL_SERIAL Serial
#define DEBUG_SERIAL SerialUSB
const int DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN
#elif defined(ARDUINO_SAM_ZERO) // When using DynamixelShield
#define DXL_SERIAL Serial1
#define DEBUG_SERIAL SerialUSB
const int DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN
#elif defined(ARDUINO_OpenCM904) // When using official ROBOTIS board with DXL circuit.
#define DXL_SERIAL Serial3 //OpenCM9.04 EXP Board's DXL port Serial. (Serial1 for the DXL port on the OpenCM 9.04 board)
#define DEBUG_SERIAL Serial
const int DXL_DIR_PIN = 22; //OpenCM9.04 EXP Board's DIR PIN. (28 for the DXL port on the OpenCM 9.04 board)
#elif defined(ARDUINO_OpenCR) // When using official ROBOTIS board with DXL circuit.
// For OpenCR, there is a DXL Power Enable pin, so you must initialize and control it.
// Reference link : https://github.com/ROBOTIS-GIT/OpenCR/blob/master/arduino/opencr_arduino/opencr/libraries/DynamixelSDK/src/dynamixel_sdk/port_handler_arduino.cpp#L78
#define DXL_SERIAL Serial3
#define DEBUG_SERIAL Serial
const int DXL_DIR_PIN = 84; // OpenCR Board's DIR PIN.
#elif defined(ARDUINO_OpenRB) // When using OpenRB-150
//OpenRB does not require the DIR control pin.
#define DXL_SERIAL Serial1
#define DEBUG_SERIAL Serial
const int DXL_DIR_PIN = -1;
#else // Other boards when using DynamixelShield
#define DXL_SERIAL Serial1
#define DEBUG_SERIAL Serial
const int DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN
#endif
/* syncRead
DYNAMIXEL PROTOCOL 1.0 does NOT support Sync Read feature.
Structures containing the necessary information to process the 'syncRead' packet.
typedef struct XELInfoBulkRead{
uint16_t addr;
uint16_t addr_length;
uint8_t *p_recv_buf;
uint8_t id;
uint8_t error;
} __attribute__((packed)) XELInfoBulkRead_t;
typedef struct InfoBulkReadInst{
XELInfoBulkRead_t* p_xels;
uint8_t xel_count;
bool is_info_changed;
InfoSyncBulkBuffer_t packet;
} __attribute__((packed)) InfoBulkReadInst_t;
*/
/* syncWrite
DYNAMIXEL PROTOCOL 1.0 supports Control Table address up to 255.
Structures containing the necessary information to process the 'syncWrite' packet.
typedef struct XELInfoBulkWrite{
uint16_t addr;
uint16_t addr_length;
uint8_t* p_data;
uint8_t id;
} __attribute__((packed)) XELInfoBulkWrite_t;
typedef struct InfoBulkWriteInst{
XELInfoBulkWrite_t* p_xels;
uint8_t xel_count;
bool is_info_changed;
InfoSyncBulkBuffer_t packet;
} __attribute__((packed)) InfoBulkWriteInst_t;
*/
const uint8_t BROADCAST_ID = 254;
const float DYNAMIXEL_PROTOCOL_VERSION = 2.0;
const uint8_t DXL_ID_CNT = 2;
const uint8_t DXL_ID_LIST[DXL_ID_CNT] = {1, 2};
const uint16_t user_pkt_buf_cap = 128;
uint8_t user_pkt_buf[user_pkt_buf_cap];
const uint16_t SR_START_ADDR = 126; // Starting Data Addr, Can differ Depending on what address to access
const uint16_t SR_ADDR_LEN = 10; // Data Length (2+4+4), Can differ depending on how many address to access.
const uint16_t SW_START_ADDR = 104;
const uint16_t SW_ADDR_LEN = 4;
typedef struct sr_data{
int16_t present_current;
int32_t present_velocity;
int32_t present_position;
} __attribute__((packed)) sr_data_t;
typedef struct sw_data{
int32_t goal_velocity;
} __attribute__((packed)) sw_data_t;
sr_data_t sr_data[DXL_ID_CNT];
DYNAMIXEL::InfoSyncReadInst_t sr_infos;
DYNAMIXEL::XELInfoSyncRead_t info_xels_sr[DXL_ID_CNT];
sw_data_t sw_data[DXL_ID_CNT];
DYNAMIXEL::InfoSyncWriteInst_t sw_infos;
DYNAMIXEL::XELInfoSyncWrite_t info_xels_sw[DXL_ID_CNT];
Dynamixel2Arduino dxl(DXL_SERIAL, DXL_DIR_PIN);
//This namespace is required to use Control table item names
using namespace ControlTableItem;
void setup() {
// put your setup code here, to run once:
uint8_t i;
pinMode(LED_BUILTIN, OUTPUT);
DEBUG_SERIAL.begin(115200);
dxl.begin(57600);
dxl.setPortProtocolVersion(DYNAMIXEL_PROTOCOL_VERSION);
for(i=0; i<DXL_ID_CNT; i++){
dxl.torqueOff(DXL_ID_LIST[i]);
dxl.setOperatingMode(DXL_ID_LIST[i], OP_VELOCITY);
}
dxl.torqueOn(BROADCAST_ID);
// Fill the members of structure to syncRead using external user packet buffer
sr_infos.packet.p_buf = user_pkt_buf;
sr_infos.packet.buf_capacity = user_pkt_buf_cap;
sr_infos.packet.is_completed = false;
sr_infos.addr = SR_START_ADDR;
sr_infos.addr_length = SR_ADDR_LEN;
sr_infos.p_xels = info_xels_sr;
sr_infos.xel_count = 0;
for(i=0; i<DXL_ID_CNT; i++){
info_xels_sr[i].id = DXL_ID_LIST[i];
info_xels_sr[i].p_recv_buf = (uint8_t*)&sr_data[i];
sr_infos.xel_count++;
}
sr_infos.is_info_changed = true;
// Fill the members of structure to syncWrite using internal packet buffer
sw_infos.packet.p_buf = nullptr;
sw_infos.packet.is_completed = false;
sw_infos.addr = SW_START_ADDR;
sw_infos.addr_length = SW_ADDR_LEN;
sw_infos.p_xels = info_xels_sw;
sw_infos.xel_count = 0;
sw_data[0].goal_velocity = 0;
sw_data[1].goal_velocity = 100;
for(i=0; i<DXL_ID_CNT; i++){
info_xels_sw[i].id = DXL_ID_LIST[i];
info_xels_sw[i].p_data = (uint8_t*)&sw_data[i].goal_velocity;
sw_infos.xel_count++;
}
sw_infos.is_info_changed = true;
}
void loop() {
// put your main code here, to run repeatedly:
static uint32_t try_count = 0;
uint8_t i, recv_cnt;
for(i = 0; i < DXL_ID_CNT; i++){
sw_data[i].goal_velocity = 5 + sw_data[i].goal_velocity;
if(sw_data[i].goal_velocity >= 200){
sw_data[i].goal_velocity = 0;
}
}
sw_infos.is_info_changed = true;
DEBUG_SERIAL.print("\n>>>>>> Sync Instruction Test : ");
DEBUG_SERIAL.println(try_count++);
if(dxl.syncWrite(&sw_infos) == true){
DEBUG_SERIAL.println("[SyncWrite] Success");
for(i=0; i<sw_infos.xel_count; i++){
DEBUG_SERIAL.print(" ID: ");DEBUG_SERIAL.println(sw_infos.p_xels[i].id);
DEBUG_SERIAL.print("\t Goal Velocity: ");DEBUG_SERIAL.println(sw_data[i].goal_velocity);
}
}else{
DEBUG_SERIAL.print("[SyncWrite] Fail, Lib error code: ");
DEBUG_SERIAL.print(dxl.getLastLibErrCode());
}
DEBUG_SERIAL.println();
delay(250);
recv_cnt = dxl.syncRead(&sr_infos);
if(recv_cnt > 0){
DEBUG_SERIAL.print("[SyncRead] Success, Received ID Count: ");
DEBUG_SERIAL.println(recv_cnt);
for(i=0; i<recv_cnt; i++){
DEBUG_SERIAL.print(" ID: ");DEBUG_SERIAL.print(sr_infos.p_xels[i].id);
DEBUG_SERIAL.print(", Error: ");DEBUG_SERIAL.println(sr_infos.p_xels[i].error);
DEBUG_SERIAL.print("\t Present Current: ");DEBUG_SERIAL.println(sr_data[i].present_current);
DEBUG_SERIAL.print("\t Present Velocity: ");DEBUG_SERIAL.println(sr_data[i].present_velocity);
DEBUG_SERIAL.print("\t Present Position: ");DEBUG_SERIAL.println(sr_data[i].present_position);
}
}else{
DEBUG_SERIAL.print("[SyncRead] Fail, Lib error code: ");
DEBUG_SERIAL.println(dxl.getLastLibErrCode());
}
DEBUG_SERIAL.println("=======================================================");
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(750);
}