in simulation_ws/src/rover/hector_gazebo_plugins/src/diffdrive_plugin_6w.cpp [82:169]
void DiffDrivePlugin6W::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
world = _model->GetWorld();
// default parameters
namespace_.clear();
topic_ = "cmd_vel";
wheelSep = 0.34;
wheelDiam = 0.15;
torque = 10.0;
// load parameters
if (_sdf->HasElement("robotNamespace"))
namespace_ = _sdf->GetElement("robotNamespace")->GetValue()->GetAsString();
if (_sdf->HasElement("topicName"))
topic_ = _sdf->GetElement("topicName")->GetValue()->GetAsString();
if (_sdf->HasElement("bodyName"))
{
link_name_ = _sdf->GetElement("bodyName")->GetValue()->GetAsString();
link = _model->GetLink(link_name_);
} else {
link = _model->GetLink();
link_name_ = link->GetName();
}
// assert that the body by link_name_ exists
if (!link)
{
ROS_FATAL("DiffDrivePlugin6W plugin error: bodyName: %s does not exist\n", link_name_.c_str());
return;
}
if (_sdf->HasElement("frontLeftJoint")) joints[FRONT_LEFT] = _model->GetJoint(_sdf->GetElement("frontLeftJoint")->GetValue()->GetAsString());
if (_sdf->HasElement("frontRightJoint")) joints[FRONT_RIGHT] = _model->GetJoint(_sdf->GetElement("frontRightJoint")->GetValue()->GetAsString());
if (_sdf->HasElement("midLeftJoint")) joints[MID_LEFT] = _model->GetJoint(_sdf->GetElement("midLeftJoint")->GetValue()->GetAsString());
if (_sdf->HasElement("midRightJoint")) joints[MID_RIGHT] = _model->GetJoint(_sdf->GetElement("midRightJoint")->GetValue()->GetAsString());
if (_sdf->HasElement("rearLeftJoint")) joints[REAR_LEFT] = _model->GetJoint(_sdf->GetElement("rearLeftJoint")->GetValue()->GetAsString());
if (_sdf->HasElement("rearRightJoint")) joints[REAR_RIGHT] = _model->GetJoint(_sdf->GetElement("rearRightJoint")->GetValue()->GetAsString());
if (!joints[FRONT_LEFT]) ROS_FATAL("diffdrive_plugin_6w error: The controller couldn't get front left joint");
if (!joints[FRONT_RIGHT]) ROS_FATAL("diffdrive_plugin_6w error: The controller couldn't get front right joint");
if (!joints[MID_LEFT]) ROS_FATAL("diffdrive_plugin_6w error: The controller couldn't get mid left joint");
if (!joints[MID_RIGHT]) ROS_FATAL("diffdrive_plugin_6w error: The controller couldn't get mid right joint");
if (!joints[REAR_LEFT]) ROS_FATAL("diffdrive_plugin_6w error: The controller couldn't get rear left joint");
if (!joints[REAR_RIGHT]) ROS_FATAL("diffdrive_plugin_6w error: The controller couldn't get rear right joint");
if (_sdf->HasElement("wheelSeparation"))
_sdf->GetElement("wheelSeparation")->GetValue()->Get(wheelSep);
if (_sdf->HasElement("wheelDiameter"))
_sdf->GetElement("wheelDiameter")->GetValue()->Get(wheelDiam);
if (_sdf->HasElement("torque"))
_sdf->GetElement("torque")->GetValue()->Get(torque);
// Make sure the ROS node for Gazebo has already been initialized
if (!ros::isInitialized())
{
ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
<< "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
return;
}
rosnode_ = new ros::NodeHandle(namespace_);
tf_prefix_ = tf::getPrefixParam(*rosnode_);
transform_broadcaster_ = new tf::TransformBroadcaster();
// ROS: Subscribe to the velocity command topic (usually "cmd_vel")
ros::SubscribeOptions so =
ros::SubscribeOptions::create<geometry_msgs::Twist>(topic_, 1,
boost::bind(&DiffDrivePlugin6W::cmdVelCallback, this, _1),
ros::VoidPtr(), &queue_);
sub_ = rosnode_->subscribe(so);
pub_ = rosnode_->advertise<nav_msgs::Odometry>("odom", 1);
callback_queue_thread_ = boost::thread(boost::bind(&DiffDrivePlugin6W::QueueThread, this));
Reset();
// New Mechanism for Updating every World Cycle
// Listen to the update event. This event is broadcast every
// simulation iteration.
updateConnection = event::Events::ConnectWorldUpdateBegin(
boost::bind(&DiffDrivePlugin6W::Update, this));
}