etherCAT_compatibility_hand.cpp
Go to the documentation of this file.
1 
29 
30 #include <time.h>
31 #include <ros/ros.h>
32 #include <ros/topic.h>
33 #include <string>
34 #include <vector>
35 #include <std_msgs/Float64.h>
36 #include <control_msgs/JointControllerState.h>
37 #include <sr_robot_msgs/JointControllerState.h>
38 
39 namespace shadowrobot
40 {
42  SRArticulatedRobot(), n_tilde("~")
43  {
44  ROS_WARN(
45  "This interface is deprecated, you should probably use "
46  "the interface provided by the etherCAT driver directly.");
47 
48  initializeMap();
49 
51  this);
52  }
53 
55  {
56  }
57 
59  {
60  joints_map_mutex.lock();
61  JointData tmpData;
62  JointData tmpDataZero;
63  tmpDataZero.isJointZero = 1;
64  tmpDataZero.max = 180.0;
65  tmpData.max = 90.0;
66 
67  std::string full_topic = "";
68 
69  full_topic = findControllerTopicName("ffj0");
70  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
71  int tmp_index = 0;
72  tmpDataZero.publisher_index = tmp_index;
73  joints_map["FFJ0"] = tmpDataZero;
74  joints_map["FFJ1"] = tmpData;
75  joints_map["FFJ2"] = tmpData;
76 
77  full_topic = findControllerTopicName("ffj3");
78  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
79  tmp_index++;
80  tmpData.publisher_index = tmp_index;
81  joints_map["FFJ3"] = tmpData;
82 
83  tmpData.min = -20.0;
84  tmpData.max = 20.0;
85  full_topic = findControllerTopicName("ffj4");
86  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
87  tmp_index++;
88  tmpData.publisher_index = tmp_index;
89  joints_map["FFJ4"] = tmpData;
90 
91  full_topic = findControllerTopicName("mfj0");
92  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
93  tmp_index++;
94  tmpDataZero.publisher_index = tmp_index;
95  joints_map["MFJ0"] = tmpDataZero;
96 
97  tmpData.min = 0.0;
98  tmpData.max = 90.0;
99  joints_map["MFJ1"] = tmpData;
100  joints_map["MFJ2"] = tmpData;
101 
102  full_topic = findControllerTopicName("mfj3");
103  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
104  tmp_index++;
105  tmpData.publisher_index = tmp_index;
106  joints_map["MFJ3"] = tmpData;
107 
108  tmpData.min = -20.0;
109  tmpData.max = 20.0;
110  full_topic = findControllerTopicName("mfj4");
111  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
112  tmp_index++;
113  tmpData.publisher_index = tmp_index;
114  joints_map["MFJ4"] = tmpData;
115 
116  full_topic = findControllerTopicName("rfj0");
117  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
118  tmp_index++;
119  tmpDataZero.publisher_index = tmp_index;
120  joints_map["RFJ0"] = tmpDataZero;
121 
122  tmpData.min = 0.0;
123  tmpData.max = 90.0;
124  joints_map["RFJ1"] = tmpData;
125  joints_map["RFJ2"] = tmpData;
126  full_topic = findControllerTopicName("rfj3");
127  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
128  tmp_index++;
129  tmpData.publisher_index = tmp_index;
130  joints_map["RFJ3"] = tmpData;
131 
132  tmpData.min = -20.0;
133  tmpData.max = 20.0;
134  full_topic = findControllerTopicName("rfj4");
135  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
136  tmp_index++;
137  tmpData.publisher_index = tmp_index;
138  joints_map["RFJ4"] = tmpData;
139 
140  full_topic = findControllerTopicName("lfj0");
141  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
142  tmp_index++;
143  tmpDataZero.publisher_index = tmp_index;
144  joints_map["LFJ0"] = tmpDataZero;
145 
146  tmpData.min = 0.0;
147  tmpData.max = 90.0;
148  joints_map["LFJ1"] = tmpData;
149  joints_map["LFJ2"] = tmpData;
150 
151  full_topic = findControllerTopicName("lfj3");
152  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
153  tmp_index++;
154  tmpData.publisher_index = tmp_index;
155  joints_map["LFJ3"] = tmpData;
156 
157  tmpData.min = -20.0;
158  tmpData.max = 20.0;
159  full_topic = findControllerTopicName("lfj4");
160  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
161  tmp_index++;
162  tmpData.publisher_index = tmp_index;
163  joints_map["LFJ4"] = tmpData;
164 
165  tmpData.min = 0.0;
166  tmpData.max = 45.0;
167  full_topic = findControllerTopicName("lfj5");
168  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
169  tmp_index++;
170  tmpData.publisher_index = tmp_index;
171  joints_map["LFJ5"] = tmpData;
172 
173  tmpData.min = 0.0;
174  tmpData.max = 90.0;
175  full_topic = findControllerTopicName("thj1");
176  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
177  tmp_index++;
178  tmpData.publisher_index = tmp_index;
179  joints_map["THJ1"] = tmpData;
180 
181  tmpData.min = -40.0;
182  tmpData.max = 40.0;
183  full_topic = findControllerTopicName("thj2");
184  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
185  tmp_index++;
186  tmpData.publisher_index = tmp_index;
187  joints_map["THJ2"] = tmpData;
188 
189  tmpData.min = -15.0;
190  tmpData.max = 15.0;
191  full_topic = findControllerTopicName("thj3");
192  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
193  tmp_index++;
194  tmpData.publisher_index = tmp_index;
195  joints_map["THJ3"] = tmpData;
196 
197  tmpData.min = 0.0;
198  tmpData.max = 75.0;
199  full_topic = findControllerTopicName("thj4");
200  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
201  tmp_index++;
202  tmpData.publisher_index = tmp_index;
203  joints_map["THJ4"] = tmpData;
204 
205  tmpData.min = -60.0;
206  tmpData.max = 60.0;
207  full_topic = findControllerTopicName("thj5");
208  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
209  tmp_index++;
210  tmpData.publisher_index = tmp_index;
211  joints_map["THJ5"] = tmpData;
212 
213  tmpData.min = -30.0;
214  tmpData.max = 45.0;
215  full_topic = findControllerTopicName("wrj1");
216  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
217  tmp_index++;
218  tmpData.publisher_index = tmp_index;
219  joints_map["WRJ1"] = tmpData;
220 
221  tmpData.min = -30.0;
222  tmpData.max = 10.0;
223  full_topic = findControllerTopicName("wrj2");
224  etherCAT_publishers.push_back(node.advertise<std_msgs::Float64>(full_topic, 2));
225  tmp_index++;
226  tmpData.publisher_index = tmp_index;
227  joints_map["WRJ2"] = tmpData;
228 
229  joints_map_mutex.unlock();
230  }
231 
232  std::string EtherCATCompatibilityHand::findControllerTopicName(std::string joint_name)
233  {
234  std::string joint_prefix;
235  n_tilde.param("joint_prefix", joint_prefix, std::string(""));
236 
237 
238  std::string controller_suffix = "position_controller";
239 
240  std::string searched_param;
241  n_tilde.searchParam("controller_suffix", searched_param);
242  n_tilde.param(searched_param, controller_suffix, std::string("position_controller"));
243 
244  std::string topic_prefix = "/sh_";
245  std::string topic_suffix = "_" + controller_suffix + "/command";
246  std::string full_topic = topic_prefix + joint_prefix + joint_name + topic_suffix;
247 
248  return full_topic;
249  }
250 
251  int16_t EtherCATCompatibilityHand::sendupdate(std::string joint_name, double target)
252  {
253  if (!joints_map_mutex.try_lock())
254  {
255  return -1;
256  }
257  JointsMap::iterator iter = joints_map.find(joint_name);
258  std_msgs::Float64 target_msg;
259 
260  // not found
261  if (iter == joints_map.end())
262  {
263  ROS_DEBUG("Joint %s not found", joint_name.c_str());
264 
265  joints_map_mutex.unlock();
266  return -1;
267  }
268 
269  // joint found
270  JointData tmpData(iter->second);
271 
272  if (target < tmpData.min)
273  {
274  target = tmpData.min;
275  }
276  if (target > tmpData.max)
277  {
278  target = tmpData.max;
279  }
280 
281  tmpData.target = target;
282 
283  joints_map[joint_name] = tmpData;
284  // the targets are in radians
285  target_msg.data = sr_math_utils::to_rad(target);
286 
287  etherCAT_publishers[tmpData.publisher_index].publish(target_msg);
288 
289  joints_map_mutex.unlock();
290  return 0;
291  }
292 
294  {
295  JointData noData;
296  if (!joints_map_mutex.try_lock())
297  {
298  return noData;
299  }
300 
301  JointsMap::iterator iter = joints_map.find(joint_name);
302 
303  // joint found
304  if (iter != joints_map.end())
305  {
306  JointData tmp = JointData(iter->second);
307 
308  joints_map_mutex.unlock();
309  return tmp;
310  }
311 
312  ROS_ERROR("Joint %s not found.", joint_name.c_str());
313  joints_map_mutex.unlock();
314  return noData;
315  }
316 
318  {
319  return joints_map;
320  }
321 
322  int16_t EtherCATCompatibilityHand::setContrl(std::string contrlr_name, JointControllerData ctrlr_data)
323  {
324  ROS_WARN(
325  "The set Controller function is not implemented in the EtherCAT compatibility wrapper, "
326  "please use the provided services directly.");
327  return 0;
328  }
329 
331  {
332  JointControllerData no_result;
333  ROS_WARN(
334  "The get Controller function is not implemented in the EtherCAT compatibility wrapper, "
335  "please use the provided services directly.");
336  return no_result;
337  }
338 
339  int16_t EtherCATCompatibilityHand::setConfig(std::vector<std::string> myConfig)
340  {
341  ROS_WARN("The set config function is not implemented.");
342  return 0;
343  }
344 
345  void EtherCATCompatibilityHand::getConfig(std::string joint_name)
346  {
347  ROS_WARN("The get config function is not implemented.");
348  }
349 
350  std::vector<DiagnosticData> EtherCATCompatibilityHand::getDiagnostics()
351  {
352  std::vector<DiagnosticData> returnVect;
353  return returnVect;
354  }
355 
356  void EtherCATCompatibilityHand::joint_states_callback(const sensor_msgs::JointStateConstPtr &msg)
357  {
358  if (!joints_map_mutex.try_lock())
359  {
360  return;
361  }
362  std::string fj0char;
363  bool fj0flag = false;
364  double fj0pos, fj0vel, fj0eff;
365  // loop on all the names in the joint_states message
366  for (unsigned int index = 0; index < msg->name.size(); ++index)
367  {
368  std::string joint_name = msg->name[index];
369  JointsMap::iterator iter = joints_map.find(joint_name);
370 
371  // not found => can be a joint from the arm
372  if (iter == joints_map.end())
373  {
374  continue;
375  }
376  else
377  {
378  // joint found but may be a FJ1 or FJ2 to combine into FJ0
379  if (joint_name.find("FJ1") != std::string::npos)
380  {
381  // ROS_INFO("FJ1found");
382  if (fj0flag && joint_name[0] == fj0char[0]) // J2 already found for SAME joint
383  {
384  std::string j0name = fj0char + "FJ0";
385  JointsMap::iterator myiter = joints_map.find(j0name);
386  if (myiter == joints_map.end())
387  {
388  // joint is not existing
389  continue;
390  }
391  else
392  {
393  JointData tmpDatazero(myiter->second);
394  tmpDatazero.position = fj0pos + sr_math_utils::to_degrees(msg->position[index]);
395  tmpDatazero.velocity = fj0vel + msg->effort[index];
396  tmpDatazero.force = fj0eff + msg->velocity[index];
397  joints_map[j0name] = tmpDatazero;
398 
399  fj0pos = 0;
400  fj0vel = 0;
401  fj0eff = 0;
402  fj0char.clear();
403  fj0flag = false;
404  }
405  }
406  else
407  {
408  fj0pos = sr_math_utils::to_degrees(msg->position[index]);
409  fj0eff = msg->effort[index];
410  fj0vel = msg->velocity[index];
411  fj0char.push_back(joint_name[0]);
412  fj0flag = true;
413  }
414  }
415  else if (joint_name.find("FJ2") != std::string::npos)
416  {
417  // ROS_INFO("FJ2found");
418  if (fj0flag && joint_name[0] == fj0char[0]) // J1 already found for SAME joint
419  {
420  std::string j0name = fj0char + "FJ0";
421  JointsMap::iterator myiter = joints_map.find(j0name);
422  if (myiter == joints_map.end())
423  {
424  // joint is not existing
425  continue;
426  }
427  else
428  {
429  JointData tmpDatazero(myiter->second);
430  tmpDatazero.position = fj0pos + sr_math_utils::to_degrees(msg->position[index]);
431  tmpDatazero.force = fj0vel + msg->effort[index];
432  tmpDatazero.velocity = fj0eff + msg->velocity[index];
433  joints_map[j0name] = tmpDatazero;
434 
435  fj0pos = 0;
436  fj0vel = 0;
437  fj0eff = 0;
438  fj0char.clear();
439  fj0flag = false;
440  }
441  }
442  else
443  {
444  fj0pos = sr_math_utils::to_degrees(msg->position[index]);
445  fj0eff = msg->effort[index];
446  fj0vel = msg->velocity[index];
447  fj0char.push_back(joint_name[0]);
448  fj0flag = true;
449  }
450  }
451  // any way do fill the found joint data and update the joint map.
452  JointData tmpData(iter->second);
453  tmpData.position = sr_math_utils::to_degrees(msg->position[index]);
454  tmpData.force = msg->effort[index];
455  tmpData.velocity = msg->velocity[index];
456  joints_map[joint_name] = tmpData;
457  }
458  }
459  joints_map_mutex.unlock();
460  }
461 } // namespace shadowrobot
462 
463 /* For the emacs weenies in the crowd.
464 Local Variables:
465  c-basic-offset: 2
466 End:
467 */
virtual JointData getJointData(std::string joint_name)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::map< std::string, JointData > JointsMap
static double to_degrees(double rad)
#define ROS_WARN(...)
static double to_rad(double degrees)
std::string findControllerTopicName(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
JointsMap joints_map
A mapping between the joint names and the information regarding those joints.
virtual std::vector< DiagnosticData > getDiagnostics()
virtual JointControllerData getContrl(std::string ctrlr_name)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
This compatibility interface is a wrapper around the new etherCAT Hand ROS driver. It is used to present the same interface as the CAN hand.
#define ROS_DEPRECATED
This is a parent class for the different types of Shadowhand we can have. It makes it possible to swa...
virtual int16_t sendupdate(std::string joint_name, double target)
virtual void getConfig(std::string joint_name)
void joint_states_callback(const sensor_msgs::JointStateConstPtr &msg)
virtual int16_t setContrl(std::string contrlr_name, JointControllerData ctrlr_data)
#define ROS_ERROR(...)
virtual int16_t setConfig(std::vector< std::string > myConfig)
#define ROS_DEBUG(...)


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