16 #include <boost/algorithm/string.hpp> 17 #include <boost/bind.hpp> 18 #include <boost/shared_ptr.hpp> 22 #include <gazebo/gazebo.hh> 23 #include <gazebo/msgs/msgs.hh> 24 #include <gazebo/physics/Link.hh> 25 #include <gazebo/physics/Model.hh> 26 #include <gazebo/physics/PhysicsEngine.hh> 27 #include <gazebo/physics/World.hh> 28 #include <gazebo/transport/TransportTypes.hh> 42 ThrusterPlugin::ThrusterPlugin() : thrustForce(0),
44 clampMin(std::numeric_limits<double>::lowest()),
45 clampMax(std::numeric_limits<double>::max()),
46 thrustMin(std::numeric_limits<double>::lowest()),
47 thrustMax(std::numeric_limits<double>::max()),
50 thrustEfficiency(1.0),
51 propellerEfficiency(1.0),
61 #if GAZEBO_MAJOR_VERSION >= 8 73 GZ_ASSERT(_model != NULL,
"Invalid model pointer");
76 this->
node = transport::NodePtr(
new transport::Node());
77 #if GAZEBO_MAJOR_VERSION >= 8 78 this->
node->Init(_model->GetWorld()->Name());
80 this->
node->Init(_model->GetWorld()->GetName());
84 GZ_ASSERT(_sdf->HasElement(
"linkName"),
"Could not find linkName.");
85 std::string linkName = _sdf->Get<std::string>(
"linkName");
87 GZ_ASSERT(this->thrusterLink,
"thruster link is invalid");
90 GZ_ASSERT(_sdf->HasElement(
"thrusterID"),
"Thruster ID was not provided");
91 this->
thrusterID = _sdf->Get<
int>(
"thrusterID");
94 GZ_ASSERT(_sdf->HasElement(
"dynamics"),
"Could not find dynamics.");
97 _sdf->GetElement(
"dynamics")));
100 GZ_ASSERT(_sdf->HasElement(
"conversion"),
"Could not find dynamics");
103 _sdf->GetElement(
"conversion")));
107 if (_sdf->HasElement(
"jointName"))
108 this->
joint = _model->GetJoint(_sdf->Get<std::string>(
"jointName"));
111 if (_sdf->HasElement(
"clampMin"))
112 this->
clampMin = _sdf->Get<
double>(
"clampMin");
114 if (_sdf->HasElement(
"clampMax"))
115 this->
clampMax = _sdf->Get<
double>(
"clampMax");
119 gzmsg <<
"clampMax must be greater than clampMin, returning to default values..." << std::endl;
120 this->
clampMin = std::numeric_limits<double>::lowest();
121 this->
clampMax = std::numeric_limits<double>::max();
125 if (_sdf->HasElement(
"thrustMin"))
126 this->
thrustMin = _sdf->Get<
double>(
"thrustMin");
128 if (_sdf->HasElement(
"thrustMax"))
129 this->
thrustMax = _sdf->Get<
double>(
"thrustMax");
133 gzmsg <<
"thrustMax must be greater than thrustMin, returning to default values..." << std::endl;
134 this->
thrustMin = std::numeric_limits<double>::lowest();
135 this->
thrustMax = std::numeric_limits<double>::max();
139 if (_sdf->HasElement(
"gain"))
140 this->
gain = _sdf->Get<
double>(
"gain");
142 if (_sdf->HasElement(
"thrust_efficiency"))
147 gzmsg <<
"Invalid thrust efficiency factor, setting it to 100%" 153 if (_sdf->HasElement(
"propeller_efficiency"))
159 "Invalid propeller dynamics efficiency factor, setting it to 100%" 165 std::stringstream strs;
166 strs <<
"/" << _model->GetName() <<
"/thrusters/" << this->thrusterID <<
"/";
184 #if GAZEBO_MAJOR_VERSION >= 8 187 this->
thrusterAxis = this->
joint->GetWorldPose().rot.Ign().RotateVectorReverse(this->
joint->GetGlobalAxis(0).Ign());
206 "nan in this->inputCommand");
208 double dynamicsInput;
214 clamped = std::min(clamped, this->
clampMax);
215 clamped = std::max(clamped, this->
clampMin);
217 dynamicsInput = this->
gain*clamped;
228 GZ_ASSERT(!std::isnan(dynamicState),
"Invalid dynamic state");
232 GZ_ASSERT(!std::isnan(this->
thrustForce),
"Invalid thrust force");
246 this->
joint->SetVelocity(0, dynamicState);
250 msgs::Vector3d thrustMsg;
251 msgs::Set(&thrustMsg, ignition::math::Vector3d(this->
thrustForce, 0., 0.));
void Update(const common::UpdateInfo &_info)
Update the simulation state.
virtual ~ThrusterPlugin()
Destructor.
double clampMax
: Optional: Commands greater than this value will be clamped.
physics::LinkPtr thrusterLink
Pointer to the thruster link.
physics::JointPtr joint
Optional: The rotor joint, used for visualization.
bool isOn
Optional: Flag to indicate if the thruster is turned on or off.
Model plugin for description of the thruster dynamics.
double propellerEfficiency
Optional: Propeller angular velocity efficiency term.
transport::PublisherPtr thrustTopicPublisher
Publisher to the output thrust topic.
const boost::shared_ptr< const uuv_gazebo_plugins_msgs::msgs::Double > ConstDoublePtr
Definition of a pointer to the floating point message.
std::shared_ptr< ConversionFunction > conversionFunction
Thruster conversion function.
GZ_REGISTER_MODEL_PLUGIN(UmbilicalPlugin)
common::Time thrustForceStamp
Time stamp of latest thrust force.
double clampMin
: Optional: Commands less than this value will be clamped.
double inputCommand
Input command, typically desired angular velocity of the rotor.
std::shared_ptr< Dynamics > thrusterDynamics
Thruster dynamic model.
static DynamicsFactory & GetInstance()
Returns the singleton instance of this factory.
virtual void Reset()
Custom plugin reset behavior.
double thrustMax
: Optional: Maximum thrust force output
transport::SubscriberPtr commandSubscriber
Subscriber to the reference signal topic.
transport::NodePtr node
Gazebo node.
double thrustMin
: Optional: Minimum thrust force output
int thrusterID
Thruster ID, used to generated topic names automatically.
double thrustEfficiency
Optional: Output thrust efficiency factor of the thruster.
Class for the thruster plugin.
ignition::math::Vector3d thrusterAxis
The axis about which the thruster rotates.
double gain
: Optional: Gain factor: Desired angular velocity = command * gain
void UpdateInput(ConstDoublePtr &_msg)
Callback for the input topic subscriber.
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
event::ConnectionPtr updateConnection
Update event.
std::string topicPrefix
Thruster topics prefix.
static ConversionFunctionFactory & GetInstance()
Return the singleton instance of this factory.
double thrustForce
Latest thrust force in [N].