ros_control_interface.hpp
Go to the documentation of this file.
1 #ifndef __ROS_CONTROL_INTERFACE__
2 #define __ROS_CONTROL_INTERFACE__
3 
6 #include <urdf/model.h>
8 
10 {
12 {
16 };
17 
21 template <class JointInterface>
23  : public controller_interface::Controller<JointInterface>
24 {
25  public:
27 
28  bool init(JointInterface* hw, ros::NodeHandle& nh);
29  void starting(const ros::Time& time);
30  void update(const ros::Time& time, const ros::Duration& period);
31  void stopping(const ros::Time& time);
32 
33  protected:
42  virtual std::shared_ptr<ControllerBase> initController(
43  ros::NodeHandle& nh) const = 0;
44 
45  private:
46  std::shared_ptr<ControllerBase> controller_;
48  unsigned int n_joints_;
49  std::vector<std::string> joint_names_;
50  std::vector<urdf::JointConstSharedPtr> joint_urdfs_;
51  std::vector<hardware_interface::JointHandle> joints_;
52 };
53 
54 // Implementation in header file due to being a template class.
55 
56 template <class JointInterface>
58 {
59  controller_ = NULL;
60 
61  // check interface type
62  JointInterface* hw = new JointInterface();
64  dynamic_cast<hardware_interface::EffortJointInterface*>(hw))
65  {
67  }
69  dynamic_cast<hardware_interface::VelocityJointInterface*>(hw))
70  {
72  }
74  dynamic_cast<hardware_interface::PositionJointInterface*>(hw))
75  {
77  }
78  else
79  {
80  throw std::logic_error("UNKNOWN JOINT INTERFACE TYPE");
81  }
82 }
83 
84 template <class JointInterface>
86  ros::NodeHandle& nh)
87 {
89  if (!controller_)
90  {
91  ROS_ERROR_STREAM("Failed to initialize controller algorithm! (namespace: "
92  << nh.getNamespace() << ").");
93  }
94 
95  // joint initialization from
96  // https://github.com/ros-controls/ros_controllers/blob/melodic-devel/effort_controllers/src/joint_group_position_controller.cpp
97 
98  // joint_names_ = hw->getNames();
99  if (!nh.getParam("joints", joint_names_))
100  {
101  ROS_ERROR("Missing joint names parameter");
102  return false;
103  }
104 
105  n_joints_ = joint_names_.size();
106 
107  // Get URDF
109  if (!urdf.initParam("robot_description"))
110  {
111  ROS_ERROR("Failed to parse urdf file");
112  return false;
113  }
114 
115  for (unsigned int i = 0; i < n_joints_; i++)
116  {
117  const std::string& name = joint_names_[i];
118 
119  try
120  {
121  joints_.push_back(hw->getHandle(name));
122  }
124  {
125  ROS_ERROR_STREAM("Exception: " << e.what());
126  }
127 
128  urdf::JointConstSharedPtr joint_urdf = urdf.getJoint(name);
129  if (!joint_urdf)
130  {
131  ROS_ERROR("Could not find joint '%s' in urdf", name.c_str());
132  return false;
133  }
134  joint_urdfs_.push_back(joint_urdf);
135  }
136 
137  controller_->resetInternalState();
138 
139  return true;
140 }
141 
142 template <class JointInterface>
144 {
145  controller_->resetInternalState();
146 }
147 
148 template <class JointInterface>
150  const ros::Duration& period)
151 {
152  sensor_msgs::JointState curr_state, command;
153 
154  for (unsigned int i = 0; i < n_joints_; i++)
155  {
156  curr_state.name.push_back(joint_names_[i]);
157  curr_state.position.push_back(joints_[i].getPosition());
158  curr_state.velocity.push_back(joints_[i].getVelocity());
159  curr_state.effort.push_back(joints_[i].getEffort());
160  }
161 
162  command = controller_->updateControl(curr_state, period);
163 
164  for (unsigned int i = 0; i < n_joints_; i++)
165  {
166  const std::string& name = joint_names_[i];
167 
168  for (unsigned int j = 0; j < command.name.size(); j++)
169  {
170  if (command.name[j] == name)
171  {
172  switch (joint_type_)
173  {
174  case EFFORT:
175  joints_[i].setCommand(command.effort[j]);
176  break;
177  case VELOCITY:
178  joints_[i].setCommand(command.velocity[j]);
179  break;
180  case POSITION:
181  joints_[i].setCommand(command.position[j]);
182  break;
183  }
184  }
185  }
186  }
187 }
188 
189 template <class JointInterface>
191 {
192  controller_->resetInternalState();
193 }
194 } // namespace generic_control_toolbox
195 #endif
#define ROS_ERROR(...)
const char * what() const noexcept override
ROSLIB_DECL std::string command(const std::string &cmd)
bool getParam(const std::string &key, std::string &s) const
bool init(JointInterface *hw, ros::NodeHandle &nh)
void update(const ros::Time &time, const ros::Duration &period)
URDF_EXPORT bool initParam(const std::string &param)
std::vector< urdf::JointConstSharedPtr > joint_urdfs_
virtual std::shared_ptr< ControllerBase > initController(ros::NodeHandle &nh) const =0
std::vector< hardware_interface::JointHandle > joints_
const std::string & getNamespace() const
#define ROS_ERROR_STREAM(args)
std::shared_ptr< ControllerBase > controller_


generic_control_toolbox
Author(s): diogo
autogenerated on Mon Feb 28 2022 22:24:37