#include "header.h" #include #include "global.h" #include "mat.h" #include "StringTool.h" #include #include #include void mattoabc(float M[3][4], float &a, float &b, float &c) { float norm; float sa; float ca; norm = sqrt((M[0][0]*M[0][0])+(M[1][0]*M[1][0])); if (norm>1e-5) { sa = M[1][0]/norm; ca = M[0][0]/norm; a = atan2(sa,ca); } else { sa = 0; ca = 1; a = 0; } b = atan2(-M[2][0],ca*M[0][0]+sa*M[1][0]); c = atan2(sa*M[0][2]-ca*M[1][2],-sa*M[0][1]+ca*M[1][1]); } void debugMat(Mat4f M) { #if 0 for (int i=0;iabs(maxRangeJnt[i])) { // join angle is too big return false; } } // no join angle is too big return false; } Mat4f vecToMat(float vec[12]) { Mat4f result; for (int i=0; i<3; i++) { for (int j=0; j<4; j++) { result(i,j) = vec[i*4+j]; } } result(3,3)= 1.0f; return result; } float* matToVec(Mat4f mat) { float* vec = new float[12]; for (int j=0;j<3;j++) { for (int k=0;k<4;k++) { vec[j*4+k] = mat(j,k); } } return vec; } Mat4f getTranslation(double tx, double ty, double tz) { Mat4f transl; transl(0,0) = 1; transl(1,1) = 1; transl(2,2) = 1; transl(0,3) = tx; transl(1,3) = ty; transl(2,3) = tz; transl(3,3) = 1; return transl; } SvrHandling::SvrHandling() { } SvrHandling::~SvrHandling() { } void SvrHandling::run() { this->run(SVR_DEFAULT_PORT); } void SvrHandling::run(int port) { cout << timestamp() + "Starting " << SVR_NAME << "\n"; port = SVR_DEFAULT_PORT; while (c_state !=done) { SocketObject *socket = new SocketObject; if (socket->Bind(port) <0) { exit (1); } socket->Listen(); while(1) { SocketObject *client = new SocketObject; if (socket->Accept(*client)) { cout << timestamp() + "Client accepted!\n"; if (handshakeAccepted(*client)) { handle(*client); } client->Disconnect(); cout << timestamp() + "Client disconnected.\n"; } else { cout << timestamp() + "Client bin error.\n"; } delete client; } delete socket; } } bool SvrHandling::handshakeAccepted(SocketObject& client) { this->c_state = handshake; string message = SVR_HELLO_MSG; client.Send(message); client.Recv(message); if (message.find(SVR_HANDSHAKE,0) != string::npos) { c_state = accepted; message = SVR_ACCEPTED; } else { c_state = waiting; message = SVR_FAILED; cout << timestamp() + "Handshake failed. " << "Invalid recv msg \'" << message <<"\'"; } client.Send(message); return(c_state == accepted); } void SvrHandling::handle(SocketObject& client) { string message, cmd, arg; while (c_state == accepted) { if (client.Recv(message) > 0) { cout< tokens; int i = 0; while (ss >> buf) { if (i>LBR_MNJ-1) { client.Send(SVR_FALSE_RSP); return; } tokens.push_back(buf); ss2f.flush(); ss2f.clear(); ss2f << buf; ss2f >> temp[i]; i++; } if (i tokens; int i = 0; while (ss >> buf) { if (i>15-1) { client.Send(SVR_FALSE_RSP); return; } tokens.push_back(buf); ss2f.flush(); ss2f.clear(); ss2f << buf; ss2f >> temp[i]; i++; } if (i<15-1) { client.Send(SVR_FALSE_RSP); return ; } for (int i=0 ;i<3; i++) { for (int j=0;j<4;j++) { MSRCMDPOSE[i][j]=temp[i*4+j]; } } float arg[3]; for (int i=0;i<3;i++) { arg[i] = temp[12+i]; } //Backward Calculation double* CalcJoints=NULL; CalcJoints = kukaBackwardCalc(temp, arg); //Check for Joint Range if (!checkJntRange(CalcJoints)) { string out = "Error: Joints out of Range!"; client.Send(out); return; } // Jmove for (int i=0 ;i tokens; int i = 0; if (args == "") { client.Send(SVR_FALSE_RSP); return; } while (ss >> buf) { if (i>0) { client.Send(SVR_FALSE_RSP); return; } tokens.push_back(buf); ss2f.flush(); ss2f.clear(); ss2f << buf; ss2f >> temp[i]; i++; } if ((temp[0] < 1)||(temp[0]>1000)) { client.Send(SVR_FALSE_RSP); return; } VELOCITY = temp[0]; client.Send(SVR_TRUE_RSP); __SVR_CURRENT_JOB = false; } void SvrHandling::SetAccel(SocketObject& client, string& args) { __SETACCEL = true; __SVR_CURRENT_JOB = true; string buf; float temp[1]; stringstream ss(args); stringstream ss2f; vector tokens; int i = 0; if (args == "") { client.Send(SVR_FALSE_RSP); return; } while (ss >> buf) { if (i>0) { client.Send(SVR_FALSE_RSP); return; } tokens.push_back(buf); ss2f.flush(); ss2f.clear(); ss2f << buf; ss2f >> temp[i]; i++; } if ((temp[0] < 1)||(temp[0]>100)) { client.Send(SVR_FALSE_RSP); return; } ACCELARATION = temp[0]; client.Send(SVR_TRUE_RSP); __SVR_CURRENT_JOB = false; } void SvrHandling::StartPotFieldMode(SocketObject& client, string& args) { __MSRSTARTPOTFIELD = false; __MSRSTOPPOTFIELD = false; //Set current Joint Vals for (int i=0; i tokens; int i = 0; while (ss >> buf) { if (i>15-1) { client.Send(SVR_FALSE_RSP); return; } tokens.push_back(buf); ss2f.flush(); ss2f.clear(); ss2f << buf; ss2f >> temp[i]; i++; } if (i<15-1) { client.Send(SVR_FALSE_RSP); return ; } for (int i=0 ;i<3; i++) { for (int j=0;j<4;j++) { MSRCMDPOSE[i][j]=temp[i*4+j]; } } float arg[3]; for (int i=0;i<3;i++) { arg[i] = temp[12+i]; } //Backward Calculation double* CalcJoints=NULL; CalcJoints = kukaBackwardCalc(temp, arg); //Check for Joint Range if (!checkJntRange(CalcJoints)) { string out = "Error: Joints out of Range!"; client.Send(out); return; } // Set global Position __MSRSETPOS = true; globi = 1; for (int i=0 ;i tokens; int i = 0; while (ss >> buf) { if (i>LBR_MNJ-1) { client.Send(SVR_FALSE_RSP); return; } tokens.push_back(buf); ss2f.flush(); ss2f.clear(); ss2f << buf; ss2f >> temp[i]; i++; } if (i tokens; int i = 0; while (ss >> buf) { if (i>15-1) { client.Send(SVR_FALSE_RSP); return; } tokens.push_back(buf); ss2f.flush(); ss2f.clear(); ss2f << buf; ss2f >> temp[i]; i++; } if (i<15-1) { client.Send(SVR_FALSE_RSP); return ; } float arg[3]; for (int i=0;i<3;i++) { arg[i] = temp[12+i]; } //Calculating end-effector position for target "TRobot" Mat4f Tsurface; Tsurface = vecToMat(temp); Mat4f TRobot; TRobot = Tsurface; //Calculating end-effector position for approach position "ApRobot" Mat4f ApRobot; Mat4f ApSurface; Mat4f TransZ; // 100mm approach position TransZ = getTranslation(0,0,-0.1); ApSurface = Tsurface * TransZ; ApRobot = ApSurface ; float* tempVec = NULL; tempVec = matToVec(TRobot); for (int i=0; i<12; i++) USTarget[i] = tempVec[i]; //tempVec = matToVec(ApRobot); for (int i=0; i<12; i++) USApproach[i] = tempVec[i]; client.Send(SVR_TRUE_RSP); __DOUS2 = true; } void SvrHandling::quit(SocketObject& client) { client.Send(SVR_TRUE_RSP); client.Disconnect(); } void SvrHandling::debug(SocketObject& client) { __DEBUG = true; //check = true; //float temp[7]; //while (1){ // friInst.setToKRLInt(15,1); // friInst.doSendData(); // //client.Send(friInst.getFrmKRLInt(2)); // for ( int i= 0; i < LBR_MNJ; i++) //{ // temp[i]=friInst.getMsrMsrJntPosition()[i]; //} //friInst.doPositionControl(temp); //} }