wavegauge_plugin.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 20109 Brian Bingham
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 <functional>
19 #include <string>
20 #include <gazebo/common/Assert.hh>
21 #include <gazebo/common/Events.hh>
22 #include <ignition/math/Pose3.hh>
23 
28 
29 using namespace asv;
30 using namespace gazebo;
31 
33 WaveguagePlugin::WaveguagePlugin()
34  : fluidLevel(0.0)
35 {
36 }
37 
39 void WaveguagePlugin::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
40 {
41  GZ_ASSERT(_model != NULL, "Received NULL model pointer");
42  GZ_ASSERT(_sdf != NULL, "Received NULL SDF pointer");
43 
44  // Capture the model pointer.
45  this->model = _model;
46 
47  if (_sdf->HasElement("wave_model"))
48  {
49  this->waveModelName = _sdf->Get<std::string>("wave_model");
50  }
51  if (_sdf->HasElement("fluid_level"))
52  {
53  this->fluidLevel = _sdf->Get<double>("fluid_level");
54  }
55 }
56 
59 {
60  this->updateConnection = event::Events::ConnectWorldUpdateBegin(
61  std::bind(&WaveguagePlugin::OnUpdate, this));
62 }
63 
66 {
67  // Retrieve the wave model...
68 
69  std::shared_ptr<const WaveParameters> waveParams \
70  = WavefieldModelPlugin::GetWaveParams(
71  this->model->GetWorld(), this->waveModelName);
72 
73  // No ocean waves...
74  if (waveParams == nullptr)
75  {
76  return;
77  }
78  #if GAZEBO_MAJOR_VERSION >= 8
79  ignition::math::Pose3d modelPose = this->model->WorldPose();
80  #else
81  ignition::math::Pose3d modelPose = this->model->GetWorldPose().Ign();
82  #endif
83 
84  // Compute the wave displacement at the model location
85  #if GAZEBO_MAJOR_VERSION >= 8
86  double waveHeightS = WavefieldSampler::ComputeDepthSimply(
87  *waveParams, modelPose.Pos(),
88  this->model->GetWorld()->SimTime().Double());
89  #else
90  double waveHeightS = WavefieldSampler::ComputeDepthSimply(
91  *waveParams, modelPose.Pos(),
92  this->model->GetWorld()->GetSimTime().Double());
93  #endif
94 
95  // Add the mean water level
96  waveHeightS += this->fluidLevel;
97 
98  // Set vertical location to match the wave height
99  modelPose.Pos().Z(waveHeightS);
100  this->model->SetWorldPose(modelPose);
101 }
102 
103 GZ_REGISTER_MODEL_PLUGIN(WaveguagePlugin)
A plugin that sets the model height (z) to be the same as the wave height calculated for the physics...
virtual void OnUpdate()
Callback for World Update events.
physics::ModelPtr model
Pointer to the model.
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
event::ConnectionPtr updateConnection
Connection to World Update events.
double fluidLevel
The height of the fluid/air interface [m]. Defaults to 0.
This file defines a Gazebo ModelPlugin used to manage a wave field.
std::string waveModelName
The name of the wave model.
Definition: Geometry.hh:28
This file contains definitions for classes used to manage a wave field. This includes wave parameters...
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