LinearBatteryROSPlugin.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 <gazebo/plugins/LinearBatteryPlugin.hh>
19 #include <gazebo/physics/Model.hh>
20 #include <gazebo/common/Plugin.hh>
21 
22 namespace gazebo
23 {
26 {
27  this->robotNamespace = "";
28 }
29 
32 {
33  this->rosNode->shutdown();
34 }
35 
37 void LinearBatteryROSPlugin::Load(physics::ModelPtr _parent,
38  sdf::ElementPtr _sdf)
39 {
40  try
41  {
42  LinearBatteryPlugin::Load(_parent, _sdf);
43  }
44  catch(common::Exception &_e)
45  {
46  gzerr << "Error loading plugin."
47  << "Please ensure that your model is correct."
48  << '\n';
49  return;
50  }
51 
52  if (!ros::isInitialized())
53  {
54  gzerr << "Not loading plugin since ROS has not been "
55  << "properly initialized. Try starting gazebo with ros plugin:\n"
56  << " gazebo -s libgazebo_ros_api_plugin.so\n";
57  return;
58  }
59 
60  if (_sdf->HasElement("namespace"))
61  this->robotNamespace = _sdf->Get<std::string>("namespace");
62 
63  double updateRate = 2;
64  if (_sdf->HasElement("update_rate"))
65  updateRate = _sdf->Get<double>("update_rate");
66 
67  if (updateRate <= 0.0)
68  {
69  gzmsg << "Invalid update rate, setting it to 2 Hz, rate=" << updateRate
70  << std::endl;
71  updateRate = 2;
72  }
73  this->rosNode.reset(new ros::NodeHandle(this->robotNamespace));
74 
75  this->batteryStatePub = this->rosNode->advertise<sensor_msgs::BatteryState>
76  ("battery_state", 0);
77 
78  this->updateTimer = this->rosNode->createTimer(
79  ros::Duration(1 / updateRate),
81 
82  gzmsg << "ROS Battery Plugin for link <" << this->link->GetName()
83  << "> initialized\n"
84  << "\t- Initial charge [Ah]=" << this->q0 << '\n'
85  << "\t- Update rate [Hz]=" << updateRate
86  << std::endl;
87 }
88 
91 {
92  this->batteryStateMsg.header.stamp = ros::Time().now();
93  this->batteryStateMsg.header.frame_id = this->link->GetName();
94 
95  this->batteryStateMsg.charge = this->q;
96  this->batteryStateMsg.percentage = this->q / this->q0;
97  this->batteryStateMsg.voltage = this->battery->Voltage();
98  this->batteryStateMsg.design_capacity = this->q0;
99 
100  this->batteryStateMsg.power_supply_status =
101  sensor_msgs::BatteryState::POWER_SUPPLY_STATUS_DISCHARGING;
102  this->batteryStateMsg.power_supply_health =
103  sensor_msgs::BatteryState::POWER_SUPPLY_HEALTH_GOOD;
104  this->batteryStateMsg.power_supply_technology =
105  sensor_msgs::BatteryState::POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
106  this->batteryStateMsg.present = true;
107 
108  // Publish battery message
110 }
111 
114 {
115  LinearBatteryPlugin::Init();
116 }
117 
120 {
121  LinearBatteryPlugin::Reset();
122 }
123 
125 }
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL bool isInitialized()
GZ_REGISTER_MODEL_PLUGIN(UmbilicalPlugin)
ros::Publisher batteryStatePub
Publisher for the dynamic state efficiency.
void PublishBatteryState()
Publish battery states.
virtual void Reset()
Reset Module.
sensor_msgs::BatteryState batteryStateMsg
Battery state ROS message.
virtual void Init()
Initialize Module.
void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
Load module and read parameters from SDF.
std::string robotNamespace
Namespace for this ROS node.
virtual ~LinearBatteryROSPlugin()
Destructor.
static Time now()
boost::scoped_ptr< ros::NodeHandle > rosNode
Pointer to this ROS node&#39;s handle.
ros::Timer updateTimer
Connection for callbacks on update world.


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