hebiros_services_physical.cpp
Go to the documentation of this file.
2 #include "hebiros.h"
4 
5 #include <memory>
6 
8 
9  services["hebiros/entry_list"] = HebirosNode::n_ptr->advertiseService(
10  "hebiros/entry_list", &HebirosServicesPhysical::entryList, this);
11 
12  services["hebiros/add_group_from_names"] = HebirosNode::n_ptr->advertiseService(
13  "hebiros/add_group_from_names", &HebirosServicesPhysical::addGroupFromNames, this);
14 
15  services["hebiros/add_group_from_urdf"] = HebirosNode::n_ptr->advertiseService(
16  "hebiros/add_group_from_urdf", &HebirosServicesPhysical::addGroupFromURDF, this);
17 
18  services["hebiros/add_model_from_urdf"] = HebirosNode::n_ptr->advertiseService(
19  "hebiros/add_model_from_urdf", &HebirosServicesPhysical::addModelFromURDF, this);
20 }
21 
22 void HebirosServicesPhysical::registerGroupServices(std::string group_name) {
23 
24  services["hebiros/"+group_name+"/size"] =
25  HebirosNode::n_ptr->advertiseService<SizeSrv::Request, SizeSrv::Response>(
26  "hebiros/"+group_name+"/size",
27  boost::bind(&HebirosServicesPhysical::size, this, _1, _2, group_name));
28 
29  services["hebiros/"+group_name+"/set_feedback_frequency"] =
30  HebirosNode::n_ptr->advertiseService<SetFeedbackFrequencySrv::Request,
31  SetFeedbackFrequencySrv::Response>(
32  "hebiros/"+group_name+"/set_feedback_frequency",
33  boost::bind(&HebirosServicesPhysical::setFeedbackFrequency, this, _1, _2, group_name));
34 
35  services["hebiros/"+group_name+"/set_command_lifetime"] =
36  HebirosNode::n_ptr->advertiseService<SetCommandLifetimeSrv::Request,
37  SetCommandLifetimeSrv::Response>(
38  "hebiros/"+group_name+"/set_command_lifetime",
39  boost::bind(&HebirosServicesPhysical::setCommandLifetime, this, _1, _2, group_name));
40 
41  services["hebiros/"+group_name+"/send_command_with_acknowledgement"] =
42  HebirosNode::n_ptr->advertiseService<SendCommandWithAcknowledgementSrv::Request,
43  SendCommandWithAcknowledgementSrv::Response>(
44  "hebiros/"+group_name+"/send_command_with_acknowledgement",
45  boost::bind(&HebirosServicesPhysical::sendCommandWithAcknowledgement, this, _1, _2, group_name));
46 }
47 
49  EntryListSrv::Request &req, EntryListSrv::Response &res) {
50 
51  EntryListMsg entry_list_msg;
52  EntryMsg entry_msg;
53 
54  std::shared_ptr<Lookup::EntryList> entry_list = lookup.getEntryList();
55  entry_list_msg.size = entry_list->size();
56 
57  ROS_INFO("Entry list:");
58  for (auto entry : *entry_list) {
59  entry_msg.name = entry.name_;
60  entry_msg.family = entry.family_;
61  // TODO: add entry.mac_address_ here! (it is now properly filled in)
62  entry_msg.mac_address = 0;
63 
64  ROS_INFO("/%s/%s/%2x:%2x:%2x:%2x:%2x:%2x", entry_msg.family.c_str(), entry_msg.name.c_str(),
65  entry.mac_address_[0], entry.mac_address_[1], entry.mac_address_[2],
66  entry.mac_address_[3], entry.mac_address_[4], entry.mac_address_[5]);
67  entry_list_msg.entries.push_back(entry_msg);
68  }
69 
70  res.entry_list = entry_list_msg;
71  return true;
72 }
73 
75  AddGroupFromNamesSrv::Request &req, AddGroupFromNamesSrv::Response &res,
76  std::map<std::string, std::string> joint_full_names) {
77 
78  auto& registry = HebirosGroupRegistry::Instance();
79 
80  if (registry.hasGroup(req.group_name)) {
81 
82  ROS_WARN("Group [%s] already exists", req.group_name.c_str());
83  return true;
84  }
85 
86  std::shared_ptr<hebi::Group> group_ptr = lookup.getGroupFromNames(req.families, req.names);
87 
88  if (!group_ptr) {
89 
90  ROS_WARN("Lookup of group [%s] failed", req.group_name.c_str());
91  return false;
92  }
93 
94  std::unique_ptr<HebirosGroupPhysical> group(new HebirosGroupPhysical(group_ptr));
95  group->joint_full_names = joint_full_names;
96 
97  if (!HebirosServices::addGroup(req, res, joint_full_names, std::move(group))) {
98  return false;
99  }
100 
101  registerGroupServices(req.group_name);
105 
106  return true;
107 }
108 
110  AddGroupFromNamesSrv::Request &req, AddGroupFromNamesSrv::Response &res) {
111 
112  std::map<std::string, std::string> joint_full_names;
113 
114  return HebirosNode::services_physical.addGroup(req, res, joint_full_names);
115 }
116 
118  AddGroupFromURDFSrv::Request &req, AddGroupFromURDFSrv::Response &res) {
119 
120  std::string urdf_name("robot_description");
121  urdf::Model urdf_model;
122  if (!urdf_model.initParam(urdf_name))
123  {
124  ROS_WARN("Could not load robot_description");
125  return false;
126  }
127  else {
129  }
130 
131  std::set<std::string> joint_names;
132  std::set<std::string> family_names;
133  std::map<std::string, std::string> joint_full_names;
134 
135  HebirosServices::addJointChildren(joint_names, family_names, joint_full_names,
136  urdf_model.getRoot().get());
137 
138  AddGroupFromNamesSrv::Request names_req;
139  AddGroupFromNamesSrv::Response names_res;
140  names_req.group_name = req.group_name;
141  names_req.families.insert(names_req.families.end(), family_names.begin(), family_names.end());
142  names_req.names.insert(names_req.names.end(), joint_names.begin(), joint_names.end());
143 
144  return HebirosNode::services_physical.addGroup(names_req, names_res, joint_full_names);
145 }
146 
148  AddModelFromURDFSrv::Request &req, AddModelFromURDFSrv::Response &res) {
149 
150  return HebirosServices::addModelFromURDF(req, res);
151 }
152 
154  SizeSrv::Request &req, SizeSrv::Response &res, std::string group_name) {
155 
156  HebirosServices::size(req, res, group_name);
157 
158  return true;
159 }
160 
161 // TODO: move this function to HebirosServices, not Physical
163  SetFeedbackFrequencySrv::Request &req, SetFeedbackFrequencySrv::Response &res,
164  std::string group_name) {
165 
166  auto& registry = HebirosGroupRegistry::Instance();
167 
168  HebirosGroup* group = registry.getGroup(group_name);
169  if (!group)
170  return false;
171  group->setFeedbackFrequency(req.feedback_frequency);
172 
173  HebirosServices::setFeedbackFrequency(req, res, group_name);
174 
175  return true;
176 }
177 
178 // TODO: move this function to HebirosServices, not Physical
180  SetCommandLifetimeSrv::Request &req, SetCommandLifetimeSrv::Response &res,
181  std::string group_name) {
182 
183  auto& registry = HebirosGroupRegistry::Instance();
184 
185  HebirosGroup* group = registry.getGroup(group_name);
186  if (!group)
187  return false;
188  group->setCommandLifetime(req.command_lifetime);
189 
190  HebirosServices::setCommandLifetime(req, res, group_name);
191 
192  return true;
193 }
194 
196  SendCommandWithAcknowledgementSrv::Request &req,
197  SendCommandWithAcknowledgementSrv::Response &res, std::string group_name) {
198 
199  // TODO: replace with better abstraction later -- move send command into group
200  HebirosGroupPhysical* group = dynamic_cast<HebirosGroupPhysical*>
202  if (!group) {
203  ROS_WARN("Improper group type during command call");
204  return false;
205  }
206 
207  GroupCommand group_command(group->size);
208 
209  sensor_msgs::JointState joint_data;
210  joint_data.name = req.command.name;
211  joint_data.position = req.command.position;
212  joint_data.velocity = req.command.velocity;
213  joint_data.effort = req.command.effort;
214  SettingsMsg settings_data;
215  settings_data = req.command.settings;
216 
217  HebirosSubscribersPhysical::addJointCommand(&group_command, joint_data, group_name);
218  HebirosSubscribersPhysical::addSettingsCommand(&group_command, settings_data, group_name);
219 
220  return group->group_ptr->sendCommandWithAcknowledgement(group_command);
221 
222  return true;
223 }
224 
225 
226 
bool sendCommandWithAcknowledgement(SendCommandWithAcknowledgementSrv::Request &req, SendCommandWithAcknowledgementSrv::Response &res, std::string group_name)
static HebirosPublishersPhysical publishers_physical
Definition: hebiros.h:80
bool addModelFromURDF(AddModelFromURDFSrv::Request &req, AddModelFromURDFSrv::Response &res)
bool addGroup(AddGroupFromNamesSrv::Request &req, AddGroupFromNamesSrv::Response &res, const std::map< std::string, std::string > &joint_full_names, std::unique_ptr< HebirosGroup > group_tmp)
virtual void setFeedbackFrequency(float frequency_hz)
bool setCommandLifetime(SetCommandLifetimeSrv::Request &req, SetCommandLifetimeSrv::Response &res, std::string group_name)
void registerGroupPublishers(std::string group_name)
void registerGroupSubscribers(std::string group_name)
bool setFeedbackFrequency(SetFeedbackFrequencySrv::Request &req, SetFeedbackFrequencySrv::Response &res, std::string group_name)
void addJointChildren(std::set< std::string > &names, std::set< std::string > &families, std::map< std::string, std::string > &full_names, const urdf::Link *link)
#define ROS_WARN(...)
std::shared_ptr< EntryList > getEntryList()
Definition: lookup.cpp:185
static std::shared_ptr< ros::NodeHandle > n_ptr
Definition: hebiros.h:78
bool addGroupFromNames(AddGroupFromNamesSrv::Request &req, AddGroupFromNamesSrv::Response &res)
static void addSettingsCommand(hebi::GroupCommand *group_command, hebiros::SettingsMsg data, std::string group_name)
bool setFeedbackFrequency(SetFeedbackFrequencySrv::Request &req, SetFeedbackFrequencySrv::Response &res, std::string group_name)
virtual void setCommandLifetime(float lifetime_ms)
std::shared_ptr< hebi::Group > group_ptr
bool entryList(EntryListSrv::Request &req, EntryListSrv::Response &res)
#define ROS_INFO(...)
bool addModelFromURDF(AddModelFromURDFSrv::Request &req, AddModelFromURDFSrv::Response &res)
static HebirosGroupRegistry & Instance()
static void addJointCommand(hebi::GroupCommand *group_command, sensor_msgs::JointState data, std::string group_name)
HebirosGroup * getGroup(const std::string &name)
bool addGroup(AddGroupFromNamesSrv::Request &req, AddGroupFromNamesSrv::Response &res, std::map< std::string, std::string > joint_full_names)
URDF_EXPORT bool initParam(const std::string &param)
void registerGroupActions(std::string group_name)
A list of Command objects appropriate for sending to a Group of modules; the size() must match the nu...
void registerGroupServices(std::string group_name)
bool size(SizeSrv::Request &req, SizeSrv::Response &res, std::string group_name)
static HebirosSubscribersPhysical subscribers_physical
Definition: hebiros.h:82
bool size(SizeSrv::Request &req, SizeSrv::Response &res, std::string group_name)
static std::map< std::string, ros::ServiceServer > services
static HebirosActions actions
Definition: hebiros.h:86
bool setCommandLifetime(SetCommandLifetimeSrv::Request &req, SetCommandLifetimeSrv::Response &res, std::string group_name)
static HebirosServicesPhysical services_physical
Definition: hebiros.h:84
bool addGroupFromURDF(AddGroupFromURDFSrv::Request &req, AddGroupFromURDFSrv::Response &res)
bool addGroupFromURDF(AddGroupFromURDFSrv::Request &req, AddGroupFromURDFSrv::Response &res)
std::shared_ptr< Group > getGroupFromNames(const std::vector< std::string > &families, const std::vector< std::string > &names, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from modules with the given names and families.
Definition: lookup.cpp:16


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