FinROSPlugin.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 namespace uuv_simulator_ros
26 {
29 {
30  this->rosPublishPeriod = gazebo::common::Time(0.05);
31  this->lastRosPublishTime = gazebo::common::Time(0.0);
32 }
33 
36 {
37 #if GAZEBO_MAJOR_VERSION >= 8
38  this->rosPublishConnection.reset();
39 #else
40  gazebo::event::Events::DisconnectWorldUpdateBegin(
41  this->rosPublishConnection);
42 #endif
43  this->rosNode->shutdown();
44 }
45 
48  const uuv_gazebo_ros_plugins_msgs::FloatStamped::ConstPtr &_msg)
49 {
50  if (std::isnan(_msg->data))
51  {
52  ROS_WARN("FinROSPlugin: Ignoring nan command");
53  return;
54  }
55 
56  this->inputCommand = _msg->data;
57 }
58 
60 gazebo::common::Time FinROSPlugin::GetRosPublishPeriod()
61 {
62  return this->rosPublishPeriod;
63 }
64 
67 {
68  if (_hz > 0.0)
69  this->rosPublishPeriod = 1.0 / _hz;
70  else
71  this->rosPublishPeriod = 0.;
72 }
73 
76 {
77  FinPlugin::Init();
78 }
79 
82 {
83  this->lastRosPublishTime.Set(0, 0);
84 }
85 
87 void FinROSPlugin::Load(gazebo::physics::ModelPtr _parent, sdf::ElementPtr _sdf)
88 {
89  try {
90  FinPlugin::Load(_parent, _sdf);
91  } catch(gazebo::common::Exception &_e)
92  {
93  gzerr << "Error loading plugin."
94  << "Please ensure that your model is correct."
95  << '\n';
96  return;
97  }
98 
99  if (!ros::isInitialized())
100  {
101  gzerr << "Not loading plugin since ROS has not been "
102  << "properly initialized. Try starting gazebo with ros plugin:\n"
103  << " gazebo -s libgazebo_ros_api_plugin.so\n";
104  return;
105  }
106 
107  this->rosNode.reset(new ros::NodeHandle(""));
108 
109  this->subReference = this->rosNode->subscribe<
110  uuv_gazebo_ros_plugins_msgs::FloatStamped
111  >(this->commandSubscriber->GetTopic(), 10,
112  boost::bind(&FinROSPlugin::SetReference, this, _1));
113 
114  this->pubState = this->rosNode->advertise<
115  uuv_gazebo_ros_plugins_msgs::FloatStamped
116  >(this->anglePublisher->GetTopic(), 10);
117 
118  std::string wrenchTopic;
119  if (_sdf->HasElement("wrench_topic"))
120  wrenchTopic = _sdf->Get<std::string>("wrench_topic");
121  else
122  wrenchTopic = this->topicPrefix + "wrench_topic";
123 
124  this->pubFinForce =
125  this->rosNode->advertise<geometry_msgs::WrenchStamped>(wrenchTopic, 10);
126 
127  std::stringstream stream;
128  stream << _parent->GetName() << "/fins/" << this->finID <<
129  "/get_lift_drag_params";
130  this->services["get_lift_drag_params"] = this->rosNode->advertiseService(
131  stream.str(), &FinROSPlugin::GetLiftDragParams, this);
132 
133  gzmsg << "Fin #" << this->finID << " initialized" << std::endl
134  << "\t- Link: " << this->link->GetName() << std::endl
135  << "\t- Robot model: " << _parent->GetName() << std::endl
136  << "\t- Input command topic: " <<
137  this->commandSubscriber->GetTopic() << std::endl
138  << "\t- Output topic: " <<
139  this->anglePublisher->GetTopic() << std::endl;
140 
141  this->rosPublishConnection = gazebo::event::Events::ConnectWorldUpdateBegin(
142  boost::bind(&FinROSPlugin::RosPublishStates, this));
143 }
144 
147 {
148  // Limit publish rate according to publish period
149  if (this->angleStamp - this->lastRosPublishTime >=
150  this->rosPublishPeriod)
151  {
152  this->lastRosPublishTime = this->angleStamp;
153 
154  // Publish the current angle of attack
155  uuv_gazebo_ros_plugins_msgs::FloatStamped state_msg;
156  state_msg.header.stamp = ros::Time().now();
157  state_msg.header.frame_id = this->link->GetName();
158  state_msg.data = this->angle;
159  this->pubState.publish(state_msg);
160 
161  // Publish the lift and drag forces
162  geometry_msgs::WrenchStamped msg;
163  msg.header.stamp = ros::Time().now();
164  msg.header.frame_id = this->link->GetName();
165  msg.wrench.force.x = this->finForce.X();
166  msg.wrench.force.y = this->finForce.Y();
167  msg.wrench.force.z = this->finForce.Z();
168 
169  this->pubFinForce.publish(msg);
170  }
171 }
172 
175  uuv_gazebo_ros_plugins_msgs::GetListParam::Request& _req,
176  uuv_gazebo_ros_plugins_msgs::GetListParam::Response& _res)
177 {
178  _res.description = this->liftdrag->GetType();
179  for (auto& item : this->liftdrag->GetListParams())
180  {
181  _res.tags.push_back(item.first);
182  _res.data.push_back(item.second);
183  }
184 
185  return true;
186 }
187 
189 }
void Load(gazebo::physics::ModelPtr _parent, sdf::ElementPtr _sdf)
Load module and read parameters from SDF.
Definition: FinROSPlugin.cc:87
ignition::math::Vector3d finForce
std::map< std::string, ros::ServiceServer > services
Map of services.
Definition: FinROSPlugin.hh:85
void publish(const boost::shared_ptr< M > &message) const
ros::Publisher pubFinForce
Publisher for current actual thrust.
Definition: FinROSPlugin.hh:76
ROSCPP_DECL bool isInitialized()
GZ_REGISTER_MODEL_PLUGIN(UmbilicalPlugin)
virtual void Reset()
Reset Module.
Definition: FinROSPlugin.cc:81
#define ROS_WARN(...)
common::Time angleStamp
gazebo::event::ConnectionPtr rosPublishConnection
Connection for callbacks on update world.
Definition: FinROSPlugin.hh:79
void RosPublishStates()
Publish state via ROS.
void SetRosPublishRate(double _hz)
Set the ROS publish frequency (Hz).
Definition: FinROSPlugin.cc:66
bool GetLiftDragParams(uuv_gazebo_ros_plugins_msgs::GetListParam::Request &_req, uuv_gazebo_ros_plugins_msgs::GetListParam::Response &_res)
Return the list of paramaters of the lift and drag model.
gazebo::common::Time rosPublishPeriod
Period after which we should publish a message via ROS.
Definition: FinROSPlugin.hh:82
void SetReference(const uuv_gazebo_ros_plugins_msgs::FloatStamped::ConstPtr &_msg)
Set new set point.
Definition: FinROSPlugin.cc:47
virtual void Init()
Initialize Module.
Definition: FinROSPlugin.cc:75
boost::scoped_ptr< ros::NodeHandle > rosNode
Pointer to this ROS node&#39;s handle.
Definition: FinROSPlugin.hh:67
std::string topicPrefix
gazebo::common::Time lastRosPublishTime
Last time we published a message via ROS.
Definition: FinROSPlugin.hh:88
physics::LinkPtr link
static Time now()
transport::SubscriberPtr commandSubscriber
ros::Publisher pubState
Publisher for current state.
Definition: FinROSPlugin.hh:73
transport::PublisherPtr anglePublisher
ros::Subscriber subReference
Subscriber reacting to new reference set points.
Definition: FinROSPlugin.hh:70
std::shared_ptr< LiftDrag > liftdrag
gazebo::common::Time GetRosPublishPeriod()
Return the ROS publish period.
Definition: FinROSPlugin.cc:60


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