00001
00008
00009
00010
00011
00012 #include <ros/ros.h>
00013 #include "std_msgs/String.h"
00014 #include <sstream>
00015 #include <string>
00016
00017 #include <trafeiro/fanuc_experiment.h>
00018
00019 #include <boost/array.hpp>
00020 #include <boost/asio.hpp>
00021 #include <boost/thread.hpp>
00022
00023 #include <serialcom/SerialCom.h>
00024 #include <algorithm>
00025
00026 using namespace std;
00027
00028 using boost::asio::ip::tcp;
00033 class Position
00034 {
00035 public:
00036 Position()
00037 {
00038 x=0;
00039 y=0;
00040 z=0;
00041 w=0;
00042 p=0;
00043 r=0;
00044 conf1=0;
00045 conf2=0;
00046 conf3=0;
00047 turn1=0;
00048 turn2=0;
00049 turn3=0;
00050 }
00051
00052 double x,y,z;
00053 double w,p,r;
00054 int conf1,conf2,conf3;
00055 int turn1,turn2,turn3;
00056 };
00057
00062 class CommunicationHandler
00063 {
00064 public:
00065 CommunicationHandler(boost::asio::io_service& io_service,tcp::resolver::iterator endpoint_iterator)
00066 :io_service_(io_service),
00067 socket_(io_service)
00068 {
00069 tcp::endpoint endpoint = *endpoint_iterator;
00070 socket_.async_connect(endpoint, boost::bind(&CommunicationHandler::handle_connect, this,boost::asio::placeholders::error, ++endpoint_iterator));
00071 }
00072
00073 void getcrcpos(Position& pos)
00074 {
00075 string data;
00076
00077 char msg[1000];
00078 strcpy(msg,"GETCRCPOS\n");
00079
00080 write(msg,strlen(msg));
00081
00082 boost::asio::streambuf response;
00083 istream response_stream(&response);
00084
00085 boost::asio::read_until(socket_, response, "\n");
00086 getline(response_stream, data);
00087
00088 boost::asio::read_until(socket_, response, "\n");
00089 getline(response_stream, data);
00090
00091
00092 istringstream iss(data);
00093 for(int a=0;a<3;a++)
00094 {
00095 if (a==0) iss >> pos.x;
00096 if (a==1) iss >> pos.y;
00097 if (a==2) iss >> pos.z;
00098 }
00099 cout<<data<<endl;
00100
00101 boost::asio::read_until(socket_, response, "\n");
00102 getline(response_stream, data);
00103
00104 istringstream iss2(data);
00105
00106 for(int a=0;a<3;a++)
00107 {
00108 if (a==0) iss2 >> pos.w;
00109 if (a==1) iss2 >> pos.p;
00110 if (a==2) iss2 >> pos.r;
00111 }
00112
00113 boost::asio::read_until(socket_, response, "\n");
00114 getline(response_stream, data);
00115
00116
00117 boost::asio::read_until(socket_, response, "\n");
00118 getline(response_stream, data);
00119
00120 }
00121
00122 void runtpp(char *tppname)
00123 {
00124
00125 string data;
00126 char msg[1000];
00127
00128 strcpy(msg,"RUNTPP\n");
00129 strcat(msg,tppname);
00130 strcat(msg,"\n1\n");
00131
00132 write(msg,strlen(msg));
00133
00134 boost::asio::streambuf response;
00135 istream response_stream(&response);
00136
00137 boost::asio::read_until(socket_, response, "\n");
00138 getline(response_stream, data);
00139
00140
00141 }
00142
00143 void write(char* msg, int length)
00144 {
00145 do_write(msg,length);
00146 }
00147
00148 void close()
00149 {
00150 do_close();
00151 }
00152
00153
00154 private:
00155 char data_[1000];
00156
00157 void handle_connect(const boost::system::error_code& error,tcp::resolver::iterator endpoint_iterator)
00158 {
00159 if (!error)
00160 {
00161
00162 }else if (endpoint_iterator != tcp::resolver::iterator())
00163 {
00164 socket_.close();
00165 tcp::endpoint endpoint = *endpoint_iterator;
00166 socket_.async_connect(endpoint, boost::bind(&CommunicationHandler::handle_connect, this,boost::asio::placeholders::error, ++endpoint_iterator));
00167 }
00168 }
00169
00170 void do_write(char*msg,int length)
00171 {
00172 boost::asio::write(socket_,boost::asio::buffer(msg,length));
00173 }
00174
00175 void do_close()
00176 {
00177 socket_.close();
00178 }
00179
00180 private:
00181
00182 boost::asio::io_service& io_service_;
00183 tcp::socket socket_;
00184
00185 };
00186
00191 int main (int argc ,char** argv)
00192 {
00193 serialcom::SerialCom serial;
00194
00195
00196 ros::init(argc , argv , "fanux_exp");
00197 ros::NodeHandle n;
00198 ros::Publisher chatter_pub;
00199 chatter_pub = n.advertise<trafeiro::fanuc_experiment>("topic_exp", 1000);
00200
00201 ros::Rate loop_rate(20);
00202
00203 try
00204 {
00205 std::string buffer;
00206 std::string buffer_aux;
00207 std::string Ax,Ay,Az,Mx,My,Mz,Gx,Gy,Gz,Y,P,R;
00208 int MODE;
00209 std::string junk;
00210 Position p1;
00211 int findPV, findSV;
00212
00213 serial.open("/dev/ttyACM0",57600);
00214
00215 usleep(3000000);
00216
00217 boost::asio::io_service io_service;
00218
00219 tcp::resolver resolver(io_service);
00220 tcp::resolver::query query("192.168.0.230","4901");
00221
00222 tcp::resolver::iterator iterator = resolver.resolve(query);
00223
00224 CommunicationHandler c(io_service, iterator);
00225
00226 trafeiro::fanuc_experiment experiment;
00227
00228 char *tppname;
00229
00230 if (argc>2)
00231 {
00232 if (strcmp(argv[2],"1")>=0)
00233 {
00234 MODE = 1;
00235 }
00236 if (strcmp(argv[2],"0")>=0)
00237 {
00238 serial.write("#osct");
00239 MODE = 0;
00240 }
00241 if ((strcmp(argv[2],"1")<0) && (strcmp(argv[2],"0")>0))
00242 {
00243 cout<<"SENSOR output MODE invalid!"<<endl;
00244 return 0;
00245 }
00246 tppname = argv[1];
00247 }
00248 else
00249 {
00250 cout<<"TPP NAME or SENSOR MODE MISSING ... TYPE fanuc_exp FILENAME SENSOR MODE"<<endl;
00251 return 0;
00252
00253
00254
00255
00256 }
00257 c.runtpp(tppname);
00258 usleep(500000);
00259
00260
00261
00262
00263 while(ros::ok())
00264 {
00265
00266 if(MODE==1)
00267 {
00268 serial.write("#f");
00269 usleep(5000);
00270 serial.readLine(buffer);
00271 serial.readLine(buffer_aux);
00272 if(buffer.size()>0)
00273 {
00274 std::basic_string <char> str2 = buffer.substr(5);
00275
00276 findPV = str2.find(",");
00277 std::basic_string <char> inf3 = str2.substr(findPV+1);
00278 findSV = inf3.find(",");
00279
00280
00281 experiment.S_Y = (double) atof(str2.substr(0,findPV).c_str());
00282 experiment.S_P = (double) atof(inf3.substr(0,findSV).c_str());
00283 experiment.S_R = (double) atof(inf3.substr(findSV+1).c_str());
00284 }
00285 }
00286 else
00287 {
00288
00289
00290 serial.flush();
00291 serial.write("#f");
00292
00293 string sa;
00294 string sm;
00295 string sg;
00296
00297 try
00298 {
00299 serial.readLine(&sa,100);
00300 serial.readLine(&sm,100);
00301 serial.readLine(&sg,100);
00302 }catch(serialcom::Exception ex)
00303 {
00304 cout<<ex.what()<<endl;
00305 }
00306
00307 int ncsa=count(sa.begin(), sa.end(), ',');
00308 int ncsm=count(sm.begin(), sm.end(), ',');
00309 int ncsg=count(sg.begin(), sg.end(), ',');
00310 int na=count(sa.begin(), sa.end(), 'A');
00311 int nm=count(sm.begin(), sm.end(), 'M');
00312 int ng=count(sg.begin(), sg.end(), 'G');
00313
00314 if((ncsa!=3 || ncsm!=3 || ncsg!=3) && (na==1 && nm==1 && ng==1))
00315 {
00316 exit(0);
00317 }
00318 else
00319 {
00320
00321 }
00322
00323 istringstream ss(sa);
00324 getline(ss,junk,'=');
00325 getline(ss,Ax, ',');
00326 getline(ss,Ay, ',');
00327 getline(ss,Az, ',');
00328 experiment.S_Ax = (double) atof(Ax.c_str());
00329 experiment.S_Ay = (double) atof(Ay.c_str());
00330 experiment.S_Az = (double) atof(Az.c_str());
00331
00332
00333 istringstream ss2(sm);
00334 getline(ss2,junk,'=');
00335 getline(ss2,Mx, ',');
00336 getline(ss2,My, ',');
00337 getline(ss2,Mz, ',');
00338 experiment.S_Mx = (double) atof(Mx.c_str());
00339 experiment.S_My = (double) atof(My.c_str());
00340 experiment.S_Mz = (double) atof(Mz.c_str());
00341
00342
00343 istringstream ss3(sg);
00344 getline(ss3,junk,'=');
00345 getline(ss3,Gx, ',');
00346 getline(ss3,Gy, ',');
00347 getline(ss3,Gz, ',');
00348 experiment.S_Gx = (double) atof(Gx.c_str());
00349 experiment.S_Gy = (double) atof(Gy.c_str());
00350 experiment.S_Gz = (double) atof(Gz.c_str());
00351
00352
00353 }
00354
00355 c.getcrcpos(p1);
00356
00357
00358 experiment.F_x = p1.x;
00359 experiment.F_y = p1.y;
00360 experiment.F_z = p1.z;
00361 experiment.F_w = p1.w;
00362 experiment.F_r = p1.r;
00363 experiment.F_p = p1.p;
00364
00365 chatter_pub.publish(experiment);
00366
00367 loop_rate.sleep();
00368
00369 }
00370
00371 c.close();
00372 serial.close();
00373 }catch (std::exception& e)
00374 {
00375 std::cerr << "Exception: " << e.what() << "\n";
00376 }
00377
00378 return 0;
00379
00380 }