-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathgazebo_ros_controller_manager.cpp
462 lines (387 loc) · 17 KB
/
gazebo_ros_controller_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Copyright (c) 2008, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <fstream>
#include <iostream>
#include <math.h>
#include <unistd.h>
#include <set>
#include <map>
#include <angles/angles.h>
#include <urdf/model.h>
//#include <gazebo/XMLConfig.hh>
//#include "physics/physics.h"
#include <gazebo/physics/physics.hh>
#include <gazebo/sensors/sensors.hh>
#include <gazebo/common/common.hh>
#include <sdf/sdf.hh>
#include <sdf/Param.hh>
#include "pr2_gazebo_plugins/gazebo_ros_controller_manager.h"
namespace gazebo {
GazeboRosControllerManager::GazeboRosControllerManager()
{
}
/// \brief callback for setting models joints states
bool setModelsJointsStates(pr2_gazebo_plugins::SetModelsJointsStates::Request &req,
pr2_gazebo_plugins::SetModelsJointsStates::Response &res)
{
return true;
}
GazeboRosControllerManager::~GazeboRosControllerManager()
{
ROS_DEBUG("Calling FiniChild in GazeboRosControllerManager");
//pr2_hardware_interface::ActuatorMap::const_iterator it;
//for (it = hw_.actuators_.begin(); it != hw_.actuators_.end(); ++it)
// delete it->second; // why is this causing double free corrpution?
this->cm_->~ControllerManager();
this->rosnode_->shutdown();
#ifdef USE_CBQ
this->controller_manager_queue_.clear();
this->controller_manager_queue_.disable();
this->controller_manager_callback_queue_thread_.join();
#endif
this->ros_spinner_thread_.join();
delete this->cm_;
delete this->rosnode_;
if (this->fake_state_)
{
// why does this cause double free corrpution in destruction of RobotState?
//this->fake_state_->~RobotState();
delete this->fake_state_;
}
}
void GazeboRosControllerManager::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
{
// Get then name of the parent model
std::string modelName = _sdf->GetParent()->Get<std::string>("name");
// Get the world name.
this->world = _parent->GetWorld();
// Get a pointer to the model
this->parent_model_ = _parent;
// Error message if the model couldn't be found
if (!this->parent_model_)
gzerr << "Unable to get parent model\n";
// Listen to the update event. This event is broadcast every
// simulation iteration.
this->updateConnection = event::Events::ConnectWorldUpdateBegin(
boost::bind(&GazeboRosControllerManager::UpdateChild, this));
gzdbg << "plugin model name: " << modelName << "\n";
if (getenv("CHECK_SPEEDUP"))
{
#if GAZEBO_MAJOR_VERSION >= 8
wall_start_ = this->world->RealTime().Double();
sim_start_ = this->world->SimTime().Double();
#else
wall_start_ = this->world->GetRealTime().Double();
sim_start_ = this->world->GetSimTime().Double();
#endif
}
// check update rate against world physics update rate
// should be equal or higher to guarantee the wrench applied is not "diluted"
//if (this->updatePeriod > 0 &&
// (this->world->GetPhysicsEngine()->GetUpdateRate() > 1.0/this->updatePeriod))
// ROS_ERROR("gazebo_ros_force controller update rate is less than physics update rate, wrench applied will be diluted (applied intermittently)");
// get parameter name
this->robotNamespace = "";
if (_sdf->HasElement("robotNamespace"))
this->robotNamespace = _sdf->Get<std::string>("robotNamespace");
this->robotParam = "robot_description";
if (_sdf->HasElement("robotParam"))
this->robotParam = _sdf->Get<std::string>("robotParam");
this->robotParam = this->robotNamespace+"/" + this->robotParam;
if (!ros::isInitialized())
{
int argc = 0;
char** argv = NULL;
ros::init(argc,argv,"gazebo",ros::init_options::NoSigintHandler|ros::init_options::AnonymousName);
}
this->rosnode_ = new ros::NodeHandle(this->robotNamespace);
ROS_INFO("starting gazebo_ros_controller_manager plugin in ns: %s",this->robotNamespace.c_str());
// pr2_etherCAT calls ros::spin(), we'll thread out one spinner here to mimic that
this->ros_spinner_thread_ = boost::thread( boost::bind( &GazeboRosControllerManager::ControllerManagerROSThread,this ) );
// load a controller manager
this->cm_ = new pr2_controller_manager::ControllerManager(&hw_,*this->rosnode_);
#if GAZEBO_MAJOR_VERSION >= 8
this->hw_.current_time_ = ros::Time(this->world->SimTime().Double());
#else
this->hw_.current_time_ = ros::Time(this->world->GetSimTime().Double());
#endif
if (this->hw_.current_time_ < ros::Time(0.001)) this->hw_.current_time_ == ros::Time(0.001); // hardcoded to minimum of 1ms on start up
this->rosnode_->param("gazebo/start_robot_calibrated",this->fake_calibration_,true);
// read pr2 urdf
// setup actuators, then setup mechanism control node
ReadPr2Xml();
// Initializes the fake state (for running the transmissions backwards).
this->fake_state_ = new pr2_mechanism_model::RobotState(&this->cm_->model_);
// The gazebo joints and mechanism joints should match up.
if (this->cm_->state_ != NULL) // could be NULL if ReadPr2Xml is unsuccessful
{
for (unsigned int i = 0; i < this->cm_->state_->joint_states_.size(); ++i)
{
std::string joint_name = this->cm_->state_->joint_states_[i].joint_->name;
// fill in gazebo joints pointer
gazebo::physics::JointPtr joint = this->parent_model_->GetJoint(joint_name);
if (joint)
{
this->joints_.push_back(joint);
}
else
{
//ROS_WARN("A joint named \"%s\" is not part of Mechanism Controlled joints.\n", joint_name.c_str());
//this->joints_.push_back(NULL); // FIXME: cannot be null, must be an empty boost shared pointer
this->joints_.push_back(gazebo::physics::JointPtr()); // FIXME: cannot be null, must be an empty boost shared pointer
ROS_ERROR("A joint named \"%s\" is not part of Mechanism Controlled joints.\n", joint_name.c_str());
}
}
}
#ifdef USE_CBQ
// start custom queue for controller manager
this->controller_manager_callback_queue_thread_ = boost::thread( boost::bind( &GazeboRosControllerManager::ControllerManagerQueueThread,this ) );
#endif
}
void GazeboRosControllerManager::UpdateChild()
{
if (this->world->IsPaused()) return;
if (getenv("CHECK_SPEEDUP"))
{
#if GAZEBO_MAJOR_VERSION >= 8
double wall_elapsed = this->world->RealTime().Double() - wall_start_;
double sim_elapsed = this->world->SimTime().Double() - sim_start_;
#else
double wall_elapsed = this->world->GetRealTime().Double() - wall_start_;
double sim_elapsed = this->world->GetSimTime().Double() - sim_start_;
#endif
std::cout << " real time: " << wall_elapsed
<< " sim time: " << sim_elapsed
<< " speed up: " << sim_elapsed / wall_elapsed
<< std::endl;
}
assert(this->joints_.size() == this->fake_state_->joint_states_.size());
//--------------------------------------------------
// Pushes out simulation state
//--------------------------------------------------
//ROS_ERROR("joints_.size()[%d]",(int)this->joints_.size());
// Copies the state from the gazebo joints into the mechanism joints.
for (unsigned int i = 0; i < this->joints_.size(); ++i)
{
if (!this->joints_[i])
continue;
this->fake_state_->joint_states_[i].measured_effort_ = this->fake_state_->joint_states_[i].commanded_effort_;
if (this->joints_[i]->HasType(gazebo::physics::Base::HINGE_JOINT))
{
gazebo::physics::JointPtr hj = this->joints_[i];
#if GAZEBO_MAJOR_VERSION >= 8
this->fake_state_->joint_states_[i].position_ = this->fake_state_->joint_states_[i].position_ +
angles::shortest_angular_distance(this->fake_state_->joint_states_[i].position_,hj->Position(0));
#else
this->fake_state_->joint_states_[i].position_ = this->fake_state_->joint_states_[i].position_ +
angles::shortest_angular_distance(this->fake_state_->joint_states_[i].position_,hj->GetAngle(0).Radian());
#endif
this->fake_state_->joint_states_[i].velocity_ = hj->GetVelocity(0);
//if (this->joints_[i]->GetName() == "torso_lift_motor_screw_joint")
// ROS_WARN("joint[%s] [%f]",this->joints_[i]->GetName().c_str(), this->fake_state_->joint_states_[i].position_);
}
else if (this->joints_[i]->HasType(gazebo::physics::Base::SLIDER_JOINT))
{
gazebo::physics::JointPtr sj = this->joints_[i];
{
#if GAZEBO_MAJOR_VERSION >= 8
this->fake_state_->joint_states_[i].position_ = sj->Position(0);
#else
this->fake_state_->joint_states_[i].position_ = sj->GetAngle(0).Radian();
#endif
this->fake_state_->joint_states_[i].velocity_ = sj->GetVelocity(0);
}
//ROS_ERROR("joint[%s] is a slider [%f]",this->joints_[i]->GetName().c_str(),sj->GetAngle(0).Radian());
//if (this->joints_[i]->GetName() == "torso_lift_joint")
// ROS_WARN("joint[%s] [%f]",this->joints_[i]->GetName().c_str(), this->fake_state_->joint_states_[i].position_);
}
else
{
/*
ROS_WARN("joint[%s] is not hinge [%d] nor slider",this->joints_[i]->GetName().c_str(),
(unsigned int)gazebo::physics::Base::HINGE_JOINT
);
for (unsigned int j = 0; j < this->joints_[i]->GetTypeCount(); j++)
{
ROS_WARN(" types: %d hinge[%d] slider[%d]",(unsigned int)this->joints_[i]->GetType(j),(unsigned int)this->joints_[i]->HasType(gazebo::physics::Base::HINGE_JOINT),(unsigned int)this->joints_[i]->HasType(gazebo::physics::Base::SLIDER_JOINT));
}
*/
}
}
// Reverses the transmissions to propagate the joint position into the actuators.
this->fake_state_->propagateJointPositionToActuatorPosition();
//--------------------------------------------------
// Runs Mechanism Control
//--------------------------------------------------
#if GAZEBO_MAJOR_VERSION >= 8
this->hw_.current_time_ = ros::Time(this->world->SimTime().Double());
#else
this->hw_.current_time_ = ros::Time(this->world->GetSimTime().Double());
#endif
try
{
if (this->cm_->state_ != NULL) // could be NULL if ReadPr2Xml is unsuccessful
this->cm_->update();
}
catch (const char* c)
{
if (strcmp(c,"dividebyzero")==0)
ROS_WARN("pid controller reports divide by zero error");
else
ROS_WARN("unknown const char* exception: %s", c);
}
//--------------------------------------------------
// Takes in actuation commands
//--------------------------------------------------
// Reverses the transmissions to propagate the actuator commands into the joints.
this->fake_state_->propagateActuatorEffortToJointEffort();
// Copies the commands from the mechanism joints into the gazebo joints.
for (unsigned int i = 0; i < this->joints_.size(); ++i)
{
if (!this->joints_[i])
continue;
double effort = this->fake_state_->joint_states_[i].commanded_effort_;
double damping_coef = 0;
if (this->cm_->state_ != NULL) // could be NULL if ReadPr2Xml is unsuccessful
{
if (this->cm_->state_->joint_states_[i].joint_->dynamics)
damping_coef = this->cm_->state_->joint_states_[i].joint_->dynamics->damping;
}
if (this->joints_[i]->HasType(gazebo::physics::Base::HINGE_JOINT))
{
gazebo::physics::JointPtr hj = this->joints_[i];
double current_velocity = hj->GetVelocity(0);
double damping_force = damping_coef * current_velocity;
double effort_command = effort - damping_force;
hj->SetForce(0,effort_command);
//if (this->joints_[i]->GetName() == "torso_lift_motor_screw_joint")
// ROS_ERROR("gazebo [%s] command [%f] damping [%f]",this->joints_[i]->GetName().c_str(), effort, damping_force);
}
else if (this->joints_[i]->HasType(gazebo::physics::Base::SLIDER_JOINT))
{
gazebo::physics::JointPtr sj = this->joints_[i];
double current_velocity = sj->GetVelocity(0);
double damping_force = damping_coef * current_velocity;
double effort_command = effort-damping_force;
sj->SetForce(0,effort_command);
//if (this->joints_[i]->GetName() == "torso_lift_joint")
// ROS_ERROR("gazebo [%s] command [%f] damping [%f]",this->joints_[i]->GetName().c_str(), effort, damping_force);
}
}
}
void GazeboRosControllerManager::ReadPr2Xml()
{
std::string urdf_param_name;
std::string urdf_string;
// search and wait for robot_description on param server
while(urdf_string.empty())
{
ROS_INFO("gazebo controller manager plugin is waiting for urdf: %s on the param server. (make sure there is a rosparam by that name in the ros parameter server, otherwise, this plugin blocks simulation forever).", this->robotParam.c_str());
if (this->rosnode_->searchParam(this->robotParam,urdf_param_name))
{
this->rosnode_->getParam(urdf_param_name,urdf_string);
ROS_DEBUG("found upstream\n%s\n------\n%s\n------\n%s",this->robotParam.c_str(),urdf_param_name.c_str(),urdf_string.c_str());
}
else
{
this->rosnode_->getParam(this->robotParam,urdf_string);
ROS_DEBUG("found in node namespace\n%s\n------\n%s\n------\n%s",this->robotParam.c_str(),urdf_param_name.c_str(),urdf_string.c_str());
}
usleep(100000);
}
ROS_INFO("gazebo controller manager got pr2.xml from param server, parsing it...");
// initialize TiXmlDocument doc with a string
TiXmlDocument doc;
if (!doc.Parse(urdf_string.c_str()) && doc.Error())
{
ROS_ERROR("Could not load the gazebo controller manager plugin's configuration file: %s\n",
urdf_string.c_str());
}
else
{
//doc.Print();
//std::cout << *(doc.RootElement()) << std::endl;
// Pulls out the list of actuators used in the robot configuration.
struct GetActuators : public TiXmlVisitor
{
std::set<std::string> actuators;
virtual bool VisitEnter(const TiXmlElement &elt, const TiXmlAttribute *)
{
if (elt.ValueStr() == std::string("actuator") && elt.Attribute("name"))
actuators.insert(elt.Attribute("name"));
else if (elt.ValueStr() == std::string("rightActuator") && elt.Attribute("name"))
actuators.insert(elt.Attribute("name"));
else if (elt.ValueStr() == std::string("leftActuator") && elt.Attribute("name"))
actuators.insert(elt.Attribute("name"));
return true;
}
} get_actuators;
doc.RootElement()->Accept(&get_actuators);
// Places the found actuators into the hardware interface.
std::set<std::string>::iterator it;
for (it = get_actuators.actuators.begin(); it != get_actuators.actuators.end(); ++it)
{
//std::cout << " adding actuator " << (*it) << std::endl;
pr2_hardware_interface::Actuator* pr2_actuator = new pr2_hardware_interface::Actuator(*it);
pr2_actuator->state_.is_enabled_ = true;
this->hw_.addActuator(pr2_actuator);
}
// Setup mechanism control node
this->cm_->initXml(doc.RootElement());
for (unsigned int i = 0; i < this->cm_->state_->joint_states_.size(); ++i)
this->cm_->state_->joint_states_[i].calibrated_ = fake_calibration_;
}
}
#ifdef USE_CBQ
////////////////////////////////////////////////////////////////////////////////
// custom callback queue
void GazeboRosControllerManager::ControllerManagerQueueThread()
{
ROS_INFO_STREAM("Callback thread id=" << boost::this_thread::get_id());
static const double timeout = 0.01;
while (this->rosnode_->ok())
{
this->controller_manager_queue_.callAvailable(ros::WallDuration(timeout));
}
}
#endif
void GazeboRosControllerManager::ControllerManagerROSThread()
{
ROS_INFO_STREAM("Callback thread id=" << boost::this_thread::get_id());
//ros::Rate rate(1000);
while (this->rosnode_->ok())
{
//rate.sleep(); // using rosrate gets stuck on model delete
usleep(1000);
ros::spinOnce();
}
}
// Register this plugin with the simulator
GZ_REGISTER_MODEL_PLUGIN(GazeboRosControllerManager)
} // namespace gazebo