in simulation_ws/src/rover/hector_gazebo_plugins/src/servo_plugin.cpp [77:172]
void ServoPlugin::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
// Get the world name.
world = _model->GetWorld();
// default parameters
topicName = "drive";
jointStateName = "joint_states";
robotNamespace.clear();
controlPeriod = 0;
proportionalControllerGain = 8.0;
derivativeControllerGain = 0.0;
maximumVelocity = 0.0;
maximumTorque = 0.0;
// load parameters
if (_sdf->HasElement("robotNamespace")) robotNamespace = _sdf->Get<std::string>("robotNamespace");
if (_sdf->HasElement("topicName")) topicName = _sdf->Get<std::string>("topicName");
if (_sdf->HasElement("jointStateName")) jointStateName = _sdf->Get<std::string>("jointStateName");
if (_sdf->HasElement("firstServoName")) servo[FIRST].name = _sdf->Get<std::string>("firstServoName");
#if (GAZEBO_MAJOR_VERSION >= 8)
if (_sdf->HasElement("firstServoAxis")) servo[FIRST].axis = _sdf->Get<ignition::math::Vector3d>("firstServoAxis");
#else
if (_sdf->HasElement("firstServoAxis")) servo[FIRST].axis = _sdf->Get<math::Vector3>("firstServoAxis");
#endif
if (_sdf->HasElement("secondServoName")) servo[SECOND].name = _sdf->Get<std::string>("secondServoName");
#if (GAZEBO_MAJOR_VERSION >= 8)
if (_sdf->HasElement("secondServoAxis")) servo[SECOND].axis = _sdf->Get<ignition::math::Vector3d>("secondServoAxis");
#else
if (_sdf->HasElement("secondServoAxis")) servo[SECOND].axis = _sdf->Get<math::Vector3>("secondServoAxis");
#endif
if (_sdf->HasElement("thirdServoName")) servo[THIRD].name = _sdf->Get<std::string>("thirdServoName");
#if (GAZEBO_MAJOR_VERSION >= 8)
if (_sdf->HasElement("thirdServoAxis")) servo[THIRD].axis = _sdf->Get<ignition::math::Vector3d>("thirdServoAxis");
#else
if (_sdf->HasElement("thirdServoAxis")) servo[THIRD].axis = _sdf->Get<math::Vector3>("thirdServoAxis");
#endif
if (_sdf->HasElement("proportionalControllerGain")) proportionalControllerGain = _sdf->Get<double>("proportionalControllerGain");
if (_sdf->HasElement("derivativeControllerGain")) derivativeControllerGain = _sdf->Get<double>("derivativeControllerGain");
if (_sdf->HasElement("maxVelocity")) maximumVelocity = _sdf->Get<double>("maxVelocity");
if (_sdf->HasElement("torque")) maximumTorque = _sdf->Get<double>("torque");
double controlRate = 0.0;
if (_sdf->HasElement("controlRate")) controlRate = _sdf->Get<double>("controlRate");
controlPeriod = controlRate > 0.0 ? 1.0/controlRate : 0.0;
servo[FIRST].joint = _model->GetJoint(servo[FIRST].name);
servo[SECOND].joint = _model->GetJoint(servo[SECOND].name);
servo[THIRD].joint = _model->GetJoint(servo[THIRD].name);
if (!servo[FIRST].joint)
gzthrow("The controller couldn't get first joint");
countOfServos = 1;
if (servo[SECOND].joint) {
countOfServos = 2;
if (servo[THIRD].joint) {
countOfServos = 3;
}
}
else {
if (servo[THIRD].joint) {
gzthrow("The controller couldn't get second joint, but third joint was loaded");
}
}
if (!ros::isInitialized()) {
int argc = 0;
char** argv = NULL;
ros::init(argc, argv, "gazebo", ros::init_options::NoSigintHandler | ros::init_options::AnonymousName);
}
rosnode_ = new ros::NodeHandle(robotNamespace);
transform_listener_ = new tf::TransformListener();
transform_listener_->setExtrapolationLimit(ros::Duration(1.0));
if (!topicName.empty()) {
ros::SubscribeOptions so = ros::SubscribeOptions::create<geometry_msgs::QuaternionStamped>(topicName, 1,
boost::bind(&ServoPlugin::cmdCallback, this, _1),
ros::VoidPtr(), &queue_);
sub_ = rosnode_->subscribe(so);
}
if (!jointStateName.empty()) {
jointStatePub_ = rosnode_->advertise<sensor_msgs::JointState>(jointStateName, 10);
}
joint_state.header.frame_id = transform_listener_->resolve(_model->GetLink()->GetName());
// 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(&ServoPlugin::Update, this));
}