-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXM430.cpp
338 lines (307 loc) · 8.88 KB
/
XM430.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
#include "arduino.h"
#include "XM430.h"
int XM430::ReadError(void)
{
int TimeCounter = 0;
unsigned char IncomingByte;
int ErrorByte=-1;
digitalWrite(RS485_EN,LOW);
while((toRS485->available() < 5) & (TimeCounter < 10))
{
TimeCounter++;
delay(1000);
Serial.println("waiting data...");
}
while (toRS485->available() > 0)
{
IncomingByte = toRS485->read();
if ((IncomingByte == 255) & toRS485->peek() == 255 )
{
toRS485->read();
toRS485->read();
toRS485->read();
ErrorByte = toRS485->read();
return (ErrorByte);
}
}
return (-1);
}
XM430::XM430(SoftwareSerial * ss)
{
toRS485 = ss;
pinMode(RS485_EN, OUTPUT);
Serial.println("motor initialized");
}
void XM430::BeginRS485(uint32_t baudrate)
{
toRS485->begin(baudrate);
Serial.println("RS485 serial started");
}
void XM430::LedWrite(byte servoID, byte newValue)
{
unsigned char instruction = 0x03;
byte ledAddress = 0x41;
unsigned char length = 0x04;
int checksum_ACK;
byte notchecksum;
checksum_ACK = servoID + length + instruction + ledAddress + newValue;
notchecksum = ~checksum_ACK;
digitalWrite(RS485_EN,HIGH);
delay(15);
toRS485->write(0xFF);
toRS485->write(0xFF);
toRS485->write(servoID);
toRS485->write(length);
toRS485->write(instruction);
toRS485->write(ledAddress);
toRS485->write(newValue);
toRS485->write(notchecksum);
delay(15);
digitalWrite(RS485_EN,LOW);
}
void XM430::TorqueEnable(byte servoID, byte newValue)
{
unsigned char instruction = 0x03;
byte torqueAddress = 0x40;
unsigned char length = 0x04;
int checksum_ACK;
byte notchecksum;
checksum_ACK = servoID + length + instruction + torqueAddress + newValue;
notchecksum = (~checksum_ACK);
digitalWrite(RS485_EN,HIGH);
delay(15);
toRS485->write(0xFF);
toRS485->write(0xFF);
toRS485->write(servoID);
toRS485->write(length);
toRS485->write(instruction);
toRS485->write(torqueAddress);
toRS485->write(newValue);
toRS485->write(notchecksum);
delay(15);
digitalWrite(RS485_EN,LOW);
//readerror:
//Serial.print("ReadError:");
//Serial.println(ReadError());
}
void XM430::int32Splitting(uint32_t Position, unsigned char bytes[4])
{
bytes[0] = (Position & 0xFF000000) >> 24;
bytes[1] = (Position & 0x00FF0000) >> 16;
bytes[2] = (Position & 0x0000FF00) >> 8;
bytes[3] = (Position & 0x000000FF);
}
void XM430::Goto(byte servoID, int position)
{
unsigned char instruction = 0x03;
byte goalPositionAddress = 0x74;
unsigned char length = 0x07;
unsigned char positionbytes[4];
int32Splitting(position, positionbytes);
int checksum_ACK;
byte notchecksum;
checksum_ACK = servoID + length + instruction + goalPositionAddress + positionbytes[3] + positionbytes[2] + positionbytes[1] + positionbytes[0];
notchecksum = (~checksum_ACK);
digitalWrite(RS485_EN,HIGH);
delay(15);
toRS485->write(0xFF);
toRS485->write(0xFF);
toRS485->write(servoID);
toRS485->write(length);
toRS485->write(instruction);
toRS485->write(goalPositionAddress);
toRS485->write(positionbytes[3]);
toRS485->write(positionbytes[2]);
toRS485->write(positionbytes[1]);
toRS485->write(positionbytes[0]);
toRS485->write(notchecksum);
delay(15);
digitalWrite(RS485_EN,LOW);
}
void XM430::SyncWrite(byte servoID1, int position1, byte servoID2, int position2)
{
//Write the goal position of two motors in only one packet
unsigned char instruction = 0x83; //sync write
byte goalPositionAddress = 0x74;
unsigned char length = 0x04; // of data to write
unsigned char LENGTH = 0xE; //the full packet
unsigned char position1bytes[4];
unsigned char position2bytes[4];
int32Splitting(position1, position1bytes);
int32Splitting(position2, position2bytes);
int checksum_ACK;
byte notchecksum;
checksum_ACK = BROADCAST_ID + LENGTH + instruction + goalPositionAddress + length + servoID1 + position1bytes[3] + position1bytes[2] + position1bytes[1] + position1bytes[0] + servoID2 + position2bytes[3] + position2bytes[2] + position2bytes[1] + position2bytes[0];
notchecksum = (~checksum_ACK);
digitalWrite(RS485_EN,HIGH);
delay(15);
toRS485->write(0xFF);
toRS485->write(0xFF);
toRS485->write(0xFE);
toRS485->write(LENGTH);
toRS485->write(instruction);
toRS485->write(goalPositionAddress);
toRS485->write(length);
toRS485->write(servoID1);
toRS485->write(position1bytes[3]);
toRS485->write(position1bytes[2]);
toRS485->write(position1bytes[1]);
toRS485->write(position1bytes[0]);
toRS485->write(servoID2);
toRS485->write(position2bytes[3]);
toRS485->write(position2bytes[2]);
toRS485->write(position2bytes[1]);
toRS485->write(position2bytes[0]);
toRS485->write(notchecksum);
delay(15);
digitalWrite(RS485_EN,LOW);
Serial.println("Send packet ?");
}
void XM430::SetP(byte servoID, uint16_t P)
{
unsigned char instruction = 0x03;
byte Paddress = 0x54;
unsigned char pbytes[2];
pbytes[0] = P >> 8;
pbytes[1] = P;
unsigned char length = 0x05;
int checksum_ACK;
byte notchecksum;
checksum_ACK = servoID + length + instruction + Paddress + pbytes[0] + pbytes[1];
notchecksum = ~checksum_ACK;
digitalWrite(RS485_EN,HIGH);
delay(15);
toRS485->write(0xFF);
toRS485->write(0xFF);
toRS485->write(servoID);
toRS485->write(length);
toRS485->write(instruction);
toRS485->write(Paddress);
toRS485->write(pbytes[0]);
toRS485->write(pbytes[1]);
toRS485->write(notchecksum);
delay(15);
digitalWrite(RS485_EN,LOW);
}
void XM430::ReadP(byte servoID)
{
unsigned char instruction = 0x02; //read
byte Paddress = 0x54; //P address (84)
unsigned char length = 0x04; //Length = number of Parameters + 2
int checksum_ACK;
byte notchecksum;
byte toread = 0x02;
checksum_ACK = servoID + length + instruction + Paddress + toread;
notchecksum = ~checksum_ACK;
// Packet Generation:
digitalWrite(RS485_EN,HIGH);
delay(15);
toRS485->write(0xFF);
toRS485->write(0xFF);
toRS485->write(servoID);
toRS485->write(length);
toRS485->write(instruction);
toRS485->write(Paddress);
toRS485->write(toread);
toRS485->write(notchecksum);
delay(15);
digitalWrite(RS485_EN,LOW);
//Try to get the result:
unsigned char Incoming_Byte;
unsigned char P_Low_Byte;
unsigned char P_High_Byte;
unsigned char Error_Byte;
int P_Long_Byte = -1;
int Time_Counter = 0;
digitalWrite(RS485_EN,LOW);
while((toRS485->available() < 8) & (Time_Counter < TIME_OUT))
{
Time_Counter++;
delay(1000);
Serial.println("waiting for data...");
Serial.println(toRS485->available());
}
while(toRS485->available() > 0)
{
Incoming_Byte = toRS485->read();
if((Incoming_Byte == 255) & (toRS485->peek() == 255))
{
toRS485->read(); // Second Start bit
toRS485->read(); // servoID
toRS485->read(); // Length
if((Error_Byte = toRS485->read()) != 0)
{
Serial.println("Error...");
}
P_Low_Byte = toRS485->read();
Serial.print(P_Low_Byte);
P_High_Byte = toRS485->read();
Serial.print(P_High_Byte);
P_Long_Byte = P_High_Byte << 8;
P_Long_Byte = P_Long_Byte + P_Low_Byte;
}
}
Serial.print("Read Value for P :");
Serial.println(P_Long_Byte);
}
void XM430::SetI(byte servoID, uint16_t I)
{
unsigned char instruction = 0x03;
byte Iaddress = 0x52; //I address (82)
unsigned char ibytes[2];
ibytes[0] = I >> 8;
ibytes[1] = I;
unsigned char length = 0x05;
int checksum_ACK;
byte notchecksum;
checksum_ACK = servoID + length + instruction + Iaddress + ibytes[0] + ibytes[1];
notchecksum = ~checksum_ACK;
digitalWrite(RS485_EN,HIGH);
delay(15);
toRS485->write(0xFF);
toRS485->write(0xFF);
toRS485->write(servoID);
toRS485->write(length);
toRS485->write(instruction);
toRS485->write(Iaddress);
toRS485->write(ibytes[0]);
toRS485->write(ibytes[1]);
toRS485->write(notchecksum);
delay(15);
digitalWrite(RS485_EN,LOW);
}
void XM430::SetD(byte servoID, uint16_t D)
{
unsigned char instruction = 0x03; //write
byte Daddress = 0x50; //D address (80)
unsigned char dbytes[2];
dbytes[0] = D >> 8;
dbytes[1] = D;
unsigned char length = 0x05;
int checksum_ACK;
byte notchecksum;
checksum_ACK = servoID + length + instruction + Daddress + dbytes[0] + dbytes[1];
notchecksum = ~checksum_ACK;
// Packet Generation:
digitalWrite(RS485_EN,HIGH);
delay(15);
toRS485->write(0xFF);
toRS485->write(0xFF);
toRS485->write(servoID);
toRS485->write(length);
toRS485->write(instruction);
toRS485->write(Daddress);
toRS485->write(dbytes[0]);
toRS485->write(dbytes[1]);
toRS485->write(notchecksum);
delay(15);
digitalWrite(RS485_EN,LOW);
}
void XM430::SetPID(byte servoID, uint16_t P, uint16_t I, uint16_t D)
{
Serial.print("Setting PID for motor: ");
Serial.println(servoID, DEC);
SetP(servoID, P);
SetI(servoID, I);
SetD(servoID, D);
}