-
Notifications
You must be signed in to change notification settings - Fork 195
/
MotionServer.c
2033 lines (1745 loc) · 69.2 KB
/
MotionServer.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
// MotionServer.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"
//-----------------------
// Function Declarations
//-----------------------
// Main Task:
void Ros_MotionServer_StartNewConnection(Controller* controller, int sd);
void Ros_MotionServer_StopConnection(Controller* controller, int connectionIndex);
// WaitForSimpleMsg Task:
void Ros_MotionServer_WaitForSimpleMsg(Controller* controller, int connectionIndex);
BOOL Ros_MotionServer_SimpleMsgProcess(Controller* controller, SimpleMsg* receiveMsg, SimpleMsg* replyMsg);
int Ros_MotionServer_MotionCtrlProcess(Controller* controller, SimpleMsg* receiveMsg, SimpleMsg* replyMsg);
BOOL Ros_MotionServer_StopMotion(Controller* controller);
BOOL Ros_MotionServer_ServoPower(Controller* controller, int servoOnOff);
BOOL Ros_MotionServer_ResetAlarm(Controller* controller);
BOOL Ros_MotionServer_StartTrajMode(Controller* controller);
BOOL Ros_MotionServer_StopTrajMode(Controller* controller);
int Ros_MotionServer_JointTrajDataProcess(Controller* controller, SimpleMsg* receiveMsg, SimpleMsg* replyMsg);
int Ros_MotionServer_InitTrajPointFull(CtrlGroup* ctrlGroup, SmBodyJointTrajPtFull* jointTrajData);
int Ros_MotionServer_InitTrajPointFullEx(CtrlGroup* ctrlGroup, SmBodyJointTrajPtExData* jointTrajDataEx, int sequence);
int Ros_MotionServer_AddTrajPointFull(CtrlGroup* ctrlGroup, SmBodyJointTrajPtFull* jointTrajData);
int Ros_MotionServer_AddTrajPointFullEx(CtrlGroup* ctrlGroup, SmBodyJointTrajPtExData* jointTrajDataEx, int sequence);
int Ros_MotionServer_JointTrajPtFullExProcess(Controller* controller, SimpleMsg* receiveMsg, SimpleMsg* replyMsg);
int Ros_MotionServer_GetDhParameters(Controller* controller, SimpleMsg* replyMsg);
int Ros_MotionServer_SetSelectedTool(Controller* controller, SimpleMsg* receiveMsg, SimpleMsg* replyMsg);
void Ros_MotionServer_EnsureEcoModeIsDisabled(Controller* controller);
// AddToIncQueue Task:
void Ros_MotionServer_AddToIncQueueProcess(Controller* controller, int groupNo);
void Ros_MotionServer_JointTrajDataToIncQueue(Controller* controller, int groupNo);
BOOL Ros_MotionServer_AddPulseIncPointToQ(Controller* controller, int groupNo, Incremental_data* dataToEnQ);
BOOL Ros_MotionServer_ClearQ_All(Controller* controller);
BOOL Ros_MotionServer_HasDataInQueue(Controller* controller);
int Ros_MotionServer_GetQueueCnt(Controller* controller, int groupNo);
void Ros_MotionServer_IncMoveLoopStart(Controller* controller);
// Utility functions:
void Ros_MotionServer_ConvertToJointMotionData(SmBodyJointTrajPtFull* jointTrajData, JointMotionData* jointMotionData);
STATUS Ros_MotionServer_DisableEcoMode(Controller* controller);
void Ros_MotionServer_PrintError(USHORT err_no, char* msgPrefix);
//-----------------------
// Function implementation
//-----------------------
//-----------------------------------------------------------------------
// Start the tasks for a new motion server connection:
// - WaitForSimpleMsg: Task that waits to receive new SimpleMessage
// - AddToIncQueueProcess: Task that take data from a message and generate Incmove
//-----------------------------------------------------------------------
void Ros_MotionServer_StartNewConnection(Controller* controller, int sd)
{
int groupNo;
int connectionIndex;
int sockOpt;
printf("Starting new connection to the Motion Server\r\n");
ATTEMPT_MOTION_CONNECTION:
//look for next available connection slot
for (connectionIndex = 0; connectionIndex < MAX_MOTION_CONNECTIONS; connectionIndex++)
{
if (controller->sdMotionConnections[connectionIndex] == INVALID_SOCKET)
{
controller->sdMotionConnections[connectionIndex] = sd;
break;
}
}
if (connectionIndex == MAX_MOTION_CONNECTIONS)
{
if (Ros_MotionServer_HasDataInQueue(controller)) //another client is actively controlling motion
{
puts("Motion server already connected... not accepting last attempt.");
mpClose(sd);
return;
}
else
{
puts("Motion server already connected... closing old connection.");
Ros_MotionServer_StopConnection(controller, 0); //close socket, cleanup resources, and delete tasks
goto ATTEMPT_MOTION_CONNECTION; //goto is sometimes useful... don't judge me
}
}
//This timeout detection takes two hours. So, it's not terribly useful. But, it still serves a purpose.
sockOpt = 1;
mpSetsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char*)&sockOpt, sizeof(sockOpt));
// If not started, start the IncMoveTask (there should be only one instance of this thread)
if(controller->tidIncMoveThread == INVALID_TASK)
{
puts("Creating new task: IncMoveTask");
controller->tidIncMoveThread = mpCreateTask(MP_PRI_IP_CLK_TAKE, MP_STACK_SIZE,
(FUNCPTR)Ros_MotionServer_IncMoveLoopStart,
(int)controller, 0, 0, 0, 0, 0, 0, 0, 0, 0);
if (controller->tidIncMoveThread == ERROR)
{
puts("Failed to create task for incremental-motion. Check robot parameters.");
mpClose(sd);
controller->tidIncMoveThread = INVALID_TASK;
Ros_Controller_SetIOState(IO_FEEDBACK_FAILURE, TRUE);
mpSetAlarm(8004, "MOTOROS FAILED TO CREATE TASK", 4);
return;
}
}
// If not started, start the AddToIncQueueProcess for each control group
for(groupNo = 0; groupNo < controller->numGroup; groupNo++)
{
if (controller->ctrlGroups[groupNo]->tidAddToIncQueue == INVALID_TASK)
{
printf("Creating new task: tidAddToIncQueue (groupNo = %d)\n", groupNo);
controller->ctrlGroups[groupNo]->tidAddToIncQueue = mpCreateTask(MP_PRI_TIME_NORMAL, MP_STACK_SIZE,
(FUNCPTR)Ros_MotionServer_AddToIncQueueProcess,
(int)controller, groupNo, 0, 0, 0, 0, 0, 0, 0, 0);
if (controller->ctrlGroups[groupNo]->tidAddToIncQueue == ERROR)
{
puts("Failed to create task for parsing motion increments. Check robot parameters.");
mpClose(sd);
controller->ctrlGroups[groupNo]->tidAddToIncQueue = INVALID_TASK;
Ros_Controller_SetIOState(IO_FEEDBACK_FAILURE, TRUE);
mpSetAlarm(8004, "MOTOROS FAILED TO CREATE TASK", 5);
return;
}
}
}
if (controller->tidMotionConnections[connectionIndex] == INVALID_TASK)
{
printf("Creating new task: tidMotionConnections (connectionIndex = %d)\n", connectionIndex);
//start new task for this specific connection
controller->tidMotionConnections[connectionIndex] = mpCreateTask(MP_PRI_TIME_NORMAL, MP_STACK_SIZE,
(FUNCPTR)Ros_MotionServer_WaitForSimpleMsg,
(int)controller, connectionIndex, 0, 0, 0, 0, 0, 0, 0, 0);
if (controller->tidMotionConnections[connectionIndex] != ERROR)
{
Ros_Controller_SetIOState(IO_FEEDBACK_MOTIONSERVERCONNECTED, TRUE); //set feedback signal indicating success
}
else
{
puts("Could not create new task in the motion server. Check robot parameters.");
mpClose(sd);
controller->sdMotionConnections[connectionIndex] = INVALID_SOCKET;
controller->tidMotionConnections[connectionIndex] = INVALID_TASK;
Ros_Controller_SetIOState(IO_FEEDBACK_FAILURE, TRUE);
mpSetAlarm(8004, "MOTOROS FAILED TO CREATE TASK", 6);
return;
}
}
}
//-----------------------------------------------------------------------
// Close a connection along with all its associated task
//-----------------------------------------------------------------------
void Ros_MotionServer_StopConnection(Controller* controller, int connectionIndex)
{
int i;
int tid;
BOOL bDeleteIncMovTask;
printf("Closing Motion Server Connection\r\n");
//close this connection
mpClose(controller->sdMotionConnections[connectionIndex]);
//mark connection as invalid
controller->sdMotionConnections[connectionIndex] = INVALID_SOCKET;
// Check if there are still some valid connection
bDeleteIncMovTask = TRUE;
for(i=0; i<MAX_MOTION_CONNECTIONS; i++)
{
if(controller->sdMotionConnections[connectionIndex] != INVALID_SOCKET)
{
bDeleteIncMovTask = FALSE;
break;
}
}
// If there is no more connection, stop the inc_move task
if(bDeleteIncMovTask)
{
//set feedback signal
Ros_Controller_SetIOState(IO_FEEDBACK_MOTIONSERVERCONNECTED, FALSE);
// Stop adding increment to queue (for each ctrlGroup
for(i=0; i < controller->numGroup; i++)
{
controller->ctrlGroups[i]->hasDataToProcess = FALSE;
tid = controller->ctrlGroups[i]->tidAddToIncQueue;
controller->ctrlGroups[i]->tidAddToIncQueue = INVALID_TASK;
mpDeleteTask(tid);
}
// terminate the inc_move task
tid = controller->tidIncMoveThread;
controller->tidIncMoveThread = INVALID_TASK;
mpDeleteTask(tid);
}
// Stop message receiption task
tid = controller->tidMotionConnections[connectionIndex];
controller->tidMotionConnections[connectionIndex] = INVALID_TASK;
printf("Motion Server Connection Closed\r\n");
mpDeleteTask(tid);
}
int Ros_MotionServer_GetExpectedByteSizeForMessageType(SimpleMsg* receiveMsg, int recvByteSize)
{
int minSize = sizeof(SmPrefix) + sizeof(SmHeader);
int expectedSize;
switch (receiveMsg->header.msgType)
{
case ROS_MSG_PING:
expectedSize = minSize;
break;
case ROS_MSG_ROBOT_STATUS:
expectedSize = minSize + sizeof(SmBodyRobotStatus);
break;
case ROS_MSG_JOINT_TRAJ_PT_FULL:
expectedSize = minSize + sizeof(SmBodyJointTrajPtFull);
break;
case ROS_MSG_JOINT_FEEDBACK:
expectedSize = minSize + sizeof(SmBodyJointFeedback);
break;
case ROS_MSG_MOTO_MOTION_CTRL:
expectedSize = minSize + sizeof(SmBodyMotoMotionCtrl);
break;
case ROS_MSG_MOTO_MOTION_REPLY:
expectedSize = minSize + sizeof(SmBodyMotoMotionReply);
break;
case ROS_MSG_MOTO_JOINT_TRAJ_PT_FULL_EX:
//Don't require the user to send data for non-existant control groups
if (recvByteSize >= (int)(minSize + sizeof(int))) //make sure I can at least get to [numberOfGroups] field
{
expectedSize = minSize + (sizeof(int) * 2);
expectedSize += (sizeof(SmBodyJointTrajPtExData) * receiveMsg->body.jointTrajDataEx.numberOfValidGroups); //check the number of groups to determine size of data
}
else
expectedSize = minSize + sizeof(SmBodyJointTrajPtFullEx);
break;
case ROS_MSG_MOTO_JOINT_FEEDBACK_EX:
expectedSize = minSize + sizeof(SmBodyJointFeedbackEx);
break;
case ROS_MSG_MOTO_READ_IO_BIT:
expectedSize = minSize + sizeof(SmBodyMotoReadIOBit);
break;
case ROS_MSG_MOTO_WRITE_IO_BIT:
expectedSize = minSize + sizeof(SmBodyMotoWriteIOBit);
break;
case ROS_MSG_MOTO_READ_IO_GROUP:
expectedSize = minSize + sizeof(SmBodyMotoReadIOGroup);
break;
case ROS_MSG_MOTO_WRITE_IO_GROUP:
expectedSize = minSize + sizeof(SmBodyMotoWriteIOGroup);
break;
case ROS_MSG_MOTO_GET_DH_PARAMETERS:
expectedSize = minSize; //no additional data on the request
break;
case ROS_MSG_MOTO_SELECT_TOOL:
expectedSize = minSize + sizeof(SmBodySelectTool);
break;
default: //invalid message type
return -1;
}
return expectedSize;
}
//-----------------------------------------------------------------------
// Task that waits to receive new SimpleMessage and then processes it
//-----------------------------------------------------------------------
void Ros_MotionServer_WaitForSimpleMsg(Controller* controller, int connectionIndex)
{
SimpleMsg receiveMsg;
SimpleMsg replyMsg;
int byteSize = 0, byteSizeResponse = 0;
int minSize = sizeof(SmPrefix) + sizeof(SmHeader);
int expectedSize;
int ret = 0;
int partialMsgByteCount = 0;
BOOL bSkipNetworkRecv = FALSE;
while (TRUE) //keep accepting messages until connection closes
{
Ros_Sleep(0); //give it some time to breathe, if needed
if (!bSkipNetworkRecv) //if I don't already have an extra complete packet buffered from the previous recv
{
//Receive message from the PC
memset((&receiveMsg) + partialMsgByteCount, 0x00, sizeof(SimpleMsg) - partialMsgByteCount);
byteSize = mpRecv(controller->sdMotionConnections[connectionIndex], (char*)((&receiveMsg) + partialMsgByteCount), sizeof(SimpleMsg) - partialMsgByteCount, 0);
if (byteSize <= 0)
break; //end connection
byteSize += partialMsgByteCount;
partialMsgByteCount = 0;
}
else
{
byteSize = partialMsgByteCount;
partialMsgByteCount = 0;
bSkipNetworkRecv = FALSE;
}
// Determine the expected size of the message
expectedSize = -1;
if(byteSize >= minSize)
{
expectedSize = Ros_MotionServer_GetExpectedByteSizeForMessageType(&receiveMsg, byteSize);
if (expectedSize == -1)
{
printf("Unknown Message Received (%d)\r\n", receiveMsg.header.msgType);
Ros_SimpleMsg_MotionReply(&receiveMsg, ROS_RESULT_INVALID, ROS_RESULT_INVALID_MSGTYPE, &replyMsg, 0);
}
else if (byteSize >= expectedSize) // Check message size
{
// Process the simple message
ret = Ros_MotionServer_SimpleMsgProcess(controller, &receiveMsg, &replyMsg);
if (ret != OK) //error during processing
{
break; //disconnect
}
else if (byteSize > expectedSize) // Received extra data in single message
{
//Special case where ROS_MSG_MOTO_JOINT_TRAJ_PT_FULL_EX message could have different lengths
if (receiveMsg.header.msgType == ROS_MSG_MOTO_JOINT_TRAJ_PT_FULL_EX &&
byteSize == (int)(minSize + sizeof(SmBodyJointTrajPtFullEx)))
{
// All good
partialMsgByteCount = 0;
}
else
{
// Preserve the remaining bytes and treat them as the start of a new message
Db_Print("MessageReceived(%d bytes): expectedSize=%d, processing rest of bytes (%d, %d, %d)\r\n", byteSize, expectedSize, sizeof(receiveMsg), receiveMsg.body.jointTrajData.sequence, ((int*)((char*)&receiveMsg + expectedSize))[5]);
partialMsgByteCount = byteSize - expectedSize;
memmove(&receiveMsg, (char*)&receiveMsg + expectedSize, partialMsgByteCount);
//Did I receive multiple full messages at once that all need to be processed before listening for new data?
if (partialMsgByteCount >= minSize)
{
expectedSize = Ros_MotionServer_GetExpectedByteSizeForMessageType(&receiveMsg, partialMsgByteCount);
bSkipNetworkRecv = (partialMsgByteCount >= expectedSize); //does my modified receiveMsg buffer contain a full message to process?
}
}
}
else // All good
partialMsgByteCount = 0;
}
else // Not enough data to process the command
{
Db_Print("MessageReceived(%d bytes): expectedSize=%d\r\n", byteSize, expectedSize);
Ros_SimpleMsg_MotionReply(&receiveMsg, ROS_RESULT_INVALID, ROS_RESULT_INVALID_MSGSIZE, &replyMsg, 0);
}
}
else // Didn't even receive a command ID
{
Db_Print("Unknown Data Received (%d bytes)\r\n", byteSize);
Ros_SimpleMsg_MotionReply(&receiveMsg, ROS_RESULT_INVALID, ROS_RESULT_INVALID_MSGSIZE, &replyMsg, 0);
}
if (receiveMsg.header.commType != ROS_COMM_SERVICE_REPLY) //don't send a reply to a reply from the pc
{
//Send reply message
byteSizeResponse = mpSend(controller->sdMotionConnections[connectionIndex], (char*)(&replyMsg), replyMsg.prefix.length + sizeof(SmPrefix), 0);
if (byteSizeResponse <= 0)
break; // Close the connection
}
}
Ros_Sleep(50); // Just in case other associated task need time to clean-up.
//close this connection
Ros_MotionServer_StopConnection(controller, connectionIndex);
}
//-----------------------------------------------------------------------
// Checks the type of message and processes it accordingly
// Return -1=Failure; 0=Success; 1=CloseConnection;
//-----------------------------------------------------------------------
int Ros_MotionServer_SimpleMsgProcess(Controller* controller, SimpleMsg* receiveMsg, SimpleMsg* replyMsg)
{
int ret = ERROR;
int invalidSubcode = 0;
switch(receiveMsg->header.msgType)
{
case ROS_MSG_PING:
memset(replyMsg, 0x00, sizeof(SimpleMsg));
replyMsg->prefix.length = sizeof(SmHeader);
replyMsg->header.msgType = ROS_MSG_PING;
replyMsg->header.commType = ROS_COMM_SERVICE_REPLY;
replyMsg->header.replyType = ROS_REPLY_SUCCESS;
ret = OK;
break;
case ROS_MSG_JOINT_TRAJ_PT_FULL:
ret = Ros_MotionServer_JointTrajDataProcess(controller, receiveMsg, replyMsg);
break;
//-----------------------
case ROS_MSG_MOTO_MOTION_CTRL:
ret = Ros_MotionServer_MotionCtrlProcess(controller, receiveMsg, replyMsg);
break;
//-----------------------
case ROS_MSG_MOTO_JOINT_TRAJ_PT_FULL_EX:
ret = Ros_MotionServer_JointTrajPtFullExProcess(controller, receiveMsg, replyMsg);
break;
//Maintain backward compatibility for users who are sending I/O over motion-server
//-----------------------
case ROS_MSG_MOTO_READ_IO_BIT:
ret = Ros_IoServer_ReadIOBit(receiveMsg, replyMsg);
break;
//-----------------------
case ROS_MSG_MOTO_WRITE_IO_BIT:
ret = Ros_IoServer_WriteIOBit(receiveMsg, replyMsg);
break;
//-----------------------
case ROS_MSG_MOTO_READ_IO_GROUP:
ret = Ros_IoServer_ReadIOGroup(receiveMsg, replyMsg);
break;
//-----------------------
case ROS_MSG_MOTO_WRITE_IO_GROUP:
ret = Ros_IoServer_WriteIOGroup(receiveMsg, replyMsg);
break;
//-----------------------
case ROS_MSG_MOTO_GET_DH_PARAMETERS:
ret = Ros_MotionServer_GetDhParameters(controller, replyMsg);
break;
//-----------------------
case ROS_MSG_MOTO_SELECT_TOOL:
ret = Ros_MotionServer_SetSelectedTool(controller, receiveMsg, replyMsg);
break;
//-----------------------
default:
printf("Invalid message type: %d\n", receiveMsg->header.msgType);
invalidSubcode = ROS_RESULT_INVALID_MSGTYPE;
break;
}
// Check Invalid Case
if(invalidSubcode != 0)
{
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_INVALID, invalidSubcode, replyMsg, 0);
ret = ERROR;
}
return ret;
}
//-----------------------------------------------------------------------
// Processes message of type: ROS_MSG_MOTO_JOINT_TRAJ_PT_FULL_EX
// Return -1=Failure; 0=Success; 1=CloseConnection;
//-----------------------------------------------------------------------
int Ros_MotionServer_JointTrajPtFullExProcess(Controller* controller, SimpleMsg* receiveMsg,
SimpleMsg* replyMsg)
{
SmBodyJointTrajPtFullEx* msgBody;
CtrlGroup* ctrlGroup;
int ret, i;
FlagsValidFields validationFlags;
msgBody = &receiveMsg->body.jointTrajDataEx;
// Check if controller is able to receive incremental move and if the incremental move thread is running
if(!Ros_Controller_IsMotionReady(controller))
{
int subcode = Ros_Controller_GetNotReadySubcode(controller);
printf("ERROR: Controller is not ready (code: %d). Can't process ROS_MSG_MOTO_JOINT_TRAJ_PT_FULL_EX.\r\n", subcode);
for (i = 0; i < msgBody->numberOfValidGroups; i += 1)
{
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_NOT_READY, subcode, replyMsg, msgBody->jointTrajPtData[i].groupNo);
}
return 0;
}
// Pre-check to ensure no groups are busy
for (i = 0; i < msgBody->numberOfValidGroups; i += 1)
{
if (Ros_Controller_IsValidGroupNo(controller, msgBody->jointTrajPtData[i].groupNo))
{
ctrlGroup = controller->ctrlGroups[msgBody->jointTrajPtData[i].groupNo];
if (ctrlGroup->hasDataToProcess)
{
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_BUSY, 0, replyMsg, msgBody->jointTrajPtData[i].groupNo);
return 0;
}
}
else
{
printf("ERROR: GroupNo %d is not valid\n", msgBody->jointTrajPtData[i].groupNo);
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_INVALID, ROS_RESULT_INVALID_GROUPNO, replyMsg, msgBody->jointTrajPtData[i].groupNo);
return 0;
}
// Check that minimum information (time, position, velocity) is valid
validationFlags = Valid_Time | Valid_Position | Valid_Velocity;
if( (msgBody->jointTrajPtData[i].validFields & validationFlags) != validationFlags)
{
printf("ERROR: Validfields = %d\r\n", msgBody->jointTrajPtData[i].validFields);
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_INVALID, ROS_RESULT_INVALID_DATA_INSUFFICIENT, replyMsg, msgBody->jointTrajPtData[i].groupNo);
return 0;
}
}
for (i = 0; i < msgBody->numberOfValidGroups; i += 1)
{
ctrlGroup = controller->ctrlGroups[msgBody->jointTrajPtData[i].groupNo];
// Check the trajectory sequence code
if(msgBody->sequence == 0) // First trajectory point
{
//It's possible for energy-saving mode to activate between /robot_enable and the first trajectory point.
Ros_MotionServer_EnsureEcoModeIsDisabled(controller); //make sure that Ros_Controller_IsMotionReady gets called above before calling this
// Initialize first point variables
ret = Ros_MotionServer_InitTrajPointFullEx(ctrlGroup, &msgBody->jointTrajPtData[i], msgBody->sequence);
// set reply
if(ret == 0)
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_SUCCESS, 0, replyMsg, msgBody->jointTrajPtData[i].groupNo);
else
{
printf("ERROR: Ros_MotionServer_InitTrajPointFullEx returned %d\n", ret);
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_INVALID, ret, replyMsg, msgBody->jointTrajPtData[i].groupNo);
return 0; //stop processing other groups in this loop
}
}
else if(msgBody->sequence > 0)// Subsequent trajectory points
{
// Add the point to the trajectory
ret = Ros_MotionServer_AddTrajPointFullEx(ctrlGroup, &msgBody->jointTrajPtData[i], msgBody->sequence);
// ser reply
if(ret == 0)
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_SUCCESS, 0, replyMsg, msgBody->jointTrajPtData[i].groupNo);
else if(ret == 1)
{
printf("ERROR: Ros_MotionServer_AddTrajPointFullEx returned %d\n", ret);
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_BUSY, 0, replyMsg, msgBody->jointTrajPtData[i].groupNo);
return 0; //stop processing other groups in this loop
}
else
{
printf("ERROR: Ros_MotionServer_AddTrajPointFullEx returned %d\n", ret);
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_INVALID, ret, replyMsg, msgBody->jointTrajPtData[i].groupNo);
return 0; //stop processing other groups in this loop
}
}
else
{
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_INVALID, ROS_RESULT_INVALID_SEQUENCE, replyMsg, msgBody->jointTrajPtData[i].groupNo);
return 0; //stop processing other groups in this loop
}
}
return 0;
}
//-----------------------------------------------------------------------
// Processes message of type: ROS_MSG_MOTO_MOTION_CTRL
// Return -1=Failure; 0=Success; 1=CloseConnection;
//-----------------------------------------------------------------------
int Ros_MotionServer_MotionCtrlProcess(Controller* controller, SimpleMsg* receiveMsg,
SimpleMsg* replyMsg)
{
SmBodyMotoMotionCtrl* motionCtrl;
//printf("In MotionCtrlProcess\r\n");
// Check the command code
motionCtrl = &receiveMsg->body.motionCtrl;
switch(motionCtrl->command)
{
case ROS_CMD_CHECK_MOTION_READY:
{
if(Ros_Controller_IsMotionReady(controller))
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_TRUE, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
else
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_FALSE, Ros_Controller_GetNotReadySubcode(controller), replyMsg, receiveMsg->body.motionCtrl.groupNo);
break;
}
case ROS_CMD_CHECK_QUEUE_CNT:
{
int count = Ros_MotionServer_GetQueueCnt(controller, motionCtrl->groupNo);
if(count >= 0)
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_TRUE, count, replyMsg, receiveMsg->body.motionCtrl.groupNo);
else
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_FAILURE, count, replyMsg, receiveMsg->body.motionCtrl.groupNo);
break;
}
case ROS_CMD_STOP_MOTION:
{
// Stop Motion
BOOL bRet = Ros_MotionServer_StopMotion(controller);
// Reply msg
if(bRet)
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_SUCCESS, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
else
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_FAILURE, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
break;
}
case ROS_CMD_START_SERVOS:
{
// Stop Motion
BOOL bRet = Ros_MotionServer_ServoPower(controller, ON);
// Reply msg
if(bRet)
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_SUCCESS, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
else
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_FAILURE, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
break;
}
case ROS_CMD_STOP_SERVOS:
{
// Stop Motion
BOOL bRet = Ros_MotionServer_ServoPower(controller, OFF);
// Reply msg
if(bRet)
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_SUCCESS, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
else
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_FAILURE, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
break;
}
case ROS_CMD_RESET_ALARM:
{
// Stop Motion
BOOL bRet = Ros_MotionServer_ResetAlarm(controller);
// Reply msg
if(bRet)
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_SUCCESS, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
else
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_FAILURE, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
break;
}
case ROS_CMD_START_TRAJ_MODE:
{
// Start Trajectory mode by starting the INIT_ROS job on the controller
BOOL bRet = Ros_MotionServer_StartTrajMode(controller);
if(bRet)
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_SUCCESS, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
else
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_NOT_READY,
Ros_Controller_GetNotReadySubcode(controller), replyMsg, receiveMsg->body.motionCtrl.groupNo);
break;
}
case ROS_CMD_STOP_TRAJ_MODE:
case ROS_CMD_DISCONNECT:
{
BOOL bRet = Ros_MotionServer_StopTrajMode(controller);
if(bRet)
{
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_SUCCESS, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
if(motionCtrl->command == ROS_CMD_DISCONNECT)
return 1;
}
else
Ros_SimpleMsg_MotionReply(receiveMsg, ROS_RESULT_FAILURE, 0, replyMsg, receiveMsg->body.motionCtrl.groupNo);
break;
}
}
return 0;
}
//-----------------------------------------------------------------------
// Stop motion by stopping message processing and clearing the queue
//-----------------------------------------------------------------------
BOOL Ros_MotionServer_StopMotion(Controller* controller)
{
// NOTE: for the time being, stop motion will stop all motion for all control group
BOOL bRet;
BOOL bStopped;
int checkCnt;
int groupNo;
// Stop any motion from being processed further
controller->bStopMotion = TRUE;
// Check that background processing of message has been stopped
for(checkCnt=0; checkCnt<MOTION_STOP_TIMEOUT; checkCnt++)
{
bStopped = TRUE;
for(groupNo=0; groupNo<controller->numGroup; groupNo++)
bStopped &= !controller->ctrlGroups[groupNo]->hasDataToProcess;
if(bStopped)
break;
else
Ros_Sleep(1);
}
// Clear queues
bRet = Ros_MotionServer_ClearQ_All(controller);
// All motion should be stopped at this point, so turn of the flag
controller->bStopMotion = FALSE;
if (checkCnt >= MOTION_STOP_TIMEOUT)
printf("WARNING: Message processing not stopped before clearing queue\r\n");
return(bStopped && bRet);
}
//-----------------------------------------------------------------------
// Sets servo power to ON or OFF
//-----------------------------------------------------------------------
BOOL Ros_MotionServer_ServoPower(Controller* controller, int servoOnOff)
{
MP_SERVO_POWER_SEND_DATA sServoData;
MP_STD_RSP_DATA stdRespData;
int ret;
STATUS status;
#ifdef DUMMY_SERVO_MODE
return TRUE;
#endif
if (servoOnOff == OFF)
Ros_MotionServer_StopMotion(controller);
if (servoOnOff == ON)
{
status = Ros_MotionServer_DisableEcoMode(controller);
if (status == NG)
{
Ros_Controller_StatusUpdate(controller);
return Ros_Controller_IsServoOn(controller) == servoOnOff;
}
}
printf("Setting servo power: %d\n", servoOnOff);
memset(&sServoData, 0x00, sizeof(sServoData));
memset(&stdRespData, 0x00, sizeof(stdRespData));
sServoData.sServoPower = servoOnOff;
ret = mpSetServoPower(&sServoData, &stdRespData);
if( (ret == 0) && (stdRespData.err_no == 0) )
{
// wait for confirmation
int checkCount;
for(checkCount=0; checkCount<MOTION_START_TIMEOUT; checkCount+=MOTION_START_CHECK_PERIOD)
{
// Update status
Ros_Controller_StatusUpdate(controller);
if(Ros_Controller_IsServoOn(controller) == servoOnOff)
break;
Ros_Sleep(MOTION_START_CHECK_PERIOD);
}
}
else
{
Ros_MotionServer_PrintError(stdRespData.err_no, "Can't turn off servo because:");
}
// Update status
Ros_Controller_StatusUpdate(controller);
return Ros_Controller_IsServoOn(controller) == servoOnOff;
}
BOOL Ros_MotionServer_ResetAlarm(Controller* controller)
{
int ret, i;
BOOL returnBoolean;
MP_ALARM_STATUS_RSP_DATA alarmstatus;
MP_STD_RSP_DATA responseData;
returnBoolean = TRUE;
ret = mpGetAlarmStatus(&alarmstatus);
if( ret != 0 )
{
printf("Could not get alarm status\n");
//Ignore this error. Continue to try and clear the alarm.
}
if (alarmstatus.sIsAlarm & MASK_ISALARM_ACTIVEALARM) //alarm is active
{
MP_ALARM_CODE_RSP_DATA alarmcode;
ret = mpGetAlarmCode(&alarmcode);
if( ret != 0 )
{
printf("Could not get alarm code\n");
//Ignore this error. Continue to try and clear the alarm.
}
else
{
for (i = 0; i < alarmcode.usAlarmNum; i += 1)
printf("Has alarm: %d[%d], resetting...\n", alarmcode.AlarmData.usAlarmNo[i], alarmcode.AlarmData.usAlarmData[i]);
}
ret = mpResetAlarm(&responseData);
if( ret != 0 )
{
printf("Could not reset the alarm, failure code: %d\n", responseData.err_no);
returnBoolean = FALSE;
}
}
if (alarmstatus.sIsAlarm & MASK_ISALARM_ACTIVEERROR) //error is active
{
MP_ALARM_CODE_RSP_DATA alarmcode;
ret = mpGetAlarmCode(&alarmcode);
if (ret != 0)
{
printf("Could not get error code\n");
//Ignore this problem. Continue to try and clear the error.
}
else
{
printf("Has error: %d[%d], resetting...\n", alarmcode.usErrorNo, alarmcode.usErrorData);
}
ret = mpCancelError(&responseData);
if (ret != 0)
{
printf("Could not cancel the error, failure code: %d\n", responseData.err_no);
returnBoolean = FALSE;
}
}
Ros_Controller_StatusUpdate(controller);
return returnBoolean;
}
//-----------------------------------------------------------------------
// Attempts to start playback of a job to put the controller in RosMotion mode
//-----------------------------------------------------------------------
BOOL Ros_MotionServer_StartTrajMode(Controller* controller)
{
int ret;
MP_STD_RSP_DATA rData;
MP_START_JOB_SEND_DATA sStartData;
int checkCount;
int grpNo;
STATUS status;
printf("In StartTrajMode\r\n");
// Update status
Ros_Controller_StatusUpdate(controller);
// Reset PFL Activation Flag
if(controller->bPFLduringRosMove)
controller->bPFLduringRosMove = FALSE;
// Reset Inc Move Error
if (controller->bMpIncMoveError)
controller->bMpIncMoveError = FALSE;
// Check if already in the proper mode
if(Ros_Controller_IsMotionReady(controller))
return TRUE;
// Check if currently in operation, we don't want to interrupt current operation
if(Ros_Controller_IsOperating(controller))
return FALSE;
#ifndef DUMMY_SERVO_MODE
// Check for condition that need operator manual intervention
if(Ros_Controller_IsEStop(controller)
|| Ros_Controller_IsHold(controller)
|| !Ros_Controller_IsRemote(controller))
return FALSE;
#endif
// Check for condition that can be fixed remotely
if(Ros_Controller_IsError(controller))
{
// Cancel error
memset(&rData, 0x00, sizeof(rData));
ret = mpCancelError(&rData);
if(ret != 0)
goto updateStatus;
}
// Check for condition that can be fixed remotely
if(Ros_Controller_IsAlarm(controller))
{
// Reset alarm
memset(&rData, 0x00, sizeof(rData));
ret = mpResetAlarm(&rData);
if(ret == 0)
{
// wait for the Alarm reset confirmation
int checkCount;
for(checkCount=0; checkCount<MOTION_START_TIMEOUT; checkCount+=MOTION_START_CHECK_PERIOD)
{
// Update status
Ros_Controller_StatusUpdate(controller);
if(Ros_Controller_IsAlarm(controller) == FALSE)
continue;
Ros_Sleep(MOTION_START_CHECK_PERIOD);
}
if(Ros_Controller_IsAlarm(controller))
goto updateStatus;
}
else
goto updateStatus;
}
#ifndef DUMMY_SERVO_MODE
// Servo On
if(Ros_Controller_IsServoOn(controller) == FALSE)
{
MP_SERVO_POWER_SEND_DATA sServoData;
memset(&sServoData, 0x00, sizeof(sServoData));
status = Ros_MotionServer_DisableEcoMode(controller);
if (status == NG)
{
goto updateStatus;
}
sServoData.sServoPower = 1; // ON
memset(&rData, 0x00, sizeof(rData));
ret = mpSetServoPower(&sServoData, &rData);
if( (ret == 0) && (rData.err_no ==0) )
{
// wait for the Servo On confirmation
int checkCount;
for(checkCount=0; checkCount<MOTION_START_TIMEOUT; checkCount+=MOTION_START_CHECK_PERIOD)
{
// Update status
Ros_Controller_StatusUpdate(controller);
if (Ros_Controller_IsServoOn(controller) == TRUE)
break;
Ros_Sleep(MOTION_START_CHECK_PERIOD);
}
if(Ros_Controller_IsServoOn(controller) == FALSE)
goto updateStatus;
}