-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
532 lines (488 loc) · 14.2 KB
/
main.c
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*******************************************************************************
* FILENAME: main.c
*
* PROJECT:
* EtherCatMotorExample
*
* FILE DESCRIPTION:
* This file has the example of controlling an EtherCat motor and using
* the SOEM library in it.
*
* COPYRIGHT:
* Copyright 2023 Paul Hutchinson.
*
* CREATED BY:
* Paul Hutchinson (31 Jul 2023)
*
******************************************************************************/
/*** HEADER FILES TO INCLUDE ***/
#include <signal.h>
#include <sys/mman.h>
#include "ethercattype.h"
#include "nicdrv.h"
#include "ethercatmain.h"
#include "ethercatcoe.h"
#include "ethercatconfig.h"
#include "ethercatprint.h"
#include "wiznet_drv.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
/*** DEFINES ***/
#define SLAVEINDEX 1 // What slave on the EtherCat chain are we going to talk to
#define REVOLUTION_PULSES 0x6500000
#define DEGREE_PULSES (REVOLUTION_PULSES/360)
#define START_ACCELERATION 0x96
#define STOP_DECELERATION 0x96
/*** MACROS ***/
/*** TYPE DEFINITIONS ***/
/*** FUNCTION PROTOTYPES ***/
bool SOEM_Write8(uint16_t Index,uint8_t SubIndex,uint8_t Value);
bool SOEM_Write16(uint16_t Index,uint8_t SubIndex,uint16_t Value);
bool SOEM_Write32(uint16_t Index,uint8_t SubIndex,uint32_t Value);
uint8_t SOEM_Read8(uint16_t Index,uint8_t SubIndex);
uint16_t SOEM_Read16(uint16_t Index,uint8_t SubIndex);
uint32_t SOEM_Read32(uint16_t Index,uint8_t SubIndex);
void PrintError(const char *fmt,...);
bool InitECat(void);
void ShutdownECat(void);
void Wait(void);
void GotoPos(int pos);
/*** VARIABLE DEFINITIONS ***/
char IOmap[4096];
int main(void)
{
if(!InitECat())
{
PrintError("Failed to init ECat");
return 1;
}
GotoPos(0);
GotoPos(360);
GotoPos(0);
/******************************************************/
/* Ok we are in a known state, start the testing here */
/******************************************************/
ShutdownECat();
return 0;
}
/*******************************************************************************
* NAME:
* PrintError
*
* SYNOPSIS:
* void PrintError(const char *fmt,...);
*
* PARAMETERS:
* fmt [I] -- The printf() formating string
* ... [I] -- printf() args
*
* FUNCTION:
* This function prints an error in red with the word ERROR: before it.
* It also adds a \n to the end of the line.
*
* RETURNS:
* NONE
*
* NOTES:
* Outputs the stdout.
*
* SEE ALSO:
*
******************************************************************************/
void PrintError(const char *fmt,...)
{
va_list args;
va_start(args,fmt);
printf("\33[1;31mERROR:"); // Red error
vprintf(fmt,args);
printf("\33[m\n"); // Normal
va_end(args);
}
/*******************************************************************************
* NAME:
* SOEM_Write8
*
* SYNOPSIS:
* bool SOEM_Write8(uint16_t Index,uint8_t SubIndex,uint8_t Value);
*
* PARAMETERS:
* Index [I] -- The EtherCat register to write to
* SubIndex [I] -- The sub index for this register
* Value [I] -- The value to write.
*
* FUNCTION:
* This function uses the ec_SDOwrite() to write to the EtherCat motor.
*
* RETURNS:
* true -- Things worked out
* false -- There was an error. Error was printed to the screen.
*
* SEE ALSO:
* SOEM_Write16(), SOEM_Write32()
******************************************************************************/
bool SOEM_Write8(uint16_t Index,uint8_t SubIndex,uint8_t Value)
{
if(ec_SDOwrite(SLAVEINDEX,Index,SubIndex,false,sizeof(Value),&Value,
EC_TIMEOUTRXM)==0)
{
PrintError("SOEM_Write8(0x%04X,0x%x)",Index,Value);
return false;
}
return true;
}
/*******************************************************************************
* NAME:
* SOEM_Write16
*
* SYNOPSIS:
* bool SOEM_Write16(uint16_t Index,uint8_t SubIndex,uint8_t Value);
*
* PARAMETERS:
* Index [I] -- The EtherCat register to write to
* SubIndex [I] -- The sub index for this register
* Value [I] -- The value to write.
*
* FUNCTION:
* This function uses the ec_SDOwrite() to write to the EtherCat motor.
*
* RETURNS:
* true -- Things worked out
* false -- There was an error. Error was printed to the screen.
*
* SEE ALSO:
* SOEM_Write8(), SOEM_Write32()
******************************************************************************/
bool SOEM_Write16(uint16_t Index,uint8_t SubIndex,uint16_t Value)
{
if(ec_SDOwrite(SLAVEINDEX,Index,SubIndex,false,sizeof(Value),&Value,
EC_TIMEOUTRXM)==0)
{
PrintError("SOEM_Write16(0x%04X,0x%x)",Index,Value);
return false;
}
return true;
}
/*******************************************************************************
* NAME:
* SOEM_Write32
*
* SYNOPSIS:
* bool SOEM_Write32(uint16_t Index,uint8_t SubIndex,uint8_t Value);
*
* PARAMETERS:
* Index [I] -- The EtherCat register to write to
* SubIndex [I] -- The sub index for this register
* Value [I] -- The value to write.
*
* FUNCTION:
* This function uses the ec_SDOwrite() to write to the EtherCat motor.
*
* RETURNS:
* true -- Things worked out
* false -- There was an error. Error was printed to the screen.
*
* SEE ALSO:
* SOEM_Write16(), SOEM_Write8()
******************************************************************************/
bool SOEM_Write32(uint16_t Index,uint8_t SubIndex,uint32_t Value)
{
if(ec_SDOwrite(SLAVEINDEX,Index,SubIndex,false,sizeof(Value),&Value,
EC_TIMEOUTRXM)==0)
{
PrintError("SOEM_Write32(0x%04X,0x%x)",Index,Value);
return false;
}
return true;
}
/*******************************************************************************
* NAME:
* SOEM_Read8
*
* SYNOPSIS:
* uint8_t SOEM_Read8(uint16_t Index,uint8_t SubIndex);
*
* PARAMETERS:
* Index [I] -- The EtherCat register to read from
* SubIndex [I] -- The sub index for this register
*
* FUNCTION:
* This function uses the ec_SDOread() to read from the motor.
*
* RETURNS:
* The value read or 0 if there was an error.
*
* SEE ALSO:
* SOEM_Read16(), SOEM_Read32()
******************************************************************************/
uint8_t SOEM_Read8(uint16_t Index,uint8_t SubIndex)
{
uint8_t Value;
int Size;
Value=0;
Size=sizeof(Value);
if(ec_SDOread(SLAVEINDEX,Index,SubIndex,false,&Size,&Value,
EC_TIMEOUTRXM)==0)
{
PrintError("SOEM_Read8(0x%04X,0x%x)",Index,SubIndex);
return 0;
}
return Value;
}
/*******************************************************************************
* NAME:
* SOEM_Read16
*
* SYNOPSIS:
* uint8_t SOEM_Read16(uint16_t Index,uint8_t SubIndex);
*
* PARAMETERS:
* Index [I] -- The EtherCat register to read from
* SubIndex [I] -- The sub index for this register
*
* FUNCTION:
* This function uses the ec_SDOread() to read from the motor.
*
* RETURNS:
* The value read or 0 if there was an error.
*
* SEE ALSO:
* SOEM_Read8(), SOEM_Read32()
******************************************************************************/
uint16_t SOEM_Read16(uint16_t Index,uint8_t SubIndex)
{
uint16_t Value;
int Size;
Value=0;
Size=sizeof(Value);
if(ec_SDOread(SLAVEINDEX,Index,SubIndex,false,&Size,&Value,
EC_TIMEOUTRXM)==0)
{
PrintError("SOEM_Read16(0x%04X,0x%x)",Index,SubIndex);
return 0;
}
return Value;
}
/*******************************************************************************
* NAME:
* SOEM_Read32
*
* SYNOPSIS:
* uint8_t SOEM_Read32(uint16_t Index,uint8_t SubIndex);
*
* PARAMETERS:
* Index [I] -- The EtherCat register to read from
* SubIndex [I] -- The sub index for this register
*
* FUNCTION:
* This function uses the ec_SDOread() to read from the motor.
*
* RETURNS:
* The value read or 0 if there was an error.
*
* SEE ALSO:
* SOEM_Read16(), SOEM_Read8()
******************************************************************************/
uint32_t SOEM_Read32(uint16_t Index,uint8_t SubIndex)
{
uint32_t Value;
int Size;
Value=0;
Size=sizeof(Value);
if(ec_SDOread(SLAVEINDEX,Index,SubIndex,false,&Size,&Value,
EC_TIMEOUTRXM)==0)
{
PrintError("SOEM_Read32(0x%04X,0x%x)",Index,SubIndex);
return 0;
}
return Value;
}
/*******************************************************************************
* NAME:
* InitECat
*
* SYNOPSIS:
* bool InitECat(void);
*
* PARAMETERS:
* NONE
*
* FUNCTION:
* This function it's the Ethercat master (SOEM lib)
*
* RETURNS:
* true -- Things worked out
* false -- There was an error.
*
* SEE ALSO:
*
******************************************************************************/
bool InitECat(void)
{
int i;
int oloop;
int iloop;
bool Working;
wiznet_hw_config(8,1,1000000); //select SPI-W5500 parameters, before ec_init
Working=false;
if(ec_init("wiznet"))
{
/* find and auto-config slaves */
if(ec_config_init(false)>0)
{
ec_config_map(&IOmap);
/* wait for all slaves to reach SAFE_OP state */
ec_statecheck(0,EC_STATE_SAFE_OP,EC_TIMEOUTSTATE*4);
oloop=ec_slave[0].Obytes;
if((oloop==0) && (ec_slave[0].Obits>0))
oloop = 1;
iloop=ec_slave[0].Ibytes;
if((iloop==0) && (ec_slave[0].Ibits>0))
iloop = 1;
ec_slave[0].state = EC_STATE_OPERATIONAL;
/* send one valid process data to make outputs in slaves happy*/
ec_send_processdata();
ec_receive_processdata(EC_TIMEOUTRET);
/* request OP state for all slaves */
ec_writestate(0);
ec_statecheck(0,EC_STATE_OPERATIONAL,EC_TIMEOUTSTATE); //wait for OP
if(ec_slave[0].state==EC_STATE_OPERATIONAL)
{
Working=true;
}
else
{
printf("Not all slaves reached operational state.\n");
ec_readstate();
for(i=1;i<=ec_slavecount;i++)
{
if(ec_slave[i].state!=EC_STATE_OPERATIONAL)
{
printf("Slave %d State=0x%2.2x StatusCode=0x%4.4x : %s\n",
i, ec_slave[i].state, ec_slave[i].ALstatuscode, ec_ALstatuscode2string(ec_slave[i].ALstatuscode));
}
}
}
}
else
{
printf("No slaves found!\n");
Working=false;
}
}
else
{
printf("No socket connection on %s\nExcecute as root\n", "wiznet");
return false;
}
return Working;
}
/*******************************************************************************
* NAME:
* ShutdownECat
*
* SYNOPSIS:
* bool ShutdownECat(void);
*
* PARAMETERS:
* NONE
*
* FUNCTION:
* This function shuts down the Ethercat init'ed with InitECat()
*
* RETURNS:
* NONE
*
* SEE ALSO:
*
******************************************************************************/
void ShutdownECat(void)
{
/* stop SOEM, close socket */
ec_slave[0].state = EC_STATE_SAFE_OP;
/* request SAFE_OP state for all slaves */
ec_writestate(0);
/* wait for all slaves to reach state */
ec_statecheck(0, EC_STATE_SAFE_OP, EC_TIMEOUTSTATE);
ec_slave[0].state = EC_STATE_PRE_OP;
/* request SAFE_OP state for all slaves */
ec_writestate(0);
/* wait for all slaves to reach state */
ec_statecheck(0, EC_STATE_PRE_OP, EC_TIMEOUTSTATE);
ec_close();
}
/*******************************************************************************
* NAME:
* Wait
*
* SYNOPSIS:
* void Wait(void);
*
* PARAMETERS:
* NONE
*
* FUNCTION:
* This function waits for the motor to get to the target.
*
* RETURNS:
* NONE
*
* SEE ALSO:
*
******************************************************************************/
void Wait(void)
{
int32_t Pos;
uint16_t Status;
for(;;)
{
Pos=SOEM_Read32(0x6064,0x00);
Status=SOEM_Read16(0x6041,0x00);
printf("POS:0x%08X,0x%04X\r",Pos,Status);
fflush(stdout);
if(!(Status & (1<<12)))
break;
}
printf("\n");
}
/*******************************************************************************
* NAME:
* GotoPos
*
* SYNOPSIS:
* void GotoPos(int pos);
*
* PARAMETERS:
* pos [I] -- The position to goto in deg.
*
* FUNCTION:
* This function tells the motor to goto a position, waits for it to get
* there.
*
* RETURNS:
* NONE
*
* LIMITATIONS:
* Resets the motor before it start it.
*
* SEE ALSO:
*
******************************************************************************/
void GotoPos(int pos)
{
printf("GOTO %d\n",pos);
SOEM_Write8(0x6060,0x00,1); // Set mode
SOEM_Write16(0x6040,0x00,0x0000); // Clear bits
SOEM_Write16(0x6040,0x00,0x0006); // Shutdown
SOEM_Write16(0x6040,0x00,0x0007); // Switch on
SOEM_Write16(0x6040,0x00,0x000F); // Enable
SOEM_Write32(0x6083,0x00,0x96); // Set profile acceleration
SOEM_Write32(0x6084,0x00,0x96); // Set profile deceleration
SOEM_Write32(0x6081,0x00,0x0200); // Set profile velocity
SOEM_Write32(0x607A,0x00,REVOLUTION_PULSES/360*pos); // Set target
SOEM_Write16(0x6040,0x00,0x000F); // Mask control word to "enable"
SOEM_Write16(0x6040,0x00,0x001F); // Engage new set point (absolute)
Wait();
}