hebiros_subscribers_physical.cpp
Go to the documentation of this file.
3 #include "hebiros.h"
4 
5 using namespace hebi;
6 
7 
9 
10  subscribers["hebiros/"+group_name+"/command"] =
11  HebirosNode::n_ptr->subscribe<CommandMsg>("hebiros/"+group_name+"/command", 100,
12  boost::bind(&HebirosSubscribersPhysical::command, this, _1, group_name));
13 
14  subscribers["hebiros/"+group_name+"/command/joint_state"] =
15  HebirosNode::n_ptr->subscribe<sensor_msgs::JointState>(
16  "hebiros/"+group_name+"/command/joint_state", 100,
17  boost::bind(&HebirosSubscribersPhysical::jointCommand, this, _1, group_name));
18 
19  // TODO: replace with better abstraction later
20  HebirosGroupPhysical* group = dynamic_cast<HebirosGroupPhysical*>
22  if (!group) {
23  ROS_WARN("Improper group type during register subscribers call");
24  return;
25  }
26 
27  group->group_ptr->requestInfo(group->group_info);
28 
29  group->group_ptr->addFeedbackHandler([this, group_name](const GroupFeedback& group_fbk) {
30  this->feedback(group_name, group_fbk);
31  });
32 
33  group->setFeedbackFrequency(
34  HebirosParameters::getInt("hebiros/feedback_frequency"));
35  group->setCommandLifetime(
36  HebirosParameters::getInt("hebiros/command_lifetime"));
37 }
38 
40  std::string group_name) {
41 
42  // TODO: replace with better abstraction later -- move send command into group
43  HebirosGroupPhysical* group = dynamic_cast<HebirosGroupPhysical*>
45  if (!group) {
46  ROS_WARN("Improper group type during command call");
47  return;
48  }
49 
50  sensor_msgs::JointState joint_data;
51  joint_data.name = data->name;
52  joint_data.position = data->position;
53  joint_data.velocity = data->velocity;
54  joint_data.effort = data->effort;
55  SettingsMsg settings_data;
56  settings_data = data->settings;
57 
58  GroupCommand group_command(group->size);
59  addJointCommand(&group_command, joint_data, group_name);
60  addSettingsCommand(&group_command, settings_data, group_name);
61 
62  group->group_ptr->sendCommand(group_command);
63 }
64 
66  const boost::shared_ptr<sensor_msgs::JointState const> data, std::string group_name) {
67 
68  // TODO: replace with better abstraction later -- move send command into group
69  HebirosGroupPhysical* group = dynamic_cast<HebirosGroupPhysical*>
71  if (!group) {
72  ROS_WARN("Improper group type during jointCommand call");
73  return;
74  }
75 
76  sensor_msgs::JointState joint_data;
77  joint_data.name = data->name;
78  joint_data.position = data->position;
79  joint_data.velocity = data->velocity;
80  joint_data.effort = data->effort;
81 
82  GroupCommand group_command(group->size);
83  addJointCommand(&group_command, joint_data, group_name);
84 
85  group->group_ptr->sendCommand(group_command);
86 }
87 
89  sensor_msgs::JointState data, std::string group_name) {
90 
92 
93  for (int i = 0; i < data.name.size(); i++) {
94  if (HebirosSubscribers::jointFound(group_name, data.name[i])) {
95 
96  int joint_index = group->joints[data.name[i]];
97 
98  if (i < data.position.size()) {
99  (*group_command)[joint_index].actuator().position().set(data.position[i]);
100  }
101  if (i < data.velocity.size()) {
102  (*group_command)[joint_index].actuator().velocity().set(data.velocity[i]);
103  }
104  if (i < data.effort.size()) {
105  (*group_command)[joint_index].actuator().effort().set(data.effort[i]);
106  }
107  }
108  else {
109  HebirosSubscribers::jointNotFound(data.name[i]);
110  }
111  }
112 }
113 
115  SettingsMsg data, std::string group_name) {
116 
118 
119  for (int i = 0; i < data.name.size(); i++) {
120  if (HebirosSubscribers::jointFound(group_name, data.name[i])) {
121 
122  int joint_index = group->joints[data.name[i]];
123 
124  if (i < data.save_current_settings.size()) {
125  if (data.save_current_settings[i]) {
126  (*group_command)[joint_index].settings().saveCurrentSettings().set();
127  }
128  }
129  if (i < data.control_strategy.size()) {
130  Command::ControlStrategy strategy = static_cast<Command::ControlStrategy>(
131  data.control_strategy[i]);
132  (*group_command)[joint_index].
133  settings().actuator().controlStrategy().set(strategy);
134  }
135  }
136  else {
137  HebirosSubscribers::jointNotFound(data.name[i]);
138  }
139  }
140 
141  addPositionGainsCommand(group_command, data.position_gains, group_name);
142  addVelocityGainsCommand(group_command, data.velocity_gains, group_name);
143  addEffortGainsCommand(group_command, data.effort_gains, group_name);
144 }
145 
147  PidGainsMsg data, std::string group_name) {
148 
150 
151  for (int i = 0; i < data.name.size(); i++) {
152  if (HebirosSubscribers::jointFound(group_name, data.name[i])) {
153 
154  int joint_index = group->joints[data.name[i]];
155 
156  if (i < data.kp.size()) {
157  (*group_command)[joint_index].
158  settings().actuator().positionGains().kP().set(data.kp[i]);
159  }
160  if (i < data.ki.size()) {
161  (*group_command)[joint_index].
162  settings().actuator().positionGains().kI().set(data.ki[i]);
163  }
164  if (i < data.kd.size()) {
165  (*group_command)[joint_index].
166  settings().actuator().positionGains().kD().set(data.kd[i]);
167  }
168  if (i < data.feed_forward.size()) {
169  (*group_command)[joint_index].
170  settings().actuator().positionGains().feedForward().set(data.feed_forward[i]);
171  }
172  if (i < data.dead_zone.size()) {
173  (*group_command)[joint_index].
174  settings().actuator().positionGains().deadZone().set(data.dead_zone[i]);
175  }
176  if (i < data.i_clamp.size()) {
177  (*group_command)[joint_index].
178  settings().actuator().positionGains().iClamp().set(data.i_clamp[i]);
179  }
180  if (i < data.punch.size()) {
181  (*group_command)[joint_index].
182  settings().actuator().positionGains().punch().set(data.punch[i]);
183  }
184  if (i < data.min_target.size()) {
185  (*group_command)[joint_index].
186  settings().actuator().positionGains().minTarget().set(data.min_target[i]);
187  }
188  if (i < data.max_target.size()) {
189  (*group_command)[joint_index].
190  settings().actuator().positionGains().maxTarget().set(data.max_target[i]);
191  }
192  if (i < data.target_lowpass.size()) {
193  (*group_command)[joint_index].
194  settings().actuator().positionGains().targetLowpass().set(data.target_lowpass[i]);
195  }
196  if (i < data.min_output.size()) {
197  (*group_command)[joint_index].
198  settings().actuator().positionGains().minOutput().set(data.min_output[i]);
199  }
200  if (i < data.max_output.size()) {
201  (*group_command)[joint_index].
202  settings().actuator().positionGains().maxOutput().set(data.max_output[i]);
203  }
204  if (i < data.output_lowpass.size()) {
205  (*group_command)[joint_index].
206  settings().actuator().positionGains().outputLowpass().set(data.output_lowpass[i]);
207  }
208  if (i < data.d_on_error.size()) {
209  (*group_command)[joint_index].
210  settings().actuator().positionGains().dOnError().set(data.d_on_error[i]);
211  }
212  }
213  else {
214  HebirosSubscribers::jointNotFound(data.name[i]);
215  }
216  }
217 }
218 
220  PidGainsMsg data, std::string group_name) {
221 
223 
224  for (int i = 0; i < data.name.size(); i++) {
225  if (HebirosSubscribers::jointFound(group_name, data.name[i])) {
226 
227  int joint_index = group->joints[data.name[i]];
228 
229  if (i < data.kp.size()) {
230  (*group_command)[joint_index].
231  settings().actuator().velocityGains().kP().set(data.kp[i]);
232  }
233  if (i < data.ki.size()) {
234  (*group_command)[joint_index].
235  settings().actuator().velocityGains().kI().set(data.ki[i]);
236  }
237  if (i < data.kd.size()) {
238  (*group_command)[joint_index].
239  settings().actuator().velocityGains().kD().set(data.kd[i]);
240  }
241  if (i < data.feed_forward.size()) {
242  (*group_command)[joint_index].
243  settings().actuator().velocityGains().feedForward().set(data.feed_forward[i]);
244  }
245  if (i < data.dead_zone.size()) {
246  (*group_command)[joint_index].
247  settings().actuator().velocityGains().deadZone().set(data.dead_zone[i]);
248  }
249  if (i < data.i_clamp.size()) {
250  (*group_command)[joint_index].
251  settings().actuator().velocityGains().iClamp().set(data.i_clamp[i]);
252  }
253  if (i < data.punch.size()) {
254  (*group_command)[joint_index].
255  settings().actuator().velocityGains().punch().set(data.punch[i]);
256  }
257  if (i < data.min_target.size()) {
258  (*group_command)[joint_index].
259  settings().actuator().velocityGains().minTarget().set(data.min_target[i]);
260  }
261  if (i < data.max_target.size()) {
262  (*group_command)[joint_index].
263  settings().actuator().velocityGains().maxTarget().set(data.max_target[i]);
264  }
265  if (i < data.target_lowpass.size()) {
266  (*group_command)[joint_index].
267  settings().actuator().velocityGains().targetLowpass().set(data.target_lowpass[i]);
268  }
269  if (i < data.min_output.size()) {
270  (*group_command)[joint_index].
271  settings().actuator().velocityGains().minOutput().set(data.min_output[i]);
272  }
273  if (i < data.max_output.size()) {
274  (*group_command)[joint_index].
275  settings().actuator().velocityGains().maxOutput().set(data.max_output[i]);
276  }
277  if (i < data.output_lowpass.size()) {
278  (*group_command)[joint_index].
279  settings().actuator().velocityGains().outputLowpass().set(data.output_lowpass[i]);
280  }
281  if (i < data.d_on_error.size()) {
282  (*group_command)[joint_index].
283  settings().actuator().velocityGains().dOnError().set(data.d_on_error[i]);
284  }
285  }
286  else {
287  HebirosSubscribers::jointNotFound(data.name[i]);
288  }
289  }
290 }
291 
293  PidGainsMsg data, std::string group_name) {
294 
296 
297  for (int i = 0; i < data.name.size(); i++) {
298  if (HebirosSubscribers::jointFound(group_name, data.name[i])) {
299 
300  int joint_index = group->joints[data.name[i]];
301 
302  if (i < data.kp.size()) {
303  (*group_command)[joint_index].
304  settings().actuator().effortGains().kP().set(data.kp[i]);
305  }
306  if (i < data.ki.size()) {
307  (*group_command)[joint_index].
308  settings().actuator().effortGains().kI().set(data.ki[i]);
309  }
310  if (i < data.kd.size()) {
311  (*group_command)[joint_index].
312  settings().actuator().effortGains().kD().set(data.kd[i]);
313  }
314  if (i < data.feed_forward.size()) {
315  (*group_command)[joint_index].
316  settings().actuator().effortGains().feedForward().set(data.feed_forward[i]);
317  }
318  if (i < data.dead_zone.size()) {
319  (*group_command)[joint_index].
320  settings().actuator().effortGains().deadZone().set(data.dead_zone[i]);
321  }
322  if (i < data.i_clamp.size()) {
323  (*group_command)[joint_index].
324  settings().actuator().effortGains().iClamp().set(data.i_clamp[i]);
325  }
326  if (i < data.punch.size()) {
327  (*group_command)[joint_index].
328  settings().actuator().effortGains().punch().set(data.punch[i]);
329  }
330  if (i < data.min_target.size()) {
331  (*group_command)[joint_index].
332  settings().actuator().effortGains().minTarget().set(data.min_target[i]);
333  }
334  if (i < data.max_target.size()) {
335  (*group_command)[joint_index].
336  settings().actuator().effortGains().maxTarget().set(data.max_target[i]);
337  }
338  if (i < data.target_lowpass.size()) {
339  (*group_command)[joint_index].
340  settings().actuator().effortGains().targetLowpass().set(data.target_lowpass[i]);
341  }
342  if (i < data.min_output.size()) {
343  (*group_command)[joint_index].
344  settings().actuator().effortGains().minOutput().set(data.min_output[i]);
345  }
346  if (i < data.max_output.size()) {
347  (*group_command)[joint_index].
348  settings().actuator().effortGains().maxOutput().set(data.max_output[i]);
349  }
350  if (i < data.output_lowpass.size()) {
351  (*group_command)[joint_index].
352  settings().actuator().effortGains().outputLowpass().set(data.output_lowpass[i]);
353  }
354  if (i < data.d_on_error.size()) {
355  (*group_command)[joint_index].
356  settings().actuator().effortGains().dOnError().set(data.d_on_error[i]);
357  }
358  }
359  else {
360  HebirosSubscribers::jointNotFound(data.name[i]);
361  }
362  }
363 }
364 
365 
366 void HebirosSubscribersPhysical::feedback(std::string group_name, const GroupFeedback& group_fbk) {
367 
368  // TODO: replace with better abstraction later
369  HebirosGroupPhysical* group = dynamic_cast<HebirosGroupPhysical*>
371  if (!group) {
372  ROS_WARN("Improper group type during feedback call");
373  return;
374  }
375 
376  FeedbackMsg feedback_msg;
377  sensor_msgs::JointState joint_state_msg;
378 
379  for (int i = 0; i < group_fbk.size(); i++) {
380  std::string name = group->group_info[i].settings().name().get();
381  std::string family = group->group_info[i].settings().family().get();
382  double position = group_fbk[i].actuator().position().get();
383  double velocity = group_fbk[i].actuator().velocity().get();
384  double effort = group_fbk[i].actuator().effort().get();
385 
386  joint_state_msg.header.stamp = ros::Time::now();
387  joint_state_msg.name.push_back(family+"/"+name);
388  joint_state_msg.position.push_back(position);
389  joint_state_msg.velocity.push_back(velocity);
390  joint_state_msg.effort.push_back(effort);
391 
392  feedback_msg.name.push_back(family+"/"+name);
393  feedback_msg.position.push_back(position);
394  feedback_msg.velocity.push_back(velocity);
395  feedback_msg.effort.push_back(effort);
396  feedback_msg.position_command.push_back(group_fbk[i].actuator().positionCommand().get());
397  feedback_msg.velocity_command.push_back(group_fbk[i].actuator().velocityCommand().get());
398  feedback_msg.effort_command.push_back(group_fbk[i].actuator().effortCommand().get());
399  geometry_msgs::Vector3 accelerometer_msg;
400  accelerometer_msg.x = group_fbk[i].imu().accelerometer().get().getX();
401  accelerometer_msg.y = group_fbk[i].imu().accelerometer().get().getY();
402  accelerometer_msg.z = group_fbk[i].imu().accelerometer().get().getZ();
403  feedback_msg.accelerometer.push_back(accelerometer_msg);
404  geometry_msgs::Vector3 gyro_msg;
405  gyro_msg.x = group_fbk[i].imu().gyro().get().getX();
406  gyro_msg.y = group_fbk[i].imu().gyro().get().getY();
407  gyro_msg.z = group_fbk[i].imu().gyro().get().getZ();
408  feedback_msg.gyro.push_back(gyro_msg);
409  geometry_msgs::Quaternion orient_msg;
410  const auto& orient = group_fbk[i].imu().orientation().get();
411  orient_msg.w = orient.getW();
412  orient_msg.x = orient.getX();
413  orient_msg.y = orient.getY();
414  orient_msg.z = orient.getZ();
415  feedback_msg.orientation.push_back(orient_msg);
416  feedback_msg.deflection.push_back(group_fbk[i].actuator().deflection().get());
417  feedback_msg.deflection_velocity.push_back(group_fbk[i].actuator().deflectionVelocity().get());
418  feedback_msg.motor_velocity.push_back(group_fbk[i].actuator().motorVelocity().get());
419  feedback_msg.motor_current.push_back(group_fbk[i].actuator().motorCurrent().get());
420  feedback_msg.motor_winding_current.push_back(group_fbk[i].actuator().motorWindingCurrent().get());
421  feedback_msg.motor_sensor_temperature.push_back(
422  group_fbk[i].actuator().motorSensorTemperature().get());
423  feedback_msg.motor_winding_temperature.push_back(
424  group_fbk[i].actuator().motorWindingTemperature().get());
425  feedback_msg.motor_housing_temperature.push_back(
426  group_fbk[i].actuator().motorHousingTemperature().get());
427  feedback_msg.board_temperature.push_back(group_fbk[i].boardTemperature().get());
428  feedback_msg.processor_temperature.push_back(group_fbk[i].processorTemperature().get());
429  feedback_msg.voltage.push_back(group_fbk[i].voltage().get());
430  std_msgs::ColorRGBA led_msg;
431  led_msg.r = group_fbk[i].led().getColor().getRed();
432  led_msg.g = group_fbk[i].led().getColor().getGreen();
433  led_msg.b = group_fbk[i].led().getColor().getBlue();
434  if (group_fbk[i].led().hasColor()) {
435  led_msg.a = 255;
436  }
437  else {
438  led_msg.a = 0;
439  }
440  feedback_msg.led_color.push_back(led_msg);
441  feedback_msg.sequence_number.push_back(group_fbk[i].actuator().sequenceNumber().get());
442  feedback_msg.receive_time.push_back(group_fbk[i].actuator().receiveTime().get());
443  feedback_msg.transmit_time.push_back(group_fbk[i].actuator().transmitTime().get());
444  feedback_msg.hardware_receive_time.push_back(group_fbk[i].actuator().hardwareReceiveTime().get());
445  feedback_msg.hardware_transmit_time.push_back(
446  group_fbk[i].actuator().hardwareTransmitTime().get());
447  }
448 
449  group->feedback_msg = feedback_msg;
450  group->joint_state_msg = joint_state_msg;
451 
452  HebirosNode::publishers_physical.feedback(feedback_msg, group_name);
453  HebirosNode::publishers_physical.feedbackJointState(joint_state_msg, group_name);
454 }
455 
456 
457 
458 
static int getInt(std::string name)
static void addEffortGainsCommand(hebi::GroupCommand *group_command, hebiros::PidGainsMsg data, std::string group_name)
A list of Feedback objects that can be received from a Group of modules; the size() must match the nu...
void command(const boost::shared_ptr< hebiros::CommandMsg const > data, std::string group_name)
static HebirosPublishersPhysical publishers_physical
Definition: hebiros.h:80
Definition: color.hpp:5
sensor_msgs::JointState joint_state_msg
Definition: hebiros_group.h:19
void registerGroupSubscribers(std::string group_name)
size_t size() const
Returns the number of module feedbacks in this group feedback.
static void addVelocityGainsCommand(hebi::GroupCommand *group_command, hebiros::PidGainsMsg data, std::string group_name)
#define ROS_WARN(...)
static std::shared_ptr< ros::NodeHandle > n_ptr
Definition: hebiros.h:78
static void addSettingsCommand(hebi::GroupCommand *group_command, hebiros::SettingsMsg data, std::string group_name)
hebiros::FeedbackMsg feedback_msg
Definition: hebiros_group.h:20
std::shared_ptr< hebi::Group > group_ptr
static HebirosGroupRegistry & Instance()
static void addJointCommand(hebi::GroupCommand *group_command, sensor_msgs::JointState data, std::string group_name)
static void addPositionGainsCommand(hebi::GroupCommand *group_command, hebiros::PidGainsMsg data, std::string group_name)
HebirosGroup * getGroup(const std::string &name)
std::map< std::string, int > joints
Definition: hebiros_group.h:18
A list of Command objects appropriate for sending to a Group of modules; the size() must match the nu...
void feedbackJointState(sensor_msgs::JointState joint_state_msg, std::string group_name)
void jointCommand(const boost::shared_ptr< sensor_msgs::JointState const > data, std::string group_name)
static bool jointFound(std::string group_name, std::string joint_name)
static Time now()
static void jointNotFound(std::string joint_name)
void feedback(std::string group_name, const hebi::GroupFeedback &group_fbk)
void feedback(hebiros::FeedbackMsg feedback_msg, std::string group_name)


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:14