-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEpos4.ino
382 lines (358 loc) · 11.1 KB
/
Epos4.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*******************************************************************************************************/
/* Maxon Epos 4 communication for Arduino
Apr 21 2018 Denis Gayraud [email protected]
EPOS4 doumentation:
http://academy.maxonjapan.co.jp/wp-content/uploads/manual/epos4/EPOS4-Communication-Guide-En.pdf
http://academy.maxonjapan.co.jp/wp-content/uploads/manual/epos4/EPOS4-Firmware-Specification-En.pdf
Copyright 2018 Denis Gayraud (dgayraud[at] club-internet [dot] fr)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.*/
/*******************************************************************************************************/
#include <SoftwareSerial.h> // For Arduino uno
SoftwareSerial SerialEpcos(2, 3); // RX, TX for arduino uno
//#define SerialEpcos Serial1 // pro micro or leonardo
#define ReceiveTimeOut 3000 // Maximum for waiting answer from EPOS4 in milli second
#define MaxLenFrame 10 // max number of words in frame
byte DataRead[264];
byte ReadOpcode;
byte len;
int pDataRead;
word rxCRC;
unsigned long CommErrorCode;
enum read_status {
RX_DLE,
RX_STX,
RX_OPCODE,
RX_LEN,
RX_DATA,
RX_CRC_LOW,
RX_CRC_HIGH,
RX_DONE,
ESC_OPCODE,
ESC_LEN,
ESC_DATA,
ESC_CRC_LOW,
ESC_CRC_HIGH
}read_st;
/*****************************************************************************************************/
/* CalcFieldCRC : code from EPOS4-Communication-Guide-En.pdf */
/*****************************************************************************************************/
word CalcFieldCRC(word* pDataArray, word numberOfints)
{
word shifter, c;
word carry;
word CRC = 0;
//Calculate pDataArray Word by Word
while(numberOfints--)
{
shifter = 0x8000; //Initialize BitX to Bit15
c = *pDataArray++; //Copy next DataWord to c
do
{
carry = CRC & 0x8000; //Check if Bit15 of CRC is set
CRC <<= 1; //CRC = CRC * 2
if(c & shifter) CRC++; //CRC = CRC + 1, if BitX is set in c
if(carry) CRC ^= 0x1021; //CRC = CRC XOR G(x), if carry is true
shifter >>= 1; //Set BitX to next lower Bit, shifter = shifter/2
} while(shifter);
}
return CRC;
}
inline void SerialEpcosStuffing(byte BYTE)
{
if (BYTE==0x90) SerialEpcos.write(BYTE);
SerialEpcos.write(BYTE);
}
void SendFrame(byte OpCode,word* pDataArray,byte numberOfwords)
{
word CRC;
word pDataCRC[MaxLenFrame+2];
pDataCRC[0] = OpCode | (numberOfwords<<8);
for (int i=0; i<numberOfwords ; i++)
{
pDataCRC[i+1]=pDataArray[i];
}
pDataCRC[numberOfwords+1]=0x0000;
CRC=CalcFieldCRC(pDataCRC, (word)(numberOfwords+2));
SerialEpcos.write(0x90); // DLE=Data Link Escape
SerialEpcos.write(0x02); // STX=Start of Text
SerialEpcosStuffing(OpCode);
SerialEpcosStuffing(numberOfwords);
for (int i=0; i<numberOfwords ; i++)
{
SerialEpcosStuffing(lowByte(pDataArray[i]));
SerialEpcosStuffing(highByte(pDataArray[i]));
}
SerialEpcosStuffing(lowByte(CRC));
SerialEpcosStuffing(highByte(CRC));
}
bool WriteObject(word Index, byte SubIndex,word* pArray)
{
word data[4];
data[0]= 0x01 | (lowByte(Index)<<8); // NodeId=1
data[1]= highByte(Index)|(((word)SubIndex )<< 8);
data[2]=pArray[0];
data[3]=pArray[1];
SendFrame(0x68,data,(byte)4);
return ReadFrame();
}
bool ReadObject(word Index, byte SubIndex)
{
word data[2];
data[0]= 0x01 | (lowByte(Index)<<8); // NodeId=1
data[1]= highByte(Index)|(((word)SubIndex )<< 8);
SendFrame(0x60,data,(byte)2);
return ReadFrame();
}
// For debuging:
void print_rcv_data()
{
//todo check CRC and print data
Serial. print("OPcode: ");
Serial. print(ReadOpcode,HEX);
Serial. print(" len: ");
Serial. print(len);
Serial. print(" Data:");
for (int i=0; i<2*len ; i++)
{
Serial.print(DataRead[i],HEX);
Serial.print(" , ");
}
Serial.print("CRC: ");
Serial.println(rxCRC, HEX);
}
bool ReadFrame()
{
int incomingByte = 0; // for incoming SerialEpcos data
unsigned long timer=millis();
read_st=RX_DLE;
word pDataCRC[MaxLenFrame+2];
CommErrorCode=0;
while ((read_st!=RX_DONE) and (millis()-timer < ReceiveTimeOut))
{
if (SerialEpcos.available() > 0)
{
incomingByte = SerialEpcos.read();
// Serial.print("Read: ");
// Serial.println(incomingByte, HEX);
// Serial.print("read_st: ");
// Serial.println(read_st);
switch (read_st)
{
case RX_DLE:
if (incomingByte==0x90) read_st=RX_STX;
pDataRead=0;
len=0;
break;
case RX_STX:
if (incomingByte==0x02) read_st=RX_OPCODE;
else read_st=RX_DLE;
break;
case RX_OPCODE:
if ( incomingByte == 0x90)
{
read_st=ESC_OPCODE;
break;
}
else
{
ReadOpcode=incomingByte;
read_st=RX_LEN;
}
break;
case RX_LEN:
len=incomingByte;
if ( incomingByte == 0x90)
{
read_st=ESC_LEN;
break;
}
else read_st=RX_DATA;
break;
case RX_DATA:
if ( incomingByte == 0x90)
{
read_st=ESC_DATA;
break;
}
else
{
DataRead[pDataRead]=incomingByte;
pDataRead++;
}
if ( pDataRead== (2*len) ) read_st=RX_CRC_LOW;
break;
case RX_CRC_LOW:
rxCRC=incomingByte;
if ( incomingByte == 0x90) read_st=ESC_CRC_LOW;
else read_st=RX_CRC_HIGH;
break;
case RX_CRC_HIGH:
rxCRC +=incomingByte<<8;
if ( incomingByte == 0x90) read_st=ESC_CRC_HIGH;
else read_st=RX_DONE;
break;
case ESC_OPCODE:
if (incomingByte== 0x02)
{
read_st=RX_OPCODE;
break;
}
else if (incomingByte== 0x90)
{
ReadOpcode=incomingByte;
read_st=RX_LEN;
break;
}
else Serial.println ("Protocol error: single DLE");
break;
case ESC_LEN:
if (incomingByte== 0x90)
{
read_st=RX_DATA;
break;
}
if (incomingByte== 0x02)
{
read_st=RX_OPCODE;
break;
}
Serial.println ("Protocol error: Escape len error");
break;
case ESC_DATA:
if ( incomingByte == 0x90)
{
DataRead[pDataRead]=incomingByte;
pDataRead++;
read_st=RX_DATA;
if ( pDataRead== (2*len) ) read_st=RX_CRC_LOW;
break;
}
if (incomingByte== 0x02)
{
read_st=RX_OPCODE;
break;
}
Serial.println ("Protocol error: Escape data error");
break;
case ESC_CRC_LOW:
if ( incomingByte == 0x90) read_st=RX_CRC_HIGH;
else if (incomingByte== 0x02) read_st=RX_OPCODE;
else Serial.println ("Protocol error: Escape crc l error");
break;
case ESC_CRC_HIGH:
if ( incomingByte == 0x90) read_st=RX_DONE;
else if (incomingByte== 0x02) read_st=RX_OPCODE;
else Serial.println ("Protocol error: Escape crc h error");
break;
default:
break;
}
}
}
// Check Time out:
if (millis()-timer >= ReceiveTimeOut)
{
Serial.println("Serial Time out");
return false;
}
// check CRC:
pDataCRC[0] = ReadOpcode | ((word)len)<<8;
for (int i=0 ; i< len ;i++ )
{
pDataCRC[i+1]= DataRead[2*i] | (((word)DataRead[2*i+1])<<8);
}
pDataCRC[len+1]=0x0000;
if (CalcFieldCRC(pDataCRC, (word)(len+2))!= rxCRC)
{
Serial.println("Error CRC");
return false;
}
// Serial.print("RCV CRC: ");Serial.println (CalcFieldCRC(pDataCRC, (word)(len+2)),HEX);
// Check communication error code
CommErrorCode= DataRead[0] | (((word)DataRead[1])<<8) | (((unsigned long)DataRead[2])<<16) | (((unsigned long)DataRead[3])<<24);
if (CommErrorCode!=0)
{
Serial.print( "Communication Error Code:");
Serial.println(CommErrorCode,HEX);
return false;
}
// print_rcv_data();
return true;
}
word ReadStatusWord()
{
word statusWord;
ReadObject(0x6041,0);
statusWord = DataRead[4] + (((word) DataRead[5])<<8);
Serial.print("Statusword: ");
Serial.println(statusWord,HEX);
return (statusWord);
}
/* This code enable driver, run motor for 5s and stop.
Motor configuration must be setup with EPOS studio and saved in controller*/
void setup()
{
word data[4];
// start serial port at 9600 bps and wait for port to open:
Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB port only
SerialEpcos.begin(9600);
// send 1000 in profile acc:
//Serial.println("send 1000 in profile acc");
data[0]=1000;
data[1]=0x0000;
WriteObject(0x6083,0,data);
// send 1000 in profile dec:
//Serial.println("send 1000 in profile dec");
WriteObject(0x6084,0,data);
// Send 1000 in taget velocity
//Serial.println("send 1000 in taget velocity");
data[0]=1000;
WriteObject (0x60FF,0,data);
//Todo: wait for ready to switch on
// if not ready to switch on : send shutdown command
if (!(ReadStatusWord() & 0x01))
{
Serial.println("Write 06 Controlword");
data[0]=0x0006;
WriteObject (0x6040,0,data);
}
// if Ready to switch on : enable
if (ReadStatusWord() & 0x01) // or 0x20 or 0x21?
{
Serial.println("Write 0F in Controlword");
data[0]=0x000F;
WriteObject (0x6040,0,data);
}
ReadStatusWord();
// Start operation command:
Serial.println("Write FF in Controlword");
data[0]=0x00FF;
WriteObject (0x6040,0,data);
ReadStatusWord();
delay(5000);
//Send Halt
data[0]=0x01FF;
WriteObject (0x6040,0,data);
ReadStatusWord();
}
void loop()
{
// doc example
//ReadObject(0x30B0,0);
}