00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "gcodeReceiver.hpp"
00019 #include <ocl/ComponentLoader.hpp>
00020 #include <sys/socket.h>
00021 #include <netinet/in.h>
00022 #include <iostream>
00023 #include <fcntl.h>
00024
00025 namespace OCL{
00026
00027 using namespace std;
00028 using namespace RTT;
00029 using namespace KDL;
00030
00031 gcodeReceiver::gcodeReceiver(string name):
00032 TaskContext(name,PreOperational),
00033
00034 port_prop("port","port to receive positions",50006),
00035 initial_pos("CartesianStartingPosition",Frame::Identity()),
00036 cart_pos("CartesianDesiredPosition",Frame::Identity()),
00037 received(false)
00038 {
00039
00040 log(Info) << this->getName() << ": registring and reading Properties" << endlog();
00041 properties()->addProperty(&port_prop);
00042 attributes()->addAttribute(&initial_pos);
00043
00044
00045 log(Info) << this->getName() << ": registring ports" << endlog();
00046 ports()->addPort(&cart_pos);
00047 }
00048
00049
00050 gcodeReceiver::~gcodeReceiver()
00051 {
00052 }
00053
00054 bool gcodeReceiver::configureHook()
00055 {
00056 int portno;
00057 socklen_t clilen;
00058 struct sockaddr_in serv_addr, cli_addr;
00059 sockfd = socket(AF_INET, SOCK_STREAM, 0);
00060 if (sockfd < 0) return false;
00061 bzero((char *) &serv_addr, sizeof(serv_addr));
00062 portno = port_prop;
00063 serv_addr.sin_family = AF_INET;
00064 serv_addr.sin_addr.s_addr = INADDR_ANY;
00065 serv_addr.sin_port = htons(portno);
00066 if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
00067 return false;
00068 listen(sockfd,5);
00069 clilen = sizeof(cli_addr);
00070
00071 Logger::In in(this->getName().c_str());
00072 log(Warning)<<"Waiting for connection on port: "<<portno<<endlog();
00073
00074 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
00075 if (newsockfd < 0) return false;
00076 bzero(buffer,256);
00077 int flags = fcntl(newsockfd,F_GETFL);
00078 fcntl(newsockfd,F_SETFL,flags | O_NONBLOCK);
00079
00080
00081 while(!received)
00082 this->updateHook();
00083 initial_pos.set(f);
00084
00085 return true;
00086 }
00087
00088 bool gcodeReceiver::startHook()
00089 {
00090 this->updateHook();
00091 return true;
00092 }
00093
00094
00095 void gcodeReceiver::updateHook()
00096 {
00097 int i=0;
00098 int j=0;
00099 int n =read(newsockfd,buffer,255);
00100 if(n>0){
00101 if(strchr(buffer,'\n') != NULL){
00102 log(Warning)<<"End of gcode reached. Stopping component"<<endlog();
00103 this->stop();
00104 }
00105 char * pch;
00106 pch = strtok (buffer,", ");
00107 j=0;
00108 while (pch != NULL){
00109 data[j]=strtod(pch,NULL);
00110 pch = strtok (NULL, ", ");
00111 if (j<3){
00112 f.p[j]=data[j];
00113 }
00114 if (j==5){
00115 f.M=Rotation::RPY(data[3], data[4], data[5]);
00116 }
00117 j++;
00118 }
00119 bzero(buffer,256);
00120 n = write(newsockfd,"ack",7);
00121
00122
00123 i++;
00124 cart_pos.Set(f);
00125 received=true;
00126 }
00127 }
00128
00129
00130 void gcodeReceiver::stopHook()
00131 {
00132 close(newsockfd);
00133 close(sockfd);
00134 log(Info) << this->getName() << ": shutdown" << endlog();
00135 }
00136 }
00137
00138 ORO_CREATE_COMPONENT(OCL::gcodeReceiver);
00139