sr_subscriber.cpp
Go to the documentation of this file.
1 
29 #include <iostream>
30 #include <string>
31 #include <vector>
32 
33 #include "sr_hand/sr_subscriber.h"
34 #include <boost/algorithm/string.hpp>
36 
37 using std::vector;
38 using std::string;
39 
40 namespace shadowrobot
41 {
42 
44 // CONSTRUCTOR/DESTRUCTOR //
46 
48  n_tilde("~")
49  {
50  sr_articulated_robot = sr_art_robot;
52  // Initialize the subscribers
55  }
56 
58  {
59  }
60 
62  {
63  // subscribe to sendupdate topic, set sendupdateCallback function
64  std::string prefix;
65  std::string searched_param;
66  n_tilde.searchParam("shadowhand_prefix", searched_param);
67  n_tilde.param(searched_param, prefix, std::string());
68  std::string full_topic = prefix + "sendupdate";
69 
71 
72  // subscribe to contrlr topic, set contrlrCallback function
73  full_topic = prefix + "contrlr";
75 
76  // subscribe to config topic, set configCallback function
77  full_topic = prefix + "config";
78  config_sub = node.subscribe(full_topic, 2, &SRSubscriber::configCallback, this);
79 
80  // subscribe to the new ethercat like topics: one topic per joint
81  for (SRArticulatedRobot::JointsMap::iterator joint = sr_articulated_robot->joints_map.begin();
82  joint != sr_articulated_robot->joints_map.end(); ++joint)
83  {
84  controllers_sub.push_back(node.subscribe<std_msgs::Float64>(
85  "sh_" + boost::to_lower_copy(joint->first + "_position_controller/command"), 2,
86  boost::bind(&SRSubscriber::cmd_callback, this, _1, joint->first)));
87  }
88  }
89 
91 // CALLBACK //
93  void SRSubscriber::sendupdateCallback(const sr_robot_msgs::sendupdateConstPtr &msg)
94  {
95  // loop on all the sendupdate messages received (if > 0)
96  int sendupdate_length = msg->sendupdate_length;
97  if (sendupdate_length == 0)
98  {
99  ROS_DEBUG("Received empty sendupdate command.");
100  return;
101  }
102  // OK, not empty => loop to process all the sendupdate messages
103  for (uint16_t index_msg = 0; index_msg < msg->sendupdate_length; ++index_msg)
104  {
105  float target = msg->sendupdate_list[index_msg].joint_target;
106  string sensor_name = msg->sendupdate_list[index_msg].joint_name;
107 
108  ROS_DEBUG("Received sendupdate Command [%s : %f]", sensor_name.c_str(), target);
109  sr_articulated_robot->sendupdate(sensor_name, static_cast<double>(target));
110  }
111  }
112 
113  void SRSubscriber::cmd_callback(const std_msgs::Float64ConstPtr &msg, std::string &joint_name)
114  {
115  // converting to degrees as the old can interface was expecting degrees
116  sr_articulated_robot->sendupdate(joint_name, sr_math_utils::to_degrees(msg->data));
117  }
118 
119  void SRSubscriber::contrlrCallback(const sr_robot_msgs::contrlrConstPtr &msg)
120  {
121  vector<string> list_of_parameters = msg->list_of_parameters;
122 
123  JointControllerData ctrl_data;
124 
125  // parses all the parameters transmitted in the msg
126  for (unsigned int index_param = 0; index_param < msg->length_of_list; ++index_param)
127  {
128  // split the string (around ":")
129  vector<string> splitted_string;
130  boost::split(splitted_string, msg->list_of_parameters[index_param], boost::is_any_of(":"));
131 
133  param.name = splitted_string[0];
134  param.value = splitted_string[1];
135 
136  ctrl_data.data.push_back(param);
137  }
138 
139  sr_articulated_robot->setContrl(msg->contrlr_name, ctrl_data);
140  }
141 
142  void SRSubscriber::configCallback(const sr_robot_msgs::configConstPtr &msg)
143  {
144  ROS_ERROR("Configuration command callback not implemented yet");
145  }
146 
147 } // namespace shadowrobot
std::string name
name of the parameter
void contrlrCallback(const sr_robot_msgs::contrlrConstPtr &msg)
bool param(const std::string &param_name, T &param_val, const T &default_val)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
boost::shared_ptr< SRArticulatedRobot > sr_articulated_robot
The shadowhand / shadowarm object (can be either an object connected to the real robot or a virtual h...
Definition: sr_subscriber.h:78
Subscriber config_sub
The subscriber to the config topic.
void configCallback(const sr_robot_msgs::configConstPtr &msg)
void init()
init function
static double to_degrees(double rad)
void sendupdateCallback(const sr_robot_msgs::sendupdateConstPtr &msg)
std::vector< Subscriber > controllers_sub
The vector of subscribers to the different joint topics.
Subscriber sendupdate_sub
The subscriber to the sendupdate topic.
Definition: sr_subscriber.h:96
void cmd_callback(const std_msgs::Float64ConstPtr &msg, std::string &joint_name)
bool searchParam(const std::string &key, std::string &result) const
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Subscriber contrlr_sub
The subscriber to the contrlr topic.
std::string value
value of the parameter
NodeHandle node
ros node handle
Definition: sr_subscriber.h:75
SRSubscriber(boost::shared_ptr< SRArticulatedRobot > sr_art_robot)
#define ROS_ERROR(...)
This ROS subscriber is used to issue commands to the hand / arm, from sending a set of targets...
#define ROS_DEBUG(...)


sr_hand
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:55:53