denso_hardware_interface.h
Go to the documentation of this file.
10 #include <urdf/model.h>
12 #include <iterator>
13 #include "denso_robot.h"
14 
15 using namespace hardware_interface;
22 
23 using namespace transmission_interface;
25 {
26 public:
27  DensoRobotHW() : raddeg_trans_(180/M_PI,0), m_mm_trans_(100, 0) // for gripper
28  {
30  }
31  virtual ~DensoRobotHW() {
32  robot_.reset();
33  }
34 
35  void appendJoint(const KDL::SegmentMap::const_iterator segment, std::vector<const KDL::Joint*>& joints) {
36  joints.push_back(&(segment->second.segment.getJoint()));
37  ROS_DEBUG_STREAM("append joint: " << segment->second.segment.getJoint().getName() << " at " << segment->first << ", childrensize: " << segment->second.children.size());
38  for (int i = 0; i < segment->second.children.size(); i++) {
39  appendJoint(segment->second.children[i], joints);
40  }
41  }
42 
43  virtual bool init(ros::NodeHandle& root_nh, ros::NodeHandle &robot_hw_nh) {
44  std::string robot_description;
45  if (!robot_hw_nh.getParam("robot_description", robot_description)) {
46  ROS_ERROR("Failed to get robot_description");
47  return false;
48  }
49  urdf::Model model;
50  if (!model.initString(robot_description)){
51  ROS_ERROR("Failed to parse robot_description");
52  return false;
53  }
54  KDL::Tree tree;
55  //if (!kdl_parser::treeFromUrdfModel(model, tree)) {
56  if (!kdl_parser::treeFromString(robot_description, tree)){
57  ROS_ERROR("Failed to create tree from urdf");
58  return false;
59  }
60  KDL::SegmentMap::const_iterator root_segment = tree.getRootSegment();
61  std::vector<const KDL::Joint*> joints;
62  appendJoint(root_segment->second.children[0], joints);
63  // skip fixed joint https://stackoverflow.com/questions/4478636/stdremove-if-lambda-not-removing-anything-from-the-collection
64  auto new_end = std::remove_if(joints.begin(), joints.end(),
65  [](const KDL::Joint*& joint)->bool
66  { return joint->getType() == KDL::Joint::None; });
67  joints.erase(new_end, joints.end());
68 
69  int num_joints = joints.size();
70  cmd_.resize(num_joints);
71  pos_.resize(num_joints);
72  vel_.resize(num_joints);
73  eff_.resize(num_joints);
74  a_pos_.resize(num_joints);
75  a_pos_prev_.resize(num_joints);
76  a_vel_.resize(num_joints);
77  a_eff_.resize(num_joints);
78  a_cmd_.resize(num_joints);
79 
80  a_state_data_.resize(num_joints);
81  a_cmd_data_.resize(num_joints);
82  j_state_data_.resize(num_joints);
83  j_cmd_data_.resize(num_joints);
84 
85  // connect and register the joint state interface
86 
87  for (int i = 0; i < joints.size(); i++) {
88  hardware_interface::JointStateHandle state_handle(joints[i]->getName(), &pos_[i], &vel_[i], &eff_[i]);
89  jnt_state_interface_.registerHandle(state_handle);
90 
91  j_state_data_[i].position.push_back(&pos_[i]);
92  j_state_data_[i].velocity.push_back(&vel_[i]);
93  j_state_data_[i].effort.push_back(&eff_[i]);
94  a_state_data_[i].position.push_back(&a_pos_[i]);
95  a_state_data_[i].velocity.push_back(&a_vel_[i]);
96  a_state_data_[i].effort.push_back(&a_eff_[i]);
97  }
98  registerInterface(&jnt_state_interface_);
99 
100  // connect and register the joint position interface
101  for (int i = 0; i < joints.size(); i++) {
102  std::string jointname = joints[i]->getName();
103  hardware_interface::JointHandle pos_handle(jnt_state_interface_.getHandle(jointname), &cmd_[i]);
104  jnt_eff_interface_.registerHandle(pos_handle);
105 
106  a_cmd_data_[i].position.push_back(&a_cmd_[i]); // Velocity and effort vectors are unused
107  j_cmd_data_[i].position.push_back(&cmd_[i]); // Velocity and effort vectors are unused
108 
109  // Register transmissions to each interface
110  act_to_jnt_state_.registerHandle(ActuatorToJointStateHandle(jointname + "_raddeg_trans_",
111  &raddeg_trans_,
112  a_state_data_[i],
113  j_state_data_[i]));
114 
115  jnt_to_act_pos_.registerHandle(JointToActuatorPositionHandle(jointname + "_raddeg_trans_",
116  &raddeg_trans_,
117  a_cmd_data_[i],
118  j_cmd_data_[i]));
119 
120  // Register joint limits
121  JointLimits limits;
122  SoftJointLimits soft_limits;
123  const bool urdf_limits_ok = getJointLimits(model.getJoint(jointname), limits);
124  if (!urdf_limits_ok) {
125  ROS_WARN("urdf limits int joint %s is not defined", jointname.c_str());
126  }
127  const bool urdf_soft_limits_ok = getSoftJointLimits(model.getJoint(jointname), soft_limits);
128  if (!urdf_soft_limits_ok) {
129  ROS_WARN("urdf soft limits int joint %s is not defined", jointname.c_str());
130  }
131  // Register handle in joint limits interface
132  PositionJointSoftLimitsHandle limits_handle(pos_handle, // We read the state and read/write the command
133  limits, // Limits spec
134  soft_limits); // Soft limits spec
135  //PositionJointSaturationHandle limits_handle(pos_handle, // We read the state and read/write the command
136  // limits); // Limits spec
137 
138  jnt_limits_interface_.registerHandle(limits_handle);
139  }
140  registerInterface(&jnt_eff_interface_);
141 
142  if (!robot_->initialize(root_nh, num_joints)) {
143  ROS_ERROR("failed to initialize denso robot");
144  robot_.reset();
145  return false;
146  }
147  if(!robot_->read(a_pos_)) {
148  ROS_WARN("failed to read joint values from robot");
149  robot_.reset();
150  return false;
151  }
152 
153  std::copy(a_pos_.begin(), a_pos_.end(), a_pos_prev_.begin());
154  act_to_jnt_state_.propagate();
155 
156  return true;
157  }
158 
159  void read(const ros::Time& time, const ros::Duration& period) {
160  // do nothing
161 
162  }
163 
164  void write(const ros::Time& time, const ros::Duration& period) {
165  jnt_limits_interface_.enforceLimits(period);
166  jnt_to_act_pos_.propagate();
167 
168  ROS_DEBUG_STREAM("a_cmd_: " << a_cmd_);
169  ROS_DEBUG_STREAM("cmd_: " << cmd_);
170  if(!robot_->write(a_cmd_, a_pos_)) {
171  ROS_ERROR("failed to move robot!");
172  }
173  for (int i = 0; i < a_pos_.size(); i++) {
174  a_vel_[i] = (a_pos_[i] - a_pos_prev_[i])/period.toSec();
175  }
176  act_to_jnt_state_.propagate();
177  std::copy(a_pos_.begin(), a_pos_.end(), a_pos_prev_.begin());
178  }
179 
180  ros::Time getTime() const { return ros::Time::now(); }
181  ros::Duration getPeriod() const { return ros::Duration(0.008); }
182 private:
189  // Actuator and joint space variables: wrappers around raw data
190  std::vector<ActuatorData> a_state_data_;
191  std::vector<ActuatorData> a_cmd_data_;
192  std::vector<JointData> j_state_data_;
193  std::vector<JointData> j_cmd_data_;
194  //PositionJointSaturationInterface jnt_limits_interface_;
196 
197  // joints
198  std::vector<double> cmd_;
199  std::vector<double> pos_;
200  std::vector<double> vel_;
201  std::vector<double> eff_;
202 
203  // actuators
204  std::vector<double> a_pos_, a_pos_prev_;
205  std::vector<double> a_vel_;
206  std::vector<double> a_eff_;
207  std::vector<double> a_cmd_;
208 
210 };
211 
boost::shared_ptr< DensoRobot > robot_
URDF_EXPORT bool initString(const std::string &xmlstring)
SimpleTransmission m_mm_trans_
std::vector< double > vel_
std::string getName(void *handle)
std::vector< double > a_eff_
bool getSoftJointLimits(const std::string &joint_name, const ros::NodeHandle &nh, SoftJointLimits &soft_limits)
std::vector< JointData > j_state_data_
std::vector< double > pos_
std::vector< double > eff_
#define ROS_WARN(...)
virtual bool init(ros::NodeHandle &root_nh, ros::NodeHandle &robot_hw_nh)
std::vector< double > a_cmd_
std::vector< ActuatorData > a_state_data_
ActuatorToJointStateInterface act_to_jnt_state_
ros::Duration getPeriod() const
bool getJointLimits(const std::string &joint_name, const ros::NodeHandle &nh, JointLimits &limits)
hardware_interface::JointStateInterface jnt_state_interface_
void write(const ros::Time &time, const ros::Duration &period)
std::vector< double > cmd_
void read(const ros::Time &time, const ros::Duration &period)
ros::Time getTime() const
KDL_PARSER_PUBLIC bool treeFromString(const std::string &xml, KDL::Tree &tree)
void appendJoint(const KDL::SegmentMap::const_iterator segment, std::vector< const KDL::Joint * > &joints)
#define ROS_DEBUG_STREAM(args)
hardware_interface::PositionJointInterface jnt_eff_interface_
std::vector< double > a_pos_prev_
PositionJointSoftLimitsInterface jnt_limits_interface_
SegmentMap::const_iterator getRootSegment() const
JointToActuatorPositionInterface jnt_to_act_pos_
bool getParam(const std::string &key, std::string &s) const
static Time now()
std::vector< ActuatorData > a_cmd_data_
SimpleTransmission raddeg_trans_
#define ROS_ERROR(...)
std::vector< double > a_vel_
std::vector< JointData > j_cmd_data_


denso_ros_control
Author(s):
autogenerated on Mon Jun 10 2019 13:13:14