fs100_state.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * C++ class for continous trajectory point streaming using FS100 controller mh5l robot
00004  * Author: Asger Winther-Jørgensen (awijor@elektro.dtu.dk)
00005  *
00006  * Software License Agreement (LGPL License)
00007  *
00008  *  Copyright (c) 2014, Technical University of Denmark
00009  *  All rights reserved.
00010  
00011  *  This program is free software; you can redistribute it and/or modify  
00012  *  it under the terms of the GNU Lesser General Public License as        
00013  *  published by the Free Software Foundation; either version 2 of the    
00014  *  License, or (at your option) any later version.                       
00015  *                                                                        
00016  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00017  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00018  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00019  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00020  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00022  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00024  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00026  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  *  POSSIBILITY OF SUCH DAMAGE.
00028  *********************************************************************/
00029 #include "fs100_state.h"
00030 
00031 
00032 
00033 
00034 int Fs100State::init()
00035 {
00036     sockfd = socket(AF_INET, SOCK_STREAM, 0);
00037     int opt = setsockopt( sockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&i, sizeof(i));
00038     setsockopt( sockfd, IPPROTO_TCP, TCP_QUICKACK, (void *)&i, sizeof(i));
00039     if (sockfd < 0) 
00040         socketError("ERROR opening socket");
00041     server = gethostbyname(IP);
00042     if (server == NULL)
00043     {
00044         fprintf(stderr,"ERROR, no such host\n");
00045         return 1;
00046     }
00047     bzero((char *) &serv_addr, sizeof(serv_addr));
00048     serv_addr.sin_family = AF_INET;
00049     bcopy((char *)server->h_addr, 
00050          (char *)&serv_addr.sin_addr.s_addr,
00051          server->h_length);
00052     serv_addr.sin_port = htons(portno);
00053     
00054     pthread_mutex_init(&mut_lock, NULL);
00055     pos_updated = false;
00056     printf("robot state init done\n");
00057     return 0;
00058 }
00059 
00060 int Fs100State::makeConnect()
00061     {
00062     if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
00063     {
00064         socketError("ERROR connecting");
00065         return 1;
00066     }
00067     run_thread = 1;
00068     pthread_create(&recvthreadID,NULL,recvDataThread,this);
00069     return 0;
00070 }
00071 
00072 void *Fs100State::recvDataThread(void *This)
00073 {
00074     ((Fs100State *) This)->recvData();
00075 }
00076 
00077 void Fs100State::recvData()
00078 {
00079     //sensor_msgs::JointState js;
00080     //js.position.resize(6);
00081     while(run_thread)
00082     {
00084         n = recv(sockfd,raw_data,sizeof(raw_data),0); //expect size? size is 255, eating both messages perhabs?
00085         if (n < 0) 
00086             socketError("ERROR reading from socket");
00087         setsockopt( sockfd, IPPROTO_TCP, TCP_QUICKACK, (void *)&i, sizeof(i)); //force acknowlegde
00088         byteSwap(raw_data,sizeof(raw_data)); //big-endian to litlle-endian
00089         //Checking data type (length 40=status; length 144=joint feedback)
00090         if (getLength(raw_data)==44)
00091         {
00092             printf("got status\n");
00093         }
00094         else if(getLength(raw_data)==144)
00095         {
00096             //semaphore/mutex here!
00097             pthread_mutex_lock(&mut_lock);                   
00098             deserializeJointFeedback(raw_data,&joint_data);
00099             //printf("pos[0]: %f\n",joint_data.body.jointFeedback.pos[0]);
00100             pthread_mutex_unlock(&mut_lock);
00101             for(int i=0;i<6;i++)
00102             {
00103                 //js.position[i] = joint_data.body.jointFeedback.pos[i];
00104                 current_pos[i] = joint_data.body.jointFeedback.pos[i];
00105                 pos_updated = true;
00106             }
00107             //js.header.stamp = ros::Time::now();
00108             //pub_state->publish(js);
00109             //release semaphore
00110         }
00111     }
00112 
00113 }
00114 
00115 void Fs100State::pgmClose()
00116 {
00117     run_thread = 0;
00118     pthread_join(recvthreadID,NULL);
00119     pthread_mutex_destroy(&mut_lock);
00120     close(sockfd);
00121 }
00122 
00123 void Fs100State::socketError(const char *msg)
00124 {
00125     perror(msg);
00126     return;
00127 }
00128 
00129 void Fs100State::byteSwap(char* data,int length) //destructive
00130 { 
00131     char buffer[length];
00132     memcpy(buffer,data,length);
00133     for(int i=0;i<length/4;i++) //number of int/float
00134     { 
00135         for(int k = 0;k<4;k++) //for every byte in int/float
00136         { 
00137             data[(i*4)+k] = buffer[(i*4)+(3-k)];
00138             
00139         }
00140     }
00141 }
00142 
00143 void Fs100State::serialize(SimpleMsg *msg, char *data,int size)
00144 {
00145     memcpy(data,msg,size);
00146 }
00147 
00148 void Fs100State::deserializeJointFeedback(char *data,SimpleMsg *msg)
00149 {
00150     int *q = (int*)data;
00151     //prefix
00152     msg->prefix.length =  *q; q++;
00153     //header
00154     msg->header.msgType = (SmMsgType) *q; q++;
00155     msg->header.commType =(SmCommType) *q; q++;
00156     msg->header.replyType = (SmReplyType) *q; q++;
00157     //body
00158     msg->body.jointFeedback.groupNo = *q; q++;
00159     msg->body.jointFeedback.validFields = *q; q++;
00160     //change from integer to float
00161     msg->body.jointFeedback.time = *(float*)q; q++;
00162     for(int i=0;i<10;i++)
00163     {
00164         msg->body.jointFeedback.pos[i] = *(float*)q; q++;
00165     }
00166     for(int i=0;i<10;i++)
00167     {
00168         msg->body.jointFeedback.vel[i] = *(float*)q; q++;
00169     }
00170     for(int i=0;i<10;i++)
00171     {
00172         msg->body.jointFeedback.acc[i] =  *(float*)q; q++;
00173     }
00174     
00175 }
00176 
00177 void Fs100State::deserializeMotionReply(char *data,SimpleMsg *msg)
00178 {
00179     int *q = (int*)data;
00180     //prefix
00181     msg->prefix.length =  *q; q++;
00182     
00183     //header
00184     msg->header.msgType = (SmMsgType) *q; q++;
00185     msg->header.commType =(SmCommType) *q; q++;
00186     msg->header.replyType = (SmReplyType) *q; q++;
00187     //body
00188     msg->body.motionReply.groupNo = *q; q++;
00189     msg->body.motionReply.sequence = *q; q++;
00190     msg->body.motionReply.command = *q; q++;
00191     msg->body.motionReply.result = (SmResultType) *q; q++;
00192     msg->body.motionReply.subcode = *q; q++;
00193     for(int i=0;i<10;i++)
00194     {
00195         msg->body.motionReply.data[i] = *(float*)q; q++;
00196     }    
00197 }
00198 
00199 void Fs100State::printMotionReply(SimpleMsg *msg)
00200 {
00201     printf("Prefix:\n");
00202     printf("  length: %d\n",msg->prefix.length);
00203     printf("header:\n");
00204     printf("  msgType: %d\n",msg->header.msgType);
00205     printf("  commType: %d\n",msg->header.commType);
00206     printf("  replyType: %d\n",msg->header.replyType);
00207     printf("body:\n");
00208     printf("  motionReply:\n");
00209     printf("    groupNo: %d\n",msg->body.motionReply.groupNo);
00210     printf("    sequence: %d\n",msg->body.motionReply.sequence);
00211     printf("    command: %d\n",msg->body.motionReply.command);
00212     printf("    result: %d\n",msg->body.motionReply.result);
00213     printf("    subcide: %d\n",msg->body.motionReply.subcode);
00214     printf("    data:\n");
00215     for(int i=0;i<10;i++)
00216     {
00217         printf("      data[%d]: %f\n",i,msg->body.motionReply.data[i]);
00218     }
00219 }
00220     
00221 void Fs100State::printJointFeedback()
00222 {
00223     printf("Prefix:\n");
00224     printf("  length: %d\n",joint_data.prefix.length);
00225     printf("header:\n");
00226     printf("  msgType: %d\n",joint_data.header.msgType);
00227     printf("  commType: %d\n",joint_data.header.commType);
00228     printf("  replyType: %d\n",joint_data.header.replyType);
00229     printf("body:\n");
00230     printf("  jointFeedback:\n");
00231     printf("    groupNo: %d\n",joint_data.body.jointFeedback.groupNo);
00232     printf("    validFields: %d\n",joint_data.body.jointFeedback.validFields);
00233     printf("    time: %f\n",joint_data.body.jointFeedback.time);
00234     printf("    pos:\n");
00235     for(int i=0;i<10;i++)
00236     {
00237         printf("      joint[%d]: %f\n",i+1,joint_data.body.jointFeedback.pos[i]);
00238     }
00239 }
00240 
00241 int Fs100State::getLength(char* data)
00242 {
00243     int length = *(int*) data;
00244     return length;
00245 }
00246 
00247 bool Fs100State::getJointsUpdated(float* joints)
00248 {
00249     
00250     if (!pos_updated)
00251     {
00252         return false;
00253     }
00254     //wait for semaphore
00255     //pthread_mutex_lock(&this->mut_lock);
00256     pthread_mutex_lock(&mut_lock);
00257     if (pos_updated)
00258     {
00259         for(int i=0;i<6;i++)
00260         {
00261             joints[i] = joint_data.body.jointFeedback.pos[i];
00262         }
00263         pos_updated = false;
00264         pthread_mutex_unlock(&mut_lock);
00265         return true;
00266     }
00267 
00268     pthread_mutex_unlock(&mut_lock);
00269     //release semaphore
00270     //pthread_mutex_unlock(&this->mut_lock);
00271 }
00272 
00273 


fs100_motoman
Author(s): Asger Winther-Jørgensen (Technical Univeristy of Denmark)
autogenerated on Fri Aug 28 2015 10:42:16