-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCANOpenShell.c
More file actions
executable file
·5454 lines (4523 loc) · 144 KB
/
CANOpenShell.c
File metadata and controls
executable file
·5454 lines (4523 loc) · 144 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
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
/*
This file is part of CanFestival, a library implementing CanOpen Stack.
Copyright (C): Edouard TISSERANT and Francis DUPIN
See COPYING file for copyrights details.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#if defined(WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#define CLEARSCREEN "cls"
#define SLEEP(time) Sleep(time * 1000)
#else
#include <stddef.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define CLEARSCREEN "clear"
#define SLEEP(time) sleep(time)
#endif
//****************************************************************************
// INCLUDES
#include <time.h>
#include <sys/time.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/stat.h>
#include <math.h>
#include "canfestival.h"
#include "CANOpenShell.h"
#include "CANOpenShellMasterOD.h"
#include "CANOpenShellStateMachine.h"
#include "CANOpenShellMasterError.h"
#include "file_parser.h"
#include "utils.h"
#include "smartmotor_table.h"
//****************************************************************************
// DEFINES
#define MOTOR_INDEX_FIRST 0x77
#define INTERPOLATION_DATA_INDEX_OFFSET 6
#define INTERPOLATION_START_INDEX_OFFSET 12
#define TARGET_POSITION_INDEX_OFFSET 13
#ifdef CBRN
#define POSITION_FIFO_FILE "/tmp/cbrn_spinitalia_pos_stream_pipe"
#define FAKE_POSITION_FIFO_FILE "/tmp/fake_cbrn_spinitalia_pos_stream_pipe"
#else
#define POSITION_FIFO_FILE "/tmp/alma_3d_spinitalia_pos_stream_pipe"
#define FAKE_POSITION_FIFO_FILE "/tmp/fake_alma_3d_spinitalia_pos_stream_pipe"
#endif
#define SYNC_DIVIDER_STATUS 15
#define SYNC_DIVIDER_TIMESTAMP 100
/* Macro */
#undef max
#define max(x,y) ((x) > (y) ? (x) : (y))
// bug quando do posizioni il motore si affloscia
// il simulatore è a specchio
// bug quando mando in rilasciato il programma di Enrico inonda di messaggi lo stream
// add comando azzeramento motore
/*
* Studio banda necessaria:
*
* oggetto byte temp num. motori
* -----------------------------------------------------
* TPDO2 3+5 10 ms 6
* SYNC 3 100 ms 1
* TPDO1 3+5 10 ms 6
* HEARTBEAT 3+1 100 ms 6
* TIMESTAMP 3+4 100 ms 1
*
* Per 6 motori si ha una richiesta di banda di:
*
* B = 9940 b/s = 79520 B/s
*
* In questo calcolo si dovrebbero aggiungere anche i comandi verso il motori, come
* la loro configurazione ed i punti di aggiornamento della tabella. In particolare
* questi ultimi dipendono da quanto sono distanziati i punti e del tempo utile per
* raggiungerli. Punti distanti temporalmente richiederanno meno scambio dati.
*/
/**
* Tabella degli stati:
*
* CT0
* ACCESO ------> INIZIALIZZATO
*
*
*
* EM2
* ? -------> EMERGENZA
*
*
*
* CT2 P1
* INIZIALIZZATO -----------> RICERCA_CENTRO
* CT2 P3
* INIZIALIZZATO -----------> RILASCIATO
* CT6
* INIZIALIZZATO -----------> SPENTO
*
*
*
* fine
* RICERCA_CENTRO -----------> CENTRATO
*
*
*
* CT4
* CENTRATO --------------> SIMULAZIONE
* CT1 Mx Px VMx AMx
* CENTRATO ---------------------> IN_POSIZIONE
* CT2 P3
* CENTRATO -----------> RILASCIATO
* CT6
* CENTRATO -----------> SPENTO
*
*
*
* CT5
* SIMULAZIONE --------------> FERMO
* fine
* SIMULAZIONE --------------> FERMO
*
* fine
* IN_POSIZIONE -------------> FERMO
*
*
* CT1 Mx Px VMx AMx
* FERMO ---------------------> IN_POSIZIONE
* CT2 P2
* FERMO -----------> CENTRAGGIO
* CT2 P3
* FERMO -----------> RILASCIATO
* CT6
* FERMO -----------> SPENTO
*
*
*
* fine
* CENTRAGGIO -----------> CENTRATO
*
*
* CT5 && centrato
* RILASCIATO ------------------> FERMO
* CT0 && !centrato
* RILASCIATO ------------------> INIZIALIZZATO
*
*
* CT0
* EMERGENZA ----------> INIZIALIZZATO
*
*
* Stati in cui i comandi sono validi:
*
* CT0: ACCESO, RILASCIATO, EMERGENZA
* CT2 P1: INIZIALIZZATO
* CT2 P2: FERMO
* CT4: CENTRATO
* CT5: SIMULAZIONE
* CT6: INIZIALIZZATO, FERMO, CENTRATO
*
* CT2 P3: INIZIALIZZATO, CENTRATO, FERMO
* EM2: X
*
*/
#define ERRORE_ASINCRONO 0 /**< si è verificato un errore asincrono */
#define SPENTO 1 /**< tutti i nodi canopen spenti */
#define EMERGENZA 2 /**< il motore è bello stato EM2 */
#define ACCESO 3 /**< stato iniziale del motore */
#define INIZIALIZZATO 4 /**< motore inizializzato */
#define RICERCA_CENTRO 5 /**< motore in homing */
#define CENTRATO 6 /**< motore nell'origine */
#define SIMULAZIONE 8 /**< il motore sta eseguento una simulazione */
#define FERMO 9 /**< il motore è fermo */
#define CENTRAGGIO 10 /**< in movimento verso lo zero */
#define RILASCIATO 11 /**< il controllo dei motori è spento ed è presente il freno motore */
#define IN_POSIZIONE 13 /**< movimento in modalità posizione */
#define JOYSTICK_COLLEGATO 14 /**< movimento tramite streaming */
#define MOVIMENTO_LIBERO 15 /**< movimento tramite streaming */
#define CHIUSURA 16 /**< programma in chiusura */
#define cst_str2(c1, c2) ((unsigned int)0 | \
(char)c2) << 8 | (char)c1
#define cst_str4(c1, c2, c3, c4) ((((unsigned int)0 | \
(char)c4 << 8) | \
(char)c3) << 8 | \
(char)c2) << 8 | \
(char)c1
#define INIT_ERR 2
#define QUIT 1
static volatile int robot_state = ACCESO;
static volatile int quit_flag = 0;
pthread_mutex_t robot_state_mux = PTHREAD_MUTEX_INITIALIZER
;
volatile int simulation_ready[TABLE_MAX_NUM];
void CheckReadRaw(CO_Data* d, UNS8 nodeid);
void CheckReadStringRaw(CO_Data* d, UNS8 nodeid);
void CheckWriteRaw(CO_Data* d, UNS8 nodeid);
int SmartWriteRaw(CO_Data* d, UNS8 nodeid);
void CheckWriteProgramDownload(CO_Data* d, UNS8 nodeid);
void CheckWriteProgramUpload(CO_Data* d, UNS8 nodeid);
void CheckReadProgramDownload(CO_Data* d, UNS8 nodeid);
void CheckReadProgramUpload(CO_Data* d, UNS8 nodeid);
UNS32 OnInterpUpdate(CO_Data* d, UNS8 nodeid);
int SmartStop(UNS8 nodeid, int from_callback);
void SimulationTableUpdate(CO_Data* d, UNS8 nodeid, UNS16 interpolation_status, int point_number,
int from_callback);
void SimulationTableEnd(CO_Data* d, UNS8 nodeId, int machine_state,
UNS32 return_value);
int MotorTableIndexFromNodeId(UNS8 nodeId);
void SmartRelease(UNS8 nodeid, int from_callback, int brake);
void SimulationTableStart(CO_Data* d);
void SmartStopCallback(CO_Data* d, UNS8 nodeId, int machine_state, int is_register,
UNS32 return_value);
void SmartPosition(UNS8 nodeid, long position, long velocity, long acceleration, int start,
int from_callback);
void CANOpenShellOD_post_sync(CO_Data* d);
void closing();
void Exit(CO_Data* d, UNS32 nodeid);
void RawCmdMotor(char* sdo);
//****************************************************************************
// GLOBALS
char BoardBusName[31];
char BoardBaudRate[5];
s_BOARD Board =
{
BoardBusName, BoardBaudRate
};
CO_Data* CANOpenShellOD_Data;
static timer_t timer;
static timer_t fake_update_timer;
pthread_t pipe_handler;
pthread_t pipe_write_handler;
pthread_mutex_t interpolator_mux[CANOPEN_NODE_NUMBER];
pthread_mutex_t position_mux = PTHREAD_MUTEX_INITIALIZER
;
pthread_cond_t position_ready = PTHREAD_COND_INITIALIZER
;
pthread_mutex_t exit_from_limit_mux = PTHREAD_MUTEX_INITIALIZER
;
pthread_mutex_t release_mux = PTHREAD_MUTEX_INITIALIZER
;
pthread_mutex_t motor_active_number_mutex = PTHREAD_MUTEX_INITIALIZER
;
char motor_position_write[CANOPEN_NODE_NUMBER]; /**< di chi ho già segnato la posizione */
//static struct timeval position_start_time;
struct timespec position_start_time;
volatile int interpolator_busy[CANOPEN_NODE_NUMBER];
UNS8 raw_response[33];
int raw_response_flag = -1;
UNS32 raw_response_size = 0;
int raw_report_flag = 0;
char raw_report[100];
char raw_cmd[100];
char program_file_path[100];
char LibraryPath[512];
e_nodeState node_state;
int machine_state = -1;
struct table_data motor_table[TABLE_MAX_NUM + 1]; // numero di elementi pari al numero dei motori più l'elemento di broadcast
static int simulation_first_start[CANOPEN_NODE_NUMBER];
static int can_error[CANOPEN_NODE_NUMBER];
FILE *position_fp = NULL;
int fake_flag = 0;
int exit_from_limit_complete = 0;
int release_complete = 0;
int homing_executed = 0;
/**
* @return: -1: errore, chunk_size: ok, <chunk_size: ultimi byte e fine del file
*/
int program_file_read(const char *file_path, char *program_chunk, int chunk_size)
{
FILE *file = NULL;
ssize_t read;
static int cursor_position = 0;
static int cursor_end_program = 0;
static char end_program[] =
{
0xff, 0xff, 0x20
};
memset(program_chunk, '\0', chunk_size);
file = fopen(file_path, "r");
if(file == NULL)
return -1;
if(fseek(file, cursor_position, SEEK_SET) == -1)
{
fclose(file);
return -1;
}
// I caratteri che determinano la fine della stringa sono: " ", "\t", "\n", "'", "\r", "\a"
read = fread(program_chunk, 1, chunk_size, file);
cursor_position += read;
// se non ho letto 32 byte, significa che sono arrivato alla fine del file
// e devo inserire la chiave di fine programmazione
while((read < 32) && (cursor_end_program < 3))
{
program_chunk[read++] = end_program[cursor_end_program++];
}
// se sono riuscito a scrivere tutta la chiave, resetto le
// variabili
if(cursor_end_program == 3)
{
cursor_position = 0;
cursor_end_program = 0;
}
fclose(file);
return read;
}
void SmartClear(UNS8 nodeid)
{
if(nodeid == 0)
{
int motor_index;
for(motor_index = 0; motor_index < motor_active_number; motor_index++)
{
pthread_mutex_lock(&interpolator_mux[motor_table[motor_index].nodeId]);
interpolator_busy[motor_table[motor_index].nodeId] = 0;
pthread_mutex_unlock(&interpolator_mux[motor_table[motor_index].nodeId]);
simulation_first_start[motor_table[motor_index].nodeId] = 1;
motor_started[motor_table[motor_index].nodeId] = 0;
}
}
else
{
if(motor_active[nodeid])
{
pthread_mutex_lock(&interpolator_mux[nodeid]);
interpolator_busy[nodeid] = 0;
pthread_mutex_unlock(&interpolator_mux[nodeid]);
simulation_first_start[nodeid] = 1;
motor_started[nodeid] = 0;
}
}
}
void SmartBusVoltageCallback(CO_Data* d, UNS8 nodeId, int machine_state, int is_register,
UNS32 return_value)
{
int stop_in_progress = 0;
int motor_index;
SmartClear(nodeId);
for(motor_index = 0; motor_index < motor_active_number; motor_index++)
stop_in_progress |= motor_started[motor_table[motor_index].nodeId];
if(stop_in_progress == 0)
{
pthread_mutex_lock(&robot_state_mux);
robot_state = EMERGENZA;
pthread_mutex_unlock(&robot_state_mux);
}
}
void SmartFaultCallback(CO_Data* d, UNS8 nodeId, int machine_state, int is_register,
UNS32 return_value)
{
int fault = motor_statusword0[nodeId];
if((is_register == 0) && (return_value > 0))
return;
//if(is_register)
//{
if((fault & 0b0000000000001000) > 0)
{
#ifdef CANOPENSHELL_VERBOSE
if(verbose_flag)
{
printf("ERR[%d on node %x]: Servo bus voltage fault\n", SmartMotorError, nodeId);
}
#endif
add_event(CERR_BusVoltageFault, nodeId, 0, NULL);
}
if((fault & 0b0000000000010000) > 0)
{
#ifdef CANOPENSHELL_VERBOSE
if(verbose_flag)
{
printf("ERR[%d on node %x]: Peak over-current occurred\n", SmartMotorError, nodeId);
}
#endif
add_event(CERR_OverCurrentFault, nodeId, 0, NULL);
}
if((fault & 0b0000000000100000) > 0)
{
#ifdef CANOPENSHELL_VERBOSE
if(verbose_flag)
{
printf("ERR[%d on node %x]: Excessive temperature\n", SmartMotorError, nodeId);
}
#endif
add_event(CERR_TemperatureFault, nodeId, 0, NULL);
}
if((fault & 0b0000000001000000) > 0)
{
#ifdef CANOPENSHELL_VERBOSE
if(verbose_flag)
{
printf("ERR[%d on node %x]: Excessive position error\n", SmartMotorError, nodeId);
}
#endif
add_event(CERR_PositionFault, nodeId, 0, NULL);
}
if((fault & 0b0000000010000000) > 0)
{
#ifdef CANOPENSHELL_VERBOSE
if(verbose_flag)
{
printf("ERR[%d on node %x]: Velocity limit\n", SmartMotorError, nodeId);
}
#endif
add_event(CERR_VelocityFault, nodeId, 0, NULL);
}
if((fault & 0b0000001000000000) > 0)
{
#ifdef CANOPENSHELL_VERBOSE
if(verbose_flag)
{
printf("ERR[%d on node %x]: First derivative (DE/Dt) of position error over limit\n",
SmartMotorError, nodeId);
}
#endif
add_event(CERR_DerivativeFault, nodeId, 0, NULL);
}
if((fault & 0b0001000000000000) > 0)
{
#ifdef CANOPENSHELL_VERBOSE
if(verbose_flag)
{
printf("ERR[%d on node %x]: Right(+) over travel limit\n", SmartMotorError, nodeId);
}
#endif
pthread_mutex_lock(&robot_state_mux);
if(robot_state == RILASCIATO)
{
pthread_mutex_unlock(&robot_state_mux);
add_event(CERR_RightLimitFault, nodeId, 1, NULL);
}
else
{
pthread_mutex_unlock(&robot_state_mux);
add_event(CERR_RightLimitFault, nodeId, 0, NULL);
}
}
else if((fault & 0b0010000000000000) > 0)
{
#ifdef CANOPENSHELL_VERBOSE
if(verbose_flag)
{
printf("ERR[%d on node %x]: Left(-) over travel limit\n", SmartMotorError, nodeId);
}
#endif
add_event(CERR_LeftLimitFault, nodeId, 0, NULL);
}
//}
//else
//{
SmartStopCallback(d, nodeId, machine_state, is_register, return_value);
/*if((return_value & 0b0011001011111000) == 0)
{
int stop_in_progress = 0;
int motor_index;
SmartClear(nodeId);
for(motor_index = 0; motor_index < motor_active_number; motor_index++)
stop_in_progress |= motor_started[motor_table[motor_index].nodeId];
pthread_mutex_lock(&robot_state_mux);
if((robot_state == SIMULAZIONE) || (robot_state == MOVIMENTO_LIBERO))
{
int motor_table_index = MotorTableIndexFromNodeId(nodeId);
struct table_data_read data_read;
QueueLast(&motor_table[motor_table_index], &data_read);
InterpolationTimePeriod[nodeId - MOTOR_INDEX_FIRST] = 0;
InterpolationTimeValue[nodeId - MOTOR_INDEX_FIRST] = 0;
InterpolationData[nodeId - MOTOR_INDEX_FIRST] = data_read.position;
sendPDOevent(d);
}
pthread_mutex_unlock(&robot_state_mux);
if(stop_in_progress == 0)
{
pthread_mutex_lock(&robot_state_mux);
switch(robot_state)
{
case SIMULAZIONE:
CERR("CT4", CERR_SimulationError);
OK("CT5");
robot_state = FERMO;
break;
case MOVIMENTO_LIBERO:
CERR("CB7", CERR_SimulationError);
OK("CT5");
robot_state = FERMO;
break;
case CENTRAGGIO:
robot_state = FERMO;
break;
case EMERGENZA:
OK("CT0");
break;
}
pthread_mutex_unlock(&robot_state_mux);
}
}*/
//}
}
void SmartCANFaultCallback(CO_Data* d, UNS8 nodeId, int machine_state, int is_register,
UNS32 return_value)
{
if(is_register)
{
printf("[%d] CAN STATUS: %x", nodeId, return_value);
}
else
{
SmartStopCallback(d, nodeId, machine_state, is_register, return_value);
}
}
UNS32 OnPositionUpdate(CO_Data* d, const indextable * indextable_curr,
UNS8 bSubindex)
{
static int motor_basket_num = 0;
UNS8 nodeid = NodeId;
if(fake_flag == 0)
{
if(Position_Actual_Value != motor_position[nodeid])
motor_position[nodeid] = Position_Actual_Value;
}
pthread_mutex_lock(&position_mux);
if(motor_position_write[nodeid] == 0)
{
motor_position_write[nodeid] = 1;
motor_basket_num++;
}
fflush(stdout);
if(motor_basket_num == motor_active_number)
{
motor_basket_num = 0;
memset(motor_position_write, 0, sizeof(motor_position_write));
pthread_cond_signal(&position_ready);
}
pthread_mutex_unlock(&position_mux);
return 0;
}
int MotorTableIndexFromNodeId(UNS8 nodeId)
{
int i;
for(i = 0; i < (TABLE_MAX_NUM + 1); i++)
{
if(motor_table[i].nodeId == nodeId)
{
return i;
}
}
return -1;
}
void SmartPositionTargetCallback(CO_Data* d, UNS8 nodeid, int machine_state, int is_register,
UNS32 return_value)
{
if(return_value)
return;
motor_started[nodeid] = 0;
int motor_index;
int stop_in_progress = 0;
pthread_mutex_lock(&exit_from_limit_mux);
for(motor_index = 0; motor_index < motor_active_number; motor_index++)
stop_in_progress |= motor_started[motor_table[motor_index].nodeId];
if(stop_in_progress == 0)
{
pthread_mutex_unlock(&exit_from_limit_mux);
pthread_mutex_lock(&robot_state_mux);
if((robot_state == RICERCA_CENTRO) || (robot_state == CENTRAGGIO))
{
if(robot_state == RICERCA_CENTRO)
homing_executed = 1;
OK("CT2");
fflush(stdout);
robot_state = CENTRATO;
pthread_mutex_unlock(&robot_state_mux);
}
else if((robot_state == ACCESO) || (robot_state == EMERGENZA) || (robot_state == RILASCIATO))
{
robot_state = INIZIALIZZATO;
pthread_mutex_unlock(&robot_state_mux);
homing_executed = 0;
OK("CT0");
}
else if(robot_state == IN_POSIZIONE)
{
robot_state = FERMO;
pthread_mutex_unlock(&robot_state_mux);
OK("CT1");
fflush(stdout);
}
else
pthread_mutex_unlock(&robot_state_mux);
}
else
pthread_mutex_unlock(&exit_from_limit_mux);
}
void OnCanStatusReset(CO_Data* d, UNS8 nodeId, int machine_state, int is_register,
UNS32 return_value)
{
can_error[nodeId] = 0;
}
void OnSDORead(CO_Data* d, UNS8 nodeId, int machine_state, int is_register,
UNS32 return_value)
{
printf("INFO[%d] CAN ERROR: %d\n", nodeId, return_value);
struct state_machine_struct *fault_machine[] =
{
&smart_message_machine
};
_machine_exe(d, nodeId, &OnCanStatusReset, fault_machine, 1, 1, 2, strlen("ZS"), "ZS");
/*struct state_machine_struct *fault_machine[] =
{
&smart_reset_statusword_machine
};
_machine_exe(d, nodeId, NULL, fault_machine, 1, 1, 0);*/
}
void OnSDOWrite(CO_Data* d, UNS8 nodeId, int machine_state, int is_register,
UNS32 return_value)
{
if(is_register)
{
if((return_value & 0x2) == 2)
{
struct state_machine_struct *fault_machine[] =
{
&smart_read_machine
};
_machine_exe(d, nodeId, &OnSDORead, fault_machine, 1, 1, 3, 0x2500, 0x02, visible_string);
return;
}
}
else
{
if(return_value)
{
can_error[nodeId] = 0;
return;
}
}
struct state_machine_struct *fault_machine[] =
{
&smart_read_machine
};
_machine_exe(d, nodeId, &OnSDOWrite, fault_machine, 1, 1, 3, 0x2500, 0x03, 0);
}
void OnSDOCheck(CO_Data* d, UNS8 nodeId, int machine_state, int is_register,
UNS32 return_value)
{
static char sdo_request[32];
if(is_register)
{
if((return_value & 0x1) != 0)
{
struct state_machine_struct *fault_machine[] =
{
&smart_read_machine
};
_machine_exe(d, nodeId, &OnSDOCheck, fault_machine, 1, 1, 3, 0x2500, 0x03, 0);
return;
}
else
{
struct state_machine_struct *fault_machine[] =
{
&smart_message_machine
};
sprintf(sdo_request, "RCAN");
_machine_exe(d, nodeId, &OnSDOWrite, fault_machine, 1, 1, 2, strlen(sdo_request), sdo_request);
}
}
else
{
if(return_value)
{
printf("reset can_error\n");
can_error[nodeId] = 0;
return;
}
}
}
UNS32 OnCanStatusUpdate(CO_Data* d, const indextable * indextable_curr, UNS8 bSubindex)
{
UNS8 nodeid = NodeId;
motor_statusword0[nodeid] = Motor_Status[0];
if(Motor_Status[2] & 0x10)
{
if(can_error[nodeid] == 0)
{
//printf("INFO[%d]: CAN error %x\n",nodeid, Motor_Status[2]);
can_error[nodeid] = 1;
struct state_machine_struct *fault_machine[] =
{
&smart_read_machine
};
_machine_exe(d, nodeid, &OnSDOCheck, fault_machine, 1, 1, 3, 0x2500, 0x03, 0);
}
}
return 0;
}
UNS32 OnStatusUpdate(CO_Data* d, const indextable * indextable_curr, UNS8 bSubindex)
{
UNS8 nodeid = NodeId;
int status_word_change = 0;
int status_word_index = 0;
/** Fault management **/
if(fake_flag == 0)
{
if(motor_status[nodeid] != Statusword)
{
printf("INFO[%d]: StatusWord %x\n", nodeid, Statusword);
status_word_change = motor_status[nodeid] ^ Statusword;
for(status_word_index = 0; status_word_index < 16; status_word_index++)
{
if((status_word_change >> status_word_index) & 0x01)
{
switch(status_word_index)
{
case 0:
printf(" Ready to switch on = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 1:
printf(" Switched on = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 2:
printf(" Operation enabled = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 3:
printf(" Fault = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 4:
printf(" Voltage enabled = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 5:
printf(" Quick stop = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 6:
printf(" Switch on disabled = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 7:
printf(" Warning = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 8:
printf(" subroutine is busy = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 9:
printf(" Remote = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 10:
printf(" Target reached = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 11:
printf(" Internal limit active = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 12:
printf(" Setpoint acknowledgment = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 13:
printf(" Move error = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 14:
printf(" User-controlled bit through CANCTL(12,x) = %d\n", (Statusword >> status_word_index) & 0x01);
break;
case 15:
printf(" Not used = %d\n", (Statusword >> status_word_index) & 0x01);
break;
}
}
}
}
motor_status[nodeid] = Statusword;
if(motor_mode[nodeid] != Modes_of_operation_display)
{
printf("INFO[%d]: Mode %x\n", nodeid, Modes_of_operation_display);
}
motor_mode[nodeid] = Modes_of_operation_display;
}
OnInterpUpdate(d, nodeid);
// Bus voltage fault
if((motor_status[nodeid] & 0b0000000000010000) == 0)
{
if(robot_state != EMERGENZA)
{
add_event(CERR_BusVoltageFault, nodeid, 0, NULL);
if(fake_flag == 0)
{
struct state_machine_struct *origin_machine[] =
{
&smart_stop_machine
};
_machine_exe(d, nodeid, &SmartBusVoltageCallback, origin_machine, 1, 1, 0);
}
else
{
motor_mode[nodeid] = 0x3;
#ifdef CANOPENSHELL_VERBOSE
if(verbose_flag)
{
printf("SUCC[node %x]: smart motor stopped\n", nodeid);
}
#endif
SmartBusVoltageCallback(d, nodeid, 0, 0, 0);
}
}
}
// se sono in modalità posizione ed il motore è nello stato "operation enabled"
if(motor_mode[nodeid] == 0x1)
{
pthread_mutex_lock(&robot_state_mux);
if(((robot_state == RICERCA_CENTRO) || (robot_state == CENTRAGGIO) || (robot_state == ACCESO)
|| (robot_state == EMERGENZA) || (robot_state == RILASCIATO)
|| (robot_state == IN_POSIZIONE))
&& ((motor_status[nodeid] & 0b0000000001101111) == 0b0000000000100111))
{
pthread_mutex_unlock(&robot_state_mux);
// se ha concluso la tragliettoria
if((motor_status[nodeid] & 0b0001010000000000) == 0b0001010000000000)
{
if(fake_flag == 0)
{
struct state_machine_struct *machine = &smart_stop_machine;
_machine_exe(CANOpenShellOD_Data, nodeid, &SmartPositionTargetCallback, &machine, 1, 1,
0);
}
else
{
motor_mode[nodeid] = 0x3;
smartmotor_path_reset(nodeid, &motor_status[nodeid]);
//motor_status[nodeid] &= ~0b0001010000000000;
#ifdef CANOPENSHELL_VERBOSE
if(verbose_flag)