-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjsd_egd.c
1764 lines (1514 loc) · 61.2 KB
/
jsd_egd.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
#include "jsd/jsd_egd.h"
#include <assert.h>
#include <float.h>
#include <string.h>
#include "ethercat.h"
#include "jsd/jsd.h"
#include "jsd/jsd_elmo_common.h"
#include "jsd/jsd_sdo.h"
#define JSD_EGD_MAX_BYTES_PDO_CHANNEL (32)
static void set_controlword(jsd_t* self, uint16_t slave_id,
uint16_t controlword) {
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
state->rxpdo_cs.controlword = controlword;
state->rxpdo_prof.controlword = controlword;
}
static uint16_t get_controlword(jsd_t* self, uint16_t slave_id) {
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
jsd_slave_config_t* config = &self->slave_configs[slave_id];
uint16_t cw = 0;
if (config->egd.drive_cmd_mode == JSD_EGD_DRIVE_CMD_MODE_CS) {
cw = state->rxpdo_cs.controlword;
} else if (config->egd.drive_cmd_mode == JSD_EGD_DRIVE_CMD_MODE_PROFILED) {
cw = state->rxpdo_prof.controlword;
} else {
ERROR("Bad Drive Cmd mode: %d", config->egd.drive_cmd_mode);
}
return cw;
}
static void set_mode_of_operation(jsd_t* self, uint16_t slave_id,
int8_t mode_of_operation) {
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
state->rxpdo_cs.mode_of_operation = mode_of_operation;
state->rxpdo_prof.mode_of_operation = mode_of_operation;
}
/****************************************************
* Public functions
****************************************************/
const jsd_egd_state_t* jsd_egd_get_state(jsd_t* self, uint16_t slave_id) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
return &self->slave_states[slave_id].egd.pub;
}
void jsd_egd_clear_errors(jsd_t* self, uint16_t slave_id) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
self->slave_states[slave_id].egd.pub.fault_code = JSD_EGD_FAULT_OKAY;
self->slave_states[slave_id].egd.pub.emcy_error_code = 0;
}
void jsd_egd_fault(jsd_t* self, uint16_t slave_id) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
if (self->slave_configs[slave_id].egd.drive_cmd_mode == JSD_EGD_DRIVE_CMD_MODE_CS) {
assert(sizeof(jsd_egd_rxpdo_data_cs_mode_t) == self->ecx_context.slavelist[slave_id].Obytes);
self->slave_states[slave_id].egd.rxpdo_cs.target_position = self->slave_states[slave_id].egd.pub.actual_position;
self->slave_states[slave_id].egd.rxpdo_cs.position_offset = 0;
self->slave_states[slave_id].egd.rxpdo_cs.target_velocity = 0;
self->slave_states[slave_id].egd.rxpdo_cs.velocity_offset = 0;
self->slave_states[slave_id].egd.rxpdo_cs.target_torque = 0;
}
else if (self->slave_configs[slave_id].egd.drive_cmd_mode == JSD_EGD_DRIVE_CMD_MODE_PROFILED) {
assert(sizeof(jsd_egd_rxpdo_data_profiled_mode_t) == self->ecx_context.slavelist[slave_id].Obytes);
self->slave_states[slave_id].egd.rxpdo_prof.target_position = self->slave_states[slave_id].egd.pub.actual_position;
self->slave_states[slave_id].egd.rxpdo_prof.target_velocity = 0;
self->slave_states[slave_id].egd.rxpdo_prof.target_torque = 0;
}
else {
ERROR("bad drive command mode: %d",
self->slave_configs[slave_id].egd.drive_cmd_mode);
}
}
void jsd_egd_reset(jsd_t* self, uint16_t slave_id) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
double now = jsd_time_get_mono_time_sec();
if ((now - self->slave_states[slave_id].egd.last_reset_time) >
JSD_EGD_RESET_DERATE_SEC) {
// Flag below used to latch halt commands until we are in the OPERATION ENABLED state.
self->slave_states[slave_id].egd.enabling_operation = true;
self->slave_states[slave_id].egd.new_reset = true;
self->slave_states[slave_id].egd.last_reset_time = now;
// and clear the latched errors errors
self->slave_states[slave_id].egd.pub.fault_code = JSD_EGD_FAULT_OKAY;
self->slave_states[slave_id].egd.pub.emcy_error_code = 0;
} else {
WARNING(
"EGD Reset Derate Protection feature is preventing reset, ignoring "
"request");
}
}
void jsd_egd_halt(jsd_t* self, uint16_t slave_id) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
self->slave_states[slave_id].egd.new_halt_command = true;
}
// A halt achieves the same effect, wanted this function for public API
void jsd_egd_disable_drive(jsd_t* self, uint16_t slave_id) {
jsd_egd_halt(self, slave_id);
}
void jsd_egd_set_digital_output(jsd_t* self, uint16_t slave_id,
uint8_t digital_output_index,
uint8_t output_level) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
if (digital_output_index <= 0 || digital_output_index > JSD_EGD_NUM_DIGITAL_OUTPUTS) {
ERROR("The provided digital output index %d is out of range [1,%d]",
digital_output_index, JSD_EGD_NUM_DIGITAL_OUTPUTS);
return;
}
if (self->slave_configs[slave_id].egd.drive_cmd_mode !=
JSD_EGD_DRIVE_CMD_MODE_CS) {
ERROR("Drive cmd mode: %d not suitable for digital output ",
self->slave_configs[slave_id].egd.drive_cmd_mode);
return;
}
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
if (output_level > 0) {
state->rxpdo_cs.digital_outputs |= (0x01 << (15 + digital_output_index));
} else {
state->rxpdo_cs.digital_outputs &= ~(0x01 << (15 + digital_output_index));
}
}
void jsd_egd_set_gain_scheduling_index(jsd_t* self, uint16_t slave_id,
bool lsb_byte,
uint16_t gain_scheduling_index) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
if (self->slave_configs[slave_id].egd.drive_cmd_mode !=
JSD_EGD_DRIVE_CMD_MODE_CS) {
ERROR("Drive cmd mode: %d not suitable for manual gain scheduling",
self->slave_configs[slave_id].egd.drive_cmd_mode);
return;
}
if (gain_scheduling_index < 1 || gain_scheduling_index > 63) {
ERROR("The provided gain scheduling index %d is out of range [1,63]",
gain_scheduling_index);
return;
}
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
uint16_t bitmask = 0x00FF;
if (lsb_byte) {
state->rxpdo_cs.gain_scheduling_index =
(state->rxpdo_cs.gain_scheduling_index & (bitmask << 8)) |
gain_scheduling_index;
} else {
state->rxpdo_cs.gain_scheduling_index =
(state->rxpdo_cs.gain_scheduling_index & bitmask) |
(gain_scheduling_index << 8);
}
}
void jsd_egd_set_peak_current(jsd_t* self, uint16_t slave_id,
double peak_current) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
jsd_slave_config_t* config = &self->slave_configs[slave_id];
uint16_t current = peak_current * 1e6 / state->motor_rated_current;
if (config->egd.drive_cmd_mode == JSD_EGD_DRIVE_CMD_MODE_CS) {
state->rxpdo_cs.max_current = current;
} else if (config->egd.drive_cmd_mode == JSD_EGD_DRIVE_CMD_MODE_PROFILED) {
state->rxpdo_prof.max_current = current;
} else {
ERROR("Bad Drive Cmd mode: %d", config->egd.drive_cmd_mode);
}
}
void jsd_egd_set_motion_command_prof_pos(
jsd_t* self, uint16_t slave_id,
jsd_elmo_motion_command_prof_pos_t motion_command) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
if (self->slave_configs[slave_id].egd.drive_cmd_mode !=
JSD_EGD_DRIVE_CMD_MODE_PROFILED) {
ERROR("Drive cmd mode: %d not suitable for profiled commands",
self->slave_configs[slave_id].egd.drive_cmd_mode);
return;
}
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
state->new_motion_command = true;
state->requested_mode_of_operation = JSD_EGD_MODE_OF_OPERATION_PROF_POS;
state->motion_command.prof_pos = motion_command;
}
void jsd_egd_set_motion_command_prof_vel(
jsd_t* self, uint16_t slave_id,
jsd_elmo_motion_command_prof_vel_t motion_command) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
if (self->slave_configs[slave_id].egd.drive_cmd_mode !=
JSD_EGD_DRIVE_CMD_MODE_PROFILED) {
ERROR("Drive cmd mode: %d not suitable for profiled commands",
self->slave_configs[slave_id].egd.drive_cmd_mode);
return;
}
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
state->new_motion_command = true;
state->requested_mode_of_operation = JSD_EGD_MODE_OF_OPERATION_PROF_VEL;
state->motion_command.prof_vel = motion_command;
}
void jsd_egd_set_motion_command_prof_torque(
jsd_t* self, uint16_t slave_id,
jsd_elmo_motion_command_prof_torque_t motion_command) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
if (self->slave_configs[slave_id].egd.drive_cmd_mode !=
JSD_EGD_DRIVE_CMD_MODE_PROFILED) {
ERROR("Drive cmd mode: %d not suitable for profiled commands",
self->slave_configs[slave_id].egd.drive_cmd_mode);
return;
}
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
state->new_motion_command = true;
state->requested_mode_of_operation = JSD_EGD_MODE_OF_OPERATION_PROF_TORQUE;
state->motion_command.prof_torque = motion_command;
}
void jsd_egd_set_motion_command_csp(
jsd_t* self, uint16_t slave_id,
jsd_elmo_motion_command_csp_t motion_command) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
if (self->slave_configs[slave_id].egd.drive_cmd_mode !=
JSD_EGD_DRIVE_CMD_MODE_CS) {
ERROR("Drive cmd mode: %d not suitable for Cyc. Sync. commands",
self->slave_configs[slave_id].egd.drive_cmd_mode);
return;
}
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
state->new_motion_command = true;
state->requested_mode_of_operation = JSD_EGD_MODE_OF_OPERATION_CSP;
state->motion_command.csp = motion_command;
}
void jsd_egd_set_motion_command_csv(
jsd_t* self, uint16_t slave_id,
jsd_elmo_motion_command_csv_t motion_command) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
if (self->slave_configs[slave_id].egd.drive_cmd_mode !=
JSD_EGD_DRIVE_CMD_MODE_CS) {
ERROR("Drive cmd mode: %d not suitable for Cyc. Sync. commands",
self->slave_configs[slave_id].egd.drive_cmd_mode);
return;
}
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
state->new_motion_command = true;
state->requested_mode_of_operation = JSD_EGD_MODE_OF_OPERATION_CSV;
state->motion_command.csv = motion_command;
}
void jsd_egd_set_motion_command_cst(
jsd_t* self, uint16_t slave_id,
jsd_elmo_motion_command_cst_t motion_command) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
if (self->slave_configs[slave_id].egd.drive_cmd_mode !=
JSD_EGD_DRIVE_CMD_MODE_CS) {
ERROR("Drive cmd mode: %d not suitable for Cyc. Sync. commands",
self->slave_configs[slave_id].egd.drive_cmd_mode);
return;
}
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
state->new_motion_command = true;
state->requested_mode_of_operation = JSD_EGD_MODE_OF_OPERATION_CST;
state->motion_command.cst = motion_command;
}
char* jsd_egd_mode_of_operation_to_string(jsd_egd_mode_of_operation_t mode) {
switch (mode) {
case JSD_EGD_MODE_OF_OPERATION_DISABLED:
return "Disabled";
break;
case JSD_EGD_MODE_OF_OPERATION_PROF_POS:
return "Profiled Position";
break;
case JSD_EGD_MODE_OF_OPERATION_PROF_VEL:
return "Profiled Velocity";
break;
case JSD_EGD_MODE_OF_OPERATION_PROF_TORQUE:
return "Profiled Torque";
break;
case JSD_EGD_MODE_OF_OPERATION_CSP:
return "Cyclic Synchronous Position";
break;
case JSD_EGD_MODE_OF_OPERATION_CSV:
return "Cyclic Synchronous Velocity";
break;
case JSD_EGD_MODE_OF_OPERATION_CST:
return "Cyclic Synchronous Torque";
break;
default: {
char str[JSD_NAME_LEN];
snprintf(str, JSD_NAME_LEN, "Bad Mode of Operation: 0x%x", mode);
return strdup(str);
break;
}
}
}
char* jsd_egd_fault_code_to_string(jsd_egd_fault_code_t fault_code) {
switch (fault_code) {
case JSD_EGD_FAULT_OKAY:
return "JSD_EGD_FAULT_OKAY";
break;
case JSD_EGD_FAULT_RESERVED:
return "JSD_EGD_FAULT_RESERVED";
break;
case JSD_EGD_FAULT_OVER_CURRENT:
return "JSD_EGD_FAULT_OVER_CURRENT";
break;
case JSD_EGD_FAULT_SHORT_CIRCUIT:
return "JSD_EGD_FAULT_SHORT_CIRCUIT";
break;
case JSD_EGD_FAULT_UNDER_VOLTAGE:
return "JSD_EGD_FAULT_UNDER_VOLTAGE";
break;
case JSD_EGD_FAULT_LOSS_OF_PHASE:
return "JSD_EGD_FAULT_LOSS_OF_PHASE";
break;
case JSD_EGD_FAULT_OVER_VOLTAGE:
return "JSD_EGD_FAULT_OVER_VOLTAGE";
break;
case JSD_EGD_FAULT_DRIVE_OVER_TEMP:
return "JSD_EGD_FAULT_DRIVE_OVER_TEMP";
break;
case JSD_EGD_FAULT_ECAM_DIFF:
return "JSD_EGD_FAULT_ECAM_DIFF";
break;
case JSD_EGD_FAULT_TIMING_ERROR:
return "JSD_EGD_FAULT_TIMING_ERROR";
break;
case JSD_EGD_FAULT_MOTOR_DISABLED_BY_SWITCH:
return "JSD_EGD_FAULT_MOTOR_DISABLED_BY_SWITCH";
break;
case JSD_EGD_FAULT_ABORT_MOTION:
return "JSD_EGD_FAULT_ABORT_MOTION";
break;
case JSD_EGD_FAULT_CPU_STACK_OVERFLOW:
return "JSD_EGD_FAULT_CPU_STACK_OVERFLOW";
break;
case JSD_EGD_FAULT_CPU_FATAL_EXCEPTION:
return "JSD_EGD_FAULT_CPU_FATAL_EXCEPTION";
break;
case JSD_EGD_FAULT_USER_PROG_ABORTED:
return "JSD_EGD_FAULT_USER_PROG_ABORTED";
break;
case JSD_EGD_FAULT_RPDO_MAP_ERROR:
return "JSD_EGD_FAULT_RPDO_MAP_ERROR";
break;
case JSD_EGD_FAULT_INCONSISTENT_DATABASE:
return "JSD_EGD_FAULT_INCONSISTENT_DATABASE";
break;
case JSD_EGD_FAULT_MOTOR_STUCK:
return "JSD_EGD_FAULT_MOTOR_STUCK";
break;
case JSD_EGD_FAULT_FEEDBACK_ERROR:
return "JSD_EGD_FAULT_FEEDBACK_ERROR";
break;
case JSD_EGD_FAULT_COMMUTATION_FAILED:
return "JSD_EGD_FAULT_COMMUTATION_FAILED";
break;
case JSD_EGD_FAULT_FEEBACK_LOSS:
return "JSD_EGD_FAULT_FEEBACK_LOSS";
break;
case JSD_EGD_FAULT_DIGITAL_HALL_BAD_CHANGE:
return "JSD_EGD_FAULT_DIGITAL_HALL_BAD_CHANGE";
break;
case JSD_EGD_FAULT_COMMUTATION_PROCESS_FAIL:
return "JSD_EGD_FAULT_COMMUTATION_PROCESS_FAIL";
break;
case JSD_EGD_FAULT_CAN_MSG_LOST:
return "JSD_EGD_FAULT_CAN_MSG_LOST";
break;
case JSD_EGD_FAULT_HEARTBEAT_EVENT:
return "JSD_EGD_FAULT_HEARTBEAT_EVENT";
break;
case JSD_EGD_FAULT_RECOVER_BUS_OFF:
return "JSD_EGD_FAULT_RECOVER_BUS_OFF";
break;
case JSD_EGD_FAULT_NMT_PROTOCOL_ERROR:
return "JSD_EGD_FAULT_NMT_PROTOCOL_ERROR";
break;
case JSD_EGD_FAULT_ACCESS_UNCONFIGURED_RPDO:
return "JSD_EGD_FAULT_ACCESS_UNCONFIGURED_RPDO";
break;
case JSD_EGD_FAULT_PEAK_CURRENT_EXCEEDED:
return "JSD_EGD_FAULT_PEAK_CURRENT_EXCEEDED";
break;
case JSD_EGD_FAULT_FAILED_ELECTRICAL_ZERO:
return "JSD_EGD_FAULT_FAILED_ELECTRICAL_ZERO";
break;
case JSD_EGD_FAULT_CANNOT_TUNE:
return "JSD_EGD_FAULT_CANNOT_TUNE";
break;
case JSD_EGD_FAULT_SPEED_TRACKING_ERROR:
return "JSD_EGD_FAULT_SPEED_TRACKING_ERROR";
break;
case JSD_EGD_FAULT_SPEED_LIMIT_EXCEEDED:
return "JSD_EGD_FAULT_SPEED_LIMIT_EXCEEDED";
break;
case JSD_EGD_FAULT_POSITION_TRACKING_ERROR:
return "JSD_EGD_FAULT_POSITION_TRACKING_ERROR";
break;
case JSD_EGD_FAULT_POSITION_LIMIT_EXCEEDED:
return "JSD_EGD_FAULT_POSITION_LIMIT_EXCEEDED";
break;
case JSD_EGD_FAULT_BAD_DATA_0XFF00:
return "JSD_EGD_FAULT_BAD_DATA_0XFF00";
break;
case JSD_EGD_FAULT_USER_PROG_EMIT:
return "JSD_EGD_FAULT_USER_PROG_EMIT";
break;
case JSD_EGD_FAULT_BAD_DATA_0XFF02:
return "JSD_EGD_FAULT_BAD_DATA_0XFF02";
break;
case JSD_EGD_FAULT_CANNOT_START_MOTOR:
return "JSD_EGD_FAULT_CANNOT_START_MOTOR";
break;
case JSD_EGD_FAULT_STO_ENGAGED:
return "JSD_EGD_FAULT_STO_ENGAGED";
break;
case JSD_EGD_FAULT_MODULO_OVERFLOW:
return "JSD_EGD_FAULT_MODULO_OVERFLOW";
break;
case JSD_EGD_FAULT_NUMERIC_OVERFLOW:
return "JSD_EGD_FAULT_NUMERIC_OVERFLOW";
break;
case JSD_EGD_FAULT_GANTRY_SLAVE_DISABLED:
return "JSD_EGD_FAULT_GANTRY_SLAVE_DISABLED";
break;
case JSD_EGD_FAULT_UNKNOWN:
return "JSD_EGD_FAULT_UNKNOWN";
break;
default: {
char str[JSD_NAME_LEN];
snprintf(str, JSD_NAME_LEN, "Bad fault code: 0x%x", fault_code);
return strdup(str);
break;
}
}
}
void jsd_egd_read(jsd_t* self, uint16_t slave_id) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
jsd_egd_read_PDO_data(self, slave_id);
jsd_egd_update_state_from_PDO_data(self, slave_id);
}
void jsd_egd_process(jsd_t* self, uint16_t slave_id) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
jsd_egd_process_state_machine(self, slave_id);
jsd_egd_write_PDO_data(self, slave_id);
}
/////////////////// ASYNC SDO /////////////////////////////
//
uint16_t jsd_egd_tlc_to_do(const char tlc[2]) {
if ((tlc[0] < 65 || tlc[0] > 90) || (tlc[1] < 65 || tlc[1] > 90)) {
ERROR("Two-Letter Command string must be uppercased: %s", tlc);
}
// idea: if lowercase attempt to promote to uppercase
uint16_t index = 0x3000 + (26 * (tlc[0] - 65)) + (tlc[1] - 65);
if (index < 0x3000 || index > 0x3FFF) {
ERROR(
"Two-Letter Command conversion is out of range: %s -> 0x%X not in "
"(0x3000,0x3FFF)",
tlc, index);
}
MSG_DEBUG("Converted TLC: %s to index 0x%X", tlc, index);
return index;
}
void jsd_egd_async_sdo_set_drive_position(jsd_t* self, uint16_t slave_id,
int32_t position, uint16_t app_id)
{
jsd_sdo_set_param_async(self, slave_id, jsd_egd_tlc_to_do("PX"), 1,
JSD_SDO_DATA_I32, &position, app_id);
}
void jsd_egd_async_sdo_set_unit_mode(jsd_t* self, uint16_t slave_id,
int32_t mode, uint16_t app_id)
{
jsd_sdo_set_param_async(self, slave_id, jsd_egd_tlc_to_do("UM"), 1,
JSD_SDO_DATA_I32, &mode, app_id);
}
void jsd_egd_async_sdo_set_ctrl_gain_scheduling_mode(
jsd_t* self, uint16_t slave_id, jsd_elmo_gain_scheduling_mode_t mode,
uint16_t app_id) {
if (mode < 0 || mode > 68) {
ERROR("Gain scheduling mode %d for the controller is not valid.", mode);
return;
}
int64_t mode_i64 = mode;
jsd_sdo_set_param_async(self, slave_id, jsd_egd_tlc_to_do("GS"), 2,
JSD_SDO_DATA_I64, &mode_i64, app_id);
}
/****************************************************
* Private functions
****************************************************/
bool jsd_egd_init(jsd_t* self, uint16_t slave_id) {
assert(self);
assert(jsd_egd_product_code_is_compatible(
self->ecx_context.slavelist[slave_id].eep_id));
assert(self->ecx_context.slavelist[slave_id].eep_man == JSD_ELMO_VENDOR_ID);
assert(sizeof(jsd_egd_txpdo_data_t) <= JSD_EGD_MAX_BYTES_PDO_CHANNEL);
ec_slavet* slaves = self->ecx_context.slavelist;
ec_slavet* slave = &slaves[slave_id];
jsd_slave_config_t* config = &self->slave_configs[slave_id];
config->PO2SO_success = false; // only set true in PO2SO callback
// Disables Complete Access (CA) in EGD devices
// This was needed to make PDO mapping work
slave->CoEdetails &= ~ECT_COEDET_SDOCA;
slave->PO2SOconfigx = jsd_egd_PO2SO_config;
jsd_egd_private_state_t* state = &self->slave_states[slave_id].egd;
state->last_reset_time = 0;
if (config->egd.drive_cmd_mode == JSD_EGD_DRIVE_CMD_MODE_CS) {
assert(sizeof(jsd_egd_rxpdo_data_cs_mode_t) <=
JSD_EGD_MAX_BYTES_PDO_CHANNEL);
MSG_DEBUG("rxpdo_cs size: %zu Bytes", sizeof(jsd_egd_rxpdo_data_cs_mode_t));
} else if (config->egd.drive_cmd_mode == JSD_EGD_DRIVE_CMD_MODE_PROFILED) {
assert(sizeof(jsd_egd_rxpdo_data_profiled_mode_t) <=
JSD_EGD_MAX_BYTES_PDO_CHANNEL);
MSG_DEBUG("rxpdo_prof size: %zu Bytes",
sizeof(jsd_egd_rxpdo_data_profiled_mode_t));
} else {
ERROR("Bad Drive Cmd mode: %d", config->egd.drive_cmd_mode);
}
// this has to be performed up front to share with device_state
state->motor_rated_current = config->egd.continuous_current_limit * 1000;
if (state->motor_rated_current == 0) {
ERROR("continuous_current_limit not set on EGD[%d]", slave_id);
return false;
}
jsd_egd_set_peak_current(self, slave_id, config->egd.peak_current_limit);
state->pub.fault_code = JSD_EGD_FAULT_OKAY;
state->pub.emcy_error_code = 0;
state->enabling_operation = false;
return true;
}
int jsd_egd_PO2SO_config(ecx_contextt* ecx_context, uint16_t slave_id) {
assert(ecx_context);
assert(jsd_egd_product_code_is_compatible(
ecx_context->slavelist[slave_id].eep_id));
// Since this function prototype is forced by SOEM, we have embedded a
// reference to jsd.slave_configs within th ecx_context and extract it here
jsd_slave_config_t* slave_configs =
(jsd_slave_config_t*)ecx_context->userdata;
jsd_slave_config_t* config = &slave_configs[slave_id];
if (!jsd_egd_config_PDO_mapping(ecx_context, slave_id, config)) {
ERROR("Failed to map PDO parameters on EGD slave %u", slave_id);
return 0;
}
if (!jsd_egd_config_COE_params(ecx_context, slave_id, config)) {
ERROR("Failed to set COE parameters on EGD slave %u", slave_id);
return 0;
}
if (!jsd_egd_config_TLC_params(ecx_context, slave_id, config)) {
ERROR("Failed to set TLC parameters on EGD slave %u", slave_id);
return 0;
}
config->PO2SO_success = true;
SUCCESS("EGD[%d] drive parameters successfully configured and verified",
slave_id);
return 1;
}
int jsd_egd_config_PDO_mapping(ecx_contextt* ecx_context, uint16_t slave_id,
jsd_slave_config_t* config) {
MSG_DEBUG("Attempting to map custom EGD PDOs...");
//////////////// RxPDO Mapping //////////////////////////
if (config->egd.drive_cmd_mode == JSD_EGD_DRIVE_CMD_MODE_CS) {
uint16_t map_output_pdos_1607[] = {
0x0007, // num mapped params
0x0020, 0x60FF, // target_velocity
0x0010, 0x6071, // target_torque
0x0020, 0x60B0, // position_offset
0x0020, 0x60B1, // velocity_offset
0x0010, 0x60B2, // torque_offset
0x0008, 0x6060, // mode_of_operation
0x0010, 0x6073, // max_current
};
if (!jsd_sdo_set_ca_param_blocking(ecx_context, slave_id, 0x1607, 0x00,
sizeof(map_output_pdos_1607),
&map_output_pdos_1607)) {
return 0;
}
uint16_t map_output_pdos_1608[] = {
0x0001, // num mapped params
0x0010, 0x2E00, // gain_scheduling_index
};
if (!jsd_sdo_set_ca_param_blocking(ecx_context, slave_id, 0x1608, 0x00,
sizeof(map_output_pdos_1608),
&map_output_pdos_1608)) {
return 0;
}
uint16_t map_output_RxPDO[] = {0x0003, 0x1600, 0x1607, 0x1608};
if (!jsd_sdo_set_ca_param_blocking(ecx_context, slave_id, 0x1C12, 0x00,
sizeof(map_output_RxPDO),
&map_output_RxPDO)) {
return 0;
}
} else if (config->egd.drive_cmd_mode == JSD_EGD_DRIVE_CMD_MODE_PROFILED) {
uint16_t map_output_pdos_1607[] = {
0x0006, // num mapped params
0x0010, 0x6071, // target_torque
0x0020, 0x6081, // profile_velocity
0x0020, 0x6083, // profile_accel
0x0020, 0x6084, // profile_decel
0x0020, 0x6082, // end_velocity
0x0008, 0x6060, // mode_of_operation
};
if (!jsd_sdo_set_ca_param_blocking(ecx_context, slave_id, 0x1607, 0x00,
sizeof(map_output_pdos_1607),
&map_output_pdos_1607)) {
return 0;
}
uint16_t map_output_RxPDO[] = {0x0002, 0x1604, 0x1607};
if (!jsd_sdo_set_ca_param_blocking(ecx_context, slave_id, 0x1C12, 0x00,
sizeof(map_output_RxPDO),
&map_output_RxPDO)) {
return 0;
}
} else {
ERROR("Unknown Drive Command Mode! %d", config->egd.drive_cmd_mode);
return 0;
}
//////////////// TxPDO Mapping //////////////////////////
uint16_t map_input_pdos_1a07[] = {
0x0007, // num mapped params
0x0110, 0x2205, // Analog Input
0x0020, 0x6069, // Velocity Actual Value
0x0010, 0x6078, // Current Actual Value
0x0020, 0x1002, // Status Register
0x0008, 0x6061, // Mode of Operation Display
0x0020, 0x6079, // DC link circuit voltage
0x0020, 0x2203, // Application object (configed for drive temp)
};
if (!jsd_sdo_set_ca_param_blocking(ecx_context, slave_id, 0x1A07, 0x00,
sizeof(map_input_pdos_1a07),
&map_input_pdos_1a07)) {
return 0;
}
uint16_t map_input_TxPDO[] = {0x0002, 0x1A00, 0x1A07};
if (!jsd_sdo_set_ca_param_blocking(ecx_context, slave_id, 0x1C13, 0x00,
sizeof(map_input_TxPDO),
&map_input_TxPDO)) {
return 0;
}
return 1;
}
int jsd_egd_config_COE_params(ecx_contextt* ecx_context, uint16_t slave_id,
jsd_slave_config_t* config) {
// set the Application object (0x2203) to report drive temp
uint32_t app_obj_config_word = 0x01 << (16 + 3);
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, 0x2F41, 0x00,
JSD_SDO_DATA_U32, &app_obj_config_word)) {
return 0;
}
// Check that the drive supports PROF_POS mode
// It's not 100% clear what the mode of operation should be set to when
// advancing through the EGD state machine before ENABLE_OPERATION state is reached
// since DISABLED cananot be set by application.
// When only velocity or current loops are tuned for a given drive, the PROF_POS mode
// cannot be used and the drive will through an error.
// In this version of JSD, we'll enforce that a position loop is tuned and check
// it here. In future versions we may not need this requirement on current-only or
// velocity-only drives.
uint32_t supported_drive_modes;
if (!jsd_sdo_get_param_blocking(ecx_context, slave_id, 0x6502, 0, JSD_SDO_DATA_U32,
(void*)&supported_drive_modes)) {
ERROR("EGD[%d] Could not read SDO 0x6502 Supported Drive Modes", slave_id);
return 0;
}
bool prof_pos_sup = (supported_drive_modes & 0x01);
bool prof_vel_sup = (supported_drive_modes & (0x01 << 2));
bool prof_torque_sup = (supported_drive_modes & (0x01 << 3));
bool prof_csp_sup = (supported_drive_modes & (0x01 << 7));
bool prof_csv_sup = (supported_drive_modes & (0x01 << 8));
bool prof_cst_sup = (supported_drive_modes & (0x01 << 9));
(void) prof_pos_sup;
(void) prof_vel_sup;
(void) prof_torque_sup;
(void) prof_csp_sup;
(void) prof_csv_sup;
(void) prof_cst_sup;
if(!prof_pos_sup) {
ERROR("EGD[%d] does not support PROF_POS mode. A valid position controller must be tuned before use",
slave_id);
MSG("EGD[%d] drive mode PROF_POS: %s", slave_id, (prof_pos_sup ? "Supported" : "Unsupported"));
MSG("EGD[%d] drive mode PROF_VEL: %s", slave_id, (prof_vel_sup ? "Supported" : "Unsupported"));
MSG("EGD[%d] drive mode PROF_TORQUE: %s", slave_id, (prof_torque_sup ? "Supported" : "Unsupported"));
MSG("EGD[%d] drive mode PROF_CSP: %s", slave_id, (prof_csp_sup ? "Supported" : "Unsupported"));
MSG("EGD[%d] drive mode PROF_CSV: %s", slave_id, (prof_csv_sup ? "Supported" : "Unsupported"));
MSG("EGD[%d] drive mode PROF_CST: %s", slave_id, (prof_cst_sup ? "Supported" : "Unsupported"));
return 0;
}
// by default, put drive in PROF_POS mode
int8_t ctrl_word = JSD_EGD_MODE_OF_OPERATION_PROF_POS;
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, 0x6060, 0x00,
JSD_SDO_DATA_I8, &ctrl_word)) {
return 0;
}
// Set relative motion to be relative of actual position
uint16_t pos_optcode = 0x02;
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, 0x60F2, 0x00,
JSD_SDO_DATA_U16, &pos_optcode)) {
return 0;
}
// Set interpolation time period
// This drive setting can be configured to microsec
// Contact the current JSD maintainer to add
int8_t loop_period_ms = config->egd.loop_period_ms;
if (loop_period_ms < 1) {
loop_period_ms = 1;
}
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, 0x60C2, 1,
JSD_SDO_DATA_I8, (void*)&loop_period_ms)) {
return 0;
}
// set Extrapolation cycles timeout (5 cycles based on ECAT lib testing)
int16_t extra_cycles = 5;
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, 0x2F75, 0,
JSD_SDO_DATA_I16, (void*)&extra_cycles)) {
return 0;
}
// Set quick-stop function
int16_t quick_stop_option = 2;
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, 0x605A, 0,
JSD_SDO_DATA_I16,
(void*)&quick_stop_option)) {
return 0;
}
//// set motor rated current CL[1]
uint32_t motor_rated_current = config->egd.continuous_current_limit * 1000.0;
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, 0x6075, 0,
JSD_SDO_DATA_U32,
(void*)&motor_rated_current)) {
return 0;
}
//// set torque slope for profiled torque commands
uint32_t torque_slope = config->egd.torque_slope * 1e6 / motor_rated_current;
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, 0x6087, 0,
JSD_SDO_DATA_U32, (void*)&torque_slope)) {
return 0;
}
// set max motor speed
// 0x6080 accepts units of RPM and internally converts to
// counts per second according to CA[18]. Every other speed in in cnts/sec
// so to keep the JSD api consistent perform the adjustment here.
uint32_t ca_18;
if (!jsd_sdo_get_param_blocking(
ecx_context, slave_id, jsd_egd_tlc_to_do("CA"), 18, JSD_SDO_DATA_U32,
(void*)&ca_18)) {
return 0;
}
MSG("EGD[%d] read CA[18] = %u cnts per rev", slave_id, ca_18);
double max_motor_speed_rpm = (config->egd.max_motor_speed) / (double)ca_18 * 60.0;
int32_t max_motor_speed_rpm_int = (int32_t)max_motor_speed_rpm;
MSG("EGD[%d] max_motor_speed_rpm = %lf as int: %i", slave_id,
max_motor_speed_rpm,
max_motor_speed_rpm_int);
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, 0x6080, 0,
JSD_SDO_DATA_I32,
(void*)&max_motor_speed_rpm_int)) {
return 0;
}
return 1;
}
int jsd_egd_config_TLC_params(ecx_contextt* ecx_context, uint16_t slave_id,
jsd_slave_config_t* config) {
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id,
jsd_egd_tlc_to_do("AC"), 1, JSD_SDO_DATA_U32,
(void*)&config->egd.max_profile_accel)) {
ERROR("EGD[%d] failed to set AC to %u. AC may have a "
" minimum permissible profile around 10 counts, try a higher accel!",
slave_id, config->egd.max_profile_accel);
return 0;
}
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id,
jsd_egd_tlc_to_do("DC"), 1, JSD_SDO_DATA_U32,
(void*)&config->egd.max_profile_decel)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(
ecx_context, slave_id, jsd_egd_tlc_to_do("ER"), 2, JSD_SDO_DATA_I32,
(void*)&config->egd.velocity_tracking_error)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(
ecx_context, slave_id, jsd_egd_tlc_to_do("ER"), 3, JSD_SDO_DATA_I32,
(void*)&config->egd.position_tracking_error)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(
ecx_context, slave_id, jsd_egd_tlc_to_do("PL"), 2, JSD_SDO_DATA_FLOAT,
(void*)&config->egd.peak_current_time)) {
return 0;
}
// PL[1] does not need to be set here since it is updated synchronously
// with every PDO exchange
// Let's set it anyways to help head off any potential issues. Setting the
// PDO-mapped max current doesn't appear to update PL[1]
if (!jsd_sdo_set_param_blocking(
ecx_context, slave_id, jsd_egd_tlc_to_do("PL"), 1, JSD_SDO_DATA_FLOAT,
(void*)&config->egd.peak_current_limit)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(
ecx_context, slave_id, jsd_egd_tlc_to_do("CL"), 1, JSD_SDO_DATA_FLOAT,
(void*)&config->egd.continuous_current_limit)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(
ecx_context, slave_id, jsd_egd_tlc_to_do("CL"), 2, JSD_SDO_DATA_FLOAT,
(void*)&config->egd.motor_stuck_current_level_pct)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(
ecx_context, slave_id, jsd_egd_tlc_to_do("CL"), 3, JSD_SDO_DATA_FLOAT,
(void*)&config->egd.motor_stuck_velocity_threshold)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(
ecx_context, slave_id, jsd_egd_tlc_to_do("CL"), 4, JSD_SDO_DATA_FLOAT,
(void*)&config->egd.motor_stuck_timeout)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id,
jsd_egd_tlc_to_do("HL"), 2, JSD_SDO_DATA_I32,
(void*)&config->egd.over_speed_threshold)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id,
jsd_egd_tlc_to_do("LL"), 3, JSD_SDO_DATA_I32,
(void*)&config->egd.low_position_limit)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id,
jsd_egd_tlc_to_do("HL"), 3, JSD_SDO_DATA_I32,
(void*)&config->egd.high_position_limit)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id,
jsd_egd_tlc_to_do("BP"), 1, JSD_SDO_DATA_I32,
(void*)&config->egd.brake_engage_msec)) {
return 0;
}
if (!jsd_sdo_set_param_blocking(ecx_context, slave_id,
jsd_egd_tlc_to_do("BP"), 2, JSD_SDO_DATA_I32,