hebiros_gazebo_plugin.cpp
Go to the documentation of this file.
2 
3 //Load the model and sdf from Gazebo
4 void HebirosGazeboPlugin::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf) {
5  this->model = _model;
6 
7  int argc = 0;
8  char **argv = NULL;
9  ros::init(argc, argv, "hebiros_gazebo_plugin_node");
10 
11  this->add_group_srv =
12  this->n->advertiseService<AddGroupFromNamesSrv::Request, AddGroupFromNamesSrv::Response>(
13  "hebiros_gazebo_plugin/add_group", boost::bind(
14  &HebirosGazeboPlugin::SrvAddGroup, this, _1, _2));
15 
16  this->robot_namespace = "";
17  if (_sdf->HasElement("robotNamespace")) {
18  this->robot_namespace = _sdf->GetElement("robotNamespace")->Get<std::string>();
19  }
20  if (this->robot_namespace == "") {
21  this->n.reset(new ros::NodeHandle);
22  } else {
23  this->n.reset(new ros::NodeHandle(this->robot_namespace));
24  }
25 
26  this->update_connection = event::Events::ConnectWorldUpdateBegin (
27  boost::bind(&HebirosGazeboPlugin::OnUpdate, this, _1));
28 
29  ROS_INFO("Loaded hebiros gazebo plugin");
30 }
31 
32 //Update the joints at every simulation iteration
33 void HebirosGazeboPlugin::OnUpdate(const common::UpdateInfo & _info) {
34  ros::Time current_time = ros::Time::now();
35 
36  for (auto group_pair : hebiros_groups) {
37  auto hebiros_group = group_pair.second;
38 
39  // Get the time elapsed since the last iteration
40  ros::Duration iteration_time = current_time - hebiros_group->prev_time;
41  hebiros_group->prev_time = current_time;
42  if (hebiros_group->group_added) {
43  UpdateGroup(hebiros_group, iteration_time);
44  }
45  }
46 }
47 
48 //Publish feedback and compute PID control to command a joint
49 void HebirosGazeboPlugin::UpdateGroup(std::shared_ptr<HebirosGazeboGroup> hebiros_group, const ros::Duration& iteration_time) {
50  for (auto joint_pair : hebiros_group->joints) {
51 
52  std::string joint_name = joint_pair.first;
53  std::shared_ptr<HebirosGazeboJoint> hebiros_joint = hebiros_group->joints[joint_name];
54 
55  physics::JointPtr joint = this->model->GetJoint(joint_name+"/"+hebiros_joint->model_name);
56 
57  if (joint) {
58 
59  std::shared_ptr<HebirosGazeboJoint> hebiros_joint = joint_pair.second;
60 
61  ros::Time current_time = ros::Time::now();
62  ros::Duration elapsed_time = current_time - hebiros_group->start_time;
63  ros::Duration feedback_time = current_time - hebiros_group->prev_feedback_time;
64 
65  int i = hebiros_joint->feedback_index;
66 
67  joint->SetProvideFeedback(true);
68  double position = joint->GetAngle(0).Radian();
69  double velocity = joint->GetVelocity(0);
70  physics::JointWrench wrench = joint->GetForceTorque(0);
71  auto trans = joint->GetChild()->GetInitialRelativePose().rot;
72  double effort = (-1 * (trans * wrench.body1Torque)).z;
73 
74  hebiros_group->feedback.position[i] = position;
75  hebiros_group->feedback.velocity[i] = velocity;
76  hebiros_group->feedback.effort[i] = effort;
77 
78  hebiros_group->feedback.accelerometer[i] = hebiros_joint->accelerometer;
79  hebiros_group->feedback.gyro[i] = hebiros_joint->gyro;
80 
81  // Add temperature feedback
82  hebiros_group->feedback.motor_winding_temperature[i] = hebiros_joint->temperature.getMotorWindingTemperature();
83  hebiros_group->feedback.motor_housing_temperature[i] = hebiros_joint->temperature.getMotorHousingTemperature();
84  hebiros_group->feedback.board_temperature[i] = hebiros_joint->temperature.getActuatorBodyTemperature();
85 
86  if (hebiros_group->command_received) {
87  double force = HebirosGazeboController::ComputeForce(hebiros_group, hebiros_joint,
88  position, velocity, effort, iteration_time);
89 
90  if ((hebiros_group->command_lifetime == 0) || (
91  elapsed_time.toSec() <= hebiros_group->command_lifetime/1000.0)) {
92 
93  joint->SetForce(0, force);
94  }
95 
96  int j = hebiros_joint->command_index;
97  if (i < hebiros_group->command_target.position.size()) {
98  hebiros_group->feedback.position_command[j] = hebiros_group->command_target.position[j];
99  }
100  if (i < hebiros_group->command_target.velocity.size()) {
101  hebiros_group->feedback.velocity_command[j] = hebiros_group->command_target.velocity[j];
102  }
103  if (i < hebiros_group->command_target.effort.size()) {
104  hebiros_group->feedback.effort_command[j] = hebiros_group->command_target.effort[j];
105  }
106  }
107 
108  if (!hebiros_group->feedback_pub.getTopic().empty() &&
109  feedback_time.toSec() >= 1.0/hebiros_group->feedback_frequency) {
110 
111  hebiros_group->feedback_pub.publish(hebiros_group->feedback);
112  hebiros_group->prev_feedback_time = current_time;
113  }
114  }
115  else {
116  ROS_WARN("Joint %s not found", joint_name.c_str());
117  }
118  }
119 }
120 
121 //Service callback which adds a group with corresponding joints
122 bool HebirosGazeboPlugin::SrvAddGroup(AddGroupFromNamesSrv::Request &req,
123  AddGroupFromNamesSrv::Response &res) {
124 
125  if (hebiros_groups.find(req.group_name) != hebiros_groups.end()) {
126  ROS_WARN("Group %s already exists", req.group_name.c_str());
127  return true;
128  }
129 
130  std::shared_ptr<HebirosGazeboGroup> hebiros_group =
131  std::make_shared<HebirosGazeboGroup>(req.group_name, this->n);
132 
133  hebiros_groups[req.group_name] = hebiros_group;
134 
135  for (int i = 0; i < req.families.size(); i++) {
136  for (int j = 0; j < req.names.size(); j++) {
137 
138  if ((req.families.size() == 1) ||
139  (req.families.size() == req.names.size() && i == j)) {
140 
141  std::string joint_name = req.families[i]+"/"+req.names[j];
142  hebiros_group->feedback.name.push_back(joint_name);
143 
144  AddJointToGroup(hebiros_group, joint_name);
145  }
146  }
147  }
148 
149  int size = hebiros_group->joints.size();
150 
151  hebiros_group->feedback.position.resize(size);
152  hebiros_group->feedback.motor_winding_temperature.resize(size);
153  hebiros_group->feedback.motor_housing_temperature.resize(size);
154  hebiros_group->feedback.board_temperature.resize(size);
155  hebiros_group->feedback.velocity.resize(size);
156  hebiros_group->feedback.effort.resize(size);
157  hebiros_group->feedback.position_command.resize(size);
158  hebiros_group->feedback.velocity_command.resize(size);
159  hebiros_group->feedback.effort_command.resize(size);
160  hebiros_group->feedback.accelerometer.resize(size);
161  hebiros_group->feedback.gyro.resize(size);
162 
163  hebiros_group->feedback_pub = this->n->advertise<FeedbackMsg>(
164  "hebiros_gazebo_plugin/feedback/"+req.group_name, 100);
165 
166  hebiros_group->group_added = true;
167 
168  return true;
169 }
170 
171 //Add a joint to an associated group
172 void HebirosGazeboPlugin::AddJointToGroup(std::shared_ptr<HebirosGazeboGroup> hebiros_group,
173  std::string joint_name) {
174 
175  std::string model_name = "";
176  bool is_x8 = false;
177  if (this->model->GetJoint(joint_name+"/X5_1")) {
178  model_name = "X5_1";
179  }
180  else if (this->model->GetJoint(joint_name+"/X5_4")) {
181  model_name = "X5_4";
182  }
183  else if (this->model->GetJoint(joint_name+"/X5_9")) {
184  model_name = "X5_9";
185  }
186  else if (this->model->GetJoint(joint_name+"/X8_3")) {
187  model_name = "X8_3";
188  is_x8 = true;
189  }
190  else if (this->model->GetJoint(joint_name+"/X8_9")) {
191  model_name = "X8_9";
192  is_x8 = true;
193  }
194  else if (this->model->GetJoint(joint_name+"/X8_16")) {
195  model_name = "X8_16";
196  is_x8 = true;
197  }
198 
199  std::shared_ptr<HebirosGazeboJoint> hebiros_joint =
200  std::make_shared<HebirosGazeboJoint>(joint_name, model_name, is_x8, this->n);
201 
202  hebiros_joint->feedback_index = hebiros_group->joints.size();
203  hebiros_joint->command_index = hebiros_joint->feedback_index;
204 
205  HebirosGazeboController::SetSettings(hebiros_group, hebiros_joint);
206  hebiros_group->joints[joint_name] = hebiros_joint;
207 }
208 
209 //Tell Gazebo about this plugin
ros::ServiceServer add_group_srv
bool SrvAddGroup(AddGroupFromNamesSrv::Request &req, AddGroupFromNamesSrv::Response &res)
void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
static void SetSettings(std::shared_ptr< HebirosGazeboGroup > hebiros_group, std::shared_ptr< HebirosGazeboJoint > hebiros_joint)
#define ROS_WARN(...)
event::ConnectionPtr update_connection
void OnUpdate(const common::UpdateInfo &_info)
void AddJointToGroup(std::shared_ptr< HebirosGazeboGroup > hebiros_group, std::string joint_name)
#define ROS_INFO(...)
std::map< std::string, std::shared_ptr< HebirosGazeboGroup > > hebiros_groups
argv
void UpdateGroup(std::shared_ptr< HebirosGazeboGroup > hebiros_group, const ros::Duration &iteration_time)
static Time now()
GZ_REGISTER_MODEL_PLUGIN(HebirosGazeboPlugin)
static double ComputeForce(std::shared_ptr< HebirosGazeboGroup > hebiros_group, std::shared_ptr< HebirosGazeboJoint > hebiros_joint, double position, double velocity, double effort, const ros::Duration &iteration_time)
physics::ModelPtr model
std::shared_ptr< ros::NodeHandle > n


hebiros_gazebo_plugin
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:13:55