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 #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
00080
00081 while(run_thread)
00082 {
00084 n = recv(sockfd,raw_data,sizeof(raw_data),0);
00085 if (n < 0)
00086 socketError("ERROR reading from socket");
00087 setsockopt( sockfd, IPPROTO_TCP, TCP_QUICKACK, (void *)&i, sizeof(i));
00088 byteSwap(raw_data,sizeof(raw_data));
00089
00090 if (getLength(raw_data)==44)
00091 {
00092 printf("got status\n");
00093 }
00094 else if(getLength(raw_data)==144)
00095 {
00096
00097 pthread_mutex_lock(&mut_lock);
00098 deserializeJointFeedback(raw_data,&joint_data);
00099
00100 pthread_mutex_unlock(&mut_lock);
00101 for(int i=0;i<6;i++)
00102 {
00103
00104 current_pos[i] = joint_data.body.jointFeedback.pos[i];
00105 pos_updated = true;
00106 }
00107
00108
00109
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)
00130 {
00131 char buffer[length];
00132 memcpy(buffer,data,length);
00133 for(int i=0;i<length/4;i++)
00134 {
00135 for(int k = 0;k<4;k++)
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
00152 msg->prefix.length = *q; q++;
00153
00154 msg->header.msgType = (SmMsgType) *q; q++;
00155 msg->header.commType =(SmCommType) *q; q++;
00156 msg->header.replyType = (SmReplyType) *q; q++;
00157
00158 msg->body.jointFeedback.groupNo = *q; q++;
00159 msg->body.jointFeedback.validFields = *q; q++;
00160
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
00181 msg->prefix.length = *q; q++;
00182
00183
00184 msg->header.msgType = (SmMsgType) *q; q++;
00185 msg->header.commType =(SmCommType) *q; q++;
00186 msg->header.replyType = (SmReplyType) *q; q++;
00187
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
00255
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
00270
00271 }
00272
00273