-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathepos.c
2390 lines (1861 loc) · 67.7 KB
/
epos.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file epos.c
\brief libEPOS - a library to control an EPOS 24/5
*/
/*! \mainpage libEPOS - a library to control an EPOS motor control unit
\b Because MAXON does not provide an EPOS driver for STM32, so I wrote this
driver. This driver is modified form Marcus Hauser's libEPOS and I ported it
to STM32. This driver thinks you are using CAN to connect the EPOS driver and
you uses the HAL version of the STM32 peripheral driver.
It based on the following maxon motor documents:
- EPOS 2 Positioning Controller - Firmware specification (Edition October 2014)
- EPOS 2 Positioning Controller - Communication Guide (Edition April 2013)
The only fully implemented and tested "Operation Mode" are the SDO
control method of "Profile Position Mode" and "Profile Velocity Mode",
but adding support for other OpModes should be fairly easy, since
the main work was implementing the data exchange with EPOS. The PDO
control mode is highly configurable, so you have to configure the PDO
in the driver first to use the PDO functions
I have only checked the library to work with an EPOS 2 24/5 (firmware
v2126h). Since I have no access to other hardware, I have no chance to
check other EPOS versions. But there is no hint at all that it should
NOT work with other EPOS variants.
\author Liyu Wang, SJTU
\date April 2016
*/
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <errno.h> /* Error number definitions */
#include <stdlib.h>
#include <stdint.h> /* int types with given size */
#include <math.h>
#include "SEGGER_RTT.h"
#include "epos.h"
#include "main.h"
/* ********************************************* */
/* definitions used only internal in epos.c */
/* ********************************************* */
/*! starting point for (slow!) homing movement. If the zero point is
not off too far, this will speed things up enormously!
*/
#define E_STARTPOS_HOMING -200000
//#define DEBUG
/* EPOS codes */
#define E_OK 0x4f ///< EPOS answer code for <em>all fine</em>
#define E_FAIL 0x46 ///< EPOS answer code to indicate a <em>failure</em>
#define E_ANS 0x00 ///< EPOS code to indicate an answer <em>frame</em>
/* EPOS error codes (Communication Guide, 6.4) */
/* CANopen defined error codes */
#define E_NOERR 0x00000000 ///< Error code: no error
#define E_TOGGLE 0x05030000 ///< Error code: Toggle bit not alternated
#define E_SDOTOUT 0x05040000 ///< Error code: SDO protocol timed out
#define E_CMDUKNOWN 0x05040001 ///< Error code: Client/server command specifier not valid or unknown
#define E_INVBLKSIZE 0x05040002 ///< Error code: Invalid block size (block mode only)
#define E_INVSEQ 0x05040003 ///< Error code: Invalid sequence number (block mode only)
#define E_CRCERR 0x05040004 ///< Error code: CRC error (block mode only)
#define E_OUTMEM 0x05040005 ///< Error code: out of memory
#define E_NOACCES 0x06010000 ///< Error code: Unsupported access to an object
#define E_WRITEONLY 0x06010001 ///< Error code: Attempt to read a write-only object
#define E_READONLY 0x06010002 ///< Error code: Attempt to write a read-only object
#define E_ONOTEX 0x06020000 ///< Error code: object does not exist
#define E_PDOMAP 0x06040041 ///< Error code: The object cannot be mapped to the PDO
#define E_PDOLEN 0x06040042 ///< Error code: The number and length of the objects to be would exceed PDO length
#define E_PARAMINCOMP 0x06040043 ///< Error code: general parameter incompatibility
#define E_INTINCOMP 0x06040047 ///< Error code: general internal incompatibility in the device
#define E_HWERR 0x06060000 ///< Error code: access failed due to an hardware error
#define E_SVCPAR 0x06070010 ///< Error code: Data type does not match, length or service does not match
#define E_SVCPARHI 0x06070012 ///< Error code: Data type does not match, length or service too high
#define E_SVCPARLO 0x06070013 ///< Error code: Data type does not match, length or service too low
#define E_SUBINEX 0x06090011 ///< Error code: Last read or write command had wrong object SubIndex
#define E_PRAGNEX 0x06090030 ///< Error code: value range of parameter exeeded
#define E_PARHIGH 0x06090031 ///< Error code: value of parameter written is too high
#define E_PARLOW 0x06090032 ///< Error code: value of parameter written is too low
#define E_PARREL 0x06090036 ///< Error code: maximum value is less than minimum value
#define E_GENERR 0x08000000 ///< Error code: General error
#define E_TFERSTORE 0x08000020 ///< Error code: Data cannot be transferred or stored
#define E_LOCALCTL 0x08000021 ///< Error code: Data cannot be transferred or stored to because of local control
#define E_DEVSTAT 0x08000022 ///< Error code: Data cannot be transferred or stored to because of the present device state
/* maxon specific error codes */
#define E_NMTSTATE 0x0F00FFC0 ///< Error code: wrong NMT state
#define E_RS232 0x0F00FFBF ///< Error code: rs232 command illegeal
#define E_PASSWD 0x0F00FFBE ///< Error code: password incorrect
#define E_NSERV 0x0F00FFBC ///< Error code: device not in service mode
#define E_NODEID 0x0F00FFB9 ///< Error code: error in Node-ID
/* EPOS Device Error */
#define EP_NOERR 0x0000
#define EP_GENERR 0x1000
#define EP_OCERR 0x2310
#define EP_OVERR 0x3210
#define EP_UVERR 0x3220
#define EP_OTERR 0x4210
#define EP_SUPVOLLOW 0x5113
#define EP_OUTVOLLOW 0x5114
#define EP_INTSOFT 0x6100
#define EP_SOFTPAR 0x6320
#define EP_POSSENS 0x7320
#define EP_OBJLOST 0x8110
#define EP_CANOVRUN 0x8111
#define EP_CANPASS 0x8120
#define EP_HEARTBEAT 0x8130
/* EPOS Statusword -- singe bits, see firmware spec 14.1.58 */
#define E_BIT15 0x8000 ///< bit code: position referenced to home position
#define E_BIT14 0x4000 ///< bit code: refresh cycle of power stage
#define E_BIT13 0x2000 ///< bit code: OpMode specific, some error
#define E_BIT12 0x1000 ///< bit code: OpMode specific
#define E_BIT11 0x0800 ///< bit code: NOT USED
#define E_BIT10 0x0400 ///< bit code: Target reached
#define E_BIT09 0x0200 ///< bit code: Remote (?)
#define E_BIT08 0x0100 ///< bit code: offset current measured (?)
#define E_BIT07 0x0080 ///< bit code: WARNING
#define E_BIT06 0x0040 ///< bit code: switch on disable
#define E_BIT05 0x0020 ///< bit code: quick stop
#define E_BIT04 0x0010 ///< bit code: voltage enabled
#define E_BIT03 0x0008 ///< bit code: FAULT
#define E_BIT02 0x0004 ///< bit code: operation enable
#define E_BIT01 0x0002 ///< bit code: switched on
#define E_BIT00 0x0001 ///< bit code: ready to switch on
/* EPOS modes of operation, firmware spec 14.1.59 (p.133, tbl. 72) */
#define E_HOMING 6 ///< EPOS operation mode: homing
#define E_PROFVEL 3 ///< EPOS operation mode: profile velocity mode
#define E_PROFPOS 1 ///< EPOS operation mode: profile position mode
// the modes below should not be used by user, defined here only for
// completeness
#define E_POSMOD -1 ///< EPOS operation mode: position mode
#define E_VELMOD -2 ///< EPOS operation mode: velocity mode
#define E_CURRMOD -3 ///< EPOS operation mode: current mode
#define E_DIAGMOD -4 ///< EPOS operation mode: diagnostics mode
#define E_MASTERENCMOD -5 ///< EPOS operation mode:internal
#define E_STEPDIRECMOD -6 ///< EPOS operation mode:internal
/* Implement read functions defined in EPOS Communication Guide, 6.3.1 */
/*! \brief Read Object from EPOS memory, firmware definition 6.3.1.1*/
static int ReadObject(epos_t *epos, WORD index, BYTE subindex, DWORD *answer);
/* 6.3.2: write functions */
/*! 6.3.2.1 WriteObject()
WORD *data is a pointer to a 2 WORDs array (== 4 BYTES)
holding data to transmit
*/
static int WriteObject(epos_t *epos, WORD index, BYTE subindex, WORD param[]);
/* helper functions below */
/*! \brief send command to EPOS, taking care of all neccessary 'ack' and
checksum tests*/
static int sendCom(epos_t *epos);
/*! \brief int readAnswer(WORD **ptr) - read an answer frame from EPOS */
static int readAnswer(epos_t *epos);
/*! \brief compare two 16bit bitmasks, return 1 (true) or 0 (false) */
static int bitcmp(WORD a, WORD b);
/* Global Varibles */
bool SDOBusy = false;
bool CAN_RxReady = false;
bool CAN_TxReady = false;
static bool isPDO = false;
CanRxMsgTypeDef CANMsgBuf[16];
uint8_t pCANMsg = 0;
/************************************************************/
/* implementation of functions are following */
/************************************************************/
/************************************************************/
/* open/close device */
/************************************************************/
/*! establish the connection to EPOS
\param dev the handle to the CAN device
to, e.g. hcan1
\param ID the CAN ID of the EPOS device.
\retval 0 success
\retval -1 failure
*/
epos_t* openEPOS(CAN_HandleTypeDef *dev, uint8_t ID) {
epos_t *epos = NULL;
#ifndef __cplusplus
if ((epos = malloc(sizeof(epos_t)))) {
#else
if ((epos = (epos_t *)malloc(sizeof(epos_t)))) {
#endif
epos->dev = dev;
epos->dev->pTxMsg = &epos->TxMessage;
epos->dev->pRxMsg = &epos->RxMessage;
epos->Node_ID = ID;
epos->PDO1RcvFlag = false;
epos->PDO2RcvFlag = false;
epos->PDO3RcvFlag = false;
epos->PDO4RcvFlag = false;
epos->SDORcvFlag = false;
if(HAL_CAN_Receive_IT(epos->dev, CAN_FIFO0) != HAL_OK)
{
//Error Handler
}
}
return epos;
}
int checkEPOS(epos_t *epos) {
if (epos->dev < 0) {
SEGGER_RTT_printf(0, "ERROR: EPOS device not open!");
return (-1);
}
return (0);
}
/************************************************************/
/* high-level read functions */
/************************************************************/
/*! read EPOS status word
\param epos pointer on the EPOS object.
\param status pointer to WORD, the content of EPOS statusword will be placed there
\retval 0 success
\retval -1 failure
*/
int readStatusword(epos_t *epos, WORD *status) {
DWORD answer;
int n = 0;
if (!epos) return -1;
if (checkEPOS(epos) != 0)
return (-1);
if ((n = ReadObject(epos, 0x6041, 0x00, &answer)) < 0) {
SEGGER_RTT_printf(0, " *** %s: ReadObject() returned %d **\n",
__func__, n);
return (-1);
}
// check error code
checkEPOSerror(epos);
#ifdef DEBUG
SEGGER_RTT_printf(0, "==> EPOS status word: %#06x\n", answer);
#endif
*status = answer & 0xFFFF;
return (0);
}
int read_DevErr(epos_t *epos, BYTE idx, WORD *err)
{
DWORD answer;
int n = 0;
if (!epos) return -1;
if (checkEPOS(epos) != 0)
return (-1);
if ((n = ReadObject(epos, 0x1003, idx, &answer)) < 0) {
SEGGER_RTT_printf(0, " *** %s: ReadObject() returned %d **\n",
__func__, n);
return (-1);
}
// check error code
checkEPOSerror(epos);
#ifdef DEBUG
SEGGER_RTT_printf(0, "==> EPOS error word: %#06x\n", answer);
#endif
*err = answer & 0xFFFF;
return (0);
}
/*! pretty-print Statusword to stdout
\param s WORD variable holding the statusword
*/
int printEPOSstatusword(WORD s) {
SEGGER_RTT_printf(0, "\nmeaning of EPOS statusword %#06x is:\n", s);
SEGGER_RTT_printf(0, "15: position referenced to home position: ");
if ((s & E_BIT15) == E_BIT15) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "14: refresh cycle of power stage: ");
if ((s & E_BIT14) == E_BIT14) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "13: OpMode specific, some error: ");
if ((s & E_BIT13) == E_BIT13) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "12: OpMode specific: ");
if ((s & E_BIT12) == E_BIT12) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "11: NOT USED ");
if ((s & E_BIT11) == E_BIT11) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "10: Target reached: ");
if ((s & E_BIT10) == E_BIT10) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "09: Remote (?) ");
if ((s & E_BIT09) == E_BIT09) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "08: offset current measured (?) ");
if ((s & E_BIT08) == E_BIT08) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "07: WARNING ");
if ((s & E_BIT07) == E_BIT07) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "06: switch on disable ");
if ((s & E_BIT06) == E_BIT06) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "05: quick stop ");
if ((s & E_BIT05) == E_BIT05) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "04: voltage enabled ");
if ((s & E_BIT04) == E_BIT04) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "03: FAULT ");
if ((s & E_BIT03) == E_BIT03) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "02: operation enable ");
if ((s & E_BIT02) == E_BIT02) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "01: switched on ");
if ((s & E_BIT01) == E_BIT01) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, "00: ready to switch on ");
if ((s & E_BIT00) == E_BIT00) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
return (0);
}
/*! check EPOS state, firmware spec 8.1.1
\param epos pointer on the EPOS object.
\return EPOS status as defined in firmware specification 8.1.1
*/
int checkEPOSstate(epos_t *epos) {
WORD w = 0x0;
int n;
if (!epos) return -1;
if ((n = readStatusword(epos, &w)) < 0) {
SEGGER_RTT_printf(0, " *** %s: readStatusword() returned %d **\n",
__func__, n);
return (-1);
}
/* state 'start' (0)
fedc ba98 7654 3210
w == x0xx xxx0 x000 0000 */
if (!bitcmp(w, E_BIT00) && !bitcmp(w, E_BIT01)
&& !bitcmp(w, E_BIT02) && !bitcmp(w, E_BIT03)
&& !bitcmp(w, E_BIT04) && !bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && !bitcmp(w, E_BIT08)
&& !bitcmp(w, E_BIT14)) return (0);
/* state 'not ready to switch on' (1)
fedc ba98 7654 3210
w == x0xx xxx1 x000 0000 */
if (!bitcmp(w, E_BIT00) && !bitcmp(w, E_BIT01)
&& !bitcmp(w, E_BIT02) && !bitcmp(w, E_BIT03)
&& !bitcmp(w, E_BIT04) && !bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& !bitcmp(w, E_BIT14)) return (1);
/* state 'switch on disabled' (2)
fedc ba98 7654 3210
w == x0xx xxx1 x100 0000 */
if (!bitcmp(w, E_BIT00) && !bitcmp(w, E_BIT01)
&& !bitcmp(w, E_BIT02) && !bitcmp(w, E_BIT03)
&& !bitcmp(w, E_BIT04) && !bitcmp(w, E_BIT05)
&& bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& !bitcmp(w, E_BIT14)) return (2);
/* state 'ready to switch on' (3)
fedc ba98 7654 3210
w == x0xx xxx1 x010 0001 */
if (bitcmp(w, E_BIT00) && !bitcmp(w, E_BIT01)
&& !bitcmp(w, E_BIT02) && !bitcmp(w, E_BIT03)
&& !bitcmp(w, E_BIT04) && bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& !bitcmp(w, E_BIT14)) return (3);
/* state 'switched on' (4)
fedc ba98 7654 3210
w == x0xx xxx1 x010 0011 */
if (bitcmp(w, E_BIT00) && bitcmp(w, E_BIT01)
&& !bitcmp(w, E_BIT02) && !bitcmp(w, E_BIT03)
&& !bitcmp(w, E_BIT04) && bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& !bitcmp(w, E_BIT14)) return (4);
/* state 'refresh' (5)
fedc ba98 7654 3210
w == x1xx xxx1 x010 0011 */
if (bitcmp(w, E_BIT00) && bitcmp(w, E_BIT01)
&& !bitcmp(w, E_BIT02) && !bitcmp(w, E_BIT03)
&& !bitcmp(w, E_BIT04) && bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& bitcmp(w, E_BIT14)) return (5);
/* state 'measure init' (6)
fedc ba98 7654 3210
w == x1xx xxx1 x011 0011 */
if (bitcmp(w, E_BIT00) && bitcmp(w, E_BIT01)
&& !bitcmp(w, E_BIT02) && !bitcmp(w, E_BIT03)
&& bitcmp(w, E_BIT04) && bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& bitcmp(w, E_BIT14)) return (6);
/* state 'operation enable' (7)
fedc ba98 7654 3210
w == x0xx xxx1 x011 0111 */
if (bitcmp(w, E_BIT00) && bitcmp(w, E_BIT01)
&& bitcmp(w, E_BIT02) && !bitcmp(w, E_BIT03)
&& bitcmp(w, E_BIT04) && bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& !bitcmp(w, E_BIT14)) return (7);
/* state 'quick stop active' (8)
fedc ba98 7654 3210
w == x0xx xxx1 x001 0111 */
if (bitcmp(w, E_BIT00) && bitcmp(w, E_BIT01)
&& bitcmp(w, E_BIT02) && !bitcmp(w, E_BIT03)
&& bitcmp(w, E_BIT04) && !bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& !bitcmp(w, E_BIT14)) return (8);
/* state 'fault reaction active (disabled)' (9)
fedc ba98 7654 3210
w == x0xx xxx1 x000 1111 */
if (bitcmp(w, E_BIT00) && bitcmp(w, E_BIT01)
&& bitcmp(w, E_BIT02) && bitcmp(w, E_BIT03)
&& !bitcmp(w, E_BIT04) && !bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& !bitcmp(w, E_BIT14)) return (9);
/* state 'fault reaction active (enabled)' (10)
fedc ba98 7654 3210
w == x0xx xxx1 x001 1111 */
if (bitcmp(w, E_BIT00) && bitcmp(w, E_BIT01)
&& bitcmp(w, E_BIT02) && bitcmp(w, E_BIT03)
&& bitcmp(w, E_BIT04) && !bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& !bitcmp(w, E_BIT14)) return (10);
/* state 'fault' (11)
fedc ba98 7654 3210
w == x0xx xxx1 x000 1000 */
if (!bitcmp(w, E_BIT00) && !bitcmp(w, E_BIT01)
&& !bitcmp(w, E_BIT02) && bitcmp(w, E_BIT03)
&& !bitcmp(w, E_BIT04) && !bitcmp(w, E_BIT05)
&& !bitcmp(w, E_BIT06) && bitcmp(w, E_BIT08)
&& !bitcmp(w, E_BIT14)) return (11);
// if we get down here, statusword has a unknown value!
SEGGER_RTT_printf(0, "WARNING: EPOS status word %#06x is an unkown state!\n", w);
SEGGER_RTT_printf(0, "(function %s() in file %s, line %d)\n",
__func__, __FILE__, __LINE__);
return (-2);
}
/* pretty-print EPOS state */
int printEPOSstate(epos_t *epos) {
if (!epos) return -1;
SEGGER_RTT_printf(0, "\nEPOS is in state ");
switch (checkEPOSstate(epos)) {
case 0:
SEGGER_RTT_printf(0, "start\n"); break;
case 1:
SEGGER_RTT_printf(0, "Not ready to switch on.\n"); break;
case 2:
SEGGER_RTT_printf(0, "Switch on disabled.\n"); break;
case 3:
SEGGER_RTT_printf(0, "Ready to switch on.\n"); break;
case 4:
SEGGER_RTT_printf(0, "Switched on.\n"); break;
case 5:
SEGGER_RTT_printf(0, "Refresh.\n"); break;
case 6:
SEGGER_RTT_printf(0, "Measure init.\n"); break;
case 7:
SEGGER_RTT_printf(0, "Operation enable.\n"); break;
case 8:
SEGGER_RTT_printf(0, "Quick stop active\n"); break;
case 9:
SEGGER_RTT_printf(0, "Fault reaction active (disabled)\n"); break;
case 10:
SEGGER_RTT_printf(0, "Fault reaction active (enabled)\n"); break;
case 11:
SEGGER_RTT_printf(0, "FAULT\n"); break;
default:
SEGGER_RTT_printf(0, "UNKNOWN!\n");
return (-1);
}
return (0);
}
/* change EPOS state according to firmware spec 8.1.3 */
int changeEPOSstate(epos_t *epos, int32_t state) {
WORD dw[2];
int n;
if (!epos) return -1;
dw[1] = 0x0000; // high WORD of DWORD is not used here
/* ! DO NOT READ OLD CONTROLWORD BACK, JUST SET THE BITS. It works
this way, but does NOT work otherways! -- mh, 07.07.06
*/
dw[0] = 0x0000;
switch (state) {
case 0: //shutdown, controlword: 0xxx x110
dw[0] &= ~E_BIT15; // bit 15 ->0
dw[0] |= E_BIT02; // bit 02 ->1
dw[0] |= E_BIT01;
dw[0] &= ~E_BIT00;
n = WriteObject(epos, 0x6040, 0x00, dw);
if (n < 0) {
SEGGER_RTT_printf(0, "%s: writeObject() returned %d at %s, line %d\n",
__func__, n, __FILE__, __LINE__);
return (-1);
}
break;
case 1: // switch on, controllword: 0xxx x111
dw[0] &= ~E_BIT15;
dw[0] |= E_BIT02;
dw[0] |= E_BIT01;
dw[0] |= E_BIT00;
n = WriteObject(epos, 0x6040, 0x00, dw);
if (n < 0) {
SEGGER_RTT_printf(0, "%s: writeObject() returned %d at %s, line %d\n",
__func__, n, __FILE__, __LINE__);
return (-1);
}
break;
case 2: // disable voltage, controllword: 0xxx xx0x
dw[0] &= ~E_BIT15;
dw[0] &= ~E_BIT02;
n = WriteObject(epos, 0x6040, 0x00, dw);
if (n < 0) {
SEGGER_RTT_printf(0, "%s: writeObject() returned %d at %s, line %d\n",
__func__, n, __FILE__, __LINE__);
return (-1);
}
break;
case 3: // quick stop, controllword: 0xxx x01x
dw[0] &= ~E_BIT15;
dw[0] &= ~E_BIT02;
dw[0] |= E_BIT02;
n = WriteObject(epos, 0x6040, 0x00, dw);
if (n < 0) {
SEGGER_RTT_printf(0, "%s: writeObject() returned %d at %s, line %d\n",
__func__, n, __FILE__, __LINE__);
return (-1);
}
break;
case 4: // disable operation, controllword: 0xxx 0111
dw[0] &= ~E_BIT15;
dw[0] &= ~E_BIT03;
dw[0] |= E_BIT02;
dw[0] |= E_BIT01;
dw[0] |= E_BIT00;
n = WriteObject(epos, 0x6040, 0x00, dw);
if (n < 0) {
SEGGER_RTT_printf(0, "%s: writeObject() returned %d at %s, line %d\n",
__func__, n, __FILE__, __LINE__);
return (-1);
}
break;
case 5: // enable operation, controllword: 0xxx 1111
dw[0] &= ~E_BIT15;
dw[0] |= E_BIT03;
dw[0] |= E_BIT02;
dw[0] |= E_BIT01;
dw[0] |= E_BIT00;
n = WriteObject(epos, 0x6040, 0x00, dw);
if (n < 0) {
SEGGER_RTT_printf(0, "%s: writeObject() returned %d at %s, line %d\n",
__func__, n, __FILE__, __LINE__);
return (-1);
}
break;
case 6: // fault reset, controllword: 1xxx xxxx
//dw[0] |= E_BIT15; this is according to firmware spec 8.1.3,
//but does not work!
dw[0] |= E_BIT07; // this is according to firmware spec 14.1.57
// and IS working!
/* WORD estatus = 0x0; */
/* if ( ( n = readStatusword(&estatus) ) < 0) checkEPOSerror(); */
/* printEPOSstatusword(estatus); */
n = WriteObject(epos, 0x6040, 0x00, dw);
if (n < 0) {
SEGGER_RTT_printf(0, "%s: writeObject() returned %d at %s, line %d\n",
__func__, n, __FILE__, __LINE__);
return (-1);
}
/* if ( ( n = readStatusword(&estatus) ) < 0) checkEPOSerror(); */
/* printEPOSstatusword(estatus); */
break;
default:
SEGGER_RTT_printf(0, "ERROR: demanded state %d is UNKNOWN!\n", state);
return (-1);
}
return (0);
}
/* returns software version as HEX -- 14.1.33*/
uint16_t readSWversion(epos_t *epos) {
DWORD answer;
int n = 0;
if (!epos) return -1;
if (checkEPOS(epos) != 0) return (-1);
if ((n = ReadObject(epos, 0x2003, 0x01, &answer)) < 0) {
SEGGER_RTT_printf(0, " *** %s: ReadObject() returned %d **\n",
__func__, n);
return (-1);
}
// check error code
checkEPOSerror(epos);
n = answer&0xFFFF;
return (n);
}
/* read digital input functionality polarity -- firmware spec 14.1.47 */
int readDInputPolarity(epos_t *epos, WORD *w) {
DWORD answer;
int n = 0;
if (!epos) return -1;
if (checkEPOS(epos) != 0) return (-1);
if ((n = ReadObject(epos, 0x2071, 0x03, &answer)) < 0) {
SEGGER_RTT_printf(0, " *** %s: ReadObject() returned %d **\n",
__func__, n);
return (-1);
}
// check error code
checkEPOSerror(epos);
*w = answer&0xFFFF;
return (0);
}
/* set home switch polarity -- firmware spec 14.1.47 */
int setHomePolarity(epos_t *epos, int32_t pol) {
DWORD answer;
WORD mask = 0x00;
WORD dw[2] = { 0x0, 0x0 };
int n = 0;
if (!epos) return -1;
if (pol != 0 && pol != 1) {
SEGGER_RTT_printf(0, "ERROR: polarity must be 0 (hight active) or 1 (low active)\n");
return (-1);
}
if (checkEPOS(epos) != 0) return (-1);
// read present functionalities polarity mask
if (readDInputPolarity(epos, &mask)) {
SEGGER_RTT_printf(0, "\aERROR while reading digital input polarity!\n");
return (-2);
}
// set bit 2 (==home switch) to 0 or 1:
if (pol == 0) mask &= ~E_BIT02;
else if (pol == 1) mask |= E_BIT02;
dw[1] = 0x0000; // high WORD of DWORD is not used here
dw[0] = mask;
n = WriteObject(epos, 0x2071, 0x03, dw);
if (n < 0) {
SEGGER_RTT_printf(0, "%s: writeObject() returned %d at %s, line %d\n",
__func__, n, __FILE__, __LINE__);
return (-1);
}
return (0);
}
/* read EPOS control word (firmware spec 14.1.57) */
int readControlword(epos_t *epos, WORD *w) {
DWORD answer;
int n = 0;
if (!epos) return -1;
if (checkEPOS(epos) != 0) return (-1);
if ((n = ReadObject(epos, 0x6040, 0x00, &answer)) < 0) {
SEGGER_RTT_printf(0, " *** %s: ReadObject() returned %d **\n",
__func__, n);
return (-1);
}
// check error code
checkEPOSerror(epos);
*w = answer&0xFFFF;
return (0);
}
/* pretty-print Controlword */
int printEPOScontrolword(WORD s) {
SEGGER_RTT_printf(0, "\nmeaning of EPOS controlword %#06x is:\n", s);
// bit 15..11 not in use
// bit 10, 9 reserved
SEGGER_RTT_printf(0, " HALT: ");
if ((s & E_BIT08) == E_BIT08) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, " fault reset ");
if ((s & E_BIT07) == E_BIT07) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, " Op mode specific ");
if ((s & E_BIT06) == E_BIT06) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, " Op mode specific ");
if ((s & E_BIT05) == E_BIT05) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, " Op mode specific ");
if ((s & E_BIT04) == E_BIT04) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, " enable operation ");
if ((s & E_BIT03) == E_BIT03) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, " quick stop ");
if ((s & E_BIT02) == E_BIT02) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, " enable voltage ");
if ((s & E_BIT01) == E_BIT01) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
SEGGER_RTT_printf(0, " switch on ");
if ((s & E_BIT00) == E_BIT00) SEGGER_RTT_printf(0, "true\n");
else SEGGER_RTT_printf(0, "false\n");
return (0);
}
/* set mode of operation --- 14.1.59 */
int setOpMode(epos_t *epos, int m) {
WORD dw[2] = { 0x0, 0x0 };
int n = 0;
if (!epos) return -1;
dw[1] = 0x0000; // high WORD of DWORD is not used here
dw[0] = m;
n = WriteObject(epos, 0x6060, 0x00, dw);
if (n < 0) {
SEGGER_RTT_printf(0, "%s: writeObject() returned %d at %s, line %d\n",
__func__, n, __FILE__, __LINE__);
return (-1);
}
return (0);
}
/** read mode of operation --- 14.1.60
\return RETURN(0) MEANS ERROR! -1 is a valid OpMode, but 0 is not!
*/
int readOpMode(epos_t *epos) {
DWORD answer;
//short int *i;
int8_t aa;
int n = 0;
if (!epos) return -1;
if ((n = ReadObject(epos, 0x6061, 0x00, &answer)) < 0) {
SEGGER_RTT_printf(0, " *** %s: ReadObject() returned %d **\n",
__func__, n);
return (0);
}
aa = answer&0xFF;
// check error code
checkEPOSerror(epos);
// give warning, if internal mode is used
if (aa < 0)
SEGGER_RTT_printf(0, "WARNING: EPOS is set to internal mode of operation (%hd).\n Make sure that this was really intended!\n", aa);
//return(*i);
return (aa);
}
/* read demand position; 14.1.61 */
int readDemandPosition(epos_t *epos, int32_t *pos) {
DWORD answer;
int n = 0;
if (!epos) return -1;
if (checkEPOS(epos) != 0)
return (-1);
if ((n = ReadObject(epos, 0x6062, 0x00, &answer)) < 0) {
SEGGER_RTT_printf(0, " *** %s: ReadObject() returned %d **\n",
__func__, n);
return (-1);
}
// check error code
checkEPOSerror(epos);
// return value is a 32bit integer (==long int)
*pos = answer;
#ifdef DEBUG
SEGGER_RTT_printf(0, "==> EPOS actual position: %ld\n", *pos);
#endif
return (0);
}
/*! read actual position; firmware description 14.1.62
\retval 0 success
\retval <0 some error, check with checkEPOSerror()
*/
int readActualPosition(epos_t *epos, int32_t *pos) {
DWORD answer;
int n = 0;
if (!epos) return -1;
if (checkEPOS(epos) != 0) return (-1);
if ((n = ReadObject(epos, 0x6064, 0x00, &answer)) < 0) {
SEGGER_RTT_printf(0, " *** %s: ReadObject() returned %d **\n",
__func__, n);
return (-1);
}
// check error code
checkEPOSerror(epos);
// return value is a 32bit integer (==long int)
epos->RxPosition = answer;
*pos = answer;
#ifdef DEBUG
SEGGER_RTT_printf(0, "==> %s(): EPOS actual position: %ld\n", __func__, *pos);
#endif
return (0);
}