WavefieldModelPlugin.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Rhys Mainwaring
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #include <algorithm>
19 #include <iostream>
20 #include <string>
21 #include <thread>
22 
23 #include <gazebo/common/Assert.hh>
24 #include <gazebo/common/common.hh>
25 #include <gazebo/physics/physics.hh>
26 
31 using namespace gazebo;
32 
33 namespace asv
34 {
35  GZ_REGISTER_MODEL_PLUGIN(WavefieldModelPlugin)
36 
37 
38  // WavefieldModelPluginPrivate
39 
40 
41  class WavefieldModelPluginPrivate
43  {
45  public: physics::WorldPtr world;
46 
48  public: physics::ModelPtr model;
49 
52 
54  public: bool isStatic;
55 
57  public: double updateRate;
58 
60  public: common::Time prevTime;
61 
63  public: event::ConnectionPtr updateConnection;
64  };
65 
67 // WavefieldModelPlugin
68 
69  WavefieldModelPlugin::~WavefieldModelPlugin()
70  {
71  // Clean up.
72  this->data->wavefieldEntity.reset();
73 
74  // Reset connections and transport.
75  this->data->updateConnection.reset();
76  }
77 
78  WavefieldModelPlugin::WavefieldModelPlugin() :
79  ModelPlugin(),
80  data(new WavefieldModelPluginPrivate())
81  {
82  }
83 
84  void WavefieldModelPlugin::Load(physics::ModelPtr _model, \
85  sdf::ElementPtr _sdf)
86  {
87  // @DEBUG_INFO
88  // std::thread::id threadId = std::this_thread::get_id();
89  // gzmsg << "Load WavefieldModelPlugin [thread: "
90  // << threadId << "]" << std::endl;
91 
92  GZ_ASSERT(_model != nullptr, "Invalid parameter _model");
93  GZ_ASSERT(_sdf != nullptr, "Invalid parameter _sdf");
94 
95  // Capture the Model pointer
96  this->data->model = _model;
97  this->data->world = _model->GetWorld();
98  GZ_ASSERT(this->data->world != nullptr, "Model has invalid World");
99 
100  // Bind the update callback to the world update event
101  this->data->updateConnection = event::Events::ConnectWorldUpdateBegin(
102  std::bind(&WavefieldModelPlugin::OnUpdate, this));
103 
104  // Parameters
105  this->data->isStatic = Utilities::SdfParamBool(*_sdf, "static", false);
106  this->data->updateRate = Utilities::SdfParamDouble(*_sdf, "update_rate", \
107  30.0);
108  // Wavefield
109  this->data->wavefieldEntity.reset( \
110  new ::asv::WavefieldEntity(this->data->model));
111  this->data->wavefieldEntity->Load(_sdf);
112  this->data->wavefieldEntity->Init();
113 
114  // Generate the entity name and add as a child
115  this->data->wavefieldEntity->SetName(
116  WavefieldEntity::MakeName(this->data->model->GetName()));
117  this->data->model->AddChild(this->data->wavefieldEntity);
118  }
119 
121  {
122  // @DEBUG_INFO
123  // std::thread::id threadId = std::this_thread::get_id();
124  // gzmsg << "Init WavefieldModelPlugin [thread: "
125  // << threadId << "]" << std::endl;
126  }
127 
129  {
130  }
131 
133  {
134  // Reset time
135  #if GAZEBO_MAJOR_VERSION >= 8
136  this->data->prevTime = this->data->world->SimTime();
137  #else
138  this->data->prevTime = this->data->world->GetSimTime();
139  #endif
140  }
141 
143  {
144  GZ_ASSERT(this->data->world != nullptr, "World is NULL");
145  GZ_ASSERT(this->data->model != nullptr, "Model is NULL");
146  GZ_ASSERT(this->data->wavefieldEntity != nullptr, \
147  "Wavefield Entity is NULL");
148 
149  if (!this->data->isStatic)
150  {
151  // Throttle update [30 FPS by default]
152  auto updatePeriod = 1.0/this->data->updateRate;
153  #if GAZEBO_MAJOR_VERSION >= 8
154  auto currentTime = this->data->world->SimTime();
155  #else
156  auto currentTime = this->data->world->GetSimTime();
157  #endif
158 
159  if ((currentTime - this->data->prevTime).Double() < updatePeriod)
160  {
161  return;
162  }
163  this->data->prevTime = currentTime;
164 
165  // Wavefield
166  this->data->wavefieldEntity->Update();
167  }
168  }
169 
170  std::shared_ptr<const WaveParameters> WavefieldModelPlugin::GetWaveParams(
171  gazebo::physics::WorldPtr _world,
172  const std::string& _waveModelName)
173  {
174  GZ_ASSERT(_world != nullptr, "World is null");
175  #if GAZEBO_MAJOR_VERSION >= 8
176  physics::ModelPtr wavefieldModel = _world->ModelByName(_waveModelName);
177  #else
178  physics::ModelPtr wavefieldModel = _world->GetModel(_waveModelName);
179  #endif
180  if (wavefieldModel == nullptr)
181  {
182  gzerr << "No Wavefield Model found with name '"
183  << _waveModelName << "'." << std::endl;
184  return nullptr;
185  }
186 
187  std::string wavefieldEntityName(WavefieldEntity::MakeName(_waveModelName));
188 
189  physics::BasePtr base = wavefieldModel->GetChild(wavefieldEntityName);
190  boost::shared_ptr<WavefieldEntity> wavefieldEntity
191  = boost::dynamic_pointer_cast<WavefieldEntity>(base);
192  if (wavefieldEntity == nullptr)
193  {
194  gzerr << "Wavefield Entity is null: "
195  << wavefieldEntityName << std::endl;
196  return nullptr;
197  }
198  // BSB - appears this method is not a part of the feature/vrx branch?
199  // GZ_ASSERT(wavefieldEntity->GetWavefield() != nullptr, \
200  // "Wavefield is null.");
201 
202  return wavefieldEntity->GetWaveParams();
203  }
204 }
double updateRate
Update rate [30].
This file defines utilities for extracting parameters from SDF.
void OnUpdate()
Callback for World Update events.
event::ConnectionPtr updateConnection
Connection to the World Update events.
std::shared_ptr< WavefieldModelPluginPrivate > data
A class to manage a wave field that can be accessed from the World.
static bool SdfParamBool(sdf::Element &_sdf, const std::string &_paramName, const bool _defaultVal)
Extract a named bool parameter from an SDF element.
Definition: Utilities.cc:57
void Load(gazebo::physics::ModelPtr _model, sdf::ElementPtr _sdf)
static double SdfParamDouble(sdf::Element &_sdf, const std::string &_paramName, const double _defaultVal)
Extract a named double parameter from an SDF element.
Definition: Utilities.cc:69
This file defines a Gazebo ModelPlugin used to manage a wave field.
bool isStatic
Set the wavefield to be static [false].
static std::string MakeName(const std::string &_parentName)
Make a wave field entity name given a parent object name.
static std::shared_ptr< const WaveParameters > GetWaveParams(gazebo::physics::WorldPtr _world, const std::string &_waveModelName)
Retrive a pointer to the wavefield parameters from the Wavefield plugin.
Definition: Geometry.hh:28
physics::WorldPtr world
World pointer.
common::Time prevTime
Previous update time.
This file contains definitions for classes used to manage a wave field. This includes wave parameters...
physics::ModelPtr model
Model pointer.
boost::shared_ptr<::asv::WavefieldEntity > wavefieldEntity
WavefieldEntity pointer.
This file contains the definition for a Gazebo physics object that allows a wave field to be added in...


wave_gazebo_plugins
Author(s): Rhys Mainwaring
autogenerated on Thu May 7 2020 03:54:44