00001 #include "ros/ros.h"
00002 #include "sensor_msgs/JointState.h"
00003
00004 typedef std::map<std::string, double> MapType;
00005 MapType jointpositions;
00006 MapType jointvelocities;
00007
00008 void jsCallback(const sensor_msgs::JointState::ConstPtr& msg)
00009 {
00010 MapType::iterator iter = jointpositions.begin();
00011 for(int i = 0; i < msg->name.size(); i++)
00012 {
00013
00014
00015 iter = jointpositions.find(msg->name[i]);
00016
00017 if (iter != jointpositions.end() )
00018 iter->second = msg->position[i];
00019
00020
00021
00022
00023
00024 }
00025
00026 }
00027
00028 sensor_msgs::JointState getJointStatesMessage()
00029 {
00030 sensor_msgs::JointState msg;
00031 msg.header.stamp = ros::Time::now();
00032 msg.name.resize(jointpositions.size());
00033 msg.position.resize(jointpositions.size());
00034 msg.velocity.resize(jointvelocities.size());
00035 msg.effort.resize(jointvelocities.size());
00036
00037
00038 MapType::const_iterator end = jointpositions.end();
00039 MapType::iterator iter = jointvelocities.begin();
00040 int count = 0;
00041 for (MapType::const_iterator it = jointpositions.begin(); it != end; ++it)
00042 {
00043 msg.name[count] = it->first;
00044 msg.position[count] = it->second;
00045 iter = jointvelocities.find(it->first);
00046
00047 if (iter != jointvelocities.end() )
00048 msg.velocity[count] = iter->second;
00049
00050 msg.effort[count] = 0;
00051
00052 count++;
00053 }
00054 return msg;
00055 }
00056
00057 int main(int argc, char **argv)
00058 {
00059 ros::init(argc, argv, "cob_joint_state_aggregator");
00060 ros::NodeHandle n;
00061
00062 ros::Subscriber sub = n.subscribe("/joint_states", 50, jsCallback);
00063 ros::Publisher topicPub_JointState_ = n.advertise<sensor_msgs::JointState>("/joint_states_combined", 1);
00064
00065 XmlRpc::XmlRpcValue JointName_param_;
00066 std::string JointName;
00067 if (n.hasParam("joint_names"))
00068 {
00069 n.getParam("joint_names", JointName_param_);
00070 }
00071 else
00072 {
00073 ROS_ERROR("Parameter joint_names not set");
00074 }
00075 for (int i = 0; i<JointName_param_.size(); i++ )
00076 {
00077 JointName = (std::string)JointName_param_[i];
00078 ROS_DEBUG("Inserting joint %s",JointName.c_str());
00079 jointpositions.insert(std::pair<std::string,double>(JointName,0.0));
00080 jointvelocities.insert(std::pair<std::string,double>(JointName,0.0));
00081 }
00082
00083 ros::Rate loop_rate(100);
00084 while (ros::ok())
00085 {
00086 sensor_msgs::JointState msg = getJointStatesMessage();
00087 topicPub_JointState_.publish(msg);
00088 ros::spinOnce();
00089 loop_rate.sleep();
00090 }
00091
00092 return 0;
00093 }