16 #include <gazebo/gazebo.hh> 17 #include <gazebo/physics/Collision.hh> 18 #include <gazebo/physics/Link.hh> 19 #include <gazebo/physics/Model.hh> 20 #include <gazebo/physics/PhysicsEngine.hh> 21 #include <gazebo/physics/Shape.hh> 22 #include <gazebo/physics/World.hh> 23 #include <gazebo/transport/TransportTypes.hh> 24 #include <gazebo/transport/transport.hh> 33 UnderwaterObjectPlugin::UnderwaterObjectPlugin() : useGlobalCurrent(true)
41 #if GAZEBO_MAJOR_VERSION >= 8 52 GZ_ASSERT(_model != NULL,
"Invalid model pointer");
53 GZ_ASSERT(_sdf != NULL,
"Invalid SDF element pointer");
56 this->
world = _model->GetWorld();
59 this->
node = transport::NodePtr(
new transport::Node());
60 std::string worldName;
61 #if GAZEBO_MAJOR_VERSION >= 8 62 worldName = this->
world->Name();
64 worldName = this->
world->GetName();
66 this->
node->Init(worldName);
69 if (_sdf->HasElement(
"flow_velocity_topic"))
71 std::string flowTopic = _sdf->Get<std::string>(
"flow_velocity_topic");
72 GZ_ASSERT(!flowTopic.empty(),
73 "Fluid velocity topic tag cannot be empty");
75 gzmsg <<
"Subscribing to current velocity topic: " << flowTopic
81 double fluidDensity = 1028.0;
83 if (_sdf->HasElement(
"fluid_density"))
84 fluidDensity = _sdf->Get<
double>(
"fluid_density");
86 if (_sdf->HasElement(
"use_global_current"))
90 bool debugFlag =
false;
91 if (_sdf->HasElement(
"debug"))
92 debugFlag = static_cast<bool>(_sdf->Get<
int>(
"debug"));
95 ignition::math::Vector3d cob;
98 #if GAZEBO_MAJOR_VERSION >= 8 99 gAcc = std::abs(this->
world->Gravity().Z());
101 gAcc = std::abs(this->
world->GetPhysicsEngine()->GetGravity().z);
104 if (_sdf->HasElement(
"link"))
106 for (sdf::ElementPtr linkElem = _sdf->GetElement(
"link"); linkElem;
107 linkElem = linkElem->GetNextElement(
"link"))
109 physics::LinkPtr link;
110 std::string linkName =
"";
112 if (linkElem->HasAttribute(
"name"))
114 linkName = linkElem->Get<std::string>(
"name");
115 std::size_t found = linkName.find(
"base_link");
116 if (found != std::string::npos)
118 this->baseLinkName = linkName;
119 gzmsg <<
"Name of the BASE_LINK: " << this->baseLinkName << std::endl;
122 link = this->
model->GetLink(linkName);
125 gzwarn <<
"Specified link [" << linkName <<
"] not found." 132 gzwarn <<
"Attribute name missing from link [" << linkName
142 hydro->SetFluidDensity(fluidDensity);
143 hydro->SetGravity(gAcc);
148 this->
models[link] = hydro;
149 this->
models[link]->Print(
"all");
162 std::string rootTopic =
"/debug/forces/" + _link->GetName() +
"/";
163 std::vector<std::string> topics {
"restoring",
"damping",
"added_mass",
165 for (
auto topic : topics)
167 this->
hydroPub[_link->GetName() +
"/" + topic] =
168 this->
node->Advertise<msgs::WrenchStamped>(rootTopic + topic);
172 _hydro->SetDebugFlag(
true);
191 double time = _info.simTime.Double();
193 for (std::map<gazebo::physics::LinkPtr,
197 physics::LinkPtr link = it->first;
200 double linearAccel, angularAccel;
201 #if GAZEBO_MAJOR_VERSION >= 8 202 linearAccel = link->RelativeLinearAccel().Length();
203 angularAccel = link->RelativeAngularAccel().Length();
205 linearAccel = link->GetRelativeLinearAccel().GetLength();
206 angularAccel = link->GetRelativeAngularAccel().GetLength();
209 GZ_ASSERT(!std::isnan(linearAccel) && !std::isnan(angularAccel),
210 "Linear or angular accelerations are invalid.");
212 hydro->ApplyHydrodynamicForces(time, this->
flowVelocity);
256 physics::LinkPtr _link)
258 if (this->
models.count(_link))
260 if (!this->
models[_link]->GetDebugFlag())
263 ignition::math::Vector3d restoring = this->
models[_link]->GetStoredVector(
266 msgs::WrenchStamped msg;
267 this->
GenWrenchMsg(restoring, ignition::math::Vector3d(0, 0, 0), msg);
268 this->
hydroPub[_link->GetName() +
"/restoring"]->Publish(msg);
274 physics::LinkPtr _link)
276 if (this->
models.count(_link))
278 if (!this->
models[_link]->GetDebugFlag())
280 msgs::WrenchStamped msg;
281 ignition::math::Vector3d force, torque;
288 this->
hydroPub[_link->GetName() +
"/added_mass"]->Publish(msg);
295 this->
hydroPub[_link->GetName() +
"/damping"]->Publish(msg);
302 this->
hydroPub[_link->GetName() +
"/added_coriolis"]->Publish(msg);
308 ignition::math::Vector3d _torque, gazebo::msgs::WrenchStamped &_output)
310 common::Time curTime;
311 #if GAZEBO_MAJOR_VERSION >= 8 312 curTime = this->
world->SimTime();
314 curTime = this->
world->GetSimTime();
317 msgs::Wrench * wrench = _output.mutable_wrench();
318 msgs::Time * t = _output.mutable_time();
319 msgs::Vector3d * msgForce = wrench->mutable_force();
320 msgs::Vector3d * msgTorque = wrench->mutable_torque();
323 ignition::math::Vector3d(_torque.X(), _torque.Y(), _torque.Z()));
325 ignition::math::Vector3d(_force.X(), _force.Y(), _force.Z()));
327 t->set_sec(curTime.sec);
328 t->set_nsec(curTime.nsec);
std::map< gazebo::physics::LinkPtr, HydrodynamicModelPtr > models
Pairs of links & corresponding hydrodynamic models.
gazebo::event::ConnectionPtr updateConnection
Update event.
std::map< std::string, gazebo::transport::PublisherPtr > hydroPub
Publishers of hydrodynamic and hydrostatic forces and torques in the case the debug flag is on...
virtual void PublishHydrodynamicWrenches(gazebo::physics::LinkPtr _link)
Publish hydrodynamic wrenches.
#define UUV_ADDED_CORIOLIS_FORCE
virtual void PublishIsSubmerged()
Publishes the state of the vehicle (is submerged)
virtual void PublishRestoringForce(gazebo::physics::LinkPtr _link)
Publish restoring force.
void UpdateFlowVelocity(ConstVector3dPtr &_msg)
Reads flow velocity topic.
Class declaration for the underwater objects subject to buoyancy, lift and drag forces.
gazebo::physics::WorldPtr world
Pointer to the world plugin.
GZ_REGISTER_MODEL_PLUGIN(UmbilicalPlugin)
virtual void Connect()
Connects the update event callback.
gazebo::transport::NodePtr node
Gazebo node.
ignition::math::Vector3d flowVelocity
Flow velocity vector read from topic.
virtual ~UnderwaterObjectPlugin()
Destructor.
#define UUV_DAMPING_TORQUE
static HydrodynamicModelFactory & GetInstance()
Returns the singleton instance of this factory.
bool useGlobalCurrent
Flag to use the global current velocity or the individually assigned current velocity.
#define UUV_ADDED_CORIOLIS_TORQUE
virtual void InitDebug(gazebo::physics::LinkPtr _link, gazebo::HydrodynamicModelPtr _hydro)
Sets the topics used for publishing the intermediate data during the simulation.
#define UUV_ADDED_MASS_FORCE
std::string baseLinkName
Name of vehicle's base_link.
virtual void Update(const gazebo::common::UpdateInfo &_info)
Update the simulation state.
#define UUV_ADDED_MASS_TORQUE
virtual void PublishCurrentVelocityMarker()
Publish current velocity marker.
virtual void GenWrenchMsg(ignition::math::Vector3d _force, ignition::math::Vector3d _torque, gazebo::msgs::WrenchStamped &_output)
Returns the wrench message for debugging topics.
gazebo::physics::ModelPtr model
Pointer to the model structure.
#define UUV_DAMPING_FORCE
boost::shared_ptr< HydrodynamicModel > HydrodynamicModelPtr
Pointer to model.
gazebo::transport::SubscriberPtr flowSubscriber
Subcriber to flow message.
virtual void Load(gazebo::physics::ModelPtr _model, sdf::ElementPtr _sdf)