00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00038 #include "../include/IPSA/tools/NVRemoteConnection.h"
00039 #include "../include/IPSA/network/TCPSocket.h"
00040 #include "../include/IPSA/viewer/instructions/NVInstructionAnswerParsing.h"
00041
00042 #include <iostream>
00043 #include <cassert>
00044 #include <algorithm>
00045 #include <iterator>
00046
00047
00056 NVRemoteConnection::NVRemoteConnection(bool connectOnCreate , const std::string& hostname , unsigned int port )
00057 : commandStream("")
00058 {
00059 m_pSocket = new CTCPSocket(hostname.c_str(), port);
00060 if (connectOnCreate)
00061 this->connect();
00062 }
00063
00064
00068 NVRemoteConnection::~NVRemoteConnection()
00069 {
00070 delete m_pSocket;
00071 m_pSocket = NULL;
00072 }
00073
00074
00081 void NVRemoteConnection::sendCommands()
00082 {
00083 std::vector<float> answer(3, 0);
00084 std::string command = "";
00085 std::cout << "Sending commands " << std::endl;
00086 std::vector<std::string> jList = this->getJointList();
00087 std::cout << "JointList: " << std::endl;
00088 std::copy(jList.begin(), jList.end(),
00089 std::ostream_iterator<std::string>(std::cout, "\n"));
00090
00091
00092 this->scheduleOde();
00093
00094 if (jList.empty())
00095 return;
00096
00097 int sleepTimeSeconds = 4;
00098 std::cout << "Sleeping for: " << sleepTimeSeconds << " seconds" << std::endl;
00099 #ifdef WIN32
00100 Sleep(sleepTimeSeconds);
00101 #else
00102 sleep(sleepTimeSeconds);
00103 #endif
00104
00105 std::cout << "Joint Forces: " << std::endl;
00106 std::map<std::string, std::vector<float> > forceMap;
00107 forceMap = this->getJointForces(jList);
00108 for (unsigned int i = 0; i < forceMap.size(); i++)
00109 {
00110 answer = forceMap[jList.at(i)];
00111 std::copy(answer.begin(), answer.end(),
00112 std::ostream_iterator<float>(std::cout, " "));
00113 std::cout << std::endl;
00114 }
00115
00116
00117 std::cout << "Joint Torques: " << std::endl;
00118 std::map<std::string, std::vector<float> > torqueMap;
00119 torqueMap = this->getJointForces(jList);
00120 for (unsigned int i = 0; i < torqueMap.size(); i++)
00121 {
00122 answer = torqueMap[jList.at(i)];
00123 std::copy(answer.begin(), answer.end(),
00124 std::ostream_iterator<float>(std::cout, " "));
00125 std::cout << std::endl;
00126 }
00127
00128 sleepTimeSeconds = 2;
00129 #ifdef WIN32
00130 Sleep(sleepTimeSeconds);
00131 #else
00132 sleep(sleepTimeSeconds);
00133 #endif
00134
00135 std::cout << "Joint Speed: " << std::endl;
00136 this->setJointSpeed(jList.front(), 5);
00137
00138 std::cout << "done " << std::endl;
00139 }
00140
00141
00147 void NVRemoteConnection::scheduleOde(bool scheduleSimulation )
00148 {
00149 std::string scheduleCommand = "schedule\n";
00150 if (!scheduleSimulation)
00151 scheduleCommand = "unschedule\n";
00152
00153 m_pSocket->send(scheduleCommand);
00154 }
00155
00156
00168 std::vector<float>
00169 NVRemoteConnection::getVectorForCommand(const std::string& commandName, const std::string& nodeName, bool firstBody )
00170 {
00171 commandStream.str("");
00172 commandStream << commandName << " " << nodeName;
00173 if (!commandName.compare("getjointtorque") || !commandName.compare("getjointforce"))
00174 commandStream << (firstBody ? " 1" : " 2");
00175 commandStream << std::endl;
00176
00177 m_pSocket->send(commandStream.str());
00178 std::string answerString = "";
00179 std::vector<float> resultVector(3, 0);
00180
00181 if (this->receiveAnswer(answerString))
00182 convertAnswerToVector<float>(answerString, resultVector);
00183
00184 return resultVector;
00185 }
00186
00187
00198 std::vector<float> NVRemoteConnection::getJointForce(const std::string& jointName, bool firstBody )
00199 {
00200 return getVectorForCommand("getjointforce", jointName, firstBody);
00201 }
00202
00203
00215 std::vector<float> NVRemoteConnection::getJointTorque(const std::string& jointName, bool firstBody )
00216 {
00217 return getVectorForCommand("getjointtorque", jointName, firstBody);
00218 }
00219
00220
00230 float NVRemoteConnection::getValueForCommand(const std::string& commandName, const std::string& nodeName)
00231 {
00232 commandStream.str("");
00233 commandStream << commandName << " " << nodeName;
00234 commandStream << std::endl;
00235
00236 m_pSocket->send(commandStream.str());
00237 static std::string AnswerString = "";
00238 float resultValue = 0.0f;
00239
00240 if (this->receiveAnswer(AnswerString))
00241 convertAnswerToType<float>(AnswerString, resultValue);
00242
00243 return resultValue;
00244 }
00245
00246
00252 float NVRemoteConnection::getJointAngle(const std::string& jointName)
00253 {
00254 return getValueForCommand("getjointangle", jointName);
00255 }
00256
00257
00263 float NVRemoteConnection::getJointAngleRate(const std::string& jointName)
00264 {
00265 return getValueForCommand("getjointanglerate", jointName);
00266 }
00267
00268
00274 float NVRemoteConnection::getJointPosition(const std::string& jointName)
00275 {
00276 return getValueForCommand("getjointposition", jointName);
00277 }
00278
00279
00285 float NVRemoteConnection::getJointPositionRate(const std::string& jointName)
00286 {
00287 return getValueForCommand("getjointpositionrate", jointName);
00288 }
00289
00290
00298 std::map<std::string, std::vector<float> >
00299 NVRemoteConnection::getMapForNodesAndCommand(const std::vector<std::string>& nodeList, std::vector<float> (NVRemoteConnection::*commandFunction)(const std::string&, bool))
00300 {
00301 std::map<std::string, std::vector<float> > valueMap;
00302
00303 if (!nodeList.empty())
00304 {
00305 std::vector<std::string>::const_iterator iter = nodeList.begin();
00306 while (iter != nodeList.end())
00307 {
00308
00309 valueMap[(*iter)] = ((*this.*commandFunction)((*iter), true));
00310 iter++;
00311 }
00312
00313 }
00314 return valueMap;
00315 }
00316
00317
00325 std::map<std::string, float>
00326 NVRemoteConnection::getMapForNodesAndCommand(const std::vector<std::string>& nodeList, float (NVRemoteConnection::*commandFunction)(const std::string&))
00327 {
00328 std::map<std::string, float> valueMap;
00329
00330 if (!nodeList.empty())
00331 {
00332 std::vector<std::string>::const_iterator iter = nodeList.begin();
00333 while (iter != nodeList.end())
00334 {
00335
00336 valueMap[(*iter)] = ((*this.*commandFunction)(*iter));
00337 iter++;
00338 }
00339
00340 }
00341 return valueMap;
00342 }
00343
00344
00352 std::map<std::string, std::vector<float> >
00353 NVRemoteConnection::getJointForces(const std::vector<std::string>& jointList)
00354 {
00355 return getMapForNodesAndCommand(jointList, &NVRemoteConnection::getJointForce);
00356 }
00357
00358
00366 std::map<std::string, std::vector<float> >
00367 NVRemoteConnection::getJointTorques(const std::vector<std::string>& jointList)
00368 {
00369 return getMapForNodesAndCommand(jointList, &NVRemoteConnection::getJointTorque);
00370 }
00371
00372
00380 std::map<std::string, float> NVRemoteConnection::getJointAngles(const std::vector<std::string>& jointList)
00381 {
00382 return getMapForNodesAndCommand(jointList, &NVRemoteConnection::getJointAngle);
00383 }
00384
00385
00393 std::map<std::string, float> NVRemoteConnection::getJointAngleRates(const std::vector<std::string>& jointList)
00394 {
00395 return getMapForNodesAndCommand(jointList, &NVRemoteConnection::getJointAngleRate);
00396 }
00397
00398
00406 std::map<std::string, float> NVRemoteConnection::getJointPositions(const std::vector<std::string>& jointList)
00407 {
00408 return getMapForNodesAndCommand(jointList, &NVRemoteConnection::getJointPosition);
00409 }
00410
00411
00419 std::map<std::string, float> NVRemoteConnection::getJointPositionRates(const std::vector<std::string>& jointList)
00420 {
00421 return getMapForNodesAndCommand(jointList, &NVRemoteConnection::getJointPositionRate);
00422 }
00423
00424
00434 std::vector<std::string> NVRemoteConnection::getListForCommand(const std::string& commandName)
00435 {
00436 commandStream.str("");
00437 commandStream << "setVerbosity 0" << std::endl
00438 << commandName << std::endl;
00439 m_pSocket->send(commandStream.str());
00440 std::string jointNames = "";
00441 std::vector<std::string> jointList(0, "");
00442
00443 if (this->receiveAnswer(jointNames) && (jointNames.length() > 0))
00444 {
00445
00446
00447 std::string::size_type firstIndex = jointNames.find_first_not_of("\n");
00448 std::string::size_type nextIndex = 0;
00449 while (std::string::npos != firstIndex)
00450 {
00451 nextIndex = jointNames.find_first_of("\n", firstIndex);
00452 if (std::string::npos == nextIndex)
00453 nextIndex = jointNames.length();
00454 jointList.push_back(jointNames.substr(firstIndex, (nextIndex - firstIndex - 1)));
00455 firstIndex = jointNames.find_first_not_of("\n", nextIndex);
00456 }
00457 }
00458 return jointList;
00459 }
00460
00461
00469 std::vector<std::string> NVRemoteConnection::getJointList()
00470 {
00471 return getListForCommand("jlist");
00472 }
00473
00474
00482 std::vector<std::string> NVRemoteConnection::getBodyList()
00483 {
00484 return getListForCommand("blist");
00485 }
00486
00487
00500 void NVRemoteConnection::set3DVectorForCommandAndNode(const std::vector<float>& values, const std::string& commandName, const std::string& nodeName)
00501 {
00502 assert(3 == values.size() && "NVRemoteConnection::setVector3DForCommandAndNode() -> value vector does not have 3 elements!");
00503
00504 commandStream.str("");
00505 commandStream << commandName << " " << nodeName
00506 << " " << values.at(0)
00507 << " " << values.at(1)
00508 << " " << values.at(2)
00509 << std::endl;
00510 m_pSocket->send(commandStream.str());
00511 }
00512
00513
00526 void NVRemoteConnection::setValueForCommandAndNode(const float& value, const std::string& commandName, const std::string& nodeName)
00527 {
00528 commandStream.str("");
00529 commandStream << commandName << " " << nodeName
00530 << " " << value
00531 << std::endl;
00532 m_pSocket->send(commandStream.str());
00533 }
00534
00535
00542 void NVRemoteConnection::setJointForce(const std::string& jointName, float force)
00543 {
00544 setValueForCommandAndNode(force, "jointforce", jointName);
00545 }
00546
00547
00554 void NVRemoteConnection::setJointForce2(const std::string& jointName, float force2)
00555 {
00556 setValueForCommandAndNode(force2, "jointforce2", jointName);
00557 }
00558
00559
00566 void NVRemoteConnection::setJointSpeed(const std::string& jointName, float speed)
00567 {
00568 setValueForCommandAndNode(speed, "jointspeed", jointName);
00569 }
00570
00571
00578 void NVRemoteConnection::setJointSpeed2(const std::string& jointName, float speed2)
00579 {
00580 setValueForCommandAndNode(speed2, "jointspeed2", jointName);
00581 }
00582
00583
00590 void NVRemoteConnection::setJointTorque(const std::string& jointName, float torque)
00591 {
00592 setValueForCommandAndNode(torque, "jointtorque", jointName);
00593 }
00594
00595
00602 void NVRemoteConnection::setJointTorque2(const std::string& jointName, float torque2)
00603 {
00604 setValueForCommandAndNode(torque2, "jointtorque2", jointName);
00605 }
00606
00607
00620 bool NVRemoteConnection::receiveAnswer(std::string& answer)
00621 {
00622 answer.clear();
00623
00624 const int LENGTH = 4096;
00625 int rLength = 0;
00626 char res[LENGTH];
00627 memset(res, 0, LENGTH);
00628
00629 if( !this->m_pSocket->setNonBlocking(-1))
00630 return false;
00631 while (0 < (rLength = m_pSocket->receive(res, LENGTH)))
00632 {
00633 if (NULL != res)
00634 answer += std::string(res, rLength);
00635 else
00636 {
00637 answer.clear();
00638 std::cout << "NVRemoteConnection::receiveAnswer(): Buffer was NULL" << std::endl;
00639 return false;
00640 }
00641 if (!this->m_pSocket->setNonBlocking(0))
00642 return false;
00643 }
00644
00645 return true;
00646 }
00647
00648
00655 bool NVRemoteConnection::connect()
00656 {
00657 return m_pSocket->openSocket();
00658 }
00659
00660
00666 bool NVRemoteConnection::isConnected()
00667 {
00668 return m_pSocket->isConnected();
00669 }