00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "fs100_cmd.h"
00031
00032 int Fs100Cmd::init()
00033 {
00034
00035
00036
00037
00038 sockfd = socket(AF_INET, SOCK_STREAM, 0);
00039 int opt = setsockopt( sockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&i, sizeof(i));
00040 setsockopt( sockfd, IPPROTO_TCP, TCP_QUICKACK, (void *)&i, sizeof(i));
00041 setsockopt( sockfd, IPPROTO_TCP, TCP_QUICKACK, (void *)&i, sizeof(i));
00042 if (sockfd < 0)
00043 socketError("ERROR opening socket");
00044 server = gethostbyname(IP);
00045 if (server == NULL)
00046 {
00047 fprintf(stderr,"ERROR, no such host\n");
00048 return 1;
00049 }
00050 bzero((char *) &serv_addr, sizeof(serv_addr));
00051 serv_addr.sin_family = AF_INET;
00052 bcopy((char *)server->h_addr,
00053 (char *)&serv_addr.sin_addr.s_addr,
00054 server->h_length);
00055 serv_addr.sin_port = htons(portno);
00056 thread_sleep = 1000;
00057
00058
00059 printf("robot commander init done\n");
00060 seq = 0;
00061 stop_all = false;
00062 return 0;
00063 }
00064
00065
00066
00067 int Fs100Cmd::makeConnect()
00068 {
00069
00070
00071
00072 if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
00073 {
00074 socketError("ERROR connecting");
00075 return 1;
00076 }
00077 return 0;
00078 }
00079
00080 void *Fs100Cmd::cmdPushThread(void *This)
00081 {
00082
00083
00084
00085 ((Fs100Cmd *) This)->cmdPush();
00086 }
00087
00088 void Fs100Cmd::cmdPush()
00089 {
00090
00091
00092
00093
00094 while(run_thread && !stop_all)
00095 {
00096
00097 int retry = 1;
00099 while(!cmdList.empty())
00100 {
00101
00102
00103 cmd currentCmd = cmdList.front();
00104
00105
00106 pushTraj(currentCmd.pos,currentCmd.vel,currentCmd.time,seq);
00107
00108
00109
00110 seq ++;
00111 cmdList.pop();
00112 }
00113 usleep(thread_sleep);
00114
00115 }
00116
00117 }
00118
00119 int Fs100Cmd::start(int retry)
00120 {
00121
00122
00123
00124 abs_time = 0.0;
00125 run_thread = 1;
00126 char motion_buf[68];
00127 char motion_reply_buffer[76];
00128
00129 SimpleMsg traj_start, motion_check;
00130
00131 printf("sending motion check message\n");
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 printf("sending trajecotry start message\n");
00160 int last_result = 0;
00161 int last_subcode = 0;
00162 int result;
00163 int subcode;
00164 while(retry && !stop_all)
00165 {
00166 trajectoryStart(&traj_start);
00167 serialize(&traj_start,motion_buf,sizeof(motion_buf));
00168 byteSwap(motion_buf,sizeof(motion_buf));
00169 n = write(sockfd,motion_buf,sizeof(motion_buf));
00170 if (n < 0)
00171 socketError("ERROR writing trajectory start to socket");
00172 n = recv(sockfd,motion_reply_buffer,sizeof(motion_reply_buffer),0);
00173 byteSwap(motion_reply_buffer,sizeof(motion_reply_buffer));
00174 deserializeMotionReply(motion_reply_buffer,&motion_rpl);
00175 result = motion_rpl.body.motionReply.result;
00176 subcode = motion_rpl.body.motionReply.subcode;
00177 if (motion_rpl.body.motionReply.result != 0)
00178 {
00179 if (last_result != result || last_subcode != subcode)
00180 printErrorCodeMsg(&motion_rpl);
00181 }
00182 else
00183 {
00184 retry = 0;
00185 }
00186 last_subcode = subcode;
00187 last_result = result;
00188 }
00189 printErrorCodeMsg(&motion_rpl);
00190
00191
00192
00193 pthread_create(&cmdthreadID,NULL,cmdPushThread,this);
00194 return 0;
00195 }
00196
00197 bool Fs100Cmd::pushTraj(float pos[6],float vel[6],float time,int seq)
00198 {
00199
00200
00201
00202 abs_time += time;
00203
00204 SimpleMsg traj;
00205 buildTrajFull(&traj,pos,vel,abs_time,seq);
00206 serialize(&traj,traj_buffer,sizeof(traj_buffer));
00207 byteSwap(traj_buffer,sizeof(traj_buffer));
00208 int retry = 1;
00209 while(retry && !stop_all)
00210 {
00211 write(sockfd,traj_buffer,sizeof(traj_buffer));
00212 recv(sockfd,motion_reply_buffer,sizeof(motion_reply_buffer),0);
00213 byteSwap(motion_reply_buffer,sizeof(motion_reply_buffer));
00214 deserializeMotionReply(motion_reply_buffer,&motion_rpl);
00215
00216 if(motion_rpl.body.motionReply.result == 1);
00217 else if (motion_rpl.body.motionReply.result == 0)
00218 {
00219 retry = 0;
00220 }
00221 else
00222 {
00223 printErrorCodeMsg(&motion_rpl);
00224 retry = 0;
00225 }
00226
00227 }
00228 return true;
00229
00230 }
00231
00232 bool Fs100Cmd::addCmdToQueue(cmd cmd_point)
00233 {
00234
00235
00236
00237 cmdList.push(cmd_point);
00238 }
00239
00240 bool Fs100Cmd::addPointToQueue(float pos[6],float vel[6],float time)
00241 {
00242
00243
00244
00245 cmd temp;
00246 for(int i=0;i<6;i++)
00247 {
00248 temp.pos[i] = pos[i];
00249 temp.vel[i] = vel[i];
00250 }
00251 temp.time = time;
00252 cmdList.push(temp);
00253 }
00254
00255 bool Fs100Cmd::resetTrajectory(int *retry)
00256 {
00257
00258
00259
00260
00261 printf("reseting buffer\n");
00262 SimpleMsg traj_stop, m_reply;
00263 char motion_buf[68];
00264 char motion_reply_buffer[76];
00265 trajectoryStop(&traj_stop);
00266 serialize(&traj_stop,motion_buf,sizeof(motion_buf));
00267 byteSwap(motion_buf,sizeof(motion_buf));
00268 n = write(sockfd,motion_buf,sizeof(motion_buf));
00269 if (n < 0)
00270 socketError("ERROR writing trajectory stop to socket");
00271 n = recv(sockfd,motion_reply_buffer,sizeof(motion_reply_buffer),0);
00272 byteSwap(motion_reply_buffer,sizeof(motion_reply_buffer));
00273 deserializeMotionReply(motion_reply_buffer,&m_reply);
00274 printf("reset:\n");
00275 printErrorCodeMsg(&m_reply);
00276
00277
00278 SimpleMsg traj_start;
00279 printf("sending trajecotry start message\n");
00280 int last_result = 0;
00281 int last_subcode = 0;
00282 int result;
00283 int subcode;
00284 bool run = true;
00285 while(*retry && run)
00286 {
00287
00288 trajectoryStart(&traj_start);
00289 serialize(&traj_start,motion_buf,sizeof(motion_buf));
00290 byteSwap(motion_buf,sizeof(motion_buf));
00291 n = write(sockfd,motion_buf,sizeof(motion_buf));
00292 if (n < 0)
00293 socketError("ERROR writing trajectory start to socket");
00294 n = recv(sockfd,motion_reply_buffer,sizeof(motion_reply_buffer),0);
00295 byteSwap(motion_reply_buffer,sizeof(motion_reply_buffer));
00296 deserializeMotionReply(motion_reply_buffer,&m_reply);
00297
00298 result = m_reply.body.motionReply.result;
00299 subcode = m_reply.body.motionReply.subcode;
00300 if (m_reply.body.motionReply.result != 0)
00301 {
00302 if (last_result != result || last_subcode != subcode)
00303 printErrorCodeMsg(&m_reply);
00304 }
00305 else
00306 {
00307 last_subcode = subcode;
00308 last_result = result;
00309 run = false;
00310 }
00311
00312 last_subcode = subcode;
00313 last_result = result;
00314 }
00315 printErrorCodeMsg(&m_reply);
00316 abs_time = 0.0;
00317 seq = 0;
00318
00319
00320 return true;
00321 }
00322
00323 void Fs100Cmd::socketError(const char *msg)
00324 {
00325
00326
00327
00328 perror(msg);
00329 return;
00330 }
00331
00332 void Fs100Cmd::byteSwap(char* data,int length)
00333 {
00334
00335
00336
00337 char buffer[length];
00338 memcpy(buffer,data,length);
00339 for(int i=0;i<length/4;i++)
00340 {
00341 for(int k = 0;k<4;k++)
00342 {
00343 data[(i*4)+k] = buffer[(i*4)+(3-k)];
00344
00345 }
00346 }
00347 }
00348
00349 void Fs100Cmd::serialize(SimpleMsg *msg, char *data,int size)
00350 {
00351 memcpy(data,msg,size);
00352 }
00353
00354
00355
00356 void Fs100Cmd::deserializeMotionReply(char *data,SimpleMsg *msg)
00357 {
00358
00359
00360
00361 int *q = (int*)data;
00362
00363 msg->prefix.length = *q; q++;
00364
00365 msg->header.msgType = (SmMsgType) *q; q++;
00366 msg->header.commType =(SmCommType) *q; q++;
00367 msg->header.replyType = (SmReplyType) *q; q++;
00368
00369 msg->body.motionReply.groupNo = *q; q++;
00370 msg->body.motionReply.sequence = *q; q++;
00371 msg->body.motionReply.command = *q; q++;
00372 msg->body.motionReply.result = (SmResultType) *q; q++;
00373 msg->body.motionReply.subcode = *q; q++;
00374 for(int i=0;i<10;i++){
00375 msg->body.motionReply.data[i] = *(float*)q; q++;
00376 }
00377 }
00378
00379 void Fs100Cmd::printMotionReply(SimpleMsg *msg)
00380 {
00381
00382
00383
00384 printf("Prefix:\n");
00385 printf(" length: %d\n",msg->prefix.length);
00386 printf("header:\n");
00387 printf(" msgType: %d\n",msg->header.msgType);
00388 printf(" commType: %d\n",msg->header.commType);
00389 printf(" replyType: %d\n",msg->header.replyType);
00390 printf("body:\n");
00391 printf(" motionReply:\n");
00392 printf(" groupNo: %d\n",msg->body.motionReply.groupNo);
00393 printf(" sequence: %d\n",msg->body.motionReply.sequence);
00394 printf(" command: %d\n",msg->body.motionReply.command);
00395 printf(" result: %d\n",msg->body.motionReply.result);
00396 printf(" subcode: %d\n",msg->body.motionReply.subcode);
00397 printf(" data:\n");
00398 for(int i=0;i<10;i++){
00399 printf(" data[%d]: %f\n",i,msg->body.motionReply.data[i]);
00400 }
00401 }
00402
00403 void Fs100Cmd::printTrajFull(SimpleMsg *msg)
00404 {
00405
00406
00407
00408 printf("Prefix:\n");
00409 printf(" length: %d\n",msg->prefix.length);
00410 printf("header:\n");
00411 printf(" msgType: %d\n",msg->header.msgType);
00412 printf(" commType: %d\n",msg->header.commType);
00413 printf(" replyType: %d\n",msg->header.replyType);
00414 printf("body:\n");
00415 printf(" jointTrajData:\n");
00416 printf(" groupNo: %d\n",msg->body.jointTrajData.groupNo);
00417 printf(" validFields: %d\n",msg->body.jointTrajData.validFields);
00418 printf(" sequence: %d\n",msg->body.jointTrajData.sequence);
00419 printf(" time: %f\n",msg->body.jointTrajData.time);
00420 printf(" pos:\n");
00421 for(int i=0;i<10;i++)
00422 {
00423 printf(" joint[%d]: %f\n",i+1,msg->body.jointTrajData.pos[i]);
00424 }
00425 printf(" vel:\n");
00426 for(int i=0;i<10;i++)
00427 {
00428 printf(" joint[%d]: %f\n",i+1,msg->body.jointTrajData.vel[i]);
00429 }
00430 printf(" acc:\n");
00431 for(int i=0;i<10;i++)
00432 {
00433 printf(" joint[%d]: %f\n",i+1,msg->body.jointTrajData.acc[i]);
00434 }
00435 }
00436
00437 void Fs100Cmd::printErrorCodeMsg(SimpleMsg *msg)
00438 {
00439
00440
00441
00442 printf("Result code: ");
00443 int result = msg->body.motionReply.result;
00444 int subcode = msg->body.motionReply.subcode;
00445 if (result == ROS_RESULT_SUCCESS || result == ROS_RESULT_TRUE){
00446 printf("success\n");
00447 }
00448 else printf("error!\n");
00449 printf(" Result type: %d, ",result);
00450 if(result == ROS_RESULT_SUCCESS) printf("success/true\n");
00451 else if(result == ROS_RESULT_TRUE) printf("success/true\n");
00452 else if(result == ROS_RESULT_BUSY) printf("busy\n");
00453 else if(result == ROS_RESULT_FAILURE) printf("failure/false\n");
00454 else if(result == ROS_RESULT_FALSE) printf("failure/false\n");
00455 else if(result == ROS_RESULT_INVALID) printf("invalid\n");
00456 else if(result == ROS_RESULT_ALARM) printf("alarm\n");
00457 else if(result == ROS_RESULT_NOT_READY) printf("not ready\n");
00458 else if(result == ROS_RESULT_MP_FAILURE) printf("mp failure\n");
00459 if (result != ROS_RESULT_SUCCESS && result!=ROS_RESULT_TRUE)
00460 {
00461 printf(" Subcode type: %d, ",subcode);
00462 if(subcode == ROS_RESULT_INVALID_UNSPECIFIED) printf("invalid unspecified\n");
00463 else if (subcode == ROS_RESULT_INVALID_MSGSIZE) printf("invalid message size\n");
00464 else if (subcode == ROS_RESULT_INVALID_MSGHEADER) printf("invalid message header\n");
00465 else if (subcode == ROS_RESULT_INVALID_MSGTYPE) printf("invalid message type\n");
00466 else if (subcode == ROS_RESULT_INVALID_GROUPNO) printf("invalid groupno\n");
00467 else if (subcode == ROS_RESULT_INVALID_SEQUENCE) printf("invalid sequence\n");
00468 else if (subcode == ROS_RESULT_INVALID_COMMAND) printf("invalid command\n");
00469 else if (subcode == ROS_RESULT_INVALID_DATA) printf("invalid data\n");
00470 else if (subcode == ROS_RESULT_INVALID_DATA_START_POS) printf("invalid data start position\n");
00471 else if (subcode == ROS_RESULT_INVALID_DATA_POSITION) printf("invalid data position\n");
00472 else if (subcode == ROS_RESULT_INVALID_DATA_SPEED) printf("invalid data speed\n");
00473 else if (subcode == ROS_RESULT_INVALID_DATA_ACCEL) printf("invalid data acceleration\n");
00474 else if (subcode == ROS_RESULT_INVALID_DATA_INSUFFICIENT) printf("invalid data insufficient\n");
00475 else if (subcode == ROS_RESULT_NOT_READY_UNSPECIFIED) printf("not ready unspecified\n");
00476 else if (subcode == ROS_RESULT_NOT_READY_ALARM) printf("not ready alarm\n");
00477 else if (subcode == ROS_RESULT_NOT_READY_ERROR) printf("not ready error\n");
00478 else if (subcode == ROS_RESULT_NOT_READY_ESTOP) printf("not ready emergency stop\n");
00479 else if (subcode == ROS_RESULT_NOT_READY_NOT_PLAY) printf("not ready not in play\n");
00480 else if (subcode == ROS_RESULT_NOT_READY_NOT_REMOTE) printf("not ready not in remote\n");
00481 else if (subcode == ROS_RESULT_NOT_READY_SERVO_OFF) printf("not ready servo power off\n");
00482 else if (subcode == ROS_RESULT_NOT_READY_HOLD) printf("not ready hold\n");
00483 else if (subcode == ROS_RESULT_NOT_READY_NOT_STARTED) printf("not ready not started\n");
00484 else if (subcode == ROS_RESULT_NOT_READY_WAITING_ROS) printf("not ready waiting for ROS\n");
00485 else if (subcode == ROS_RESULT_NOT_READY_SKILLSEND) printf("not ready skillsend\n");
00486 }
00487 else printf(" Subcode type: 0, success\n");
00488 }
00489
00490
00491 int Fs100Cmd::buildTrajPos(SimpleMsg *tm,float pos[6],float time,int seq)
00492 {
00493
00494
00495
00496
00497 tm->prefix.length = sizeof(SmHeader) + sizeof(SmBodyJointTrajPtFull);
00498
00499
00500 tm->header.msgType = ROS_MSG_JOINT_TRAJ_PT_FULL;
00501 tm->header.commType = ROS_COMM_SERVICE_RESQUEST;
00502 tm->header.replyType = ROS_REPLY_INVALID;
00503
00504
00505 tm->body.jointTrajData.groupNo = 0;
00506 tm->body.jointTrajData.validFields = 7;
00507 tm->body.jointTrajData.sequence = seq;
00508 tm->body.jointTrajData.time = time;
00509 for(int i=0;i<6;i++)
00510 {
00511 tm->body.jointTrajData.pos[i] = pos[i];
00512 tm->body.jointTrajData.vel[i] = (float)0.0;
00513 tm->body.jointTrajData.acc[i] = (float)0.0;
00514 }
00515 for(int i=6;i<10;i++)
00516 {
00517 tm->body.jointTrajData.pos[i] = (float)0.0;
00518 tm->body.jointTrajData.vel[i] = (float)0.0;
00519 tm->body.jointTrajData.acc[i] = (float)0.0;
00520 }
00521
00522 }
00523
00524 int Fs100Cmd::buildTrajFull(SimpleMsg *tm,float pos[6],float vel[6],float time,int seq)
00525 {
00526
00527
00528
00529
00530 tm->prefix.length = sizeof(SmHeader) + sizeof(SmBodyJointTrajPtFull);
00531
00532
00533 tm->header.msgType = ROS_MSG_JOINT_TRAJ_PT_FULL;
00534 tm->header.commType = ROS_COMM_SERVICE_RESQUEST;
00535 tm->header.replyType = ROS_REPLY_INVALID;
00536
00537
00538 tm->body.jointTrajData.groupNo = 0;
00539 tm->body.jointTrajData.validFields = 7;
00540 tm->body.jointTrajData.sequence = seq;
00541 tm->body.jointTrajData.time = time;
00542 for(int i=0;i<6;i++)
00543 {
00544 tm->body.jointTrajData.pos[i] = pos[i];
00545 tm->body.jointTrajData.vel[i] = vel[i];
00546 tm->body.jointTrajData.acc[i] = (float)0.0;
00547 }
00548 for(int i=6;i<10;i++)
00549 {
00550 tm->body.jointTrajData.pos[i] = (float)0.0;
00551 tm->body.jointTrajData.vel[i] = (float)0.0;
00552 tm->body.jointTrajData.acc[i] = (float)0.0;
00553 }
00554
00555 }
00556
00557 int Fs100Cmd::getLength(char* data)
00558 {
00559
00560
00561
00562 int length = *(int*) data;
00563 return length;
00564 }
00565
00566 void Fs100Cmd::motionReady(SimpleMsg *msg)
00567 {
00568
00569
00570
00571
00572 msg->prefix.length = sizeof(SmHeader) + sizeof(SmBodyMotoMotionCtrl);
00573
00574 msg->header.msgType = ROS_MSG_MOTO_MOTION_CTRL;
00575 msg->header.commType = ROS_COMM_SERVICE_RESQUEST;
00576 msg->header.replyType = ROS_REPLY_INVALID;
00577
00578 msg->body.motionCtrl.groupNo = 0;
00579 msg->body.motionCtrl.sequence = 0;
00580 msg->body.motionCtrl.command = ROS_CMD_CHECK_MOTION_READY;
00581 for(int i=0;i<10;i++)
00582 {
00583 msg->body.motionCtrl.data[i] = (float) 0.0;
00584 }
00585 }
00586
00587 void Fs100Cmd::trajectoryStart(SimpleMsg *msg)
00588 {
00589
00590
00591
00592
00593 msg->prefix.length = sizeof(SmHeader) + sizeof(SmBodyMotoMotionCtrl);
00594
00595 msg->header.msgType = ROS_MSG_MOTO_MOTION_CTRL;
00596 msg->header.commType = ROS_COMM_SERVICE_RESQUEST;
00597 msg->header.replyType = ROS_REPLY_INVALID;
00598
00599 msg->body.motionCtrl.groupNo = 0;
00600 msg->body.motionCtrl.sequence = 0;
00601 msg->body.motionCtrl.command = ROS_CMD_START_TRAJ_MODE;
00602 for(int i=0;i<10;i++)
00603 {
00604 msg->body.motionCtrl.data[i] = (float) 0.0;
00605 }
00606 }
00607
00608 void Fs100Cmd::trajectoryStop(SimpleMsg *msg)
00609 {
00610
00611
00612
00613
00614 msg->prefix.length = sizeof(SmHeader) + sizeof(SmBodyMotoMotionCtrl);
00615
00616 msg->header.msgType = ROS_MSG_MOTO_MOTION_CTRL;
00617 msg->header.commType = ROS_COMM_SERVICE_RESQUEST;
00618 msg->header.replyType = ROS_REPLY_INVALID;
00619
00620 msg->body.motionCtrl.groupNo = 0;
00621 msg->body.motionCtrl.sequence = 0;
00622 msg->body.motionCtrl.command = ROS_CMD_STOP_TRAJ_MODE;
00623 for(int i=0;i<10;i++)
00624 {
00625 msg->body.motionCtrl.data[i] = (float) 0.0;
00626 }
00627 }
00628
00629 void Fs100Cmd::pgmClose()
00630 {
00631
00632
00633
00634 stop_all = true;
00635 if (run_thread == 1)
00636 {
00637 run_thread = 0;
00638 pthread_join(cmdthreadID,NULL);
00639 }
00640 SimpleMsg traj_stop;
00641 char motion_buf[68];
00642 char motion_reply_buffer[76];
00643 trajectoryStop(&traj_stop);
00644 serialize(&traj_stop,motion_buf,sizeof(motion_buf));
00645 byteSwap(motion_buf,sizeof(motion_buf));
00646 n = write(sockfd,motion_buf,sizeof(motion_buf));
00647 if (n < 0)
00648 socketError("ERROR writing trajectory stop to socket");
00649
00650 n = recv(sockfd,motion_reply_buffer,sizeof(motion_reply_buffer),0);
00651 byteSwap(motion_reply_buffer,sizeof(motion_reply_buffer));
00652 deserializeMotionReply(motion_reply_buffer,&motion_rpl);
00653 printf("\n\ntrajectory stop sent, reply:\n");
00654 printErrorCodeMsg(&motion_rpl);
00655
00656 close(sockfd);
00657 }