-
Notifications
You must be signed in to change notification settings - Fork 195
/
Controller.c
1018 lines (853 loc) · 32.5 KB
/
Controller.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
// Controller.c
//
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2013, Yaskawa America, Inc.
* All rights reserved.
*
* Redistribution and use in binary form, with or without modification,
* is permitted provided that the following conditions are met:
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Yaskawa America, Inc., nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "MotoROS.h"
extern STATUS setsockopt
(
int s, /* target socket */
int level, /* protocol level of option */
int optname, /* option name */
char * optval, /* pointer to option value */
int optlen /* option length */
);
//-----------------------
// Function Declarations
//-----------------------
BOOL Ros_Controller_Init(Controller* controller);
BOOL Ros_Controller_WaitInitReady(Controller* controller);
BOOL Ros_Controller_IsValidGroupNo(Controller* controller, int groupNo);
int Ros_Controller_OpenSocket(int tcpPort);
void Ros_Controller_ConnectionServer_Start(Controller* controller);
// Status related
void Ros_Controller_StatusInit(Controller* controller);
BOOL Ros_Controller_IsAlarm(Controller* controller);
BOOL Ros_Controller_IsError(Controller* controller);
BOOL Ros_Controller_IsPlay(Controller* controller);
BOOL Ros_Controller_IsTeach(Controller* controller);
BOOL Ros_Controller_IsRemote(Controller* controller);
BOOL Ros_Controller_IsOperating(Controller* controller);
BOOL Ros_Controller_IsHold(Controller* controller);
BOOL Ros_Controller_IsServoOn(Controller* controller);
BOOL Ros_Controller_IsEStop(Controller* controller);
BOOL Ros_Controller_IsWaitingRos(Controller* controller);
int Ros_Controller_GetNotReadySubcode(Controller* controller);
int Ros_Controller_StatusToMsg(Controller* controller, SimpleMsg* sendMsg);
BOOL Ros_Controller_StatusRead(Controller* controller, USHORT ioStatus[IO_ROBOTSTATUS_MAX]);
BOOL Ros_Controller_StatusUpdate(Controller* controller);
// Wrapper around MPFunctions
BOOL Ros_Controller_GetIOState(ULONG signal);
void Ros_Controller_SetIOState(ULONG signal, BOOL status);
int Ros_Controller_GetAlarmCode();
void Ros_Controller_ErrNo_ToString(int errNo, char errMsg[ERROR_MSG_MAX_SIZE], int errMsgSize);
//-----------------------
// Function implementation
//-----------------------
//Report version info to display on pendant (DX200 only)
void reportVersionInfoToController()
{
#if DX100 || FS100
return;
#else
MP_APPINFO_SEND_DATA appInfoSendData;
MP_STD_RSP_DATA stdResponseData;
sprintf(appInfoSendData.AppName, "MotoROS");
sprintf(appInfoSendData.Version, "v%s", APPLICATION_VERSION);
sprintf(appInfoSendData.Comment, "Motoman ROS-I driver");
mpApplicationInfoNotify(&appInfoSendData, &stdResponseData); //don't care about return value
#endif
}
//-------------------------------------------------------------------
// Initialize the controller structure
// This should be done before the controller is used for anything
//-------------------------------------------------------------------
BOOL Ros_Controller_Init(Controller* controller)
{
int grpNo;
int i;
BOOL bInitOk;
STATUS status;
printf("Initializing controller\r\n");
reportVersionInfoToController();
// Turn off all I/O signal
Ros_Controller_SetIOState(IO_FEEDBACK_WAITING_MP_INCMOVE, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_MP_INCMOVE_DONE, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_INITIALIZATION_DONE, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_CONNECTSERVERRUNNING, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_MOTIONSERVERCONNECTED, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_STATESERVERCONNECTED, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_IOSERVERCONNECTED, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_FAILURE, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_RESERVED_1, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_RESERVED_2, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_RESERVED_3, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_RESERVED_4, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_RESERVED_5, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_RESERVED_6, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_RESERVED_7, FALSE);
Ros_Controller_SetIOState(IO_FEEDBACK_RESERVED_8, FALSE);
// Init variables and controller status
bInitOk = TRUE;
controller->bRobotJobReady = FALSE;
controller->bStopMotion = FALSE;
status = GP_isPflEnabled(&controller->bPFLEnabled);
if (status != OK)
bInitOk = FALSE;
if (controller->bPFLEnabled)
printf("System has PFL Enabled\r\n");
controller->bPFLduringRosMove = FALSE;
controller->bMpIncMoveError = FALSE;
Ros_Controller_StatusInit(controller);
Ros_Controller_StatusRead(controller, controller->ioStatus);
// wait for controller to be ready for reading parameter
Ros_Controller_WaitInitReady(controller);
#if (DX100)
// Determine if the robot is a DX100 SDA which requires a special case for the motion API
status = GP_isSdaRobot(&controller->bIsDx100Sda);
if (status != OK)
bInitOk = FALSE;
#endif
// Get the interpolation clock
status = GP_getInterpolationPeriod(&controller->interpolPeriod);
if(status!=OK)
bInitOk = FALSE;
// Get the number of groups
controller->numGroup = GP_getNumberOfGroups();
#ifdef DEBUG
printf("controller->numGroup = %d\n", controller->numGroup);
#endif
if(controller->numGroup < 1)
bInitOk = FALSE;
if (controller->numGroup > MAX_CONTROLLABLE_GROUPS)
{
printf("!!!---Detected %d control groups. MotoROS will only control %d.---!!!\n", controller->numGroup, MAX_CONTROLLABLE_GROUPS);
controller->numGroup = MAX_CONTROLLABLE_GROUPS;
}
controller->numRobot = 0;
// Check for each group
for(grpNo=0; grpNo < MP_GRP_NUM; grpNo++)
{
if(grpNo < controller->numGroup)
{
// Determine if specific group exists and allocate memory for it
controller->ctrlGroups[grpNo] = Ros_CtrlGroup_Create(grpNo, //Zero based index of the group number(0 - 3)
(grpNo==(controller->numGroup-1)), //TRUE if this is the final group that is being initialized. FALSE if you plan to call this function again.
controller->interpolPeriod); //Value of the interpolation period (ms) for the robot controller.
if(controller->ctrlGroups[grpNo] != NULL)
{
Ros_CtrlGroup_GetPulsePosCmd(controller->ctrlGroups[grpNo], controller->ctrlGroups[grpNo]->prevPulsePos); // set the current commanded pulse
controller->numRobot++; //This counter is required for DX100 controllers with two control-groups (robot OR ext axis)
}
else
bInitOk = FALSE;
}
else
controller->ctrlGroups[grpNo] = NULL;
}
#ifdef DEBUG
printf("controller->numRobot = %d\n", controller->numRobot);
#endif
// Initialize Thread ID and Socket to invalid value
controller->tidConnectionSrv = INVALID_TASK;
for (i = 0; i < MAX_IO_CONNECTIONS; i++)
{
controller->sdIoConnections[i] = INVALID_SOCKET;
controller->tidIoConnections[i] = INVALID_TASK;
}
for (i = 0; i < MAX_STATE_CONNECTIONS; i++)
{
controller->sdStateConnections[i] = INVALID_SOCKET;
controller->tidStateSendState[i] = INVALID_TASK;
}
for (i = 0; i < MAX_MOTION_CONNECTIONS; i++)
{
controller->sdMotionConnections[i] = INVALID_SOCKET;
controller->tidMotionConnections[i] = INVALID_TASK;
}
controller->tidIncMoveThread = INVALID_TASK;
#ifdef DX100
controller->bSkillMotionReady[0] = FALSE;
controller->bSkillMotionReady[1] = FALSE;
#endif
if(bInitOk)
{
// Turn on initialization done I/O signal
Ros_Controller_SetIOState(IO_FEEDBACK_INITIALIZATION_DONE, TRUE);
}
else
{
Ros_Controller_SetIOState(IO_FEEDBACK_FAILURE, TRUE);
printf("Failure to initialize controller\r\n");
}
return bInitOk;
}
//-------------------------------------------------------------------
// Wait for the controller to be ready to start initialization
//-------------------------------------------------------------------
BOOL Ros_Controller_WaitInitReady(Controller* controller)
{
do //minor alarms can be delayed briefly after bootup
{
puts("Waiting for robot alarms to clear...");
Ros_Sleep(2500);
Ros_Controller_StatusRead(controller, controller->ioStatus);
}while(Ros_Controller_IsAlarm(controller));
return TRUE;
}
//-------------------------------------------------------------------
// Check the number of inc_move currently in the specified queue
//-------------------------------------------------------------------
BOOL Ros_Controller_IsValidGroupNo(Controller* controller, int groupNo)
{
if((groupNo >= 0) && (groupNo < controller->numGroup))
return TRUE;
else
{
printf("ERROR: Attempt to access invalid Group No. (%d) \r\n", groupNo);
return FALSE;
}
}
//-------------------------------------------------------------------
// Open a socket to listen for incomming connection on specified port
// return: <0 : Error
// >=0 : socket descriptor
//-------------------------------------------------------------------
int Ros_Controller_OpenSocket(int tcpPort)
{
int sd; // socket descriptor
struct sockaddr_in serverSockAddr;
int ret;
// Open the socket
sd = mpSocket(AF_INET, SOCK_STREAM, 0);
if (sd < 0)
return -1;
// Set structure
memset(&serverSockAddr, 0, sizeof(struct sockaddr_in));
serverSockAddr.sin_family = AF_INET;
serverSockAddr.sin_addr.s_addr = INADDR_ANY;
serverSockAddr.sin_port = mpHtons(tcpPort);
//bind to network interface
ret = mpBind(sd, (struct sockaddr *)&serverSockAddr, sizeof(struct sockaddr_in));
if (ret < 0)
goto closeSockHandle;
//prepare to accept connections
ret = mpListen(sd, SOMAXCONN);
if (ret < 0)
goto closeSockHandle;
return sd;
closeSockHandle:
printf("Error in Ros_Controller_OpenSocket\r\n");
if(sd >= 0)
mpClose(sd);
return -2;
}
//-----------------------------------------------------------------------
// Main Connection Server Task that listens for new connections
//-----------------------------------------------------------------------
void Ros_Controller_ConnectionServer_Start(Controller* controller)
{
int sdMotionServer = INVALID_SOCKET;
int sdStateServer = INVALID_SOCKET;
int sdIoServer = INVALID_SOCKET;
int sdMax;
struct fd_set fds;
int sdAccepted = INVALID_SOCKET;
struct sockaddr_in clientSockAddr;
int sizeofSockAddr;
int useNoDelay = 1;
STATUS s;
//Set feedback signal (Application is installed and running)
Ros_Controller_SetIOState(IO_FEEDBACK_CONNECTSERVERRUNNING, TRUE);
printf("Controller connection server running\r\n");
//New sockets for server listening to multiple port
sdMotionServer = Ros_Controller_OpenSocket(TCP_PORT_MOTION);
if(sdMotionServer < 0)
goto closeSockHandle;
sdStateServer = Ros_Controller_OpenSocket(TCP_PORT_STATE);
if(sdStateServer < 0)
goto closeSockHandle;
sdIoServer = Ros_Controller_OpenSocket(TCP_PORT_IO);
if(sdIoServer < 0)
goto closeSockHandle;
sdMax = max(sdMotionServer, sdStateServer);
sdMax = max(sdMax, sdIoServer);
FOREVER //Continue to accept multiple connections forever
{
FD_ZERO(&fds);
FD_SET(sdMotionServer, &fds);
FD_SET(sdStateServer, &fds);
FD_SET(sdIoServer, &fds);
if(mpSelect(sdMax+1, &fds, NULL, NULL, NULL) > 0)
{
memset(&clientSockAddr, 0, sizeof(clientSockAddr));
sizeofSockAddr = sizeof(clientSockAddr);
//Check motion server
if(FD_ISSET(sdMotionServer, &fds))
{
sdAccepted = mpAccept(sdMotionServer, (struct sockaddr *)&clientSockAddr, &sizeofSockAddr);
if (sdAccepted < 0)
break;
s = setsockopt(sdAccepted, IPPROTO_TCP, TCP_NODELAY, (char*)&useNoDelay, sizeof (int));
if( OK != s )
{
printf("Failed to set TCP_NODELAY.\r\n");
}
Ros_MotionServer_StartNewConnection(controller, sdAccepted);
}
//Check state server
if(FD_ISSET(sdStateServer, &fds))
{
sdAccepted = mpAccept(sdStateServer, (struct sockaddr *)&clientSockAddr, &sizeofSockAddr);
if (sdAccepted < 0)
break;
s = setsockopt(sdAccepted, IPPROTO_TCP, TCP_NODELAY, (char*)&useNoDelay, sizeof (int));
if( OK != s )
{
printf("Failed to set TCP_NODELAY.\r\n");
}
Ros_StateServer_StartNewConnection(controller, sdAccepted);
}
//Check IO server
if(FD_ISSET(sdIoServer, &fds))
{
sdAccepted = mpAccept(sdIoServer, (struct sockaddr *)&clientSockAddr, &sizeofSockAddr);
if (sdAccepted < 0)
break;
s = setsockopt(sdAccepted, IPPROTO_TCP, TCP_NODELAY, (char*)&useNoDelay, sizeof (int));
if( OK != s )
{
printf("Failed to set TCP_NODELAY.\r\n");
}
Ros_IoServer_StartNewConnection(controller, sdAccepted);
}
}
}
closeSockHandle:
printf("Error!?... Connection Server is aborting. Reboot the controller.\r\n");
if(sdIoServer >= 0)
mpClose(sdIoServer);
if(sdMotionServer >= 0)
mpClose(sdMotionServer);
if(sdStateServer >= 0)
mpClose(sdStateServer);
//disable feedback signal
Ros_Controller_SetIOState(IO_FEEDBACK_CONNECTSERVERRUNNING, FALSE);
}
/**** Controller Status functions ****/
//-------------------------------------------------------------------
// Initialize list of Specific Input to keep track of
//-------------------------------------------------------------------
void Ros_Controller_StatusInit(Controller* controller)
{
controller->ioStatusAddr[IO_ROBOTSTATUS_ALARM_MAJOR].ulAddr = 50010; // Alarm
controller->ioStatusAddr[IO_ROBOTSTATUS_ALARM_MINOR].ulAddr = 50011; // Alarm
controller->ioStatusAddr[IO_ROBOTSTATUS_ALARM_SYSTEM].ulAddr = 50012; // Alarm
controller->ioStatusAddr[IO_ROBOTSTATUS_ALARM_USER].ulAddr = 50013; // Alarm
controller->ioStatusAddr[IO_ROBOTSTATUS_ERROR].ulAddr = 50014; // Error
controller->ioStatusAddr[IO_ROBOTSTATUS_PLAY].ulAddr = 50054; // Play
controller->ioStatusAddr[IO_ROBOTSTATUS_TEACH].ulAddr = 50053; // Teach
controller->ioStatusAddr[IO_ROBOTSTATUS_REMOTE].ulAddr = 80011; //50056; // Remote // Modified E.M. 7/9/2013
controller->ioStatusAddr[IO_ROBOTSTATUS_OPERATING].ulAddr = 50070; // Operating
controller->ioStatusAddr[IO_ROBOTSTATUS_HOLD].ulAddr = 50071; // Hold
controller->ioStatusAddr[IO_ROBOTSTATUS_SERVO].ulAddr = 50073; // Servo ON
controller->ioStatusAddr[IO_ROBOTSTATUS_ESTOP_EX].ulAddr = 80025; // External E-Stop
controller->ioStatusAddr[IO_ROBOTSTATUS_ESTOP_PP].ulAddr = 80026; // Pendant E-Stop
controller->ioStatusAddr[IO_ROBOTSTATUS_ESTOP_CTRL].ulAddr = 80027; // Controller E-Stop
controller->ioStatusAddr[IO_ROBOTSTATUS_WAITING_ROS].ulAddr = IO_FEEDBACK_WAITING_MP_INCMOVE; // Job input signaling ready for external motion
controller->ioStatusAddr[IO_ROBOTSTATUS_INECOMODE].ulAddr = 50727; // Energy Saving Mode
#if (YRC1000||YRC1000u)
controller->ioStatusAddr[IO_ROBOTSTATUS_PFL_STOP].ulAddr = 81702; // PFL function stopped the motion
controller->ioStatusAddr[IO_ROBOTSTATUS_PFL_ESCAPE].ulAddr = 81703; // PFL function escape from clamping motion
controller->ioStatusAddr[IO_ROBOTSTATUS_PFL_AVOIDING].ulAddr = 15120; // PFL function avoidance operating
controller->ioStatusAddr[IO_ROBOTSTATUS_PFL_AVOID_JOINT].ulAddr = 15124; // PFL function avoidance joint enabled
controller->ioStatusAddr[IO_ROBOTSTATUS_PFL_AVOID_TRANS].ulAddr = 15125; // PFL function avoidance translation enabled
#endif
controller->alarmCode = 0;
}
BOOL Ros_Controller_IsAlarm(Controller* controller)
{
return ((controller->ioStatus[IO_ROBOTSTATUS_ALARM_MAJOR]!=0)
|| (controller->ioStatus[IO_ROBOTSTATUS_ALARM_MINOR]!=0)
|| (controller->ioStatus[IO_ROBOTSTATUS_ALARM_SYSTEM]!=0)
|| (controller->ioStatus[IO_ROBOTSTATUS_ALARM_USER]!=0) );
}
BOOL Ros_Controller_IsError(Controller* controller)
{
return ((controller->ioStatus[IO_ROBOTSTATUS_ERROR]!=0));
}
BOOL Ros_Controller_IsPlay(Controller* controller)
{
return ((controller->ioStatus[IO_ROBOTSTATUS_PLAY]!=0));
}
BOOL Ros_Controller_IsTeach(Controller* controller)
{
return ((controller->ioStatus[IO_ROBOTSTATUS_TEACH]!=0));
}
BOOL Ros_Controller_IsRemote(Controller* controller)
{
return ((controller->ioStatus[IO_ROBOTSTATUS_REMOTE]!=0));
}
BOOL Ros_Controller_IsOperating(Controller* controller)
{
return ((controller->ioStatus[IO_ROBOTSTATUS_OPERATING]!=0));
}
BOOL Ros_Controller_IsHold(Controller* controller)
{
return ((controller->ioStatus[IO_ROBOTSTATUS_HOLD]!=0));
}
BOOL Ros_Controller_IsServoOn(Controller* controller)
{
return ((controller->ioStatus[IO_ROBOTSTATUS_SERVO] != 0) && (controller->ioStatus[IO_ROBOTSTATUS_INECOMODE] == 0));
}
BOOL Ros_Controller_IsEcoMode(Controller* controller)
{
return (controller->ioStatus[IO_ROBOTSTATUS_INECOMODE] != 0);
}
BOOL Ros_Controller_IsEStop(Controller* controller)
{
return ((controller->ioStatus[IO_ROBOTSTATUS_ESTOP_EX]==0)
|| (controller->ioStatus[IO_ROBOTSTATUS_ESTOP_PP]==0)
|| (controller->ioStatus[IO_ROBOTSTATUS_ESTOP_CTRL]==0) );
}
BOOL Ros_Controller_IsWaitingRos(Controller* controller)
{
return ((controller->ioStatus[IO_ROBOTSTATUS_WAITING_ROS]!=0));
}
BOOL Ros_Controller_IsMotionReady(Controller* controller)
{
BOOL bMotionReady;
#ifndef DUMMY_SERVO_MODE
bMotionReady = controller->bRobotJobReady && Ros_Controller_IsOperating(controller) && Ros_Controller_IsRemote(controller)
&& !Ros_Controller_IsPflActive(controller) && !controller->bMpIncMoveError && !controller->bStopMotion;
#else
bMotionReady = controller->bRobotJobReady && Ros_Controller_IsOperating(controller);
#endif
#ifdef DX100
if(controller->numRobot < 2)
bMotionReady = bMotionReady && controller->bSkillMotionReady[0];
else
bMotionReady = bMotionReady && controller->bSkillMotionReady[0] && controller->bSkillMotionReady[1];
#endif
return bMotionReady;
}
BOOL Ros_Controller_IsPflActive(Controller* controller)
{
#if (YRC1000||YRC1000u)
if (controller->bPFLEnabled) {
if (controller->bPFLduringRosMove || controller->ioStatus[IO_ROBOTSTATUS_PFL_STOP] || controller->ioStatus[IO_ROBOTSTATUS_PFL_ESCAPE] ||
(controller->ioStatus[IO_ROBOTSTATUS_PFL_AVOIDING]
&& (controller->ioStatus[IO_ROBOTSTATUS_PFL_AVOID_JOINT] || controller->ioStatus[IO_ROBOTSTATUS_PFL_AVOID_TRANS])) != 0)
{
return TRUE;
}
}
#endif
return FALSE;
}
int Ros_Controller_GetNotReadySubcode(Controller* controller)
{
// Check alarm
if(Ros_Controller_IsAlarm(controller))
return ROS_RESULT_NOT_READY_ALARM;
// Check error
if(Ros_Controller_IsError(controller))
return ROS_RESULT_NOT_READY_ERROR;
// Check e-stop
if(Ros_Controller_IsEStop(controller))
return ROS_RESULT_NOT_READY_ESTOP;
// Check play mode
if(!Ros_Controller_IsPlay(controller))
return ROS_RESULT_NOT_READY_NOT_PLAY;
#ifndef DUMMY_SERVO_MODE
// Check remote
if(!Ros_Controller_IsRemote(controller))
return ROS_RESULT_NOT_READY_NOT_REMOTE;
// Check servo power
if(!Ros_Controller_IsServoOn(controller))
return ROS_RESULT_NOT_READY_SERVO_OFF;
#endif
// Check hold
if(Ros_Controller_IsHold(controller))
return ROS_RESULT_NOT_READY_HOLD;
// Check PFL active
if (Ros_Controller_IsPflActive(controller))
return ROS_RESULT_NOT_READY_PFL_ACTIVE;
// Check if Incremental Motion was rejected
if (controller->bMpIncMoveError)
return ROS_RESULT_NOT_READY_INC_MOVE_ERROR;
// Check operating
if(!Ros_Controller_IsOperating(controller))
return ROS_RESULT_NOT_READY_NOT_STARTED;
// Check ready I/O signal (should confirm wait)
if(!Ros_Controller_IsWaitingRos(controller))
return ROS_RESULT_NOT_READY_WAITING_ROS;
// Check skill send command
if(!Ros_Controller_IsMotionReady(controller))
return ROS_RESULT_NOT_READY_SKILLSEND;
return ROS_RESULT_NOT_READY_UNSPECIFIED;
}
BOOL Ros_Controller_IsInMotion(Controller* controller)
{
int i;
int groupNo;
long fbPulsePos[MAX_PULSE_AXES];
long cmdPulsePos[MAX_PULSE_AXES];
BOOL bDataInQ;
CtrlGroup* ctrlGroup;
bDataInQ = Ros_MotionServer_HasDataInQueue(controller);
if (bDataInQ == TRUE)
return TRUE;
else if (bDataInQ == ERROR)
return ERROR;
else
{
//for each control group
for (groupNo = 0; groupNo < controller->numGroup; groupNo++)
{
//Check group number valid
if (!Ros_Controller_IsValidGroupNo(controller, groupNo))
continue;
//Check if the feeback position has caught up to the command position
ctrlGroup = controller->ctrlGroups[groupNo];
Ros_CtrlGroup_GetFBPulsePos(ctrlGroup, fbPulsePos);
Ros_CtrlGroup_GetPulsePosCmd(ctrlGroup, cmdPulsePos);
for (i = 0; i < MP_GRP_AXES_NUM; i += 1)
{
if (ctrlGroup->axisType.type[i] != AXIS_INVALID)
{
// Check if position matches current command position
if (abs(fbPulsePos[i] - cmdPulsePos[i]) > START_MAX_PULSE_DEVIATION)
return TRUE;
}
}
}
}
return FALSE;
}
// Creates a simple message of type: ROS_MSG_ROBOT_STATUS = 13
// Simple message containing the current state of the controller
int Ros_Controller_StatusToMsg(Controller* controller, SimpleMsg* sendMsg)
{
//initialize memory
memset(sendMsg, 0x00, sizeof(SimpleMsg));
// set prefix: length of message excluding the prefix
sendMsg->prefix.length = sizeof(SmHeader) + sizeof(SmBodyRobotStatus);
// set header information
sendMsg->header.msgType = ROS_MSG_ROBOT_STATUS;
sendMsg->header.commType = ROS_COMM_TOPIC;
sendMsg->header.replyType = ROS_REPLY_INVALID;
// set body
sendMsg->body.robotStatus.drives_powered = (int)(Ros_Controller_IsServoOn(controller));
sendMsg->body.robotStatus.e_stopped = (int)(Ros_Controller_IsEStop(controller));
sendMsg->body.robotStatus.error_code = controller->alarmCode;
sendMsg->body.robotStatus.in_error = (int)Ros_Controller_IsAlarm(controller);
sendMsg->body.robotStatus.in_motion = (int)(Ros_Controller_IsInMotion(controller));
if(Ros_Controller_IsPlay(controller))
sendMsg->body.robotStatus.mode = 2;
else
sendMsg->body.robotStatus.mode = 1;
sendMsg->body.robotStatus.motion_possible = (int)(Ros_Controller_IsMotionReady(controller));
return(sendMsg->prefix.length + sizeof(SmPrefix));
}
//-------------------------------------------------------------------
// Get I/O state on the controller
//-------------------------------------------------------------------
BOOL Ros_Controller_StatusRead(Controller* controller, USHORT ioStatus[IO_ROBOTSTATUS_MAX])
{
return (mpReadIO(controller->ioStatusAddr, ioStatus, IO_ROBOTSTATUS_MAX) == 0);
}
//-------------------------------------------------------------------
// Update I/O state on the controller
//-------------------------------------------------------------------
BOOL Ros_Controller_StatusUpdate(Controller* controller)
{
USHORT ioStatus[IO_ROBOTSTATUS_MAX];
int i;
BOOL prevReadyStatus;
prevReadyStatus = Ros_Controller_IsMotionReady(controller);
if(Ros_Controller_StatusRead(controller, ioStatus))
{
// Check for change of state and potentially react to the change
for(i=0; i<IO_ROBOTSTATUS_MAX; i++)
{
if(controller->ioStatus[i] != ioStatus[i])
{
//printf("Change of ioStatus[%d]\r\n", i);
controller->ioStatus[i] = ioStatus[i];
switch(i)
{
case IO_ROBOTSTATUS_ALARM_MAJOR: // alarm
case IO_ROBOTSTATUS_ALARM_MINOR: // alarm
case IO_ROBOTSTATUS_ALARM_SYSTEM: // alarm
case IO_ROBOTSTATUS_ALARM_USER: // alarm
{
if ((ioStatus[IO_ROBOTSTATUS_ALARM_MAJOR] == 0) &&
(ioStatus[IO_ROBOTSTATUS_ALARM_MINOR] == 0) &&
(ioStatus[IO_ROBOTSTATUS_ALARM_SYSTEM] == 0) &&
(ioStatus[IO_ROBOTSTATUS_ALARM_USER] == 0))
controller->alarmCode = 0;
else
{
controller->alarmCode = Ros_Controller_GetAlarmCode();
Ros_MotionServer_ClearQ_All(controller);
}
break;
}
case IO_ROBOTSTATUS_WAITING_ROS: // Job input signaling ready for external motion
{
if(ioStatus[IO_ROBOTSTATUS_WAITING_ROS] == 0) // signal turned OFF
{
// Job execution stopped take action
controller->bRobotJobReady = FALSE;
Ros_MotionServer_ClearQ_All(controller);
}
else // signal turned ON (rising edge)
controller->bRobotJobReady = TRUE; //job is ready (even if other factors will prevent motion)
break;
}
#if (YRC1000||YRC1000u)
case IO_ROBOTSTATUS_PFL_STOP: // PFL Stop
case IO_ROBOTSTATUS_PFL_ESCAPE: // PFL Escaping
case IO_ROBOTSTATUS_PFL_AVOIDING: // PFL Avoidance
{
if (controller->bPFLEnabled && controller->bRobotJobReady && Ros_Controller_IsPflActive(controller))
{
// Job execution stopped by PFL take action
controller->bPFLduringRosMove = TRUE; //force job to be restarted with new ROS_CMD_START_TRAJ_MODE command
Ros_MotionServer_ClearQ_All(controller);
}
break;
}
#endif
}
}
}
if (!prevReadyStatus && Ros_Controller_IsMotionReady(controller))
printf("Robot job is ready for ROS commands.\r\n");
return TRUE;
}
else
return FALSE;
}
/**** Wrappers on MP standard function ****/
//-------------------------------------------------------------------
// Get I/O state on the controller
//-------------------------------------------------------------------
BOOL Ros_Controller_GetIOState(ULONG signal)
{
MP_IO_INFO ioInfo;
USHORT ioState;
int ret;
//set feedback signal
ioInfo.ulAddr = signal;
ret = mpReadIO(&ioInfo, &ioState, 1);
if(ret != 0)
printf("mpReadIO failure (%d)\r\n", ret);
return (ioState != 0);
}
//-------------------------------------------------------------------
// Set I/O state on the controller
//-------------------------------------------------------------------
void Ros_Controller_SetIOState(ULONG signal, BOOL status)
{
MP_IO_DATA ioData;
int ret;
//set feedback signal
ioData.ulAddr = signal;
ioData.ulValue = status;
ret = mpWriteIO(&ioData, 1);
if(ret != 0)
printf("mpWriteIO failure (%d)\r\n", ret);
}
//-------------------------------------------------------------------
// Get the code of the first alarm on the controller
//-------------------------------------------------------------------
int Ros_Controller_GetAlarmCode()
{
MP_ALARM_CODE_RSP_DATA alarmData;
memset(&alarmData, 0x00, sizeof(alarmData));
if(mpGetAlarmCode(&alarmData) == 0)
{
if(alarmData.usAlarmNum > 0)
return(alarmData.AlarmData.usAlarmNo[0]);
else
return 0;
}
return -1;
}
//-------------------------------------------------------------------
// Convert error code to string
//-------------------------------------------------------------------
void Ros_Controller_ErrNo_ToString(int errNo, char errMsg[ERROR_MSG_MAX_SIZE], int errMsgSize)
{
switch(errNo)
{
case 0x2010: memcpy(errMsg, "Robot is in operation", errMsgSize); break;
case 0x2030: memcpy(errMsg, "In HOLD status (PP)", errMsgSize); break;
case 0x2040: memcpy(errMsg, "In HOLD status (External)", errMsgSize); break;
case 0x2050: memcpy(errMsg, "In HOLD status (Command)", errMsgSize); break;
case 0x2060: memcpy(errMsg, "In ERROR/ALARM status", errMsgSize); break;
case 0x2070: memcpy(errMsg, "In SERVO OFF status", errMsgSize); break;
case 0x2080: memcpy(errMsg, "Wrong operation mode", errMsgSize); break;
case 0x3040: memcpy(errMsg, "The home position is not registered", errMsgSize); break;
case 0x3050: memcpy(errMsg, "Out of range (ABSO data", errMsgSize); break;
case 0x3400: memcpy(errMsg, "Cannot operate MASTER JOB", errMsgSize); break;
case 0x3410: memcpy(errMsg, "The JOB name is already registered in another task", errMsgSize); break;
case 0x4040: memcpy(errMsg, "Specified JOB not found", errMsgSize); break;
case 0x5200: memcpy(errMsg, "Over data range", errMsgSize); break;
default: memcpy(errMsg, "Unspecified reason", errMsgSize); break;
}
}
#ifdef DX100
void Ros_Controller_ListenForSkill(Controller* controller, int sl)
{
SYS2MP_SENS_MSG skillMsg;
STATUS apiRet;
controller->bSkillMotionReady[sl - MP_SL_ID1] = FALSE;
memset(&skillMsg, 0x00, sizeof(SYS2MP_SENS_MSG));
FOREVER
{
//SKILL complete
//This will cause the SKILLSND command to complete the cursor to move to the next line.
//Make sure all preparation is complete to move.
//mpEndSkillCommandProcess(sl, &skillMsg);
mpEndSkillCommandProcess(sl, &skillMsg);
Ros_Sleep(4); //sleepy time
//Get SKILL command
//task will wait for a skillsnd command in INFORM
apiRet = mpReceiveSkillCommand(sl, &skillMsg);
if (skillMsg.main_comm != MP_SKILL_COMM)
{
printf("Ignoring command, because it's not a SKILL command\n");
continue;
}
//Process SKILL command
switch(skillMsg.sub_comm)
{
case MP_SKILL_SEND:
if(strcmp(skillMsg.cmd, "ROS-START") == 0)
{
controller->bSkillMotionReady[sl - MP_SL_ID1] = TRUE;
}
else if(strcmp(skillMsg.cmd, "ROS-STOP") == 0)
{
controller->bSkillMotionReady[sl - MP_SL_ID1] = FALSE;
}
else
{
printf ("MP_SKILL_SEND(SL_ID=%d) - %s \n", sl, skillMsg.cmd);
}
#ifdef DEBUG
printf("controller->bSkillMotionReady[%d] = %d\n", (sl - MP_SL_ID1), controller->bSkillMotionReady[sl - MP_SL_ID1]);
#endif
if(Ros_Controller_IsMotionReady(controller))
printf("Robot job is ready for ROS commands.\r\n");
break;
case MP_SKILL_END:
//ABORT!!!
controller->bSkillMotionReady[sl - MP_SL_ID1] = FALSE;
break;
}
}
}
#endif
#if DX100
// VxWorks 5.5 do not have vsnprintf, use vsprintf instead...
int vsnprintf(char *s, size_t sz, const char *fmt, va_list args)
{
char tmpBuf[1024]; // Hopefully enough...
size_t res;
res = vsprintf(tmpBuf, fmt, args);
tmpBuf[sizeof(tmpBuf) - 1] = 0; // be sure ending \0 is there
if (res >= sz)
{
// Buffer overrun...
printf("Logging.. Error vsnprintf:%d max:%d, anyway:\r\n", (int)res, (int)sz);
printf("%s", tmpBuf);
res = -res;
}
strncpy(s, tmpBuf, sz);
s[sz - 1] = 0; // be sure ending \0 is there
return res;
}
// VxWorks 5.5 do not have snprintf
int snprintf(char *s, size_t sz, const char *fmt, ...)
{
size_t res;
char tmpBuf[1024]; // Hopefully enough...
va_list va;
va_start(va, fmt);
res = vsnprintf(tmpBuf, sz, fmt, va);
va_end(va);
strncpy(s, tmpBuf, sz);
s[sz - 1] = 0; // be sure ending \0 is there
return res;
}
#endif
void motoRosAssert(BOOL mustBeTrue, ROS_ASSERTION_CODE subCodeIfFalse, char* msgFmtIfFalse, ...)
{
const int MAX_MSG_LEN = 32;
char msg[MAX_MSG_LEN];
char subMsg[MAX_MSG_LEN];
va_list va;
if (!mustBeTrue)
{
memset(msg, 0x00, MAX_MSG_LEN);
memset(subMsg, 0x00, MAX_MSG_LEN);
va_start(va, msgFmtIfFalse);
vsnprintf(subMsg, MAX_MSG_LEN, msgFmtIfFalse, va);
va_end(va);
snprintf(msg, MAX_MSG_LEN, "MotoROS:%s", subMsg); //add "MotoROS" to distinguish from other controller alarms
Ros_Controller_SetIOState(IO_FEEDBACK_FAILURE, TRUE);
Ros_Controller_SetIOState(IO_FEEDBACK_INITIALIZATION_DONE, FALSE);
mpSetAlarm(8000, msg, subCodeIfFalse);
while (TRUE) //forever
{
puts(msg);
Ros_Sleep(5000);
}
}
}
void Db_Print(char* msgFormat, ...)
{
#ifdef DEBUG