-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMotionServer.c
2131 lines (1827 loc) · 71.7 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
//
// History:
// 05/22/2013: Original release v.1.0.0
// 06/05/2013: Fix for multi-arm control to prevent return -3 (Invalid group)
// when calling function mpExRcsIncrementMove.
// 06/12/2013: Release v.1.0.1
// June 2014: Release v1.2.0
// Add support for multiple control groups.
// Add support for DX200 controller.
/*
* 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 "MotoPlus.h"
#include "ParameterExtraction.h"
#include "CtrlGroup.h"
#include "SimpleMessage.h"
#include "Controller.h"
#include "MotionServer.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, int byteSize, 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);
// lower 16 bits are SmResultType, upper 16 bites are the subcode
int 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);
// 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);
// IO functions:
int Ros_MotionServer_GetVersion( SimpleMsg* receiveMsg, SimpleMsg* replyMsg);
int Ros_MotionServer_ReadIOBit(SimpleMsg* receiveMsg, SimpleMsg* replyMsg);
int Ros_MotionServer_WriteIOBit(SimpleMsg* receiveMsg, SimpleMsg* replyMsg);
int Ros_MotionServer_ReadIOGroup(SimpleMsg* receiveMsg, SimpleMsg* replyMsg);
int Ros_MotionServer_WriteIOGroup(SimpleMsg* receiveMsg, SimpleMsg* replyMsg);
//-----------------------
// 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;
//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)
{
puts("Motion server already connected... not accepting last attempt.");
mpClose(sd);
return;
}
// If not started, start the IncMoveTask (there should be only one instance of this thread)
if(controller->tidIncMoveThread == INVALID_TASK)
{
#ifdef DEBUG
puts("Creating new task: IncMoveTask");
#endif
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);
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)
{
#ifdef DEBUG
printf("Creating new task: tidAddToIncQueue (groupNo = %d)\n", groupNo);
#endif
controller->ctrlGroups[groupNo]->tidAddToIncQueue = mpCreateTask(MP_PRI_TIME_CRITICAL, 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);
return;
}
}
}
if (controller->tidMotionConnections[connectionIndex] == INVALID_TASK)
{
#ifdef DEBUG
printf("Creating new task: tidMotionConnections (connectionIndex = %d)\n", connectionIndex);
#endif
//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);
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);
}
//-----------------------------------------------------------------------
// 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;
BOOL bDisconnect = FALSE;
BOOL bHasPreviousData = FALSE; // if true, then receiveMsg is already filled with valid data
BOOL bInvalidMsgType = FALSE;
while(!bDisconnect) //keep accepting messages until connection closes
{
Ros_Sleep(0); //give it some time to breathe, if needed
if (!bHasPreviousData)
{
//Receive message from the PC
memset(&receiveMsg, 0x00, sizeof(receiveMsg));
byteSize = mpRecv(controller->sdMotionConnections[connectionIndex], (char*)(&receiveMsg), sizeof(receiveMsg), 0);
if (byteSize <= 0)
break; //end connection
}
bInvalidMsgType = FALSE;
// Determine the expected size of the message
expectedSize = -1;
if(byteSize >= minSize)
{
switch(receiveMsg.header.msgType)
{
case ROS_MSG_GET_VERSION:
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 (byteSize >= (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;
default:
bInvalidMsgType = TRUE;
break;
}
}
bHasPreviousData = FALSE;
// Check message size
if(byteSize >= expectedSize && expectedSize <= sizeof(SimpleMsg))
{
// Process the simple message
ret = Ros_MotionServer_SimpleMsgProcess(controller, &receiveMsg, expectedSize, &replyMsg);
if(ret == 1)
{
bDisconnect = TRUE;
}
else if( byteSize > expectedSize )
{
printf("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]);
memmove(&receiveMsg, (char*)&receiveMsg + expectedSize, byteSize-expectedSize);
byteSize -= expectedSize;
bHasPreviousData = TRUE;
}
}
else if (bInvalidMsgType)
{
printf("Unknown Message Received(%d)\r\n", receiveMsg.header.msgType);
Ros_SimpleMsg_MotionReply(&receiveMsg, ROS_RESULT_INVALID, ROS_RESULT_INVALID_MSGTYPE, &replyMsg, 0);
}
else
{
printf("MessageReceived(%d bytes): expectedSize=%d\r\n", byteSize, expectedSize);
Ros_SimpleMsg_MotionReply(&receiveMsg, ROS_RESULT_INVALID, ROS_RESULT_INVALID_MSGSIZE, &replyMsg, 0);
// Note: If messages are being combine together because of network transmission protocol
// we may need to add code to store unused portion of the received buff that would be part of the next message
}
//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. Don't if necessary... but it doesn't hurt
//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,
int byteSize, SimpleMsg* replyMsg)
{
int ret = 0;
int expectedBytes = sizeof(SmPrefix) + sizeof(SmHeader);
int invalidSubcode = 0;
//printf("In SimpleMsgProcess\r\n");
switch(receiveMsg->header.msgType)
{
case ROS_MSG_GET_VERSION:
if(expectedBytes == byteSize)
ret = Ros_MotionServer_GetVersion(receiveMsg, replyMsg);
else
invalidSubcode = ROS_RESULT_INVALID_MSGSIZE;
break;
case ROS_MSG_JOINT_TRAJ_PT_FULL:
// Check that the appropriate message size was received
expectedBytes += sizeof(SmBodyJointTrajPtFull);
if(expectedBytes == byteSize)
ret = Ros_MotionServer_JointTrajDataProcess(controller, receiveMsg, replyMsg);
else
invalidSubcode = ROS_RESULT_INVALID_MSGSIZE;
break;
//-----------------------
case ROS_MSG_MOTO_MOTION_CTRL:
// Check that the appropriate message size was received
expectedBytes += sizeof(SmBodyMotoMotionCtrl);
if(expectedBytes == byteSize)
ret = Ros_MotionServer_MotionCtrlProcess(controller, receiveMsg, replyMsg);
else
invalidSubcode = ROS_RESULT_INVALID_MSGSIZE;
break;
//-----------------------
case ROS_MSG_MOTO_JOINT_TRAJ_PT_FULL_EX:
// Check that the appropriate message size was received
if (byteSize >= (expectedBytes + sizeof(int))) //make sure I can at least get to [numberOfGroups] field
{
expectedBytes += (sizeof(int) * 2);
expectedBytes += (sizeof(SmBodyJointTrajPtExData) * receiveMsg->body.jointTrajDataEx.numberOfValidGroups); //check the number of groups to determine size of data
}
else
expectedBytes += sizeof(SmBodyJointTrajPtFullEx);
if(expectedBytes <= byteSize)
ret = Ros_MotionServer_JointTrajPtFullExProcess(controller, receiveMsg, replyMsg);
else
invalidSubcode = ROS_RESULT_INVALID_MSGSIZE;
break;
case ROS_MSG_MOTO_READ_IO_BIT:
// Check that the appropriate message size was received
expectedBytes += sizeof(SmBodyMotoReadIOBit);
if(expectedBytes == byteSize)
ret = Ros_MotionServer_ReadIOBit(receiveMsg, replyMsg);
else
invalidSubcode = ROS_RESULT_INVALID_MSGSIZE;
break;
//-----------------------
case ROS_MSG_MOTO_WRITE_IO_BIT:
// Check that the appropriate message size was received
expectedBytes += sizeof(SmBodyMotoWriteIOBit);
if(expectedBytes == byteSize)
ret = Ros_MotionServer_WriteIOBit(receiveMsg, replyMsg);
else
invalidSubcode = ROS_RESULT_INVALID_MSGSIZE;
break;
//-----------------------
case ROS_MSG_MOTO_READ_IO_GROUP:
// Check that the appropriate message size was received
expectedBytes += sizeof(SmBodyMotoReadIOGroup);
if (expectedBytes == byteSize)
ret = Ros_MotionServer_ReadIOGroup(receiveMsg, replyMsg);
else
invalidSubcode = ROS_RESULT_INVALID_MSGSIZE;
break;
//-----------------------
case ROS_MSG_MOTO_WRITE_IO_GROUP:
// Check that the appropriate message size was received
expectedBytes += sizeof(SmBodyMotoWriteIOGroup);
if (expectedBytes == byteSize)
ret = Ros_MotionServer_WriteIOGroup(receiveMsg, replyMsg);
else
invalidSubcode = ROS_RESULT_INVALID_MSGSIZE;
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 = -1;
}
return ret;
}
int Ros_MotionServer_ReadIOBit(SimpleMsg* receiveMsg, SimpleMsg* replyMsg)
{
int apiRet;
MP_IO_INFO ioReadInfo;
USHORT ioValue;
int resultCode;
//initialize memory
memset(replyMsg, 0x00, sizeof(SimpleMsg));
// set prefix: length of message excluding the prefix
replyMsg->prefix.length = sizeof(SmHeader) + sizeof(SmBodyMotoReadIOBitReply);
// set header information of the reply
replyMsg->header.msgType = ROS_MSG_MOTO_READ_IO_BIT_REPLY;
replyMsg->header.commType = ROS_COMM_SERVICE_REPLY;
ioReadInfo.ulAddr = receiveMsg->body.readIOBit.ioAddress;
apiRet = mpReadIO(&ioReadInfo, &ioValue, 1);
if (apiRet == OK)
resultCode = ROS_REPLY_SUCCESS;
else
resultCode = ROS_REPLY_FAILURE;
replyMsg->body.readIOBitReply.value = ioValue;
replyMsg->body.readIOBitReply.resultCode = resultCode;
replyMsg->header.replyType = (SmReplyType)resultCode;
return OK;
}
int Ros_MotionServer_ReadIOGroup(SimpleMsg* receiveMsg, SimpleMsg* replyMsg)
{
int apiRet;
MP_IO_INFO ioReadInfo[8];
USHORT ioValue[8];
int resultCode;
int resultValue = 0;
int i;
//initialize memory
memset(replyMsg, 0x00, sizeof(SimpleMsg));
// set prefix: length of message excluding the prefix
replyMsg->prefix.length = sizeof(SmHeader) + sizeof(SmBodyMotoReadIOGroupReply);
// set header information of the reply
replyMsg->header.msgType = ROS_MSG_MOTO_READ_IO_GROUP_REPLY;
replyMsg->header.commType = ROS_COMM_SERVICE_REPLY;
for (i = 0; i < 8; i += 1)
{
ioReadInfo[i].ulAddr = (receiveMsg->body.readIOGroup.ioAddress * 10) + i;
}
apiRet = mpReadIO(ioReadInfo, ioValue, 8);
resultValue = 0;
for (i = 0; i < 8; i += 1)
{
resultValue |= (ioValue[i] << i);
}
if (apiRet == OK)
resultCode = ROS_REPLY_SUCCESS;
else
resultCode = ROS_REPLY_FAILURE;
replyMsg->body.readIOGroupReply.value = resultValue;
replyMsg->body.readIOGroupReply.resultCode = resultCode;
replyMsg->header.replyType = (SmReplyType)resultCode;
return OK;
}
int Ros_MotionServer_WriteIOBit(SimpleMsg* receiveMsg, SimpleMsg* replyMsg)
{
int apiRet;
MP_IO_DATA ioWriteData;
int resultCode;
//initialize memory
memset(replyMsg, 0x00, sizeof(SimpleMsg));
// set prefix: length of message excluding the prefix
replyMsg->prefix.length = sizeof(SmHeader) + sizeof(SmBodyMotoWriteIOBitReply);
// set header information of the reply
replyMsg->header.msgType = ROS_MSG_MOTO_WRITE_IO_BIT_REPLY;
replyMsg->header.commType = ROS_COMM_SERVICE_REPLY;
ioWriteData.ulAddr = receiveMsg->body.writeIOBit.ioAddress;
ioWriteData.ulValue = receiveMsg->body.writeIOBit.ioValue;
apiRet = mpWriteIO(&ioWriteData, 1);
if (apiRet == OK)
resultCode = ROS_REPLY_SUCCESS;
else
resultCode = ROS_REPLY_FAILURE;
replyMsg->body.writeIOBitReply.resultCode = resultCode;
replyMsg->header.replyType = (SmReplyType)resultCode;
return OK;
}
int Ros_MotionServer_WriteIOGroup(SimpleMsg* receiveMsg, SimpleMsg* replyMsg)
{
int apiRet;
MP_IO_DATA ioWriteData[8];
int resultCode;
int i;
//initialize memory
memset(replyMsg, 0x00, sizeof(SimpleMsg));
// set prefix: length of message excluding the prefix
replyMsg->prefix.length = sizeof(SmHeader) + sizeof(SmBodyMotoWriteIOGroupReply);
// set header information of the reply
replyMsg->header.msgType = ROS_MSG_MOTO_WRITE_IO_GROUP_REPLY;
replyMsg->header.commType = ROS_COMM_SERVICE_REPLY;
for (i = 0; i < 8; i += 1)
{
ioWriteData[i].ulAddr = (receiveMsg->body.writeIOGroup.ioAddress * 10) + i;
ioWriteData[i].ulValue = (receiveMsg->body.writeIOGroup.ioValue & (1 << i)) >> i;
}
apiRet = mpWriteIO(ioWriteData, 8);
if (apiRet == OK)
resultCode = ROS_REPLY_SUCCESS;
else
resultCode = ROS_REPLY_FAILURE;
replyMsg->body.writeIOGroupReply.resultCode = resultCode;
replyMsg->header.replyType = (SmReplyType)resultCode;
return OK;
}
int Ros_MotionServer_GetVersion(SimpleMsg* receiveMsg, SimpleMsg* replyMsg)
{
int apiRet;
int resultCode;
//initialize memory
memset(replyMsg, 0x00, sizeof(SimpleMsg));
// set prefix: length of message excluding the prefix
replyMsg->prefix.length = sizeof(SmHeader) + sizeof(SmBodyGetVersionReply);
// set header information of the reply
replyMsg->header.msgType = ROS_MSG_GET_VERSION_REPLY;
replyMsg->header.commType = ROS_COMM_SERVICE_REPLY;
if( strlen(APPLICATION_VERSION) < sizeof(replyMsg->body.versionReply.version) ) {
strcpy(replyMsg->body.versionReply.version, APPLICATION_VERSION);
}
else {
strncpy(replyMsg->body.versionReply.version, APPLICATION_VERSION, sizeof(replyMsg->body.versionReply.version)-1); // have to leave one 0
}
replyMsg->header.replyType = (SmReplyType)ROS_REPLY_SUCCESS;
return OK;
}
//-----------------------------------------------------------------------
// 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;
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
if( (msgBody->jointTrajPtData[i].validFields & 0x07) != 0x07 )
{
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
{
// 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
int result = Ros_MotionServer_StartTrajMode(controller);
Ros_SimpleMsg_MotionReply(receiveMsg, result&0xffff, (result>>16)&0xffff, 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;
return(bStopped && bRet);
}
//#define TEST_USETOOL
//-----------------------------------------------------------------------
// Sets servo power to ON or OFF
//-----------------------------------------------------------------------
BOOL Ros_MotionServer_ServoPower(Controller* controller, int servoOnOff)
{
#ifdef TEST_USETOOL
if( servoOnOff ) {
MP_SET_TOOL_NO_SEND_DATA sSetToolData;
MP_STD_RSP_DATA rSetToolData;
MP_GET_TOOL_NO_RSP_DATA rGetToolNo;
MP_TOOL_RSP_DATA rGetToolData;
memset(&sSetToolData, 0, sizeof(sSetToolData));
sSetToolData.sRobotNo = controller->ctrlGroups[0]->groupNo;
sSetToolData.sToolNo = 0;
if( mpSetToolNo(&sSetToolData, &rSetToolData) != 0 ) {
printf("failed to set tool, err=%d\n", rSetToolData.err_no);
}
memset(&rGetToolNo, 0, sizeof(rGetToolNo));
if( mpGetToolNo(controller->ctrlGroups[0]->groupNo, &rGetToolNo) != 0 ) {
printf("failed to get tool no err=%d\n", rGetToolNo.err_no);
}
else {
printf("got tool no %d\n", rGetToolNo.sToolNo);
if( mpGetToolData(rGetToolNo.sToolNo, &rGetToolData) != 0 ) {
printf("failed to get tool data\n");
}
else {
printf("selected tool weight=%.3fkg, com=(%.3f, %.3f, %.3f)mm, inertia=(%.3f, %.3f, %.3f) m^2 kg, name=%s\n", (float)rGetToolData.w*0.001, (float)rGetToolData.xg*0.001, (float)rGetToolData.yg*0.001, (float)rGetToolData.zg*0.001, (float)rGetToolData.ix*0.001, (float)rGetToolData.iy*0.001, (float)rGetToolData.iz*0.001, rGetToolData.name);
}
}
}
#endif
MP_SERVO_POWER_SEND_DATA sServoData;
MP_STD_RSP_DATA stdRespData;
int ret;
int i;
STATUS status;
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;
}
}
if(0) {//Ros_Controller_IsServoOn(controller) == servoOnOff) {
printf("servos already set to %d, so skipping servo power\n", servoOnOff);
Ros_Controller_StatusUpdate(controller);
return TRUE;
}
printf("Setting servo power: %d\n", servoOnOff);
memset(&sServoData, 0x00, sizeof(sServoData));
memset(&stdRespData, 0x00, sizeof(stdRespData));
sServoData.sServoPower = servoOnOff;
for (i = 0; i < 5; i++)
{
ret = mpSetServoPower(&sServoData, &stdRespData);
if ((ret == 0) && (stdRespData.err_no ==0))
{
break;
}
printf("setting servo power again since ret=%d, err=%d\n", ret, (int)stdRespData.err_no);
}
if( (ret == 0) && (stdRespData.err_no == 0) )
{
// wait for confirmation
int nSuccess = 0;
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) {
printf("servo power set\n");
nSuccess = 1;
break;
}
Ros_Sleep(MOTION_START_CHECK_PERIOD);
}
if( !nSuccess ) {
printf("failed to set servo power\n");
}