JointStatePublisher.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 {
20 GZ_REGISTER_MODEL_PLUGIN(JointStatePublisher)
21 
23 {
24  this->model = NULL;
25  this->world = NULL;
26 }
27 
29 {
30  this->node->shutdown();
31 }
32 
33 void JointStatePublisher::Load(gazebo::physics::ModelPtr _parent,
34  sdf::ElementPtr _sdf)
35 {
36  this->model = _parent;
37 
38  GZ_ASSERT(this->model != NULL, "Invalid model pointer");
39 
40  this->world = this->model->GetWorld();
41 
42  if (!ros::isInitialized())
43  {
44  gzerr << "ROS was not initialized. Closing plugin..." << std::endl;
45  return;
46  }
47 
49  new ros::NodeHandle(this->robotNamespace));
50  // Retrieve the namespace used to publish the joint states
51  if (_sdf->HasElement("robotNamespace"))
52  this->robotNamespace = _sdf->Get<std::string>("robotNamespace");
53  else
54  this->robotNamespace = this->model->GetName();
55 
56  gzmsg << "JointStatePublisher::robotNamespace="
57  << this->robotNamespace << std::endl;
58 
59  if (this->robotNamespace[0] != '/')
60  this->robotNamespace = "/" + this->robotNamespace;
61 
62  if (_sdf->HasElement("updateRate"))
63  this->updateRate = _sdf->Get<double>("updateRate");
64  else
65  this->updateRate = 50;
66 
67  gzmsg << "JointStatePublisher::Retrieving moving joints:" << std::endl;
68  this->movingJoints.clear();
69  double upperLimit, lowerLimit;
70  for (auto &joint : this->model->GetJoints())
71  {
72 #if GAZEBO_MAJOR_VERSION >= 8
73  lowerLimit = joint->LowerLimit(0);
74  upperLimit = joint->UpperLimit(0);
75 #else
76  lowerLimit = joint->GetLowerLimit(0).Radian();
77  upperLimit = joint->GetUpperLimit(0).Radian();
78 #endif
79  if (lowerLimit == 0 && upperLimit == 0)
80  continue;
81  else if (joint->GetType() == gazebo::physics::Base::EntityType::FIXED_JOINT)
82  continue;
83  else
84  {
85  this->movingJoints.push_back(joint->GetName());
86  gzmsg << "\t- " << joint->GetName() << std::endl;
87  }
88  }
89 
90  GZ_ASSERT(this->updateRate > 0, "Update rate must be positive");
91 
92  // Setting the update period
93  this->updatePeriod = 1.0 / this->updateRate;
94 
95  // Advertise the joint states topic
96  this->jointStatePub =
97  this->node->advertise<sensor_msgs::JointState>(
98  this->robotNamespace + "/joint_states", 1);
99 #if GAZEBO_MAJOR_VERSION >= 8
100  this->lastUpdate = this->world->SimTime();
101 #else
102  this->lastUpdate = this->world->GetSimTime();
103 #endif
104  // Connect the update function to the Gazebo callback
105  this->updateConnection = gazebo::event::Events::ConnectWorldUpdateBegin(
106  boost::bind(&JointStatePublisher::OnUpdate, this, _1));
107 }
108 
109 void JointStatePublisher::OnUpdate(const gazebo::common::UpdateInfo &_info)
110 {
111 #if GAZEBO_MAJOR_VERSION >= 8
112  gazebo::common::Time simTime = this->world->SimTime();
113 #else
114  gazebo::common::Time simTime = this->world->GetSimTime();
115 #endif
116  if (simTime - this->lastUpdate >= this->updatePeriod)
117  {
118  this->PublishJointStates();
119  this->lastUpdate = simTime;
120  }
121 }
122 
124 {
125  ros::Time stamp = ros::Time::now();
126  sensor_msgs::JointState jointState;
127 
128  jointState.header.stamp = stamp;
129  // Resize containers
130  jointState.name.resize(this->model->GetJointCount());
131  jointState.position.resize(this->model->GetJointCount());
132  jointState.velocity.resize(this->model->GetJointCount());
133  jointState.effort.resize(this->model->GetJointCount());
134 
135  int i = 0;
136  for (auto &joint : this->model->GetJoints())
137  {
138  if (!this->IsIgnoredJoint(joint->GetName()))
139  {
140  jointState.name[i] = joint->GetName();
141 #if GAZEBO_MAJOR_VERSION >= 8
142  jointState.position[i] = joint->Position(0);
143 #else
144  jointState.position[i] = joint->GetAngle(0).Radian();
145 #endif
146  jointState.velocity[i] = joint->GetVelocity(0);
147  jointState.effort[i] = joint->GetForce(0);
148  }
149  else
150  {
151  jointState.name[i] = joint->GetName();
152  jointState.position[i] = 0.0;
153  jointState.velocity[i] = 0.0;
154  jointState.effort[i] = 0.0;
155  }
156 
157  ++i;
158  }
159 
160  this->jointStatePub.publish(jointState);
161 }
162 
163 bool JointStatePublisher::IsIgnoredJoint(std::string _jointName)
164 {
165  if (this->movingJoints.empty()) return true;
166  for (auto joint : this->movingJoints)
167  if (_jointName.compare(joint) == 0)
168  return false;
169  return true;
170 }
171 }
void OnUpdate(const gazebo::common::UpdateInfo &_info)
bool IsIgnoredJoint(std::string _jointName)
void publish(const boost::shared_ptr< M > &message) const
gazebo::event::ConnectionPtr updateConnection
ROSCPP_DECL bool isInitialized()
GZ_REGISTER_MODEL_PLUGIN(UmbilicalPlugin)
void Load(gazebo::physics::ModelPtr _parent, sdf::ElementPtr _sdf)
boost::shared_ptr< ros::NodeHandle > node
static Time now()


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