-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDYN232M_Arduino_SampleCode.ino
More file actions
343 lines (314 loc) · 10.2 KB
/
DYN232M_Arduino_SampleCode.ino
File metadata and controls
343 lines (314 loc) · 10.2 KB
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
/*
DYN232M_Arduino_SourceCode.ino - DMM Technology Corp. DYN2 and DYN4 AC Servo drive Arduino UART RS232 communication code.
Version 1.0 - Release May 2018
Distributed By: DMM Technology Corp.
www.dmm-tech.com
*/
/**/
#define Go_Absolute_Pos 0x01
#define Go_Relative_Pos 0x03
#define Is_AbsPos32 0x1b
#define General_Read 0x0e
#define Is_TrqCurrent 0x1E
#define Read_MainGain 0x18
#define Set_MainGain 0x10
#define Set_SpeedGain 0x11
#define Set_IntGain 0x12
#define Set_HighSpeed 0x14
#define Set_HighAccel 0x15
#define Set_Pos_OnRange 0x16
#define Is_MainGain 0x10
#define Is_SpeedGain 0x11
#define Is_IntGain 0x12
#define Is_TrqCons 0x13
#define Is_HighSpeed 0x14
#define Is_HighAccel 0x15
#define Is_Driver_ID 0x16
#define Is_Pos_OnRange 0x17
#define Is_Status 0x19
#define Is_Config 0x1a
// Add additional function code according to user requirement
/*Variables*/
char InputBuffer[256]; //Input buffer from RS232,
char OutputBuffer[256]; //Output buffer to RS232,
unsigned char InBfTopPointer, InBfBtmPointer; //input buffer pointers
unsigned char OutBfTopPointer, OutBfBtmPointer; //output buffer pointers
unsigned char Read_Package_Buffer[8], Read_Num, Read_Package_Length, Global_Func;
unsigned char MotorPosition32Ready_Flag, MotorTorqueCurrentReady_Flag, MainGainRead_Flag;
unsigned char Driver_MainGain, Driver_SpeedGain, Driver_IntGain, Driver_TrqCons, Driver_HighSpeed, Driver_HighAccel,Driver_ReadID,Driver_Status,Driver_Config,Driver_OnRange;
long Motor_Pos32, MotorTorqueCurrent;
/*Prototypes*/
void move_rel32(char ID, long pos);
void ReadMotorTorqueCurrent(char ID);
void ReadMotorPosition32(char ID);
void move_abs32(char MotorID, long Pos32);
void Turn_const_speed(char ID, long spd);
void ReadPackage(void);
void Get_Function(void);
long Cal_SignValue(unsigned char One_Package[8]);
long Cal_Value(unsigned char One_Package[8]);
void Send_Package(char ID , long Displacement);
void Make_CRC_Send(unsigned char Plength, unsigned char B[8]);
/**/
void setup()
{
Serial.begin(38400); // Serial0 used to write to Serial Monitor
Serial3.begin(38400); // Servo drive connected to Serial3
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("PROGRAM STARTING");
}
void loop()
{
Serial.println("STARTING LOOP");
digitalWrite(LED_BUILTIN, HIGH);
move_rel32(1, 65536); delay(250);
digitalWrite(LED_BUILTIN, LOW);
move_rel32(2, 16384); delay(250);
digitalWrite(LED_BUILTIN, HIGH);
move_rel32(3, -65536); delay(1000);
ReadMotorPosition32(1); Serial.print("Axis 1 Encoder Position: "); Serial.println(Motor_Pos32);
ReadMotorPosition32(2); Serial.print("Axis 2 Encoder Position: "); Serial.println(Motor_Pos32);
ReadMotorPosition32(3); Serial.print("Axis 3 Encoder Position: "); Serial.println(Motor_Pos32);
digitalWrite(LED_BUILTIN, LOW);
move_abs32(1, 0);
move_abs32(2, 0);
move_abs32(3, 0); delay(1000);
Serial.println("END LOOP"); Serial.println("");
}
/////////////////////////// SAMPLE COMMAND FUNCTIONS ////////////////////////////////////
void move_rel32(char ID, long pos)
{
char Axis_Num = ID;
Global_Func = (char)Go_Relative_Pos;
Send_Package(Axis_Num, pos);
}
void ReadMotorTorqueCurrent(char ID)
{
Global_Func = General_Read;
Send_Package(ID , Is_TrqCurrent);
MotorTorqueCurrentReady_Flag = 0xff;
while(MotorTorqueCurrentReady_Flag != 0x00)
{
ReadPackage();
}
}
void ReadMotorPosition32(char ID)
{
Global_Func = (char)General_Read;
Send_Package(ID , Is_AbsPos32);
MotorPosition32Ready_Flag = 0xff;
while(MotorPosition32Ready_Flag != 0x00)
{
ReadPackage();
}
}
void move_abs32(char MotorID, long Pos32)
{
char Axis_Num = MotorID;
Global_Func = (char)Go_Absolute_Pos;
Send_Package(Axis_Num, Pos32);
}
void Turn_const_speed(char ID, long spd)
{
char Axis_Num = ID;
Global_Func = 0x0a;
Send_Package(Axis_Num, spd);
}
////////////////////// DYN232M SERIAL PROTOCOL PACKAGE FUNCTIONS ///////////////////////////////
void ReadPackage(void)
{
unsigned char c, cif;
while (Serial3.available() > 0) {
InputBuffer[InBfTopPointer] = Serial3.read(); //Load InputBuffer with received packets
InBfTopPointer++;
}
while (InBfBtmPointer != InBfTopPointer)
{
c = InputBuffer[InBfBtmPointer];
InBfBtmPointer++;
cif = c & 0x80;
if (cif == 0) {
Read_Num = 0;
Read_Package_Length = 0;
}
if (cif == 0 || Read_Num > 0) {
Read_Package_Buffer[Read_Num] = c;
Read_Num++;
if (Read_Num == 2) {
cif = c >> 5;
cif = cif & 0x03;
Read_Package_Length = 4 + cif;
c = 0;
}
if (Read_Num == Read_Package_Length) {
Get_Function();
Read_Num = 0;
Read_Package_Length = 0;
}
}
}
}
void Get_Function(void) {
char ID, ReceivedFunction_Code, CRC_Check;
long Temp32;
ID = Read_Package_Buffer[0] & 0x7f;
ReceivedFunction_Code = Read_Package_Buffer[1] & 0x1f;
CRC_Check = 0;
for (int i = 0; i < Read_Package_Length - 1; i++) {
CRC_Check += Read_Package_Buffer[i];
}
CRC_Check ^= Read_Package_Buffer[Read_Package_Length - 1];
CRC_Check &= 0x7f;
if (CRC_Check != 0) {
}
else {
switch (ReceivedFunction_Code)
{
case Is_AbsPos32:
Motor_Pos32 = Cal_SignValue(Read_Package_Buffer);
MotorPosition32Ready_Flag = 0x00;
break;
case Is_TrqCurrent:
MotorTorqueCurrent = Cal_SignValue(Read_Package_Buffer);
break;
case Is_Status:
Driver_Status = (char)Cal_SignValue(Read_Package_Buffer);
// Driver_Status=drive status byte data
break;
case Is_Config:
Temp32 = Cal_Value(Read_Package_Buffer);
//Driver_Config = drive configuration setting
break;
case Is_MainGain:
Driver_MainGain = (char)Cal_SignValue(Read_Package_Buffer);
Driver_MainGain = Driver_MainGain & 0x7f;
break;
case Is_SpeedGain:
Driver_SpeedGain = (char)Cal_SignValue(Read_Package_Buffer);
Driver_SpeedGain = Driver_SpeedGain & 0x7f;
break;
case Is_IntGain:
Driver_IntGain = (char)Cal_SignValue(Read_Package_Buffer);
Driver_IntGain = Driver_IntGain & 0x7f;
break;
case Is_TrqCons:
Driver_TrqCons = (char)Cal_SignValue(Read_Package_Buffer);
Driver_TrqCons = Driver_TrqCons & 0x7f;
break;
case Is_HighSpeed:
Driver_HighSpeed = (char)Cal_SignValue(Read_Package_Buffer);
Driver_HighSpeed = Driver_HighSpeed & 0x7f;
break;
case Is_HighAccel:
Driver_HighAccel = (char)Cal_SignValue(Read_Package_Buffer);
Driver_HighAccel = Driver_HighAccel & 0x7f;
break;
case Is_Driver_ID:
Driver_ReadID = ID;
break;
case Is_Pos_OnRange:
Driver_OnRange = (char)Cal_SignValue(Read_Package_Buffer);
Driver_OnRange = Driver_OnRange & 0x7f;
break;
}
}
}
long Cal_SignValue(unsigned char One_Package[8]) //Get data with sign - long
{
char Package_Length,OneChar,i;
long Lcmd;
OneChar = One_Package[1];
OneChar = OneChar>>5;
OneChar = OneChar&0x03;
Package_Length = 4 + OneChar;
OneChar = One_Package[2]; /*First byte 0x7f, bit 6 reprents sign */
OneChar = OneChar << 1;
Lcmd = (long)OneChar; /* Sign extended to 32bits */
Lcmd = Lcmd >> 1;
for(i=3;i<Package_Length-1;i++)
{
OneChar = One_Package[i];
OneChar &= 0x7f;
Lcmd = Lcmd<<7;
Lcmd += OneChar;
}
return(Lcmd); /* Lcmd : -2^27 ~ 2^27 - 1 */
}
long Cal_Value(unsigned char One_Package[8])
{
char Package_Length,OneChar,i;
long Lcmd;
OneChar = One_Package[1];
OneChar = OneChar>>5;
OneChar = OneChar&0x03;
Package_Length = 4 + OneChar;
OneChar = One_Package[2]; /*First byte 0x7f, bit 6 reprents sign */
OneChar &= 0x7f;
Lcmd = (long)OneChar; /*Sign extended to 32bits */
for(i=3;i<Package_Length-1;i++)
{
OneChar = One_Package[i];
OneChar &= 0x7f;
Lcmd = Lcmd<<7;
Lcmd += OneChar;
}
return(Lcmd); /*Lcmd : -2^27 ~ 2^27 - 1 */
}
void Send_Package(char ID , long Displacement) {
unsigned char B[8], Package_Length, Function_Code;
long TempLong;
B[1] = B[2] = B[3] = B[4] = B[5] = (unsigned char)0x80;
B[0] = ID & 0x7f;
Function_Code = Global_Func & 0x1f;
TempLong = Displacement & 0x0fffffff; //Max 28bits
B[5] += (unsigned char)TempLong & 0x0000007f;
TempLong = TempLong >> 7;
B[4] += (unsigned char)TempLong & 0x0000007f;
TempLong = TempLong >> 7;
B[3] += (unsigned char)TempLong & 0x0000007f;
TempLong = TempLong >> 7;
B[2] += (unsigned char)TempLong & 0x0000007f;
Package_Length = 7;
TempLong = Displacement;
TempLong = TempLong >> 20;
if (( TempLong == 0x00000000) || ( TempLong == 0xffffffff)) { //Three byte data
B[2] = B[3];
B[3] = B[4];
B[4] = B[5];
Package_Length = 6;
}
TempLong = Displacement;
TempLong = TempLong >> 13;
if (( TempLong == 0x00000000) || ( TempLong == 0xffffffff)) { //Two byte data
B[2] = B[3];
B[3] = B[4];
Package_Length = 5;
}
TempLong = Displacement;
TempLong = TempLong >> 6;
if (( TempLong == 0x00000000) || ( TempLong == 0xffffffff)) { //One byte data
B[2] = B[3];
Package_Length = 4;
}
B[1] += (Package_Length - 4) * 32 + Function_Code;
Make_CRC_Send(Package_Length, B);
}
void Make_CRC_Send(unsigned char Plength, unsigned char B[8]) {
unsigned char Error_Check = 0;
char RS232_HardwareShiftRegister;
for (int i = 0; i < Plength - 1; i++) {
OutputBuffer[OutBfTopPointer] = B[i];
OutBfTopPointer++;
Error_Check += B[i];
}
Error_Check = Error_Check | 0x80;
OutputBuffer[OutBfTopPointer] = Error_Check;
OutBfTopPointer++;
while (OutBfBtmPointer != OutBfTopPointer) {
RS232_HardwareShiftRegister = OutputBuffer[OutBfBtmPointer];
//Serial.print("RS232_HardwareShiftRegister: ");
//Serial.println(RS232_HardwareShiftRegister, DEC);
Serial3.write(RS232_HardwareShiftRegister);
OutBfBtmPointer++; // Change to next byte in OutputBuffer to send
}
}