RosMsgConverter.cpp
Go to the documentation of this file.
00001 #include <robodyn_utilities/RosMsgConverter.h>
00002 
00003 #include "nasa_common_logging/Logger.h"
00004 #include <tf_conversions/tf_kdl.h>
00005 #include <boost/tuple/tuple.hpp>
00006 
00007 namespace RosMsgConverter
00008 {
00021     void JointStateToJntArray(const sensor_msgs::JointState& jointState,
00022                               const std::vector<std::string> jointNames,
00023                               KDL::JntArray& jointArray)
00024     {
00025         if (jointArray.rows() != jointNames.size())
00026         {
00027             jointArray.resize(jointNames.size());
00028         }
00029 
00030         if (jointState.name.size() != jointState.position.size())
00031         {
00032             // length mismatch
00033             std::stringstream err;
00034             err << "JointStateToJntArray() - jointState name length doesn't match positions length: " << jointState.name.size() << " vs " << jointState.position.size();
00035             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJntArray", log4cpp::Priority::ERROR, err.str());
00036             throw std::logic_error(err.str());
00037             return;
00038         }
00039 
00040         // if M = jointState.name.size() & N = jointNames.size(),
00041         // a simple repetitive search would be worst case O(M*N);
00042         // let's sort instead to get worst case O((M+N)*log(M))
00043         std::map<std::string, double> jsMap;
00044 
00045         for (unsigned int i = 0; i < jointState.name.size(); ++i)
00046         {
00047             jsMap[jointState.name[i]] = jointState.position[i];
00048         }
00049 
00050         std::string jointName;
00051 
00052         for (unsigned int nameIndex = 0; nameIndex < jointNames.size(); ++nameIndex)
00053         {
00054             jointName = jointNames[nameIndex];
00055 
00056             std::map<std::string, double>::iterator jsMapIt = jsMap.find(jointName);
00057 
00058             if (jsMapIt == jsMap.end())
00059             {
00060                 // not found
00061                 std::stringstream err;
00062                 err << "JointStateToJntArray() - jointState doesn't contain joint: " << jointName;
00063                 NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJntArray", log4cpp::Priority::ERROR, err.str());
00064                 throw std::logic_error(err.str());
00065                 return;
00066             }
00067             else
00068             {
00069                 jointArray(nameIndex) = jsMapIt->second;
00070             }
00071         }
00072     }
00073 
00086     void JointStateToJntArrayVel(const sensor_msgs::JointState& jointState,
00087                                  const std::vector<std::string> jointNames,
00088                                  KDL::JntArrayVel& jointArray)
00089     {
00090         if (jointArray.q.rows() != jointNames.size())
00091         {
00092             jointArray.resize(jointNames.size());
00093         }
00094 
00095         if (jointState.name.size() != jointState.position.size())
00096         {
00097             // length mismatch
00098             std::stringstream err;
00099             err << "JointStateToJntArray() - jointState name length doesn't match positions length: " << jointState.name.size() << " vs " << jointState.position.size();
00100             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJntArray", log4cpp::Priority::ERROR, err.str());
00101             throw std::logic_error(err.str());
00102             return;
00103         }
00104 
00105         bool noVels = false;
00106 
00107         if (jointState.name.size() != jointState.velocity.size())
00108         {
00109             if (jointState.velocity.size() == 0)
00110             {
00111                 noVels = true;
00112             }
00113             else
00114             {
00115                 // length mismatch
00116                 std::stringstream err;
00117                 err << "JointStateToJntArray() - jointState name length doesn't match velocity length: " << jointState.name.size() << " vs " << jointState.velocity.size();
00118                 NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJntArray", log4cpp::Priority::ERROR, err.str());
00119                 throw std::logic_error(err.str());
00120                 return;
00121             }
00122         }
00123 
00124         // if M = jointState.name.size() & N = jointNames.size(),
00125         // a simple repetitive search would be worst case O(M*N);
00126         // let's sort instead to get worst case O((M+N)*log(M))
00127         std::map<std::string, std::pair<double, double> > jsMap;
00128 
00129         for (unsigned int i = 0; i < jointState.name.size(); ++i)
00130         {
00131             if (noVels)
00132             {
00133                 jsMap[jointState.name[i]] = std::make_pair(jointState.position[i], 0.);
00134             }
00135             else
00136             {
00137                 jsMap[jointState.name[i]] = std::make_pair(jointState.position[i], jointState.velocity[i]);
00138             }
00139         }
00140 
00141         std::string jointName;
00142 
00143         for (unsigned int nameIndex = 0; nameIndex < jointNames.size(); ++nameIndex)
00144         {
00145             jointName = jointNames[nameIndex];
00146 
00147             std::map<std::string, std::pair<double, double> >::iterator jsMapIt = jsMap.find(jointName);
00148 
00149             if (jsMapIt == jsMap.end())
00150             {
00151                 // not found
00152                 std::stringstream err;
00153                 err << "JointStateToJntArrayVel() - jointState doesn't contain joint: " << jointName;
00154                 NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJntArrayVel", log4cpp::Priority::ERROR, err.str());
00155                 throw std::logic_error(err.str());
00156                 return;
00157             }
00158             else
00159             {
00160                 jointArray.q(nameIndex)    = jsMapIt->second.first;
00161                 jointArray.qdot(nameIndex) = jsMapIt->second.second;
00162             }
00163         }
00164     }
00165 
00166     void JointStateToJntArrayAcc(const sensor_msgs::JointState& jointState,
00167                                  const std::vector<std::string> jointNames,
00168                                  KDL::JntArrayAcc& jointArray)
00169     {
00170         if (jointArray.q.rows() != jointNames.size())
00171         {
00172             jointArray.resize(jointNames.size());
00173         }
00174 
00175         if (jointState.name.size() != jointState.position.size())
00176         {
00177             // length mismatch
00178             std::stringstream err;
00179             err << "JointStateToJntArray() - jointState name length doesn't match positions length: " << jointState.name.size() << " vs " << jointState.position.size();
00180             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJntArray", log4cpp::Priority::ERROR, err.str());
00181             throw std::logic_error(err.str());
00182             return;
00183         }
00184 
00185         if (jointState.name.size() != jointState.velocity.size())
00186         {
00187             // length mismatch
00188             std::stringstream err;
00189             err << "JointStateToJntArray() - jointState name length doesn't match velocities length: " << jointState.name.size() << " vs " << jointState.velocity.size();
00190             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJntArray", log4cpp::Priority::ERROR, err.str());
00191             throw std::logic_error(err.str());
00192             return;
00193         }
00194 
00195         if (jointState.name.size() != jointState.effort.size())
00196         {
00197             // length mismatch
00198             std::stringstream err;
00199             err << "JointStateToJntArray() - jointState name length doesn't match positions length: " << jointState.name.size() << " vs " << jointState.effort.size();
00200             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJntArray", log4cpp::Priority::ERROR, err.str());
00201             throw std::logic_error(err.str());
00202             return;
00203         }
00204 
00205         // if M = jointState.name.size() & N = jointNames.size(),
00206         // a simple repetitive search would be worst case O(M*N);
00207         // let's sort instead to get worst case O((M+N)*log(M))
00208         std::map<std::string, boost::tuples::tuple<double, double, double> > jsMap;
00209 
00210         for (unsigned int i = 0; i < jointState.name.size(); ++i)
00211         {
00212             jsMap[jointState.name[i]] = boost::tuples::make_tuple(jointState.position[i], jointState.velocity[i], jointState.effort[i]);
00213         }
00214 
00215         for (unsigned int nameIndex = 0; nameIndex < jointNames.size(); ++nameIndex)
00216         {
00217             std::map<std::string, boost::tuples::tuple<double, double, double> >::iterator jsMapIt = jsMap.find(jointNames[nameIndex]);
00218 
00219             if (jsMapIt == jsMap.end())
00220             {
00221                 // not found
00222                 std::stringstream err;
00223                 err << "JointStateToJntArrayVel() - jointState doesn't contain joint: " << jointNames[nameIndex];
00224                 NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJntArrayVel", log4cpp::Priority::ERROR, err.str());
00225                 throw std::logic_error(err.str());
00226                 return;
00227             }
00228             else
00229             {
00230                 jointArray.q(nameIndex)       = jsMapIt->second.get<0>();
00231                 jointArray.qdot(nameIndex)    = jsMapIt->second.get<1>();
00232                 jointArray.qdotdot(nameIndex) = jsMapIt->second.get<2>();
00233             }
00234         }
00235     }
00236 
00248     void PoseStateToFrame(const r2_msgs::PoseState& poseState, const std::string& baseName,
00249                           const std::string& tipName, KDL::Frame& frame)
00250     {
00251         if (poseState.name.size() != poseState.positions.size())
00252         {
00253             // length mismatch
00254             std::stringstream err;
00255             err << "PoseStateToFrame() - poseState name length doesn't match pose length";
00256             NasaCommonLogging::Logger::log("gov.nasa.controllers.PoseStateToFrame", log4cpp::Priority::ERROR, err.str());
00257             throw std::logic_error(err.str());
00258             return;
00259         }
00260 
00261         // find the needed poses
00262         bool       foundBase = false;
00263         bool       foundTip  = false;
00264         KDL::Frame baseFrame;
00265         KDL::Frame tipFrame;
00266 
00267         // check poseState frame
00268         if (poseState.header.frame_id == baseName)
00269         {
00270             foundBase = true;
00271         }
00272 
00273         if (poseState.header.frame_id == tipName)
00274         {
00275             foundTip = true;
00276         }
00277 
00278         // check poses
00279         for (unsigned int i = 0; i < poseState.name.size(); ++i)
00280         {
00281             if (!foundBase && poseState.name[i] == baseName)
00282             {
00283                 foundBase = true;
00284                 tf::poseMsgToKDL(poseState.positions.at(i), baseFrame);
00285 
00286                 if (foundTip)
00287                 {
00288                     break;
00289                 }
00290             }
00291 
00292             if (!foundTip && poseState.name[i] == tipName)
00293             {
00294                 foundTip = true;
00295                 tf::poseMsgToKDL(poseState.positions.at(i), tipFrame);
00296 
00297                 if (foundBase)
00298                 {
00299                     break;
00300                 }
00301             }
00302         }
00303 
00304         if (!foundBase || !foundTip)
00305         {
00306             // not found
00307             std::stringstream err;
00308             err << "PoseStateToFrame() - poseState doesn't contain the necessary transform information for " << baseName << " to " << tipName;
00309             NasaCommonLogging::Logger::log("gov.nasa.controllers.PoseStateToFrame", log4cpp::Priority::ERROR, err.str());
00310             throw std::logic_error(err.str());
00311             return;
00312         }
00313 
00314         // transform
00315         frame = baseFrame.Inverse() * tipFrame;
00316     }
00317 
00329     void PoseStateToFrameVel(const r2_msgs::PoseState& poseState, const std::string& baseName,
00330                              const std::string& tipName, KDL::FrameVel& frame)
00331     {
00332         if (poseState.name.size() != poseState.positions.size() || poseState.name.size() != poseState.velocities.size())
00333         {
00334             // length mismatch
00335             std::stringstream err;
00336             err << "PoseStateToFrameVel() - poseState name and value lengths don't match";
00337             NasaCommonLogging::Logger::log("gov.nasa.controllers.PoseStateToFrameVel", log4cpp::Priority::ERROR, err.str());
00338             throw std::logic_error(err.str());
00339             return;
00340         }
00341 
00342         // find the needed poses
00343         bool          foundBase = false;
00344         bool          foundTip  = false;
00345         KDL::FrameVel baseFrame;
00346         KDL::FrameVel tipFrame;
00347 
00348         // check poseState frame
00349         if (poseState.header.frame_id == baseName)
00350         {
00351             foundBase = true;
00352         }
00353 
00354         if (poseState.header.frame_id == tipName)
00355         {
00356             foundTip = true;
00357         }
00358 
00359         // check poses
00360         for (unsigned int i = 0; i < poseState.name.size(); ++i)
00361         {
00362             if (!foundBase && poseState.name[i] == baseName)
00363             {
00364                 foundBase = true;
00365 
00366                 // position
00367                 baseFrame.p.p.x(poseState.positions[i].position.x);
00368                 baseFrame.p.p.y(poseState.positions[i].position.y);
00369                 baseFrame.p.p.z(poseState.positions[i].position.z);
00370                 baseFrame.M.R = KDL::Rotation::Quaternion(poseState.positions[i].orientation.x, poseState.positions[i].orientation.y,
00371                                 poseState.positions[i].orientation.z, poseState.positions[i].orientation.w);
00372 
00373                 //velocity
00374                 baseFrame.p.v.x(poseState.velocities[i].linear.x);
00375                 baseFrame.p.v.y(poseState.velocities[i].linear.y);
00376                 baseFrame.p.v.z(poseState.velocities[i].linear.z);
00377                 baseFrame.M.w.x(poseState.velocities[i].angular.x);
00378                 baseFrame.M.w.y(poseState.velocities[i].angular.y);
00379                 baseFrame.M.w.z(poseState.velocities[i].angular.z);
00380 
00381                 if (foundTip)
00382                 {
00383                     break;
00384                 }
00385             }
00386 
00387             if (!foundTip && poseState.name[i] == tipName)
00388             {
00389                 foundTip = true;
00390 
00391                 // position
00392                 tipFrame.p.p.x(poseState.positions[i].position.x);
00393                 tipFrame.p.p.y(poseState.positions[i].position.y);
00394                 tipFrame.p.p.z(poseState.positions[i].position.z);
00395                 tipFrame.M.R = KDL::Rotation::Quaternion(poseState.positions[i].orientation.x, poseState.positions[i].orientation.y,
00396                                poseState.positions[i].orientation.z, poseState.positions[i].orientation.w);
00397 
00398                 //velocity
00399                 tipFrame.p.v.x(poseState.velocities[i].linear.x);
00400                 tipFrame.p.v.y(poseState.velocities[i].linear.y);
00401                 tipFrame.p.v.z(poseState.velocities[i].linear.z);
00402                 tipFrame.M.w.x(poseState.velocities[i].angular.x);
00403                 tipFrame.M.w.y(poseState.velocities[i].angular.y);
00404                 tipFrame.M.w.z(poseState.velocities[i].angular.z);
00405 
00406                 if (foundBase)
00407                 {
00408                     break;
00409                 }
00410             }
00411         }
00412 
00413         if (!foundBase || !foundTip)
00414         {
00415             // not found
00416             std::stringstream err;
00417             err << "PoseStateToFrameVel() - poseState doesn't contain the necessary transform information for " << baseName << " to " << tipName;
00418             NasaCommonLogging::Logger::log("gov.nasa.controllers.PoseStateToFrame", log4cpp::Priority::ERROR, err.str());
00419             throw std::logic_error(err.str());
00420             return;
00421         }
00422 
00423         // transform
00424         frame = baseFrame.Inverse() * tipFrame;
00425     }
00426 
00438     void PoseStateToFrameAcc(const r2_msgs::PoseState& poseState, const std::string& baseName,
00439                              const std::string& tipName, KDL::FrameAcc& frame)
00440     {
00441         if (poseState.name.size() != poseState.positions.size() || poseState.name.size() != poseState.velocities.size()
00442                 || poseState.name.size() != poseState.accelerations.size())
00443         {
00444             // length mismatch
00445             std::stringstream err;
00446             err << "PoseStateToFrameAcc() - poseState name and value lengths don't match";
00447             NasaCommonLogging::Logger::log("gov.nasa.controllers.PoseStateToFrameAcc", log4cpp::Priority::ERROR, err.str());
00448             throw std::logic_error(err.str());
00449             return;
00450         }
00451 
00452         // find the needed poses.at(i)
00453         bool          foundBase = false;
00454         bool          foundTip  = false;
00455         KDL::FrameAcc baseFrame;
00456         KDL::FrameAcc tipFrame;
00457 
00458         // check poseState frame
00459         if (poseState.header.frame_id == baseName)
00460         {
00461             foundBase = true;
00462         }
00463 
00464         if (poseState.header.frame_id == tipName)
00465         {
00466             foundTip = true;
00467         }
00468 
00469         // check poses
00470         for (unsigned int i = 0; i < poseState.name.size(); ++i)
00471         {
00472             if (!foundBase && poseState.name[i] == baseName)
00473             {
00474                 foundBase = true;
00475 
00476                 // position
00477                 baseFrame.p.p.x(poseState.positions[i].position.x);
00478                 baseFrame.p.p.y(poseState.positions[i].position.y);
00479                 baseFrame.p.p.z(poseState.positions[i].position.z);
00480                 baseFrame.M.R = KDL::Rotation::Quaternion(poseState.positions[i].orientation.x, poseState.positions[i].orientation.y,
00481                                 poseState.positions[i].orientation.z, poseState.positions[i].orientation.w);
00482 
00483                 //velocity
00484                 baseFrame.p.v.x(poseState.velocities[i].linear.x);
00485                 baseFrame.p.v.y(poseState.velocities[i].linear.y);
00486                 baseFrame.p.v.z(poseState.velocities[i].linear.z);
00487                 baseFrame.M.w.x(poseState.velocities[i].angular.x);
00488                 baseFrame.M.w.y(poseState.velocities[i].angular.y);
00489                 baseFrame.M.w.z(poseState.velocities[i].angular.z);
00490 
00491                 //acceleration
00492                 baseFrame.p.dv.x(poseState.accelerations[i].linear.x);
00493                 baseFrame.p.dv.y(poseState.accelerations[i].linear.y);
00494                 baseFrame.p.dv.z(poseState.accelerations[i].linear.z);
00495                 baseFrame.M.dw.x(poseState.accelerations[i].angular.x);
00496                 baseFrame.M.dw.y(poseState.accelerations[i].angular.y);
00497                 baseFrame.M.dw.z(poseState.accelerations[i].angular.z);
00498 
00499                 if (foundTip)
00500                 {
00501                     break;
00502                 }
00503             }
00504 
00505             if (!foundTip && poseState.name[i] == tipName)
00506             {
00507                 foundTip = true;
00508 
00509                 // position
00510                 tipFrame.p.p.x(poseState.positions[i].position.x);
00511                 tipFrame.p.p.y(poseState.positions[i].position.y);
00512                 tipFrame.p.p.z(poseState.positions[i].position.z);
00513                 tipFrame.M.R = KDL::Rotation::Quaternion(poseState.positions[i].orientation.x, poseState.positions[i].orientation.y,
00514                                poseState.positions[i].orientation.z, poseState.positions[i].orientation.w);
00515 
00516                 //velocity
00517                 tipFrame.p.v.x(poseState.velocities[i].linear.x);
00518                 tipFrame.p.v.y(poseState.velocities[i].linear.y);
00519                 tipFrame.p.v.z(poseState.velocities[i].linear.z);
00520                 tipFrame.M.w.x(poseState.velocities[i].angular.x);
00521                 tipFrame.M.w.y(poseState.velocities[i].angular.y);
00522                 tipFrame.M.w.z(poseState.velocities[i].angular.z);
00523 
00524                 //acceleration
00525                 tipFrame.p.dv.x(poseState.accelerations[i].linear.x);
00526                 tipFrame.p.dv.y(poseState.accelerations[i].linear.y);
00527                 tipFrame.p.dv.z(poseState.accelerations[i].linear.z);
00528                 tipFrame.M.dw.x(poseState.accelerations[i].angular.x);
00529                 tipFrame.M.dw.y(poseState.accelerations[i].angular.y);
00530                 tipFrame.M.dw.z(poseState.accelerations[i].angular.z);
00531 
00532                 if (foundBase)
00533                 {
00534                     break;
00535                 }
00536             }
00537         }
00538 
00539         if (!foundBase || !foundTip)
00540         {
00541             // not found
00542             std::stringstream err;
00543             err << "PoseStateToFrameAcc() - poseState doesn't contain the necessary transform information for " << baseName << " to " << tipName;
00544             NasaCommonLogging::Logger::log("gov.nasa.controllers.PoseStateToFrame", log4cpp::Priority::ERROR, err.str());
00545             throw std::logic_error(err.str());
00546             return;
00547         }
00548 
00549         // transform
00550         frame = baseFrame.Inverse() * tipFrame;
00551     }
00552 
00558     void PoseStateToFrameMap(const r2_msgs::PoseState& poseState, const std::string& base, std::map<std::string, KDL::Frame>& frames)
00559     {
00560         KDL::Frame  frame;
00561         std::string tipName;
00562 
00563         if (frames.size() != poseState.name.size())
00564         {
00565             NasaCommonLogging::Logger::getCategory("gov.nasa.controllers.PoseStateToFrameMap") << log4cpp::Priority::DEBUG << "resizing frames";
00566             frames.clear();
00567 
00568             for (unsigned int i = 0; i < poseState.name.size(); ++i)
00569             {
00570                 frames.insert(std::make_pair(poseState.name[i], KDL::Frame()));
00571             }
00572         }
00573 
00574         for (unsigned int i = 0; i < poseState.name.size(); ++i)
00575         {
00576             tipName = poseState.name[i];
00577 
00578             try
00579             {
00580                 PoseStateToFrame(poseState, base, tipName, frame );
00581                 frames[tipName] = frame;
00582             }
00583             catch (std::exception& e)
00584             {
00585                 NasaCommonLogging::Logger::getCategory("gov.nasa.controllers.PoseStateToFrameMap") << log4cpp::Priority::WARN << "Squelched error " << e.what() << ".. continuing..";
00586             }
00587         }
00588     }
00589 
00595     void PoseStateToFrameVelMap(const r2_msgs::PoseState& poseState, const std::string& base, std::map<std::string, KDL::FrameVel>& framevels)
00596     {
00597         KDL::FrameVel frameVel;
00598         std::string   tipName;
00599 
00600         if (framevels.size() != poseState.name.size())
00601         {
00602             NasaCommonLogging::Logger::getCategory("gov.nasa.controllers.PoseStateToFrameVelMap") << log4cpp::Priority::DEBUG << "resizing framevels";
00603             framevels.clear();
00604 
00605             for (unsigned int i = 0; i < poseState.name.size(); ++i)
00606             {
00607                 framevels.insert(std::make_pair(poseState.name[i], KDL::FrameVel()));
00608             }
00609         }
00610 
00611         for (unsigned int i = 0; i < poseState.name.size(); ++i)
00612         {
00613             tipName = poseState.name[i];
00614 
00615             try
00616             {
00617                 PoseStateToFrameVel(poseState, base, tipName, frameVel );
00618                 framevels[tipName] = frameVel;
00619             }
00620             catch (std::exception& e)
00621             {
00622                 NasaCommonLogging::Logger::getCategory("gov.nasa.controllers.PoseStateToFrameVelMap") << log4cpp::Priority::WARN << "Squelched error " << e.what() << ".. continuing..";
00623             }
00624         }
00625     }
00626 
00632     void PoseStateToFrameAccMap(const r2_msgs::PoseState& poseState, const std::string& base, std::map<std::string, KDL::FrameAcc>& frameaccs)
00633     {
00634         KDL::FrameAcc frameAcc;
00635         std::string   tipName;
00636 
00637         if (frameaccs.size() != poseState.name.size())
00638         {
00639             NasaCommonLogging::Logger::getCategory("gov.nasa.controllers.PoseStateToFrameAccMap") << log4cpp::Priority::DEBUG << "resizing frameaccs";
00640             frameaccs.clear();
00641 
00642             for (unsigned int i = 0; i < poseState.name.size(); ++i)
00643             {
00644                 frameaccs.insert(std::make_pair(poseState.name[i], KDL::FrameAcc()));
00645             }
00646         }
00647 
00648         for (unsigned int i = 0; i < poseState.name.size(); ++i)
00649         {
00650             tipName = poseState.name[i];
00651 
00652             try
00653             {
00654                 RosMsgConverter::PoseStateToFrameAcc(poseState, base, tipName, frameAcc );
00655                 frameaccs[tipName] = frameAcc;
00656             }
00657             catch (std::exception& e)
00658             {
00659                 NasaCommonLogging::Logger::getCategory("gov.nasa.controllers.PoseStateToFrameAccMap") << log4cpp::Priority::WARN << "Squelched error " << e.what() << ".. continuing..";
00660             }
00661         }
00662     }
00663 
00668     void JointStateToJntMap(const sensor_msgs::JointState& jointState, std::map<std::string, double>& jointMap)
00669     {
00670         if (jointState.name.size() != jointState.position.size())
00671         {
00672             // length mismatch
00673             std::stringstream err;
00674             err << "jointState name length doesn't match positions length: " << jointState.name.size() << " vs " << jointState.position.size();
00675             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJntMap", log4cpp::Priority::ERROR, err.str());
00676             throw std::logic_error(err.str());
00677             return;
00678         }
00679 
00680         if (jointMap.empty())
00681         {
00683             for (unsigned int i = 0; i < jointState.name.size(); ++i)
00684             {
00685                 jointMap[jointState.name[i]] = jointState.position[i];
00686             }
00687         }
00688         else
00689         {
00691             std::map<std::string, double>::iterator jmIt;
00692 
00693             for (unsigned int i = 0; i < jointState.name.size(); ++i)
00694             {
00695                 jmIt = jointMap.find(jointState.name[i]);
00696 
00697                 if (jmIt != jointMap.end())
00698                 {
00699                     jmIt->second = jointState.position[i];
00700                 }
00701             }
00702         }
00703     }
00704 
00709     void JointStateToJointVelMap(const sensor_msgs::JointState& jointState, std::map<std::string, JointVel>& jointMap)
00710     {
00711         if (jointState.name.size() != jointState.velocity.size())
00712         {
00713             // length mismatch
00714             std::stringstream err;
00715             err << "jointState name length doesn't match velocity length: " << jointState.name.size() << " vs " << jointState.velocity.size();
00716             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJointVelMap", log4cpp::Priority::ERROR, err.str());
00717             throw std::logic_error(err.str());
00718             return;
00719         }
00720 
00721         if (jointState.name.size() != jointState.position.size())
00722         {
00723             // length mismatch
00724             std::stringstream err;
00725             err << "jointState name length doesn't match positions length: " << jointState.name.size() << " vs " << jointState.position.size();
00726             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJointVelMap", log4cpp::Priority::ERROR, err.str());
00727             throw std::logic_error(err.str());
00728             return;
00729         }
00730 
00731         if (jointMap.empty())
00732         {
00734             for (unsigned int i = 0; i < jointState.name.size(); ++i)
00735             {
00736                 jointMap[jointState.name[i]] = (JointVel)
00737                 {
00738                     jointState.position[i], jointState.velocity[i]
00739                 };
00740             }
00741         }
00742         else
00743         {
00745             std::map<std::string, JointVel>::iterator jmIt;
00746 
00747             for (unsigned int i = 0; i < jointState.name.size(); ++i)
00748             {
00749                 jmIt = jointMap.find(jointState.name[i]);
00750 
00751                 if (jmIt != jointMap.end())
00752                 {
00753                     jmIt->second = (JointVel)
00754                     {
00755                         jointState.position[i], jointState.velocity[i]
00756                     };
00757                 }
00758             }
00759         }
00760     }
00761 
00766     void JointStateToJointAccMap(const sensor_msgs::JointState& jointState, std::map<std::string, JointAcc>& jointMap)
00767     {
00768         if (jointState.name.size() != jointState.effort.size())
00769         {
00770             // length mismatch
00771             std::stringstream err;
00772             err << "jointState name length doesn't match effort length: " << jointState.name.size() << " vs " << jointState.effort.size();
00773             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJointAccMap", log4cpp::Priority::ERROR, err.str());
00774             throw std::logic_error(err.str());
00775             return;
00776         }
00777 
00778         if (jointState.name.size() != jointState.velocity.size())
00779         {
00780             // length mismatch
00781             std::stringstream err;
00782             err << "jointState name length doesn't match velocity length: " << jointState.name.size() << " vs " << jointState.velocity.size();
00783             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJointAccMap", log4cpp::Priority::ERROR, err.str());
00784             throw std::logic_error(err.str());
00785             return;
00786         }
00787 
00788         if (jointState.name.size() != jointState.position.size())
00789         {
00790             // length mismatch
00791             std::stringstream err;
00792             err << "jointState name length doesn't match positions length: " << jointState.name.size() << " vs " << jointState.position.size();
00793             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointStateToJointAccMap", log4cpp::Priority::ERROR, err.str());
00794             throw std::logic_error(err.str());
00795             return;
00796         }
00797 
00798         if (jointMap.empty())
00799         {
00801             for (unsigned int i = 0; i < jointState.name.size(); ++i)
00802             {
00803                 jointMap[jointState.name[i]] = (JointAcc)
00804                 {
00805                     jointState.position[i], jointState.velocity[i], jointState.effort[i]
00806                 };
00807             }
00808         }
00809         else
00810         {
00812             std::map<std::string, JointAcc>::iterator jmIt;
00813 
00814             for (unsigned int i = 0; i < jointState.name.size(); ++i)
00815             {
00816                 jmIt = jointMap.find(jointState.name[i]);
00817 
00818                 if (jmIt != jointMap.end())
00819                 {
00820                     jmIt->second = (JointAcc)
00821                     {
00822                         jointState.position[i], jointState.velocity[i], jointState.effort[i]
00823                     };
00824                 }
00825             }
00826         }
00827     }
00828 
00829     void JointControlDataToCoeffStateMap(const r2_msgs::JointControlDataArray& jointData,
00830                                          std::map<std::string, r2_msgs::JointControlCoeffState>& jointMap)
00831     {
00832         if (jointData.joint.size() != jointData.data.size())
00833         {
00834             // length mismatch
00835             std::stringstream err;
00836             err << "jointData name length doesn't match data length: " << jointData.joint.size() << " vs " << jointData.data.size();
00837             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointControlDataToCoeffLoadedMap", log4cpp::Priority::ERROR, err.str());
00838             throw std::logic_error(err.str());
00839             return;
00840         }
00841 
00842         if (jointMap.empty())
00843         {
00845             for (unsigned int i = 0; i < jointData.joint.size(); ++i)
00846             {
00847                 jointMap[jointData.joint[i]] = jointData.data[i].coeffState;
00848             }
00849         }
00850         else
00851         {
00853             std::map<std::string, r2_msgs::JointControlCoeffState>::iterator jmIt;
00854 
00855             for (unsigned int i = 0; i < jointData.joint.size(); ++i)
00856             {
00857                 jmIt = jointMap.find(jointData.joint[i]);
00858 
00859                 if (jmIt != jointMap.end())
00860                 {
00861                     jmIt->second = jointData.data[i].coeffState;
00862                 }
00863             }
00864         }
00865     }
00866 
00867     KDL::Wrench wrenchMsgToWrench(const geometry_msgs::Wrench& wrenchMsg)
00868     {
00869         return (KDL::Wrench(KDL::Vector(wrenchMsg.force.x, wrenchMsg.force.y, wrenchMsg.force.z),
00870                             KDL::Vector(wrenchMsg.torque.x, wrenchMsg.torque.y, wrenchMsg.torque.z)));
00871     }
00872 
00873     geometry_msgs::Wrench wrenchToWrenchMsg(const KDL::Wrench& wrench)
00874     {
00875         geometry_msgs::Wrench wrenchOut;
00876         wrenchOut.force.x  = wrench.force.x();
00877         wrenchOut.force.y  = wrench.force.y();
00878         wrenchOut.force.z  = wrench.force.z();
00879         wrenchOut.torque.x = wrench.torque.x();
00880         wrenchOut.torque.y = wrench.torque.y();
00881         wrenchOut.torque.z = wrench.torque.z();
00882         return wrenchOut;
00883     }
00884 
00885     void JointCommandToJointCommandMap(const r2_msgs::JointCommand& jointCommand, std::map<std::string, JointCommand>& jointCommandMap)
00886     {
00887         if (jointCommand.name.size() != jointCommand.desiredPosition.size())
00888         {
00889             // length mismatch
00890             std::stringstream err;
00891             err << "jointCommand name length doesn't match desiredPosition length: " << jointCommand.name.size() << " vs " << jointCommand.desiredPosition.size();
00892             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointCommandToJointCommandMap", log4cpp::Priority::ERROR, err.str());
00893             throw std::logic_error(err.str());
00894             return;
00895         }
00896 
00897         if (jointCommand.name.size() != jointCommand.desiredPositionVelocityLimit.size())
00898         {
00899             // length mismatch
00900             std::stringstream err;
00901             err << "jointCommand name length doesn't match desiredPositionVelocityLimit length: " << jointCommand.name.size() << " vs " << jointCommand.desiredPositionVelocityLimit.size();
00902             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointCommandToJointCommandMap", log4cpp::Priority::ERROR, err.str());
00903             throw std::logic_error(err.str());
00904             return;
00905         }
00906 
00907         if (jointCommand.name.size() != jointCommand.feedForwardTorque.size())
00908         {
00909             // length mismatch
00910             std::stringstream err;
00911             err << "jointCommand name length doesn't match feedForwardTorque length: " << jointCommand.name.size() << " vs " << jointCommand.feedForwardTorque.size();
00912             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointCommandToJointCommandMap", log4cpp::Priority::ERROR, err.str());
00913             throw std::logic_error(err.str());
00914             return;
00915         }
00916 
00917         if (jointCommand.name.size() != jointCommand.proportionalGain.size())
00918         {
00919             // length mismatch
00920             std::stringstream err;
00921             err << "jointCommand name length doesn't match proportionalGain length: " << jointCommand.name.size() << " vs " << jointCommand.proportionalGain.size();
00922             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointCommandToJointCommandMap", log4cpp::Priority::ERROR, err.str());
00923             throw std::logic_error(err.str());
00924             return;
00925         }
00926 
00927         if (jointCommand.name.size() != jointCommand.derivativeGain.size())
00928         {
00929             // length mismatch
00930             std::stringstream err;
00931             err << "jointCommand name length doesn't match derivativeGain length: " << jointCommand.name.size() << " vs " << jointCommand.derivativeGain.size();
00932             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointCommandToJointCommandMap", log4cpp::Priority::ERROR, err.str());
00933             throw std::logic_error(err.str());
00934             return;
00935         }
00936 
00937         if (jointCommand.name.size() != jointCommand.integralGain.size())
00938         {
00939             // length mismatch
00940             std::stringstream err;
00941             err << "jointCommand name length doesn't match integralGain length: " << jointCommand.name.size() << " vs " << jointCommand.integralGain.size();
00942             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointCommandToJointCommandMap", log4cpp::Priority::ERROR, err.str());
00943             throw std::logic_error(err.str());
00944             return;
00945         }
00946 
00947         if (jointCommand.name.size() != jointCommand.positionLoopTorqueLimit.size())
00948         {
00949             // length mismatch
00950             std::stringstream err;
00951             err << "jointCommand name length doesn't match positionLoopTorqueLimit length: " << jointCommand.name.size() << " vs " << jointCommand.positionLoopTorqueLimit.size();
00952             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointCommandToJointCommandMap", log4cpp::Priority::ERROR, err.str());
00953             throw std::logic_error(err.str());
00954             return;
00955         }
00956 
00957         if (jointCommand.name.size() != jointCommand.positionLoopWindupLimit.size())
00958         {
00959             // length mismatch
00960             std::stringstream err;
00961             err << "jointCommand name length doesn't match positionLoopWindupLimit length: " << jointCommand.name.size() << " vs " << jointCommand.positionLoopWindupLimit.size();
00962             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointCommandToJointCommandMap", log4cpp::Priority::ERROR, err.str());
00963             throw std::logic_error(err.str());
00964             return;
00965         }
00966 
00967         if (jointCommand.name.size() != jointCommand.torqueLoopVelocityLimit.size())
00968         {
00969             // length mismatch
00970             std::stringstream err;
00971             err << "jointCommand name length doesn't match torqueLoopVelocityLimit length: " << jointCommand.name.size() << " vs " << jointCommand.torqueLoopVelocityLimit.size();
00972             NasaCommonLogging::Logger::log("gov.nasa.controllers.JointCommandToJointCommandMap", log4cpp::Priority::ERROR, err.str());
00973             throw std::logic_error(err.str());
00974             return;
00975         }
00976 
00977         if (jointCommandMap.empty())
00978         {
00980             for (unsigned int i = 0; i < jointCommand.name.size(); ++i)
00981             {
00982                 jointCommandMap[jointCommand.name[i]] = (JointCommand)
00983                 {
00984                     jointCommand.desiredPosition[i],
00985                                                  jointCommand.desiredPositionVelocityLimit[i],
00986                                                  jointCommand.feedForwardTorque[i],
00987                                                  jointCommand.proportionalGain[i],
00988                                                  jointCommand.derivativeGain[i],
00989                                                  jointCommand.integralGain[i],
00990                                                  jointCommand.positionLoopTorqueLimit[i],
00991                                                  jointCommand.positionLoopWindupLimit[i],
00992                                                  jointCommand.torqueLoopVelocityLimit[i]
00993                 };
00994             }
00995         }
00996         else
00997         {
00999             for (unsigned int i = 0; i < jointCommand.name.size(); ++i)
01000             {
01001                 try
01002                 {
01003                     jointCommandMap.at(jointCommand.name[i]) = (JointCommand)
01004                     {
01005                         jointCommand.desiredPosition[i],
01006                                                      jointCommand.desiredPositionVelocityLimit[i],
01007                                                      jointCommand.feedForwardTorque[i],
01008                                                      jointCommand.proportionalGain[i],
01009                                                      jointCommand.derivativeGain[i],
01010                                                      jointCommand.integralGain[i],
01011                                                      jointCommand.positionLoopTorqueLimit[i],
01012                                                      jointCommand.positionLoopWindupLimit[i],
01013                                                      jointCommand.torqueLoopVelocityLimit[i]
01014                     };
01015                 }
01016                 catch (std::exception& e)
01017                 {
01018                     // not found
01019                     std::stringstream err;
01020                     err << "jointCommandMap doesn't contain joint: " << jointCommand.name[i] << ".. skipping..";
01021                     NasaCommonLogging::Logger::log("gov.nasa.controllers.JointCommandToJointCommandMap", log4cpp::Priority::DEBUG, err.str());
01022                     err.str("");
01023                 }
01024             }
01025         }
01026     }
01027 
01028     void WrenchStateToWrenchMap(const r2_msgs::WrenchState& wrenchState, std::map<std::string, KDL::Wrench>& wrenchMap)
01029     {
01030         if (wrenchState.name.size() != wrenchState.wrench.size())
01031         {
01032             std::stringstream err;
01033             err << "wrenchstate name length doesn't match wrench length: " << wrenchState.name.size() << " vs " << wrenchState.wrench.size();
01034             NasaCommonLogging::Logger::log("gov.nasa.controllers.WrenchStateToWrenchMap", log4cpp::Priority::ERROR, err.str());
01035             throw std::logic_error(err.str());
01036             return;
01037         }
01038 
01039         for (unsigned int i = 0; i < wrenchState.name.size(); ++i)
01040         {
01041             wrenchMap[wrenchState.name.at(i)] = wrenchMsgToWrench(wrenchState.wrench.at(i));
01042         }
01043 
01044     }
01050     void ToolFrameToSegment(const r2_msgs::ToolFrame& toolFrame, std::vector<ToolFrame>& segmentList)
01051     {
01052         if (toolFrame.name.size() != toolFrame.refFrame.size() || toolFrame.name.size() != toolFrame.location.size())
01053         {
01054             // length mismatch
01055             std::stringstream err;
01056             err << "ToolFrameToSegment() - toolFrame name and refFrame and location lengths don't match";
01057             NasaCommonLogging::Logger::log("gov.nasa.controllers.ToolFrameToSegment", log4cpp::Priority::ERROR, err.str());
01058             throw std::logic_error(err.str());
01059             return;
01060         }
01061 
01062         for (unsigned int i = 0; i < toolFrame.name.size(); ++i)
01063         {
01064             std::string _name = toolFrame.name[i];
01065             geometry_msgs::Pose loc = toolFrame.location[i];
01066             KDL::Rotation rotation = KDL::Rotation::Quaternion(loc.orientation.x, loc.orientation.y, loc.orientation.z, loc.orientation.w);
01067             KDL::Vector position = KDL::Vector(loc.position.x, loc.position.y, loc.position.z);
01068             KDL::Frame _f_tip(rotation, position);
01069             double mass = toolFrame.toolMass[i];
01070             KDL::Vector cog(toolFrame.toolCog[i].x, toolFrame.toolCog[i].y, toolFrame.toolCog[i].z);
01071             KDL::RotationalInertia inertia(toolFrame.toolInertia[i].ixx, toolFrame.toolInertia[i].iyy, toolFrame.toolInertia[i].izz, toolFrame.toolInertia[i].ixy, toolFrame.toolInertia[i].ixz, toolFrame.toolInertia[i].iyz);
01072             KDL::Segment segment(_name, KDL::Joint(KDL::Joint::None), _f_tip, KDL::RigidBodyInertia(mass, cog, inertia));
01073             segmentList.push_back((ToolFrame) {toolFrame.name[i], toolFrame.refFrame[i], segment});
01074         }
01075     }
01076 
01077     void ToolFrameToSegmentAdd(const r2_msgs::ToolFrame& toolFrame, std::vector<ToolFrame>& segmentList, std::vector<ToolFrame>& addSegmentList)
01078     {
01079         if (toolFrame.name.size() != toolFrame.refFrame.size() || toolFrame.name.size() != toolFrame.location.size())
01080         {
01081             // length mismatch
01082             std::stringstream err;
01083             err << "ToolFrameToSegmentAdd() - toolFrame name and refFrame and location lengths don't match";
01084             NasaCommonLogging::Logger::log("gov.nasa.controllers.ToolFrameToSegmentAdd", log4cpp::Priority::ERROR, err.str());
01085             throw std::logic_error(err.str());
01086             return;
01087         }
01088 
01089         for (unsigned int i = 0; i < toolFrame.name.size(); ++i)
01090         {
01091             std::string _name = toolFrame.name[i];
01092             geometry_msgs::Pose loc = toolFrame.location[i];
01093             KDL::Rotation rotation = KDL::Rotation::Quaternion(loc.orientation.x, loc.orientation.y, loc.orientation.z, loc.orientation.w);
01094             KDL::Vector position = KDL::Vector(loc.position.x, loc.position.y, loc.position.z);
01095             KDL::Frame _f_tip(rotation, position);
01096             double mass = toolFrame.toolMass[i];
01097             KDL::Vector cog(toolFrame.toolCog[i].x, toolFrame.toolCog[i].y, toolFrame.toolCog[i].z);
01098             KDL::RotationalInertia inertia(toolFrame.toolInertia[i].ixx, toolFrame.toolInertia[i].iyy, toolFrame.toolInertia[i].izz, toolFrame.toolInertia[i].ixy, toolFrame.toolInertia[i].ixz, toolFrame.toolInertia[i].iyz);
01099             KDL::Segment segment(_name, KDL::Joint(KDL::Joint::None), _f_tip, KDL::RigidBodyInertia(mass, cog, inertia));
01100             segmentList.push_back((ToolFrame) {toolFrame.name[i], toolFrame.refFrame[i], segment});
01101             addSegmentList.push_back((ToolFrame) {toolFrame.name[i], toolFrame.refFrame[i], segment});
01102         }
01103     }
01104 
01105     void ToolFrameToSegmentRemove(const r2_msgs::ToolFrame& toolFrame, std::vector<ToolFrame>& removeSegmentList)
01106     {
01107         if (toolFrame.name.size() != toolFrame.refFrame.size() || toolFrame.name.size() != toolFrame.location.size())
01108         {
01109             // length mismatch
01110             std::stringstream err;
01111             err << "ToolFrameToSegmentRemove() - toolFrame name and refFrame and location lengths don't match";
01112             NasaCommonLogging::Logger::log("gov.nasa.controllers.ToolFrameToSegmentAdd", log4cpp::Priority::ERROR, err.str());
01113             throw std::logic_error(err.str());
01114             return;
01115         }
01116 
01117         for (unsigned int i = 0; i < toolFrame.name.size(); ++i)
01118         {
01119             std::string _name = toolFrame.name[i];
01120             geometry_msgs::Pose loc = toolFrame.location[i];
01121             KDL::Rotation rotation = KDL::Rotation::Quaternion(loc.orientation.x, loc.orientation.y, loc.orientation.z, loc.orientation.w);
01122             KDL::Vector position = KDL::Vector(loc.position.x, loc.position.y, loc.position.z);
01123             KDL::Frame _f_tip(rotation, position);
01124             double mass = toolFrame.toolMass[i];
01125             KDL::Vector cog(toolFrame.toolCog[i].x, toolFrame.toolCog[i].y, toolFrame.toolCog[i].z);
01126             KDL::RotationalInertia inertia(toolFrame.toolInertia[i].ixx, toolFrame.toolInertia[i].iyy, toolFrame.toolInertia[i].izz, toolFrame.toolInertia[i].ixy, toolFrame.toolInertia[i].ixz, toolFrame.toolInertia[i].iyz);
01127             KDL::Segment segment(_name, KDL::Joint(KDL::Joint::None), _f_tip, KDL::RigidBodyInertia(mass, cog, inertia));
01128             removeSegmentList.push_back((ToolFrame) {toolFrame.name[i], toolFrame.refFrame[i], segment});
01129         }
01130     }
01131 
01132 }
01133 


robodyn_utilities
Author(s):
autogenerated on Thu Jun 6 2019 18:56:07