ThrusterROSPlugin.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 #include <string>
19 
20 #include <gazebo/physics/Base.hh>
21 #include <gazebo/physics/Link.hh>
22 #include <gazebo/physics/Model.hh>
23 #include <gazebo/physics/World.hh>
24 
25 #include <uuv_gazebo_ros_plugins_msgs/FloatStamped.h>
26 
27 namespace uuv_simulator_ros
28 {
31 {
32  this->rosPublishPeriod = gazebo::common::Time(0.05);
33  this->lastRosPublishTime = gazebo::common::Time(0.0);
34 }
35 
38 {
39 #if GAZEBO_MAJOR_VERSION >= 8
40  this->rosPublishConnection.reset();
41 #else
42  gazebo::event::Events::DisconnectWorldUpdateBegin(
43  this->rosPublishConnection);
44 #endif
45 
46  this->rosNode->shutdown();
47 }
48 
51  const uuv_gazebo_ros_plugins_msgs::FloatStamped::ConstPtr &_msg)
52 {
53  if (std::isnan(_msg->data))
54  {
55  ROS_WARN("ThrusterROSPlugin: Ignoring nan command");
56  return;
57  }
58 
59  this->inputCommand = _msg->data;
60 }
61 
64 {
65  return this->rosPublishPeriod;
66 }
67 
70 {
71  if (_hz > 0.0)
72  this->rosPublishPeriod = 1.0 / _hz;
73  else
74  this->rosPublishPeriod = 0.;
75 }
76 
79 {
80  ThrusterPlugin::Init();
81 }
82 
85 {
86  this->lastRosPublishTime.Set(0, 0);
87 }
88 
90 void ThrusterROSPlugin::Load(gazebo::physics::ModelPtr _parent,
91  sdf::ElementPtr _sdf)
92 {
93  try {
94  ThrusterPlugin::Load(_parent, _sdf);
95  } catch(gazebo::common::Exception &_e)
96  {
97  gzerr << "Error loading plugin."
98  << "Please ensure that your model is correct."
99  << '\n';
100  return;
101  }
102 
103  if (!ros::isInitialized())
104  {
105  gzerr << "Not loading plugin since ROS has not been "
106  << "properly initialized. Try starting gazebo with ros plugin:\n"
107  << " gazebo -s libgazebo_ros_api_plugin.so\n";
108  return;
109  }
110 
111  this->rosNode.reset(new ros::NodeHandle(""));
112 
113  this->services["set_thrust_force_efficiency"] =
114  this->rosNode->advertiseService(
115  this->topicPrefix + "set_thrust_force_efficiency",
117 
118  this->services["get_thrust_force_efficiency"] =
119  this->rosNode->advertiseService(
120  this->topicPrefix + "get_thrust_force_efficiency",
122 
123  this->services["set_dynamic_state_efficiency"] =
124  this->rosNode->advertiseService(
125  this->topicPrefix + "set_dynamic_state_efficiency",
127 
128  this->services["get_dynamic_state_efficiency"] =
129  this->rosNode->advertiseService(
130  this->topicPrefix + "get_dynamic_state_efficiency",
132 
133  this->services["set_thruster_state"] =
134  this->rosNode->advertiseService(
135  this->topicPrefix + "set_thruster_state",
137 
138  this->services["get_thruster_state"] =
139  this->rosNode->advertiseService(
140  this->topicPrefix + "get_thruster_state",
142 
143  this->services["get_thruster_conversion_fcn"] =
144  this->rosNode->advertiseService(
145  this->topicPrefix + "get_thruster_conversion_fcn",
147 
148  this->subThrustReference = this->rosNode->subscribe<
149  uuv_gazebo_ros_plugins_msgs::FloatStamped
150  >(this->commandSubscriber->GetTopic(), 10,
151  boost::bind(&ThrusterROSPlugin::SetThrustReference, this, _1));
152 
153  this->pubThrust = this->rosNode->advertise<
154  uuv_gazebo_ros_plugins_msgs::FloatStamped
155  >(this->thrustTopicPublisher->GetTopic(), 10);
156 
157  this->pubThrustWrench =
158  this->rosNode->advertise<geometry_msgs::WrenchStamped>(
159  this->thrustTopicPublisher->GetTopic() + "_wrench", 10);
160 
161  this->pubThrusterState = this->rosNode->advertise<std_msgs::Bool>(
162  this->topicPrefix + "is_on", 1);
163 
164  this->pubThrustForceEff = this->rosNode->advertise<std_msgs::Float64>(
165  this->topicPrefix + "thrust_efficiency", 1);
166 
167  this->pubDynamicStateEff = this->rosNode->advertise<std_msgs::Float64>(
168  this->topicPrefix + "dynamic_state_efficiency", 1);
169 
170  gzmsg << "Thruster #" << this->thrusterID << " initialized" << std::endl
171  << "\t- Link: " << this->thrusterLink->GetName() << std::endl
172  << "\t- Robot model: " << _parent->GetName() << std::endl
173  << "\t- Input command topic: " <<
174  this->commandSubscriber->GetTopic() << std::endl
175  << "\t- Thrust output topic: " <<
176  this->thrustTopicPublisher->GetTopic() << std::endl;
177 
178  this->rosPublishConnection = gazebo::event::Events::ConnectWorldUpdateBegin(
179  boost::bind(&ThrusterROSPlugin::RosPublishStates, this));
180 }
181 
184 {
185  // Limit publish rate according to publish period
186  if (this->thrustForceStamp - this->lastRosPublishTime >=
187  this->rosPublishPeriod)
188  {
189  this->lastRosPublishTime = this->thrustForceStamp;
190 
191  // Publish the thrust force magnitude
192  uuv_gazebo_ros_plugins_msgs::FloatStamped thrustMsg;
193  thrustMsg.header.stamp = ros::Time().now();
194  thrustMsg.header.frame_id = this->thrusterLink->GetName();
195  thrustMsg.data = this->thrustForce;
196  this->pubThrust.publish(thrustMsg);
197 
198  // Publish the thrust force vector wrt the thruster frame
199  geometry_msgs::WrenchStamped thrustWrenchMsg;
200  thrustWrenchMsg.header.stamp = ros::Time().now();
201  thrustWrenchMsg.header.frame_id = this->thrusterLink->GetName();
202  ignition::math::Vector3d thrustVector = this->thrustForce * this->thrusterAxis;
203  thrustWrenchMsg.wrench.force.x = thrustVector.X();
204  thrustWrenchMsg.wrench.force.y = thrustVector.Y();
205  thrustWrenchMsg.wrench.force.z = thrustVector.Z();
206  this->pubThrustWrench.publish(thrustWrenchMsg);
207 
208  // Publish the thruster current state (ON or OFF)
209  std_msgs::Bool isOnMsg;
210  isOnMsg.data = this->isOn;
211  this->pubThrusterState.publish(isOnMsg);
212 
213  // Publish thrust output efficiency
214  std_msgs::Float64 thrustEffMsg;
215  thrustEffMsg.data = this->thrustEfficiency;
216  this->pubThrustForceEff.publish(thrustEffMsg);
217 
218  // Publish dynamic state efficiency
219  std_msgs::Float64 dynStateEffMsg;
220  dynStateEffMsg.data = this->propellerEfficiency;
221  this->pubDynamicStateEff.publish(dynStateEffMsg);
222  }
223 }
224 
227  uuv_gazebo_ros_plugins_msgs::SetThrusterEfficiency::Request& _req,
228  uuv_gazebo_ros_plugins_msgs::SetThrusterEfficiency::Response& _res)
229 {
230  if (_req.efficiency < 0.0 || _req.efficiency > 1.0)
231  {
232  _res.success = false;
233  }
234  else
235  {
236  this->thrustEfficiency = _req.efficiency;
237  _res.success = true;
238  gzmsg << "Setting thrust efficiency at thruster " <<
239  this->thrusterLink->GetName() << "=" << _req.efficiency * 100
240  << "%" << std::endl;
241  }
242  return true;
243 }
244 
247  uuv_gazebo_ros_plugins_msgs::GetThrusterEfficiency::Request& _req,
248  uuv_gazebo_ros_plugins_msgs::GetThrusterEfficiency::Response& _res)
249 {
250  _res.efficiency = this->thrustEfficiency;
251  return true;
252 }
253 
256  uuv_gazebo_ros_plugins_msgs::SetThrusterEfficiency::Request& _req,
257  uuv_gazebo_ros_plugins_msgs::SetThrusterEfficiency::Response& _res)
258 {
259  if (_req.efficiency < 0.0 || _req.efficiency > 1.0)
260  {
261  _res.success = false;
262  }
263  else
264  {
265  this->propellerEfficiency = _req.efficiency;
266  _res.success = true;
267  gzmsg << "Setting propeller efficiency at thruster " <<
268  this->thrusterLink->GetName() << "=" << _req.efficiency * 100
269  << "%" << std::endl;
270  }
271  return true;
272 }
273 
276  uuv_gazebo_ros_plugins_msgs::GetThrusterEfficiency::Request& _req,
277  uuv_gazebo_ros_plugins_msgs::GetThrusterEfficiency::Response& _res)
278 {
279  _res.efficiency = this->propellerEfficiency;
280  return true;
281 }
282 
285  uuv_gazebo_ros_plugins_msgs::SetThrusterState::Request& _req,
286  uuv_gazebo_ros_plugins_msgs::SetThrusterState::Response& _res)
287 {
288  this->isOn = _req.on;
289  gzmsg << "Turning thruster " << this->thrusterLink->GetName() << " " <<
290  (this->isOn ? "ON" : "OFF") << std::endl;
291  _res.success = true;
292  return true;
293 }
294 
297  uuv_gazebo_ros_plugins_msgs::GetThrusterState::Request& _req,
298  uuv_gazebo_ros_plugins_msgs::GetThrusterState::Response& _res)
299 {
300  _res.is_on = this->isOn;
301  return true;
302 }
303 
306  uuv_gazebo_ros_plugins_msgs::GetThrusterConversionFcn::Request& _req,
307  uuv_gazebo_ros_plugins_msgs::GetThrusterConversionFcn::Response& _res)
308 {
309  _res.fcn.function_name = this->conversionFunction->GetType();
310 
311  double param;
312 
313  if (!_res.fcn.function_name.compare("Basic"))
314  {
315  gzmsg << "ThrusterROSPlugin::GetThrusterConversionFcn::Basic" << std::endl;
316  _res.fcn.tags.push_back("rotor_constant");
317  this->conversionFunction->GetParam("rotor_constant", param);
318  _res.fcn.data.push_back(param);
319  }
320  else if (!_res.fcn.function_name.compare("Bessa"))
321  {
322  gzmsg << "ThrusterROSPlugin::GetThrusterConversionFcn::Bessa" << std::endl;
323  _res.fcn.tags.push_back("rotor_constant_l");
324  this->conversionFunction->GetParam("rotor_constant_l", param);
325  _res.fcn.data.push_back(param);
326 
327  _res.fcn.tags.push_back("rotor_constant_r");
328  this->conversionFunction->GetParam("rotor_constant_r", param);
329  _res.fcn.data.push_back(param);
330 
331  _res.fcn.tags.push_back("delta_l");
332  this->conversionFunction->GetParam("delta_l", param);
333  _res.fcn.data.push_back(param);
334 
335  _res.fcn.tags.push_back("delta_r");
336  this->conversionFunction->GetParam("delta_r", param);
337  _res.fcn.data.push_back(param);
338  }
339  else if (!_res.fcn.function_name.compare("LinearInterp"))
340  {
341  gzmsg << "ThrusterROSPlugin::GetThrusterConversionFcn::LinearInterp" << std::endl;
342  std::map<double, double> table = this->conversionFunction->GetTable();
343 
344  for (auto& item : table)
345  {
346  gzmsg << item.first << " " << item.second << std::endl;
347  _res.fcn.lookup_table_input.push_back(item.first);
348  _res.fcn.lookup_table_output.push_back(item.second);
349  }
350  }
351 
352  return true;
353 }
354 
357 }
physics::LinkPtr thrusterLink
bool param(const std::string &param_name, T &param_val, const T &default_val)
ros::Publisher pubThrustForceEff
Publisher for the thrust force efficiency.
void publish(const boost::shared_ptr< M > &message) const
bool GetThrustForceEfficiency(uuv_gazebo_ros_plugins_msgs::GetThrusterEfficiency::Request &_req, uuv_gazebo_ros_plugins_msgs::GetThrusterEfficiency::Response &_res)
Get the thrust efficiency factor.
gazebo::common::Time GetRosPublishPeriod()
Return the ROS publish period.
transport::PublisherPtr thrustTopicPublisher
gazebo::event::ConnectionPtr rosPublishConnection
Connection for callbacks on update world.
ROSCPP_DECL bool isInitialized()
boost::scoped_ptr< ros::NodeHandle > rosNode
Pointer to this ROS node&#39;s handle.
bool SetThrusterState(uuv_gazebo_ros_plugins_msgs::SetThrusterState::Request &_req, uuv_gazebo_ros_plugins_msgs::SetThrusterState::Response &_res)
Turn thruster on/off.
ros::Publisher pubThrust
Publisher for current actual thrust.
std::shared_ptr< ConversionFunction > conversionFunction
GZ_REGISTER_MODEL_PLUGIN(UmbilicalPlugin)
ros::Publisher pubThrustWrench
Publisher for current actual thrust as wrench.
common::Time thrustForceStamp
#define ROS_WARN(...)
std::map< std::string, ros::ServiceServer > services
Map of thruster services.
void RosPublishStates()
Publish thruster state via ROS.
void SetThrustReference(const uuv_gazebo_ros_plugins_msgs::FloatStamped::ConstPtr &_msg)
Set new set point (desired thrust [N]) for thruster.
ros::Publisher pubDynamicStateEff
Publisher for the dynamic state efficiency.
virtual void Reset()
Reset Module.
transport::SubscriberPtr commandSubscriber
bool SetThrustForceEfficiency(uuv_gazebo_ros_plugins_msgs::SetThrusterEfficiency::Request &_req, uuv_gazebo_ros_plugins_msgs::SetThrusterEfficiency::Response &_res)
Set the thrust efficiency factor.
ignition::math::Vector3d thrusterAxis
bool GetDynamicStateEfficiency(uuv_gazebo_ros_plugins_msgs::GetThrusterEfficiency::Request &_req, uuv_gazebo_ros_plugins_msgs::GetThrusterEfficiency::Response &_res)
Get the dynamic state efficiency factor.
bool GetThrusterConversionFcn(uuv_gazebo_ros_plugins_msgs::GetThrusterConversionFcn::Request &_req, uuv_gazebo_ros_plugins_msgs::GetThrusterConversionFcn::Response &_res)
Get thruster conversion function parameters.
bool GetThrusterState(uuv_gazebo_ros_plugins_msgs::GetThrusterState::Request &_req, uuv_gazebo_ros_plugins_msgs::GetThrusterState::Response &_res)
Get thruster state.
void SetRosPublishRate(double _hz)
Set the ROS publish frequency (Hz).
bool SetDynamicStateEfficiency(uuv_gazebo_ros_plugins_msgs::SetThrusterEfficiency::Request &_req, uuv_gazebo_ros_plugins_msgs::SetThrusterEfficiency::Response &_res)
Set the dynamic state efficiency factor.
static Time now()
ros::Publisher pubThrusterState
Publisher for the thruster state.
void Load(gazebo::physics::ModelPtr _parent, sdf::ElementPtr _sdf)
Load module and read parameters from SDF.
ros::Subscriber subThrustReference
Subscriber reacting to new reference thrust set points.
gazebo::common::Time rosPublishPeriod
Period after which we should publish a message via ROS.
gazebo::common::Time lastRosPublishTime
Last time we published a message via ROS.
virtual void Init()
Initialize Module.


uuv_gazebo_ros_plugins
Author(s): Musa Morena Marcusso Manhaes , Sebastian Scherer , Luiz Ricardo Douat
autogenerated on Thu Jun 18 2020 03:28:28