hebiros_services.cpp
Go to the documentation of this file.
1 #include "hebiros_services.h"
2 
3 #include "hebiros.h"
4 
5 #include "hebiros_group.h"
7 
8 std::map<std::string, ros::ServiceServer> HebirosServices::services;
9 
10 void HebirosServices::registerModelServices(const std::string& model_name) {
11  services["hebiros/"+model_name+"/fk"] =
12  HebirosNode::n_ptr->advertiseService<ModelFkSrv::Request, ModelFkSrv::Response>(
13  "hebiros/"+model_name+"/fk",
14  boost::bind(&HebirosServices::fk, this, _1, _2, model_name));
15 }
16 
18  EntryListSrv::Request &req, EntryListSrv::Response &res) {
19 
20  return true;
21 }
22 
24  AddGroupFromNamesSrv::Request &req, AddGroupFromNamesSrv::Response &res,
25  const std::map<std::string, std::string>& joint_full_names, std::unique_ptr<HebirosGroup> group_tmp) {
26 
27  if (req.families.size() != 1 && req.families.size() != req.names.size()) {
28  ROS_WARN("Invalid number of familes for group [%s]", req.group_name.c_str());
29  return false;
30  }
31 
32  auto& registry = HebirosGroupRegistry::Instance();
33 
34  if (registry.hasGroup(req.group_name))
35  {
36  ROS_WARN("Group [%s] already exists!", req.group_name.c_str());
37  return false;
38  }
39 
40  registry.addGroup(req.group_name, std::move(group_tmp));
41  HebirosGroup* group = registry.getGroup(req.group_name);
42 
43  ROS_INFO("Created group [%s]:", req.group_name.c_str());
44  for (int i = 0; i < req.families.size(); i++) {
45  for (int j = 0; j < req.names.size(); j++) {
46 
47  if ((req.families.size() == 1) ||
48  (req.families.size() == req.names.size() && i == j)) {
49 
50  std::string joint_name = req.families[i]+"/"+req.names[j];
51  ROS_INFO("/%s/%s", req.group_name.c_str(), joint_name.c_str());
52 
53  group->joints[joint_name] = j;
54  }
55  }
56  }
57 
58  group->joint_full_names = joint_full_names;
59  group->size = group->joints.size();
60 
61  return true;
62 }
63 
65  AddGroupFromURDFSrv::Request &req, AddGroupFromURDFSrv::Response &res) {
66 
67  ROS_INFO("Loaded URDF from robot_description");
68 
69  return true;
70 }
71 
72 bool HebirosServices::split(const std::string &orig, std::string &name, std::string &family) {
73  std::stringstream ss(orig);
74 
75  if (!std::getline(ss, family, '/')) {
76  return false;
77  }
78  if (!std::getline(ss, name, '/')) {
79  return false;
80  }
81 
82  return true;
83 }
84 
85 void HebirosServices::addJointChildren(std::set<std::string>& names,
86  std::set<std::string>& families, std::map<std::string, std::string>& full_names,
87  const urdf::Link* link) {
88  for (auto& joint : link->child_joints) {
89 
90  if (joint->type != urdf::Joint::FIXED) {
91  std::string name, family;
92 
93  if (split(joint->name, name, family)) {
94  names.insert(name);
95  families.insert(family);
96  full_names[family+'/'+name] = joint->name;
97  }
98  }
99  }
100 
101  for (auto& link_child : link->child_links) {
102  addJointChildren(names, families, full_names, link_child.get());
103  }
104 }
105 
107  AddModelFromURDFSrv::Request &req, AddModelFromURDFSrv::Response &res) {
108  // Default to "robot_description" if not given.
109  static const std::string default_desc("robot_description");
110  auto& description_param = req.description_param.size() == 0 ? default_desc : req.description_param;
111 
112  if (HebirosModel::load(req.model_name, req.description_param)) {
113  registerModelServices(req.model_name);
114  return true;
115  }
116  return false;
117 }
118 
120  SizeSrv::Request &req, SizeSrv::Response &res, std::string group_name) {
121 
122  auto& registry = HebirosGroupRegistry::Instance();
123 
124  if (!registry.hasGroup(group_name))
125  {
126  res.size = -1;
127  ROS_INFO("hebiros/%s not found; could not get size", group_name.c_str());
128  return false;
129  }
130 
131  res.size = registry.getGroup(group_name)->size;
132 
133  ROS_INFO("hebiros/%s size=%d", group_name.c_str(), res.size);
134 
135  return true;
136 }
137 
139  SetFeedbackFrequencySrv::Request &req, SetFeedbackFrequencySrv::Response &res,
140  std::string group_name) {
141 
142  ROS_INFO("hebiros/%s feedback_frequency=%d", group_name.c_str(), req.feedback_frequency);
143 
144  return true;
145 }
146 
148  SetCommandLifetimeSrv::Request &req, SetCommandLifetimeSrv::Response &res,
149  std::string group_name) {
150 
151  ROS_INFO("hebiros/%s command_lifetime=%d", group_name.c_str(), req.command_lifetime);
152 
153  return true;
154 }
155 
157  SendCommandWithAcknowledgementSrv::Request &req,
158  SendCommandWithAcknowledgementSrv::Response &res, std::string group_name) {
159 
160  return true;
161 }
162 
163 bool HebirosServices::fk(ModelFkSrv::Request& req, ModelFkSrv::Response& res, const std::string& model_name) {
164  // Look up model
165  auto model = HebirosModel::getModel(model_name);
166  if (!model)
167  return false;
168 
169  // Convert ROS message into HEBI types
170  HebiFrameType frame_type = HebiFrameTypeOutput;
171  if (req.frame_type == ModelFkSrv::Request::FrameTypeCenterOfMass)
172  frame_type == HebiFrameTypeCenterOfMass;
173  else if (req.frame_type == ModelFkSrv::Request::FrameTypeOutput)
174  frame_type == HebiFrameTypeOutput;
175  else
176  return false; // Invalid frame type!
177  Eigen::VectorXd joints(req.positions.size());
178  for (size_t i = 0; i < joints.size(); ++i)
179  joints[i] = req.positions[i];
180 
181  // Get the frames from the robot model
183  model->getModel().getFK(HebiFrameTypeOutput, joints, frames);
184 
185  // Fill in the ROS messages
186  res.frames.resize(frames.size() * 16);
187  for (size_t i = 0; i < frames.size(); ++i) {
188  for (size_t j = 0; j < 4; ++j) {
189  for (size_t k = 0; k < 4; ++k) {
190  res.frames[i * 16 + j * 4 + k] = frames[i](j, k);
191  }
192  }
193  }
194 
195  return true;
196 }
std::vector< Matrix4d, Eigen::aligned_allocator< Eigen::Matrix4d > > Matrix4dVector
Definition: robot_model.hpp:14
bool entryList(EntryListSrv::Request &req, EntryListSrv::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)
HebiFrameType
Definition: hebi.h:280
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)
bool fk(ModelFkSrv::Request &req, ModelFkSrv::Response &res, const std::string &model_name)
void registerModelServices(const std::string &model_name)
#define ROS_WARN(...)
static std::shared_ptr< ros::NodeHandle > n_ptr
Definition: hebiros.h:78
bool split(const std::string &orig, std::string &name, std::string &family)
#define ROS_INFO(...)
bool addModelFromURDF(AddModelFromURDFSrv::Request &req, AddModelFromURDFSrv::Response &res)
std::map< std::string, int > joints
Definition: hebiros_group.h:18
hebi::robot_model::RobotModel & getModel()
bool size(SizeSrv::Request &req, SizeSrv::Response &res, std::string group_name)
static bool load(const std::string &name, const std::string &description_param)
bool sendCommandWithAcknowledgement(SendCommandWithAcknowledgementSrv::Request &req, SendCommandWithAcknowledgementSrv::Response &res, std::string group_name)
static std::map< std::string, ros::ServiceServer > services
bool setCommandLifetime(SetCommandLifetimeSrv::Request &req, SetCommandLifetimeSrv::Response &res, std::string group_name)
bool addGroupFromURDF(AddGroupFromURDFSrv::Request &req, AddGroupFromURDFSrv::Response &res)
std::map< std::string, std::string > joint_full_names
Definition: hebiros_group.h:17


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