sr_subscriber.cpp
Go to the documentation of this file.
00001 
00029 #include <iostream>
00030 
00031 #include "sr_hand/sr_subscriber.h"
00032 #include <boost/algorithm/string.hpp>
00033 #include <sr_utilities/sr_math_utils.hpp>
00034 #include <boost/algorithm/string.hpp>
00035 
00036 using namespace std;
00037 
00038 namespace shadowrobot
00039 {
00040 
00042 //    CONSTRUCTOR/DESTRUCTOR   //
00044 
00045 SRSubscriber::SRSubscriber( boost::shared_ptr<SRArticulatedRobot> sr_art_robot ) :
00046     n_tilde("~")
00047 {
00048     sr_articulated_robot = sr_art_robot;
00050     // Initialize the subscribers
00052     SRSubscriber::init();
00053 }
00054 
00055 SRSubscriber::~SRSubscriber()
00056 {
00057 }
00058 
00059 void SRSubscriber::init()
00060 {
00061     // subscribe to sendupdate topic, set sendupdateCallback function
00062     std::string prefix;
00063     std::string searched_param;
00064     n_tilde.searchParam("shadowhand_prefix", searched_param);
00065     n_tilde.param(searched_param, prefix, std::string());
00066     std::string full_topic = prefix + "sendupdate";
00067 
00068     sendupdate_sub = node.subscribe(full_topic, 2, &SRSubscriber::sendupdateCallback, this);
00069 
00070     // subscribe to contrlr topic, set contrlrCallback function
00071     full_topic = prefix + "contrlr";
00072     contrlr_sub = node.subscribe(full_topic, 2, &SRSubscriber::contrlrCallback, this);
00073 
00074     // subscribe to config topic, set configCallback function
00075     full_topic = prefix + "config";
00076     config_sub = node.subscribe(full_topic, 2, &SRSubscriber::configCallback, this);
00077 
00078     // subscribe to the new ethercat like topics: one topic per joint
00079     for(SRArticulatedRobot::JointsMap::iterator joint = sr_articulated_robot->joints_map.begin() ;
00080         joint != sr_articulated_robot->joints_map.end(); ++joint)
00081     {
00082       controllers_sub.push_back( node.subscribe<std_msgs::Float64>("sh_"+boost::to_lower_copy(joint->first+"_position_controller/command"), 2, boost::bind(&SRSubscriber::cmd_callback, this, _1, joint->first) ) );
00083     }
00084 }
00085 
00087 //         CALLBACK            //
00089 void SRSubscriber::sendupdateCallback( const sr_robot_msgs::sendupdateConstPtr& msg )
00090 {
00091     //loop on all the sendupdate messages received (if > 0)
00092     int sendupdate_length = msg->sendupdate_length;
00093     if( sendupdate_length == 0 )
00094     {
00095         ROS_DEBUG("Received empty sendupdate command.");
00096         return;
00097     }
00098     //OK, not empty => loop to process all the sendupdate messages
00099     for( unsigned short index_msg = 0; index_msg < msg->sendupdate_length; ++index_msg )
00100     {
00101         float target = msg->sendupdate_list[index_msg].joint_target;
00102         string sensor_name = msg->sendupdate_list[index_msg].joint_name;
00103 
00104         ROS_DEBUG("Received sendupdate Command [%s : %f]", sensor_name.c_str(), target);
00105         sr_articulated_robot->sendupdate(sensor_name, (double)target);
00106     }
00107 
00108 }
00109 
00110 void SRSubscriber::cmd_callback( const std_msgs::Float64ConstPtr& msg, std::string& joint_name )
00111 {
00112   //converting to degrees as the old can interface was expecting degrees
00113   sr_articulated_robot->sendupdate(joint_name, sr_math_utils::to_degrees(msg->data));
00114 }
00115 
00116 void SRSubscriber::contrlrCallback( const sr_robot_msgs::contrlrConstPtr& msg )
00117 {
00118 
00119     vector<string> list_of_parameters = msg->list_of_parameters;
00120 
00121     JointControllerData ctrl_data;
00122 
00123     //parses all the parameters transmitted in the msg
00124     for( unsigned int index_param = 0; index_param < msg->length_of_list; ++index_param )
00125     {
00126         //split the string (around ":")
00127         vector<string> splitted_string;
00128         boost::split(splitted_string, msg->list_of_parameters[index_param], boost::is_any_of(":"));
00129 
00130         Parameters param;
00131         param.name = splitted_string[0];
00132         param.value = splitted_string[1];
00133 
00134         ctrl_data.data.push_back(param);
00135     }
00136 
00137     sr_articulated_robot->setContrl(msg->contrlr_name, ctrl_data);
00138 }
00139 
00140 void SRSubscriber::configCallback( const sr_robot_msgs::configConstPtr& msg )
00141 {
00142     ROS_ERROR("Configuration command callback not implemented yet");
00143 }
00144 
00145 } // end namespace


sr_hand
Author(s): Ugo Cupcic
autogenerated on Fri Aug 21 2015 12:24:55