Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "tidyup_utils/arm_state.h"
00009 #include <string>
00010
00011 using std::string;
00012 using std::map;
00013
00014 map<string, ArmState> ArmState::states;
00015
00016 const ArmState& ArmState::get(const std::string& armStateParameter, const std::string& armName)
00017 {
00018 string key = armStateParameter + armName;
00019 map<string, ArmState>::iterator it = states.find(key);
00020 if (it == states.end())
00021 {
00022 states.insert(make_pair(key, ArmState(armStateParameter, armName)));
00023 it = states.find(key);
00024 }
00025 return it->second;
00026 }
00027
00028
00029 ArmState::ArmState(const std::string& armStateParameter, const std::string& armName)
00030 : armName(armName)
00031 {
00032
00033 string linkNames = "/hand_description/"+armName+"/arm_joints/";
00034 if (ros::param::has(linkNames))
00035 {
00036 XmlRpc::XmlRpcValue paramList;
00037 ros::param::get(linkNames, paramList);
00038 ROS_ASSERT(paramList.getType() == XmlRpc::XmlRpcValue::TypeArray);
00039 for (int32_t i = 0; i < paramList.size(); ++i)
00040 {
00041 ROS_ASSERT(paramList[i].getType() == XmlRpc::XmlRpcValue::TypeString);
00042 armState.name.push_back(static_cast<string>(paramList[i]));
00043 }
00044 }
00045 else
00046 {
00047 string prefix = armName.substr(0, 1);
00048 armState.name.push_back(prefix+"_shoulder_pan_joint");
00049 armState.name.push_back(prefix+"_shoulder_lift_joint");
00050 armState.name.push_back(prefix+"_upper_arm_roll_joint");
00051 armState.name.push_back(prefix+"_elbow_flex_joint");
00052 armState.name.push_back(prefix+"_forearm_roll_joint");
00053 armState.name.push_back(prefix+"_wrist_flex_joint");
00054 armState.name.push_back(prefix+"_wrist_roll_joint");
00055 }
00056
00057
00058 string param = armStateParameter + armName;
00059 ROS_ASSERT(ros::param::has(param));
00060 XmlRpc::XmlRpcValue paramList;
00061 ros::param::get(param, paramList);
00062 ROS_ASSERT(paramList.getType() == XmlRpc::XmlRpcValue::TypeArray);
00063 for (int32_t i = 0; i < paramList.size(); ++i)
00064 {
00065 ROS_ASSERT(paramList[i].getType() == XmlRpc::XmlRpcValue::TypeDouble);
00066 armState.position.push_back(static_cast<double>(paramList[i]));
00067 }
00068 }
00069
00070 void ArmState::replaceJointPositions(sensor_msgs::JointState& state) const
00071 {
00072 ArmState::replaceJointPositions(state, armState);
00073 }
00074
00075 void ArmState::replaceJointPositions(sensor_msgs::JointState& state, const sensor_msgs::JointState& joints)
00076 {
00077 for (unsigned int i = 0; i < joints.name.size(); i++)
00078 {
00079 string name = joints.name[i];
00080 int index = -1;
00081 for (unsigned int j = 0; j < state.name.size(); j++)
00082 {
00083 if (name.compare(state.name[j]) == 0)
00084 {
00085 index = j;
00086 }
00087 }
00088 if (index != -1)
00089 {
00090 state.position[index] = joints.position[i];
00091 }
00092 }
00093 }
00094
00095
00096
00097