sr_muscle_hand_lib.cpp
Go to the documentation of this file.
1 
28 #include <algorithm>
29 #include <utility>
30 #include <string>
31 #include <vector>
32 #include <boost/foreach.hpp>
33 #include <boost/algorithm/string.hpp>
34 
35 #include <sr_utilities/sr_math_utils.hpp>
36 
38 
39 
40 using std::vector;
41 using std::string;
42 using std::pair;
43 using std::ostringstream;
44 using sr_actuator::SrMuscleActuator;
52 using boost::shared_ptr;
53 using boost::static_pointer_cast;
54 using boost::shared_ptr;
55 
56 namespace shadow_robot
57 {
58  template<class StatusType, class CommandType>
60 
61  template<class StatusType, class CommandType>
63  {
64  "muscle_data_pressure",
65  "muscle_data_can_stats",
66  "muscle_data_slow_misc"
67  };
68 
69  template<class StatusType, class CommandType>
71  {
75  };
76 
77  template<class StatusType, class CommandType>
78  SrMuscleHandLib<StatusType, CommandType>::SrMuscleHandLib(hardware_interface::HardwareInterface *hw,
79  ros::NodeHandle nh, ros::NodeHandle nhtilde,
80  string device_id, string joint_prefix) :
81  SrMuscleRobotLib<StatusType, CommandType>(hw, nh, nhtilde, device_id, joint_prefix)
82 #ifdef DEBUG_PUBLISHER
83  // advertise the debug service, used to set which data we want to publish on the debug topics
84  , debug_service(
85  nh_tilde.advertiseService("set_debug_publishers", &SrMuscleHandLib::set_debug_data_to_publish, this))
86 #endif
87  {
88  // read the muscle polling frequency from the parameter server
89  this->muscle_update_rate_configs_vector = this->read_update_rate_configs("muscle_data_update_rate/", nb_muscle_data,
95 
96  for (unsigned int i = 0; i < this->muscle_update_rate_configs_vector.size(); ++i)
97  {
98  // The initialization parameters (assigned a -2 in the config file)
99  // are introduced in the flags map that will allow us to determine
100  // if the data has been received from every muscle driver
101  if (this->muscle_update_rate_configs_vector[i].when_to_update == -2)
102  {
103  this->from_muscle_driver_data_received_flags_[this->muscle_update_rate_configs_vector[i].what_to_update] = 0;
104  }
105  }
106 
107  for (unsigned int i = 0; i < NUM_MUSCLE_DRIVERS; ++i)
108  {
109  MuscleDriver driver(i);
110 
111  ostringstream ss;
112  ss << "reset_muscle_driver_" << i;
113  // initialize the reset muscle driver service
114  driver.reset_driver_service =
115  this->nh_tilde.template advertiseService<std_srvs::Empty::Request,
116  std_srvs::Empty::Response>(ss.str().c_str(),
117  boost::bind(
118  &SrMuscleHandLib<StatusType,
119  CommandType>::reset_muscle_driver_callback,
120  this, _1, _2, i));
121 
122  this->muscle_drivers_vector_.push_back(driver);
123  }
124 
125  vector<JointToSensor> joint_to_sensor_vect = this->read_joint_to_sensor_mapping();
126 
127  // initializing the joints vector
128  vector<string> joint_names_tmp;
129  vector<JointToMuscle> joint_to_muscle_map = read_joint_to_muscle_mapping();
130  vector<JointToSensor> joints_to_sensors;
131 
132  ROS_ASSERT(joint_to_muscle_map.size() == JOINTS_NUM_0320);
133  ROS_ASSERT(joint_to_sensor_vect.size() == JOINTS_NUM_0320);
134 
135  for (unsigned int i = 0; i < JOINTS_NUM_0320; ++i)
136  {
137  joint_names_tmp.push_back(string(joint_names[i]));
138  JointToSensor tmp_jts = joint_to_sensor_vect[i];
139  joints_to_sensors.push_back(tmp_jts);
140  }
141  initialize(joint_names_tmp, joint_to_muscle_map, joint_to_sensor_vect);
142  }
143 
144  template<class StatusType, class CommandType>
145  void SrMuscleHandLib<StatusType, CommandType>::initialize(vector<string> joint_names,
146  vector<int> actuator_ids,
147  vector<JointToSensor> joint_to_sensors)
148  {
149  ROS_ERROR("This version of SrMuscleHandLib<StatusType, CommandType>::initialize should not be used");
150  }
151 
152  template<class StatusType, class CommandType>
153  void SrMuscleHandLib<StatusType, CommandType>::initialize(vector<string> joint_names,
154  vector<JointToMuscle> actuator_ids,
155  vector<JointToSensor> joint_to_sensors)
156  {
157  for (unsigned int index = 0; index < joint_names.size(); ++index)
158  {
159  // add the joint and the vector of joints.
160  Joint joint;
161 
162  // update the joint variables
163  joint.joint_name = joint_names[index];
164  joint.joint_to_sensor = joint_to_sensors[index];
165 
166  if (actuator_ids[index].muscle_driver_id[0] != -1)
167  {
168  joint.has_actuator = true;
169  shared_ptr<MuscleWrapper> muscle_wrapper(new MuscleWrapper());
170  joint.actuator_wrapper = muscle_wrapper;
171  muscle_wrapper->muscle_driver_id[0] = actuator_ids[index].muscle_driver_id[0];
172  muscle_wrapper->muscle_driver_id[1] = actuator_ids[index].muscle_driver_id[1];
173  muscle_wrapper->muscle_id[0] = actuator_ids[index].muscle_id[0];
174  muscle_wrapper->muscle_id[1] = actuator_ids[index].muscle_id[1];
175  muscle_wrapper->actuator = static_cast<SrMuscleActuator *> (this->hw_->getActuator(
176  this->joint_prefix_ + joint.joint_name));
177  }
178  else
179  { // no muscles associated to this joint. We only check the driver 0 assuming
180  // a joint with -1 will have -1 in the driver 1 as well
181  joint.has_actuator = false;
182  }
183 
184  this->joints_vector.push_back(joint);
185  } // end for joints.
186  }
187 
188  template<class StatusType, class CommandType>
190  std_srvs::Empty::Response &response,
191  int muscle_driver_index)
192  {
193  ROS_INFO_STREAM(" resetting muscle driver " << muscle_driver_index);
194 
195  this->reset_muscle_driver_queue.push(muscle_driver_index);
196 
197  return true;
198  }
199 
200  template<class StatusType, class CommandType>
202  {
203  vector<JointToMuscle> muscle_map;
204  string param_name = "joint_to_muscle_mapping";
205 
206  XmlRpc::XmlRpcValue mapping;
207  this->nodehandle_.getParam(param_name, mapping);
209  // iterate on all the joints
210  for (int32_t i = 0; i < mapping.size(); ++i)
211  {
212  ROS_ASSERT(mapping[i].getType() == XmlRpc::XmlRpcValue::TypeArray);
213 
214  ROS_ASSERT(mapping[i][0].getType() == XmlRpc::XmlRpcValue::TypeArray);
215  ROS_ASSERT(mapping[i][0][0].getType() == XmlRpc::XmlRpcValue::TypeInt);
216  ROS_ASSERT(mapping[i][0][1].getType() == XmlRpc::XmlRpcValue::TypeInt);
217 
218  ROS_ASSERT(mapping[i][1].getType() == XmlRpc::XmlRpcValue::TypeArray);
219  ROS_ASSERT(mapping[i][1][0].getType() == XmlRpc::XmlRpcValue::TypeInt);
220  ROS_ASSERT(mapping[i][1][1].getType() == XmlRpc::XmlRpcValue::TypeInt);
221 
222  JointToMuscle joint_to_muscle;
223 
224  joint_to_muscle.muscle_driver_id[0] = mapping[i][0][0];
225  joint_to_muscle.muscle_id[0] = mapping[i][0][1];
226  joint_to_muscle.muscle_driver_id[1] = mapping[i][1][0];
227  joint_to_muscle.muscle_id[1] = mapping[i][1][1];
228 
229  muscle_map.push_back(joint_to_muscle);
230  }
231 
232  return muscle_map;
233  } // end read_joint_to_muscle_mapping
234 
235 #ifdef DEBUG_PUBLISHER
236  template <class StatusType, class CommandType>
237  bool SrMuscleHandLib<StatusType,
238  CommandType>::set_debug_data_to_publish(sr_robot_msgs::SetDebugData::Request& request,
239  sr_robot_msgs::SetDebugData::Response& response)
240  {
241  // check if the publisher_index is correct
242  if (request.publisher_index < this->nb_debug_publishers_const)
243  {
244  if (request.motor_index > NUM_MOTORS)
245  {
246  response.success = false;
247  return false;
248  }
249  if (request.motor_data_type > 0)
250  {
251  if ((request.motor_data_type < MOTOR_DATA_SGL) ||
252  (request.motor_data_type > MOTOR_DATA_DTERM))
253  {
254  response.success = false;
255  return false;
256  }
257  }
258  if (!this->debug_mutex.timed_lock(boost::posix_time::microseconds(this->debug_mutex_lock_wait_time)))
259  {
260  response.success = false;
261  return false;
262  }
263 
264  this->debug_motor_indexes_and_data[request.publisher_index] = shared_ptr<pair<int, int> >(new pair<int, int>());
265 
266  this->debug_motor_indexes_and_data[request.publisher_index]->first = request.motor_index;
267  this->debug_motor_indexes_and_data[request.publisher_index]->second = request.motor_data_type;
268  this->debug_mutex.unlock();
269  }
270  else
271  {
272  response.success = false;
273  return false;
274  }
275 
276  response.success = true;
277  return true;
278  }
279 #endif
280 
281  // Only to ensure that the template class is compiled for the types we are interested in
282  template
284 } // namespace shadow_robot
285 
286 /* For the emacs weenies in the crowd.
287 Local Variables:
288  c-basic-offset: 2
289 End:
290 */
std::vector< generic_updater::UpdateConfig > read_update_rate_configs(std::string base_param, int nb_data_defined, const char *human_readable_data_types[], const int32u data_types[])
JointToSensor joint_to_sensor
bool reset_muscle_driver_callback(std_srvs::Empty::Request &request, std_srvs::Empty::Response &response, int muscle_driver_index)
std::queue< int16_t, std::list< int16_t > > reset_muscle_driver_queue
std::vector< shadow_joints::MuscleDriver > muscle_drivers_vector_
#define NUM_MOTORS
std::vector< shadow_joints::Joint > joints_vector
The vector containing all the robot joints.
static const int32u muscle_data_types[]
MUSCLE_DATA_SLOW_MISC
int size() const
ros::NodeHandle nodehandle_
a ros nodehandle to be able to access resources out of the node namespace
std::string joint_prefix_
Prefix used to access the joints.
MOTOR_DATA_SGL
Type const & getType() const
unsigned int int32u
std::map< unsigned int, unsigned int > from_muscle_driver_data_received_flags_
ros::NodeHandle nh_tilde
a ROS nodehandle (private naming, only inside the node namespace) to be able to advertise the Force P...
virtual void initialize(std::vector< std::string > joint_names, std::vector< int > actuator_ids, std::vector< shadow_joints::JointToSensor > joint_to_sensors)
MUSCLE_DATA_PRESSURE
boost::shared_ptr< generic_updater::MuscleUpdater< CommandType > > muscle_updater_
#define JOINTS_NUM_0320
boost::shared_ptr< SrActuatorWrapper > actuator_wrapper
#define ROS_INFO_STREAM(args)
ros_ethercat_model::RobotState * hw_
ros::ServiceServer reset_driver_service
bool getParam(const std::string &key, std::string &s) const
std::vector< shadow_joints::JointToSensor > read_joint_to_sensor_mapping()
MUSCLE_DATA_CAN_STATS
static const char * human_readable_muscle_data_types[]
SrMuscleHandLib(hardware_interface::HardwareInterface *hw, ros::NodeHandle nh, ros::NodeHandle nhtilde, std::string device_id, std::string joint_prefix)
#define ROS_ASSERT(cond)
std::vector< shadow_joints::JointToMuscle > read_joint_to_muscle_mapping()
std::vector< generic_updater::UpdateConfig > muscle_update_rate_configs_vector
#define NUM_MUSCLE_DRIVERS
#define ROS_ERROR(...)
MOTOR_DATA_DTERM


sr_robot_lib
Author(s): Ugo Cupcic, Toni Oliver
autogenerated on Tue Oct 13 2020 04:01:58