ArmComponentsNameManager.cpp
Go to the documentation of this file.
00001 #ifdef DOXYGEN_SHOULD_SKIP_THIS
00002 
00021 #endif
00022 
00023 
00024 #include <arm_components_name_manager/ArmComponentsNameManager.h>
00025 #include <map>
00026 #include <string>
00027 #include <vector>
00028 
00029 using arm_components_name_manager::ArmComponentsNameManager;
00030 
00031 ArmComponentsNameManager::ArmComponentsNameManager(const std::string& _robot_namespace,
00032         bool readParams):
00033     robot_namespace(_robot_namespace),
00034     initParamCode(-1),
00035     initWithDefaults(false)
00036 {
00037     // get joint names from parameter server
00038     if (readParams) loadParameters();
00039 }
00040 
00041 ArmComponentsNameManager::ArmComponentsNameManager(const ArmComponentsNameManager& o):
00042     palm_link(o.palm_link),
00043     effector_link(o.effector_link),
00044     arm_joints(o.arm_joints),
00045     arm_links(o.arm_links),
00046     gripper_joints(o.gripper_joints),
00047     gripper_links(o.gripper_links),
00048     arm_joint_init(o.arm_joint_init),
00049     gripper_joint_init(o.gripper_joint_init),
00050     arm_effort_controller_names(o.arm_effort_controller_names),
00051     arm_velocity_controller_names(o.arm_velocity_controller_names),
00052     arm_position_controller_names(o.arm_position_controller_names),
00053     gripper_effort_controller_names(o.gripper_effort_controller_names),
00054     gripper_velocity_controller_names(o.gripper_velocity_controller_names),
00055     gripper_position_controller_names(o.gripper_position_controller_names),
00056     initParamCode(o.initParamCode),
00057     initWithDefaults(o.initWithDefaults)
00058 {
00059 }
00060 
00061 bool ArmComponentsNameManager::loadDefaults()
00062 {
00063     if (!hasDefaults()) return false;
00064     palm_link = getDefaultPalmLink();
00065     effector_link = getDefaultEffectorLink();
00066     arm_joints = getDefaultArmJoints();
00067     arm_links = getDefaultArmLinks();
00068     arm_joint_init = getDefaultArmJointsInitPose();
00069     gripper_joints = getDefaultGripperJoints();
00070     gripper_links = getDefaultGripperLinks();
00071     gripper_joint_init = getDefaultGripperJointsInitPose();
00072     initWithDefaults = true;
00073     return true;
00074 }
00075 
00076 
00077 
00078 int ArmComponentsNameManager::loadParameters(bool printErrors, bool verbose)
00079 {
00080     bool allControllersSpecified = true;
00081     int noSpec = 0;
00082     int numSpecs = 0;
00083 
00084     ros::NodeHandle robot_nh(robot_namespace);
00085     ROS_INFO_STREAM("ArmComponentsNameManager reading parameters from namespace: " << robot_nh.getNamespace());
00086 
00087     robot_nh.getParam("palm_link", palm_link);
00088     if (palm_link.empty())
00089     {
00090         ++noSpec;
00091         if (printErrors) ROS_ERROR("Parameter palm_link should be specified");
00092     }
00093     ++numSpecs;
00094     if (verbose) ROS_INFO_STREAM("Palm link: "<<palm_link);
00095 
00096     robot_nh.getParam("effector_link", effector_link);
00097     if (effector_link.empty())
00098     {
00099         ROS_INFO("INFO: effector_link not specified, defaulting to same as palm_link");
00100         effector_link = palm_link;
00101     }
00102     if (verbose) ROS_INFO_STREAM("Effector link: "<<effector_link);
00103 
00104     // --- arm parameters
00105 
00106     if (verbose) ROS_INFO_STREAM("Reading arm_joints:");
00107     robot_nh.getParam("arm_joints", arm_joints);
00108     if (arm_joints.empty())
00109     {
00110         ++noSpec;
00111         if (printErrors) ROS_ERROR("Parameter arm_joints should be specified as an array");
00112     }
00113     ++numSpecs;
00114     if (verbose) for (int i=0; i < arm_joints.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << arm_joints[i]);}
00115 
00116     if (verbose) ROS_INFO_STREAM("Reading arm_joint_init:");
00117     robot_nh.getParam("arm_joint_init", arm_joint_init);
00118     if (arm_joint_init.empty())
00119     {
00120         ++noSpec;
00121         if (printErrors) ROS_ERROR("Parameter arm_joint_init should be specified as an array");
00122     }
00123     ++numSpecs;
00124     if (verbose) for (int i=0; i < arm_joint_init.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << arm_joint_init[i]);}
00125 
00126     if (verbose) ROS_INFO_STREAM("Reading arm_joint_max_vel:");
00127     robot_nh.getParam("arm_joint_max_vel", arm_joint_max_vel);
00128     if (arm_joint_max_vel.empty())
00129     {
00130         ++noSpec;
00131         if (printErrors) ROS_ERROR("Parameter arm_joint_max_vel should be specified as an array");
00132     }
00133     ++numSpecs;
00134     if (verbose) for (int i=0; i < arm_joint_max_vel.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << arm_joint_max_vel[i]);}
00135 
00136     if (verbose) ROS_INFO_STREAM("Reading arm_joint_max_force:");
00137     robot_nh.getParam("arm_joint_max_force", arm_joint_max_force);
00138     if (arm_joint_max_force.empty())
00139     {
00140         ++noSpec;
00141         if (printErrors) ROS_ERROR("Parameter arm_joint_max_force should be specified as an array");
00142     }
00143     ++numSpecs;
00144     if (verbose) for (int i=0; i < arm_joint_max_force.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << arm_joint_max_force[i]);}
00145 
00146     if (verbose) ROS_INFO_STREAM("Reading arm_links:");
00147     robot_nh.getParam("arm_links", arm_links);
00148     if (arm_links.empty())
00149     {
00150         ++noSpec;
00151         if (printErrors) ROS_ERROR("Parameter arm_links should be specified as an array");
00152     }
00153     ++numSpecs;
00154     if (verbose) for (int i=0; i < arm_links.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << arm_links[i]);}
00155 
00156     // controllers
00157 
00158     if (verbose) ROS_INFO_STREAM("Reading arm_position_controller_names:");
00159     robot_nh.getParam("arm_position_controller_names", arm_position_controller_names);
00160     if (arm_position_controller_names.empty())
00161     {
00162         allControllersSpecified = false;
00163         ROS_INFO("INFO: Parameter arm_position_controller_names has not been specified");
00164     }
00165     if (verbose) for (int i=0; i < arm_position_controller_names.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << arm_position_controller_names[i]);}
00166 
00167     if (verbose) ROS_INFO_STREAM("Reading arm_velocity_controller_names:");
00168     robot_nh.getParam("arm_velocity_controller_names", arm_velocity_controller_names);
00169     if (arm_velocity_controller_names.empty())
00170     {
00171         allControllersSpecified = false;
00172         ROS_INFO("INFO: Parameter arm_velocity_controller_names has not been specified");
00173     }
00174     if (verbose) for (int i=0; i < arm_velocity_controller_names.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << arm_velocity_controller_names[i]);}
00175 
00176     if (verbose) ROS_INFO_STREAM("Reading arm_effort_controller_names:");
00177     robot_nh.getParam("arm_effort_controller_names", arm_effort_controller_names);
00178     if (arm_effort_controller_names.empty())
00179     {
00180         allControllersSpecified = false;
00181         ROS_INFO("INFO: Parameter arm_effort_controller_names has not been specified");
00182     }
00183     if (verbose) for (int i=0; i < arm_effort_controller_names.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << arm_effort_controller_names[i]);}
00184 
00185     // --- gripper parameters
00186 
00187     if (verbose) ROS_INFO_STREAM("Reading gripper_joints:");
00188     robot_nh.getParam("gripper_joints", gripper_joints);
00189     if (gripper_joints.empty())
00190     {
00191         // ++noSpec;
00192         ROS_WARN("Parameter gripper_joints has not been specified");
00193     }
00194     // ++numSpecs;
00195     if (verbose) for (int i=0; i < gripper_joints.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << gripper_joints[i]);}
00196 
00197     if (verbose) ROS_INFO_STREAM("Reading gripper_joint_init:");
00198     robot_nh.getParam("gripper_joint_init", gripper_joint_init);
00199     if (gripper_joint_init.empty())
00200     {
00201         // ++noSpec;
00202         ROS_WARN("Parameter gripper_joint_init has not been specified");
00203     }
00204     // ++numSpecs;
00205     if (verbose) for (int i=0; i < gripper_joint_init.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << gripper_joint_init[i]);}
00206 
00207     if (verbose) ROS_INFO_STREAM("Reading gripper_joint_max_vel:");
00208     robot_nh.getParam("gripper_joint_max_vel", gripper_joint_max_vel);
00209     if (gripper_joint_max_vel.empty())
00210     {
00211         // ++noSpec;
00212         ROS_WARN("Parameter gripper_joint_max_vel has not been specified");
00213     }
00214     // ++numSpecs;
00215     if (verbose) for (int i=0; i < gripper_joint_max_vel.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << gripper_joint_max_vel[i]);}
00216 
00217     if (verbose) ROS_INFO_STREAM("Reading gripper_joint_max_force:");
00218     robot_nh.getParam("gripper_joint_max_force", gripper_joint_max_force);
00219     if (gripper_joint_max_force.empty())
00220     {
00221         // ++noSpec;
00222         ROS_WARN("Parameter gripper_joint_max_force has not been specified");
00223     }
00224     // ++numSpecs;
00225     if (verbose) for (int i=0; i < gripper_joint_max_force.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << gripper_joint_max_force[i]);}
00226 
00227     if (verbose) ROS_INFO_STREAM("Reading gripper_links:");
00228     robot_nh.getParam("gripper_links", gripper_links);
00229     if (gripper_links.empty())
00230     {
00231         // ++noSpec;
00232         ROS_WARN("Parameter gripper_links has not been specified");
00233     }
00234     // ++numSpecs;
00235     if (verbose) for (int i=0; i < gripper_links.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << gripper_links[i]);}
00236 
00237     // controllers
00238 
00239     if (verbose) ROS_INFO_STREAM("Reading gripper_position_controller_names:");
00240     robot_nh.getParam("gripper_position_controller_names", gripper_position_controller_names);
00241     if (gripper_position_controller_names.empty())
00242     {
00243         allControllersSpecified = false;
00244         ROS_INFO("INFO: Parameter gripper_position_controller_names has not been specified");
00245     }
00246     if (verbose) for (int i=0; i < gripper_position_controller_names.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << gripper_position_controller_names[i]);}
00247 
00248     if (verbose) ROS_INFO_STREAM("Reading gripper_velocity_controller_names:");
00249     robot_nh.getParam("gripper_velocity_controller_names", gripper_velocity_controller_names);
00250     if (gripper_velocity_controller_names.empty())
00251     {
00252         allControllersSpecified = false;
00253         ROS_INFO("INFO: Parameter gripper_velocity_controller_names has not been specified");
00254     }
00255     if (verbose) for (int i=0; i < gripper_velocity_controller_names.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << gripper_velocity_controller_names[i]);}
00256 
00257     if (verbose) ROS_INFO_STREAM("Reading gripper_effort_controller_names:");
00258     robot_nh.getParam("gripper_effort_controller_names", gripper_effort_controller_names);
00259     if (gripper_effort_controller_names.empty())
00260     {
00261         allControllersSpecified = false;
00262         ROS_INFO("INFO: Parameter gripper_effort_controller_names has not been specified");
00263     }
00264     if (verbose) for (int i=0; i < gripper_effort_controller_names.size(); ++i) { ROS_INFO_STREAM("idx " << i << ": " << gripper_effort_controller_names[i]);}
00265 
00266     initParamCode = 0;
00267     if (noSpec == numSpecs) initParamCode =  -1;
00268     if ((noSpec == 0) && allControllersSpecified) initParamCode =  2;
00269     else if (noSpec == 0) initParamCode =  1;
00270     return initParamCode;
00271 }
00272 
00273 
00274 bool ArmComponentsNameManager::waitToLoadParameters(int sufficientSuccessCode, float maxWait, float waitStep)
00275 {
00276     float timeWaited = 0;
00277     while (timeWaited < maxWait)
00278     {
00279         int loadParamRet = loadParameters(false);
00280         if (loadParamRet >= sufficientSuccessCode)
00281             return true;
00282         ROS_INFO("ArmComponentsNameManager: wait for ROS parameters to be loaded....");
00283         ros::Duration(waitStep).sleep();
00284         timeWaited += waitStep;
00285     }
00286     return false;
00287 }
00288 
00289 void ArmComponentsNameManager::ReadPIDValues(const std::string& pidParameterName, float& kp, float& ki, float& kd) const
00290 {
00291     static const std::string pid_param_name = "pid";
00292 
00293     std::map<std::string, float> pid;
00294     ros::NodeHandle pub("");
00295     if (!pub.getParam(robot_namespace + "/" + pidParameterName + "/" + pid_param_name, pid))
00296     {
00297         ROS_WARN_STREAM(pidParameterName << " was not on parameter server. Keeping default values.");
00298     }
00299     else
00300     {
00301         kp = pid["p"];
00302         ki = pid["i"];
00303         kd = pid["d"];
00304     }
00305 }
00306 
00307 bool ArmComponentsNameManager::GetPosGains(const std::string& jointName, float& kp, float& ki, float& kd) const
00308 {
00309     std::vector<std::string>::const_iterator jnt = std::find(arm_joints.begin(), arm_joints.end(), jointName);
00310     if (jnt == arm_joints.end())
00311     {
00312         jnt = std::find(gripper_joints.begin(), gripper_joints.end(), jointName);
00313         if (jnt == gripper_joints.end())
00314         {
00315             ROS_ERROR_STREAM("ArmComponentsNameManager does not maintain joint name '" << jointName << "'");
00316             return false;
00317         }
00318         int idx = jnt - gripper_joints.begin();
00319         if (gripper_position_controller_names.size() <= idx)
00320         {
00321             ROS_ERROR_STREAM("ArmComponentsNameManager does have the name for position controller '" << jointName << "'");
00322             return false;
00323         }
00324         ReadPIDValues(gripper_position_controller_names[idx], kp, ki, kd);
00325     }
00326     else
00327     {
00328         int idx = jnt - arm_joints.begin();
00329         if (arm_position_controller_names.size() <= idx)
00330         {
00331             ROS_ERROR_STREAM("ArmComponentsNameManager does have the name for position controller '" << jointName << "'");
00332             return false;
00333         }
00334         ReadPIDValues(arm_position_controller_names[idx], kp, ki, kd);
00335     }
00336     return true;
00337 }
00338 
00339 bool ArmComponentsNameManager::GetVelGains(const std::string& jointName, float& kp, float& ki, float& kd) const
00340 {
00341     std::vector<std::string>::const_iterator jnt = std::find(arm_joints.begin(), arm_joints.end(), jointName);
00342     if (jnt == arm_joints.end())
00343     {
00344         jnt = std::find(gripper_joints.begin(), gripper_joints.end(), jointName);
00345         if (jnt == gripper_joints.end())
00346         {
00347             ROS_ERROR_STREAM("ArmComponentsNameManager does not maintain joint name '" << jointName << "'");
00348             return false;
00349         }
00350         int idx = jnt - gripper_joints.begin();
00351         if (gripper_velocity_controller_names.size() <= idx)
00352         {
00353             ROS_ERROR_STREAM("ArmComponentsNameManager does have the name for velocity controller '" << jointName << "'");
00354             return false;
00355         }
00356         ReadPIDValues(gripper_velocity_controller_names[idx], kp, ki, kd);
00357     }
00358     else
00359     {
00360         int idx = jnt - arm_joints.begin();
00361         if (arm_velocity_controller_names.size() <= idx)
00362         {
00363             ROS_ERROR_STREAM("ArmComponentsNameManager does have the name for velocity controller '" << jointName << "'");
00364             return false;
00365         }
00366         ReadPIDValues(arm_velocity_controller_names[idx], kp, ki, kd);
00367     }
00368     return true;
00369 }
00370 
00371 bool ArmComponentsNameManager::GetMaxVals(const std::string& jointName, float& force, float& velocity) const
00372 {
00373     std::vector<std::string>::const_iterator jnt = std::find(arm_joints.begin(), arm_joints.end(), jointName);
00374     if (jnt == arm_joints.end())
00375     {
00376         jnt = std::find(gripper_joints.begin(), gripper_joints.end(), jointName);
00377         if (jnt == gripper_joints.end())
00378         {
00379             ROS_ERROR_STREAM("ArmComponentsNameManager does not maintain joint name '" << jointName << "'");
00380             return false;
00381         }
00382         int idx = jnt - gripper_joints.begin();
00383         if ((gripper_joint_max_vel.size() <= idx) || (gripper_joint_max_force.size() <= idx))
00384         {
00385             ROS_ERROR_STREAM("ArmComponentsNameManager does not have all max values for '" << jointName << "'. Will use 0 instead");
00386             velocity = 0;
00387             force = 0;
00388             return true;
00389         }
00390         velocity=gripper_joint_max_vel[idx];
00391         force=arm_joint_max_force[idx];
00392     }
00393     else
00394     {
00395         int idx = jnt - arm_joints.begin();
00396         if ((arm_joint_max_vel.size() <= idx) || (arm_joint_max_force.size() <= idx))
00397         {
00398             ROS_WARN_STREAM("ArmComponentsNameManager does not have all max values for '" << jointName << "'. Will use 0 instead");
00399             velocity = 0;
00400             force = 0;
00401             return true;
00402         }
00403         velocity=arm_joint_max_vel[idx];
00404         force=arm_joint_max_force[idx];
00405     }
00406     return true;
00407 }
00408 
00409 
00410 
00411 void ArmComponentsNameManager::getJointNames(std::vector<std::string>& joint_names, bool withGripper, const std::string& prepend) const
00412 {
00413     joint_names.insert(joint_names.begin(), arm_joints.begin(), arm_joints.end());
00414     if (!withGripper) return;
00415     joint_names.insert(joint_names.end(), gripper_joints.begin(), gripper_joints.end());
00416     if (!prepend.empty())
00417     {
00418         for (std::vector<std::string>::iterator it = joint_names.begin();
00419                 it != joint_names.end(); ++it)
00420         {
00421             *it = prepend + *it;
00422         }
00423     }
00424 }
00425 
00426 const std::vector<std::string>& ArmComponentsNameManager::getGripperLinks() const
00427 {
00428     return gripper_links;
00429 }
00430 
00431 const std::string& ArmComponentsNameManager::getPalmLink() const
00432 {
00433     return palm_link;
00434 }
00435 
00436 const std::string& ArmComponentsNameManager::getEffectorLink() const
00437 {
00438     return effector_link;
00439 }
00440 
00441 const std::vector<std::string>& ArmComponentsNameManager::getArmLinks() const
00442 {
00443     return arm_links;
00444 }
00445 
00446 
00447 const std::vector<std::string>& ArmComponentsNameManager::getArmJoints() const
00448 {
00449     return arm_joints;
00450 }
00451 const std::vector<std::string>& ArmComponentsNameManager::getGripperJoints() const
00452 {
00453     return gripper_joints;
00454 }
00455 
00456 const std::vector<float>& ArmComponentsNameManager::getArmJointsInitPose() const
00457 {
00458     return arm_joint_init;
00459 }
00460 const std::vector<float>& ArmComponentsNameManager::getGripperJointsInitPose() const
00461 {
00462     return gripper_joint_init;
00463 }
00464 
00465 
00466 /*void ArmComponentsNameManager::initJointState(sensor_msgs::JointState& js, bool withGripper, const std::vector<float> * init_poses) const
00467 {
00468     getJointNames(js.name, withGripper);
00469     int num = arm_joints.size() + gripper_joints.size();
00470     if (!withGripper) num = arm_joints.size();
00471     js.position.resize(num, 0);
00472     js.velocity.resize(num, 0);
00473     js.effort.resize(num, 0);
00474     if (init_poses)
00475     {
00476         for (int i = 0; i < num; ++i)
00477         {
00478             js.position[i] = (*init_poses)[i];
00479         }
00480     }
00481 }*/
00482 
00483 
00484 void ArmComponentsNameManager::copyToJointState(sensor_msgs::JointState& js, int mode, const std::vector<float> * init_vals, int copyData, bool resetAll) const
00485 {
00486     // 0 = all joints; 1 = only arm joints; 2 = only gripper joints.
00487         
00488     js.name.clear();
00489     int size = -1;
00490     if (mode == 0)
00491     {
00492         size = arm_joints.size() + gripper_joints.size();
00493         getJointNames(js.name, true);
00494     }
00495     else if (mode == 1)
00496     {
00497         size = arm_joints.size();
00498         js.name = arm_joints;
00499     }
00500     else if (mode == 2)
00501     {
00502         size = gripper_joints.size();
00503         js.name = gripper_joints;
00504     }
00505        
00506     if (resetAll)
00507     {
00508         js.position.resize(size, 0);
00509         js.velocity.resize(size, 0);
00510         js.effort.resize(size, 0);
00511     }
00512     if (init_vals)
00513     {
00514         if (copyData == 0) js.position.resize(size, 0);
00515         else if (copyData == 1) js.velocity.resize(size, 0);
00516         else if (copyData == 2) js.effort.resize(size, 0);
00517         for (int i = 0; i < size; ++i)
00518         {
00519             if (copyData == 0) js.position[i] = (*init_vals)[i];
00520             else if (copyData == 1) js.velocity[i] = (*init_vals)[i];
00521             else if (copyData == 2) js.effort[i] = (*init_vals)[i];
00522         }
00523     }
00524 }
00525 
00526 bool ArmComponentsNameManager::extractFromJointState(const sensor_msgs::JointState& js,
00527     int mode, std::vector<float>& data, int extractData) const
00528 {
00529     if ((mode < 0) || mode > 2)
00530     {
00531         ROS_ERROR("Consistency: mode has to be in the range 0..2");
00532         return false;
00533     }
00534 
00535     std::vector<int> joint_indices;
00536     int idx = getJointIndices(js.name, joint_indices);
00537     if (idx < 0)
00538     {
00539         ROS_ERROR("Could not obtain indices of the arm joints in joint state");
00540         return false;
00541     }
00542 
00543     if ((idx != 0) && (idx != mode))
00544     {   // joint states does not contain all joints, and
00545         // it does not contain either arm or gripper
00546         ROS_ERROR_STREAM("Joint state does not contain the information requested with mode "
00547             << mode <<" (return code was " << idx << ")");
00548         return false;
00549     }
00550 
00551     if (joint_indices.size() != (arm_joints.size() + gripper_joints.size()))
00552     {
00553         ROS_ERROR("Inconsistency: joint_indices should be same size as all arm joints");
00554         return false;
00555     }   
00556  
00557     int startIt = 0;
00558     int endIt = arm_joints.size() + gripper_joints.size();
00559     // all joints in joint state, but only sub-state desired
00560     if (mode == 1)  // get arm
00561     {
00562         startIt = 0;
00563         endIt = arm_joints.size();
00564     } else if (mode == 2) {  // get gripper
00565         startIt = arm_joints.size(); 
00566         endIt = startIt + gripper_joints.size();
00567     }
00568 
00569     data.clear();
00570     for (int i = startIt; i < endIt; ++i)
00571     {
00572         if (joint_indices[i] < 0)
00573         {
00574             ROS_ERROR_STREAM("Joint " << i << " was not in joint state");
00575             return false;
00576         }
00577         if (i >= joint_indices.size())
00578         {
00579             ROS_ERROR_STREAM("Inconsistency: joint indices are not of expected size: is "
00580                 <<joint_indices.size()<<", trying to index "<<i);
00581             return false;
00582         }
00583         if (js.position.size() <= joint_indices[i])
00584         {
00585             ROS_ERROR_STREAM("Inconsistency: JointState position vector was not of same size as names: "
00586                 <<js.position.size()<<" idx="<<joint_indices[i] << " i = "<<i);
00587             return false;
00588         }
00589         if (extractData == 0) data.push_back(js.position[joint_indices[i]]);
00590         else if (extractData == 1) data.push_back(js.velocity[joint_indices[i]]);
00591         else if (extractData == 2) data.push_back(js.effort[joint_indices[i]]);
00592     }
00593     return true;
00594 }
00595     
00596 bool ArmComponentsNameManager::extractFromJointState(const sensor_msgs::JointState& js, int mode, sensor_msgs::JointState& result) const
00597 {
00598     std::vector<float> pos, vel, eff;
00599     if (!extractFromJointState(js,mode,pos,0)) return false;
00600     if (!extractFromJointState(js,mode,vel,1)) return false;
00601     if (!extractFromJointState(js,mode,eff,2)) return false;
00602     copyToJointState(result, mode, &pos, 0, false);
00603     copyToJointState(result, mode, &vel, 1, false);
00604     copyToJointState(result, mode, &eff, 2, false);
00605     return true; 
00606 }
00607 
00608 bool ArmComponentsNameManager::getJointIndices(const std::vector<std::string>& joint_names, std::vector<int>& idx, int mode) const
00609 {
00610     if (mode == 0) return getJointIndices(joint_names, idx) >= 0;
00611     
00612     // mode has to be 1 or 2
00613     if ((mode !=1) && (mode != 2))
00614     {
00615         ROS_ERROR("Consistency: getJointIndices() must be called with mode 0, 1 or 2");
00616         return false;
00617     }
00618 
00619     // get only subset of the indices. Get all indices first,
00620     // and then shrink the returning indices accordingly.
00621 
00622     std::vector<int> allIdx;
00623     int allRet = getJointIndices(joint_names, allIdx);
00624     if (allRet < 0) return false;
00625     if (allRet == mode)
00626     {
00627         idx = allIdx;
00628         return true;
00629     }
00630 
00631     idx.clear();
00632     int numArmJoints = getArmJoints().size();
00633     if (mode == 1)
00634     {   // only arm
00635         idx.insert(idx.begin(),allIdx.begin(), allIdx.begin()+numArmJoints);
00636     }
00637     else
00638     {   // only fingers
00639         idx.insert(idx.begin(),allIdx.begin()+numArmJoints, allIdx.end());
00640     }
00641     return true;
00642     
00643 }
00644 
00645 int ArmComponentsNameManager::getJointIndices(const std::vector<std::string>& joint_names, std::vector<int>& idx) const
00646 {
00647     typedef std::vector<std::string>::const_iterator It;
00648     idx.clear();
00649 
00650     bool armIncomplete = false;
00651     std::vector<int> arm_idx;
00652     for (int i = 0; i < arm_joints.size(); ++i)
00653     {
00654         It jnt = std::find(joint_names.begin(), joint_names.end(), arm_joints[i]);
00655         if (jnt == joint_names.end())
00656         {
00657             armIncomplete = true;
00658             break;
00659         }
00660         arm_idx.push_back(jnt - joint_names.begin());
00661     }
00662 
00663     bool grippersIncomplete = false;
00664     std::vector<int> gripper_idx;
00665     for (int i = 0; i < gripper_joints.size(); ++i)
00666     {
00667         It jnt = std::find(joint_names.begin(), joint_names.end(), gripper_joints[i]);
00668         if (jnt == joint_names.end())
00669         {
00670             grippersIncomplete = true;
00671             break;
00672         }
00673         gripper_idx.push_back(jnt - joint_names.begin());
00674     }
00675 
00676     if (armIncomplete && grippersIncomplete)
00677     {
00678         // ROS_INFO("ArmComponentsNameManager::getJointIndices: Not all joint names present. List:");
00679         // for (std::vector<std::string>::const_iterator it=joint_names.begin(); it!=joint_names.end(); ++it) ROS_INFO("%s",it->c_str());
00680         return -1;
00681     }
00682     if (!armIncomplete)
00683     {
00684         idx.insert(idx.end(), arm_idx.begin(), arm_idx.end());
00685     }
00686     else
00687     {
00688         idx.insert(idx.end(), arm_joints.size(), -1);
00689     }
00690     if (!grippersIncomplete)
00691     {
00692         idx.insert(idx.end(), gripper_idx.begin(), gripper_idx.end());
00693     }
00694     else
00695     {
00696         idx.insert(idx.end(), gripper_joints.size(), -1);
00697     }
00698 
00699     return grippersIncomplete ? 1 : armIncomplete ? 2 : 0;
00700 }
00701 
00702 
00703 bool ArmComponentsNameManager::isGripper(const std::string& name) const
00704 {
00705     std::vector<std::string>::const_iterator it;
00706     for (it = gripper_joints.begin(); it != gripper_joints.end(); ++it)
00707     {
00708         if (*it == name) return true;
00709     }
00710     return false;
00711 }
00712 
00713 int ArmComponentsNameManager::armJointNumber(const std::string& name) const
00714 {
00715     for (int i = 0; i < arm_joints.size(); ++i)
00716     {
00717         const std::string& name_i = arm_joints[i];
00718         if (name_i == name) return i;
00719     }
00720     return -1;
00721 }
00722 
00723 int ArmComponentsNameManager::gripperJointNumber(const std::string& name) const
00724 {
00725     for (int i = 0; i < gripper_joints.size(); ++i)
00726     {
00727         const std::string& name_i = gripper_joints[i];
00728         if (name_i == name) return i;
00729     }
00730     return -1;
00731 }
00732 
00733 int ArmComponentsNameManager::numArmJoints() const
00734 {
00735     return arm_joints.size();
00736 }
00737 
00738 int ArmComponentsNameManager::numGripperJoints() const
00739 {
00740     return gripper_joints.size();
00741 }
00742 
00743 
00744 void ArmComponentsNameManager::setValues(const std::string& _palm_link,
00745         const std::string& _effector_link,
00746         const std::vector<std::string>& _arm_joints, const std::vector<std::string>& _arm_links,
00747         const std::vector<std::string>& _gripper_joints, const std::vector<std::string>& _gripper_links,
00748         const std::vector<float>& _arm_joint_init, const std::vector<float>& _gripper_joint_init)
00749 {
00750     palm_link = _palm_link;
00751     effector_link = _effector_link;
00752     arm_joints = _arm_joints;
00753     arm_links = _arm_links;
00754     gripper_joints = _gripper_joints;
00755     gripper_links = _gripper_links;
00756     arm_joint_init = _arm_joint_init;
00757     gripper_joint_init = _gripper_joint_init;
00758 }
00759 
00760 
00761 void ArmComponentsNameManager::setControllerNames(const std::vector<std::string>& controller_names, bool forArm, int type)
00762 {
00763     if (forArm)
00764     {
00765         if (type == 0) arm_effort_controller_names = controller_names;
00766         else if (type == 1) arm_velocity_controller_names = controller_names;
00767         else if (type == 2) arm_position_controller_names = controller_names;
00768     }
00769     else
00770     {
00771         if (type == 0) gripper_effort_controller_names = controller_names;
00772         else if (type == 1) gripper_velocity_controller_names = controller_names;
00773         else if (type == 2) gripper_position_controller_names = controller_names;
00774     }
00775 }


arm_components_name_manager
Author(s): Jennifer Buehler
autogenerated on Sat Mar 2 2019 03:21:40