gazebo_hardware_sim.cpp
Go to the documentation of this file.
1 
26 
27 #include <string>
28 #include <vector>
29 #include <map>
30 
31 namespace sr_gazebo_sim
32 {
33 
34  const std::string SrGazeboHWSim::j0_transmission_name = "sr_mechanism_model/J0Transmission";
35  const std::string SrGazeboHWSim::simple_transmission_name = "sr_mechanism_model/SimpleTransmission";
36 
38  DefaultRobotHWSim(), fake_state_(NULL)
39  {
40  }
41 
42  template<class T>
43  void SrGazeboHWSim::fixJointName(std::vector<T> *items, const std::string old_joint_name,
44  const std::string new_joint_name) const
45  {
46  if (items->size() > 0)
47  {
48  items->at(0).name_ = new_joint_name;
49 
50  std::string xml_data = items->at(0).xml_element_;
51  for (size_t index = xml_data.find(old_joint_name, 0); std::string::npos != index;
52  index = xml_data.find(old_joint_name, index))
53  {
54  xml_data.replace(index, old_joint_name.length(), new_joint_name);
55  index += new_joint_name.length();
56  }
57  items->at(0).xml_element_ = xml_data;
58  }
59  }
60 
61  void SrGazeboHWSim::addFakeTransmissionsForJ0(std::vector<transmission_interface::TransmissionInfo> *transmissions)
62  {
63  for (size_t i = 0; i < transmissions->size(); ++i)
64  {
65  if (j0_transmission_name == transmissions->at(i).type_)
66  {
67  if (transmissions->at(i).joints_.size() > 0)
68  {
69  const std::string joint_name = transmissions->at(i).joints_[0].name_;
70  std::string second_joint_name = joint_name;
71  second_joint_name[second_joint_name.size() - 1] = '2';
72  this->j2_j1_joints_[second_joint_name] = joint_name;
73  }
74  }
75  }
76 
77  for (size_t i = transmissions->size() - 1; i != static_cast<size_t>(-1); --i)
78  {
79  if (transmissions->at(i).joints_.size() > 0)
80  {
81  const std::string joint_name = transmissions->at(i).joints_[0].name_;
82 
83  if ((joint_name.size() > 0) && ('3' == joint_name[joint_name.size() - 1]))
84  {
85  std::string second_joint_name = joint_name;
86  second_joint_name[second_joint_name.size() - 1] = '2';
87 
88  if (this->j2_j1_joints_.count(second_joint_name) > 0)
89  {
90  transmission_interface::TransmissionInfo new_transmission = transmissions->at(i);
91 
92  fixJointName(&new_transmission.joints_, joint_name, second_joint_name);
93  fixJointName(&new_transmission.actuators_, joint_name, second_joint_name);
94  transmissions->push_back(new_transmission);
95  }
96  }
97  }
98  }
99  }
100 
101  bool SrGazeboHWSim::isHandJoint(const std::vector<transmission_interface::TransmissionInfo> &transmissions,
102  const std::string &joint_name) const
103  {
104  // TODO(Andriy): Reimplement this function. It is using simple assumption that hand joint has one of
105  // two types of transmission from sr_mechanism_model package.
106  bool result = false;
107  for (size_t i = 0; i < transmissions.size(); ++i)
108  {
109  if ((transmissions[i].joints_.size() > 0)
110  && ((j0_transmission_name == transmissions[i].type_) || (simple_transmission_name == transmissions[i].type_)))
111  {
112  if (transmissions[i].joints_[0].name_ == joint_name)
113  {
114  result = true;
115  }
116  }
117  }
118 
119  return result;
120  }
121 
123  const std::vector<transmission_interface::TransmissionInfo>
124  &transmissions)
125  {
126  this->fake_state_.robot_model_ = *urdf_model;
127 
128  for (std::map<std::string, boost::shared_ptr<urdf::Joint> >::const_iterator it = urdf_model->joints_.begin();
129  it != urdf_model->joints_.end(); ++it)
130  {
131  if (this->isHandJoint(transmissions, it->first))
132  {
133  this->fake_state_.joint_states_[it->first].joint_ = it->second;
134  this->fake_state_.joint_states_[it->first].calibrated_ = true;
135  }
136  }
137 
138  robot_state_interface_.registerHandle(ros_ethercat_model::RobotStateHandle("unique_robot_hw", &fake_state_));
140  ROS_INFO_STREAM("Registered fake state");
141  }
142 
144  std::vector<transmission_interface::TransmissionInfo> transmissions)
145  {
146  for (size_t j = 0; j < transmissions.size(); j++)
147  {
148  std::vector<std::string> joint_interfaces = transmissions[j].joints_[0].hardware_interfaces_;
149  if (joint_interfaces.size() > 1)
150  {
151  // We use the second interface to allow to have the hand trajectory controller running on top of the hand
152  // position controllers.
153  // We use this trick because the DefaultRobotHWSim only deals with one (the first) hardware interface
154  if (joint_interfaces[1] == "PositionJointInterface")
155  {
156  // Create position joint interface
159  &(this->fake_state_.getJointState(joint_names_[j])->commanded_position_));
160  pj_interface_.registerHandle(joint_handle);
161  }
162  }
163  }
164  }
165 
166  bool SrGazeboHWSim::initSim(const std::string &robot_namespace, ros::NodeHandle model_nh,
167  gazebo::physics::ModelPtr parent_model, const urdf::Model *const urdf_model,
168  std::vector<transmission_interface::TransmissionInfo> transmissions)
169  {
170  this->addFakeTransmissionsForJ0(&transmissions);
171  this->initializeFakeRobotState(urdf_model, transmissions);
172 
173  bool result = gazebo_ros_control::DefaultRobotHWSim::initSim(robot_namespace, model_nh, parent_model, urdf_model,
174  transmissions);
175  if (result)
176  {
177  registerSecondHardwareInterface(transmissions);
178  }
179 
180  return result;
181  }
182 
184  {
186 
187  for (unsigned int j = 0; j < n_dof_; ++j)
188  {
189  const std::string joint_name = joint_names_[j];
190  if (NULL != this->fake_state_.getJointState(joint_name))
191  {
192  this->fake_state_.getJointState(joint_name)->position_ = this->joint_position_[j];
193  this->fake_state_.getJointState(joint_name)->velocity_ = this->joint_velocity_[j];
194  this->fake_state_.getJointState(joint_name)->effort_ = this->joint_effort_[j];
195  }
196  }
197 
198  this->fake_state_.current_time_ = time;
199  }
200 
202  {
203  for (unsigned int j = 0; j < n_dof_; ++j)
204  {
205  std::string joint_name = joint_names_[j];
206  if (this->j2_j1_joints_.count(joint_name) > 0)
207  {
208  joint_name = this->j2_j1_joints_[joint_name];
209  }
210 
211  if (NULL != this->fake_state_.getJointState(joint_name))
212  {
213  this->joint_position_command_[j] = this->fake_state_.getJointState(joint_name)->commanded_position_;
214  this->joint_velocity_command_[j] = this->fake_state_.getJointState(joint_name)->commanded_velocity_;
215  this->joint_effort_command_[j] = this->fake_state_.getJointState(joint_name)->commanded_effort_;
216  }
217  }
218 
220  }
221 
222 } // namespace sr_gazebo_sim
223 
void readSim(ros::Time time, ros::Duration period)
hardware_interface::PositionJointInterface pj_interface_
std::vector< double > joint_position_command_
virtual void readSim(ros::Time time, ros::Duration period)
Gazebo custom hardware implementation.
std::vector< double > joint_position_
ros_ethercat_model::RobotStateInterface robot_state_interface_
hardware_interface::JointStateInterface js_interface_
std::vector< double > joint_velocity_
std::vector< std::string > joint_names_
virtual void writeSim(ros::Time time, ros::Duration period)
std::vector< double > joint_effort_
std::vector< double > joint_effort_command_
void registerHandle(const JointHandle &handle)
void initializeFakeRobotState(const urdf::Model *const urdf_model, const std::vector< transmission_interface::TransmissionInfo > &transmissions)
JointStateHandle getHandle(const std::string &name)
static const std::string simple_transmission_name
bool isHandJoint(const std::vector< transmission_interface::TransmissionInfo > &transmissions, const std::string &joint_name) const
void registerSecondHardwareInterface(std::vector< transmission_interface::TransmissionInfo > transmissions)
#define ROS_INFO_STREAM(args)
virtual bool initSim(const std::string &robot_namespace, ros::NodeHandle model_nh, gazebo::physics::ModelPtr parent_model, const urdf::Model *const urdf_model, std::vector< transmission_interface::TransmissionInfo > transmissions)
void fixJointName(std::vector< T > *items, const std::string old_joint_name, const std::string new_joint_name) const
bool initSim(const std::string &robot_namespace, ros::NodeHandle model_nh, gazebo::physics::ModelPtr parent_model, const urdf::Model *const urdf_model, std::vector< transmission_interface::TransmissionInfo > transmissions)
void addFakeTransmissionsForJ0(std::vector< transmission_interface::TransmissionInfo > *transmissions)
ros_ethercat_model::RobotState fake_state_
void writeSim(ros::Time time, ros::Duration period)
std::vector< ActuatorInfo > actuators_
PLUGINLIB_EXPORT_CLASS(transmission_interface::BiDirectionalEffortJointInterfaceProvider, transmission_interface::RequisiteProvider)
std::vector< double > joint_velocity_command_
static const std::string j0_transmission_name
boost::unordered_map< std::string, std::string > j2_j1_joints_


sr_gazebo_sim
Author(s): Andriy Petlovanyy
autogenerated on Tue Oct 13 2020 03:55:43