UnderwaterCurrentROSPlugin.cc
Go to the documentation of this file.
1 // Copyright (c) 2016 The UUV Simulator Authors.
2 // All rights reserved.
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 
17 
18 namespace uuv_simulator_ros
19 {
22 {
23  this->rosPublishPeriod = gazebo::common::Time(0.05);
24  this->lastRosPublishTime = gazebo::common::Time(0.0);
25 }
26 
29 {
30 #if GAZEBO_MAJOR_VERSION >= 8
31  this->rosPublishConnection.reset();
32 #else
33  gazebo::event::Events::DisconnectWorldUpdateBegin(
34  this->rosPublishConnection);
35 #endif
36  this->rosNode->shutdown();
37 }
38 
40 void UnderwaterCurrentROSPlugin::Load(gazebo::physics::WorldPtr _world,
41  sdf::ElementPtr _sdf)
42 {
43  try
44  {
45  UnderwaterCurrentPlugin::Load(_world, _sdf);
46  } catch(gazebo::common::Exception &_e)
47  {
48  gzerr << "Error loading plugin."
49  << "Please ensure that your model is correct."
50  << '\n';
51  return;
52  }
53 
54  if (!ros::isInitialized())
55  {
56  gzerr << "Not loading plugin since ROS has not been "
57  << "properly initialized. Try starting gazebo with ros plugin:\n"
58  << " gazebo -s libgazebo_ros_api_plugin.so\n";
59  return;
60  }
61 
62  this->ns = "";
63  if (_sdf->HasElement("namespace"))
64  this->ns = _sdf->Get<std::string>("namespace");
65 
66  gzmsg << "UnderwaterCurrentROSPlugin::namespace=" << this->ns << std::endl;
67 
68  this->rosNode.reset(new ros::NodeHandle(this->ns));
69 
70  // Advertise the flow velocity as a stamped twist message
71  this->flowVelocityPub = this->rosNode->advertise<geometry_msgs::TwistStamped>(
72  this->currentVelocityTopic, 10);
73 
74  // Advertise the service to update the current velocity model
75  this->worldServices["set_current_velocity_model"] =
76  this->rosNode->advertiseService(
77  "set_current_velocity_model",
79 
80  // Advertise the service to update the current velocity model
81  this->worldServices["get_current_velocity_model"] =
82  this->rosNode->advertiseService(
83  "get_current_velocity_model",
85 
86  // Advertise the service to update the current velocity model
87  this->worldServices["set_current_horz_angle_model"] =
88  this->rosNode->advertiseService(
89  "set_current_horz_angle_model",
91 
92  // Advertise the service to update the current velocity model
93  this->worldServices["get_current_horz_angle_model"] =
94  this->rosNode->advertiseService(
95  "get_current_horz_angle_model",
97 
98  // Advertise the service to update the current velocity model
99  this->worldServices["set_current_vert_angle_model"] =
100  this->rosNode->advertiseService(
101  "set_current_vert_angle_model",
103 
104  // Advertise the service to update the current velocity model
105  this->worldServices["get_current_vert_angle_model"] =
106  this->rosNode->advertiseService(
107  "get_current_vert_angle_model",
109 
110  // Advertise the service to update the current velocity mean value
111  this->worldServices["set_current_velocity"] =
112  this->rosNode->advertiseService(
113  "set_current_velocity",
115 
116  // Advertise the service to update the current velocity mean value
117  this->worldServices["set_current_horz_angle"] =
118  this->rosNode->advertiseService(
119  "set_current_horz_angle",
121 
122  // Advertise the service to update the current velocity mean value
123  this->worldServices["set_current_vert_angle"] =
124  this->rosNode->advertiseService(
125  "set_current_vert_angle",
127 
128  this->rosPublishConnection = gazebo::event::Events::ConnectWorldUpdateBegin(
130 }
131 
134 {
135  if (this->lastUpdate - this->lastRosPublishTime >= this->rosPublishPeriod)
136  {
137  this->lastRosPublishTime = this->lastUpdate;
138  geometry_msgs::TwistStamped flowVelMsg;
139  flowVelMsg.header.stamp = ros::Time().now();
140  flowVelMsg.header.frame_id = "/world";
141 
142  flowVelMsg.twist.linear.x = this->currentVelocity.X();
143  flowVelMsg.twist.linear.y = this->currentVelocity.Y();
144  flowVelMsg.twist.linear.z = this->currentVelocity.Z();
145 
146  this->flowVelocityPub.publish(flowVelMsg);
147  }
148 }
149 
152  uuv_world_ros_plugins_msgs::SetCurrentDirection::Request& _req,
153  uuv_world_ros_plugins_msgs::SetCurrentDirection::Response& _res)
154 {
155  _res.success = this->currentHorzAngleModel.SetMean(_req.angle);
156 
157  return true;
158 }
159 
162  uuv_world_ros_plugins_msgs::SetCurrentDirection::Request& _req,
163  uuv_world_ros_plugins_msgs::SetCurrentDirection::Response& _res)
164 {
165  _res.success = this->currentVertAngleModel.SetMean(_req.angle);
166  return true;
167 }
168 
171  uuv_world_ros_plugins_msgs::SetCurrentVelocity::Request& _req,
172  uuv_world_ros_plugins_msgs::SetCurrentVelocity::Response& _res)
173 {
174  if (this->currentVelModel.SetMean(_req.velocity) &&
175  this->currentHorzAngleModel.SetMean(_req.horizontal_angle) &&
176  this->currentVertAngleModel.SetMean(_req.vertical_angle))
177  {
178  gzmsg << "Current velocity [m/s] = " << _req.velocity << std::endl
179  << "Current horizontal angle [rad] = " << _req.horizontal_angle
180  << std::endl
181  << "Current vertical angle [rad] = " << _req.vertical_angle
182  << std::endl
183  << "\tWARNING: Current velocity calculated in the ENU frame"
184  << std::endl;
185  _res.success = true;
186  }
187  else
188  {
189  gzmsg << "Error while updating the current velocity" << std::endl;
190  _res.success = false;
191  }
192  return true;
193 }
194 
197  uuv_world_ros_plugins_msgs::GetCurrentModel::Request& _req,
198  uuv_world_ros_plugins_msgs::GetCurrentModel::Response& _res)
199 {
200  _res.mean = this->currentVelModel.mean;
201  _res.min = this->currentVelModel.min;
202  _res.max = this->currentVelModel.max;
203  _res.noise = this->currentVelModel.noiseAmp;
204  _res.mu = this->currentVelModel.mu;
205  return true;
206 }
207 
210  uuv_world_ros_plugins_msgs::GetCurrentModel::Request& _req,
211  uuv_world_ros_plugins_msgs::GetCurrentModel::Response& _res)
212 {
213  _res.mean = this->currentHorzAngleModel.mean;
214  _res.min = this->currentHorzAngleModel.min;
215  _res.max = this->currentHorzAngleModel.max;
216  _res.noise = this->currentHorzAngleModel.noiseAmp;
217  _res.mu = this->currentHorzAngleModel.mu;
218  return true;
219 }
220 
223  uuv_world_ros_plugins_msgs::GetCurrentModel::Request& _req,
224  uuv_world_ros_plugins_msgs::GetCurrentModel::Response& _res)
225 {
226  _res.mean = this->currentVertAngleModel.mean;
227  _res.min = this->currentVertAngleModel.min;
228  _res.max = this->currentVertAngleModel.max;
229  _res.noise = this->currentVertAngleModel.noiseAmp;
230  _res.mu = this->currentVertAngleModel.mu;
231  return true;
232 }
233 
234 
237  uuv_world_ros_plugins_msgs::SetCurrentModel::Request& _req,
238  uuv_world_ros_plugins_msgs::SetCurrentModel::Response& _res)
239 {
240  _res.success = this->currentVelModel.SetModel(
241  std::max(0.0, _req.mean),
242  std::max(0.0, _req.min),
243  std::max(0.0, _req.max),
244  _req.mu,
245  _req.noise);
246  gzmsg << "Current velocity model updated" << std::endl
247  << "\tWARNING: Current velocity calculated in the ENU frame"
248  << std::endl;
249  this->currentVelModel.Print();
250  return true;
251 }
252 
255  uuv_world_ros_plugins_msgs::SetCurrentModel::Request& _req,
256  uuv_world_ros_plugins_msgs::SetCurrentModel::Response& _res)
257 {
258  _res.success = this->currentHorzAngleModel.SetModel(_req.mean, _req.min,
259  _req.max, _req.mu, _req.noise);
260  gzmsg << "Horizontal angle model updated" << std::endl
261  << "\tWARNING: Current velocity calculated in the ENU frame"
262  << std::endl;
264  return true;
265 }
266 
269  uuv_world_ros_plugins_msgs::SetCurrentModel::Request& _req,
270  uuv_world_ros_plugins_msgs::SetCurrentModel::Response& _res)
271 {
272  _res.success = this->currentVertAngleModel.SetModel(_req.mean, _req.min,
273  _req.max, _req.mu, _req.noise);
274  gzmsg << "Vertical angle model updated" << std::endl
275  << "\tWARNING: Current velocity calculated in the ENU frame"
276  << std::endl;
278  return true;
279 }
280 
282 GZ_REGISTER_WORLD_PLUGIN(UnderwaterCurrentROSPlugin)
283 }
GaussMarkovProcess currentVelModel
ros::Publisher flowVelocityPub
Publisher for the flow velocity in the world frame.
void publish(const boost::shared_ptr< M > &message) const
gazebo::common::Time rosPublishPeriod
Period after which we should publish a message via ROS.
bool UpdateCurrentVertAngleModel(uuv_world_ros_plugins_msgs::SetCurrentModel::Request &_req, uuv_world_ros_plugins_msgs::SetCurrentModel::Response &_res)
Service call to update the parameters for the vertical angle Gauss-Markov process model...
bool UpdateCurrentVelocity(uuv_world_ros_plugins_msgs::SetCurrentVelocity::Request &_req, uuv_world_ros_plugins_msgs::SetCurrentVelocity::Response &_res)
Service call to update the mean value of the flow velocity.
ROSCPP_DECL bool isInitialized()
bool GetCurrentVelocityModel(uuv_world_ros_plugins_msgs::GetCurrentModel::Request &_req, uuv_world_ros_plugins_msgs::GetCurrentModel::Response &_res)
Service call to read the parameters for the velocity Gauss-Markov process model.
bool UpdateHorzAngle(uuv_world_ros_plugins_msgs::SetCurrentDirection::Request &_req, uuv_world_ros_plugins_msgs::SetCurrentDirection::Response &_res)
Service call to update the mean value of the horizontal angle.
bool UpdateCurrentHorzAngleModel(uuv_world_ros_plugins_msgs::SetCurrentModel::Request &_req, uuv_world_ros_plugins_msgs::SetCurrentModel::Response &_res)
Service call to update the parameters for the horizontal angle Gauss-Markov process model...
gazebo::common::Time lastRosPublishTime
Last time we published a message via ROS.
bool SetMean(double _mean)
GaussMarkovProcess currentHorzAngleModel
boost::scoped_ptr< ros::NodeHandle > rosNode
Pointer to this ROS node&#39;s handle.
bool UpdateVertAngle(uuv_world_ros_plugins_msgs::SetCurrentDirection::Request &_req, uuv_world_ros_plugins_msgs::SetCurrentDirection::Response &_res)
Service call to update the mean value of the vertical angle.
bool UpdateCurrentVelocityModel(uuv_world_ros_plugins_msgs::SetCurrentModel::Request &_req, uuv_world_ros_plugins_msgs::SetCurrentModel::Response &_res)
Service call to update the parameters for the velocity Gauss-Markov process model.
bool SetModel(double _mean, double _min, double _max, double _mu=0, double _noise=0)
GaussMarkovProcess currentVertAngleModel
ignition::math::Vector3d currentVelocity
bool GetCurrentHorzAngleModel(uuv_world_ros_plugins_msgs::GetCurrentModel::Request &_req, uuv_world_ros_plugins_msgs::GetCurrentModel::Response &_res)
Service call to read the parameters for the horizontal angle Gauss-Markov process model...
static Time now()
std::map< std::string, ros::ServiceServer > worldServices
All underwater world services.
void Load(gazebo::physics::WorldPtr _world, sdf::ElementPtr _sdf)
Load module and read parameters from SDF.
Publishes the constant flow velocity in ROS messages and creates a service to alter the flow model in...
gazebo::event::ConnectionPtr rosPublishConnection
Connection for callbacks on update world.
bool GetCurrentVertAngleModel(uuv_world_ros_plugins_msgs::GetCurrentModel::Request &_req, uuv_world_ros_plugins_msgs::GetCurrentModel::Response &_res)
Service call to read the parameters for the vertical angle Gauss-Markov process model.


uuv_world_ros_plugins
Author(s): Musa Morena Marcusso Manhaes , Sebastian Scherer , Luiz Ricardo Douat
autogenerated on Mon Jul 1 2019 19:39:41