-
Notifications
You must be signed in to change notification settings - Fork 776
/
gazebo_ros_joint_state_publisher.cpp
154 lines (132 loc) · 6.39 KB
/
gazebo_ros_joint_state_publisher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (c) 2013, Markus Bader <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Antons Rebguns <email> ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Antons Rebguns <email> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
**/
#include <boost/algorithm/string.hpp>
#include <gazebo_plugins/gazebo_ros_joint_state_publisher.h>
#include <tf/transform_broadcaster.h>
#include <tf/transform_listener.h>
using namespace gazebo;
GazeboRosJointStatePublisher::GazeboRosJointStatePublisher() {}
// Destructor
GazeboRosJointStatePublisher::~GazeboRosJointStatePublisher() {
rosnode_->shutdown();
}
void GazeboRosJointStatePublisher::Load ( physics::ModelPtr _parent, sdf::ElementPtr _sdf ) {
// Store the pointer to the model
this->parent_ = _parent;
this->world_ = _parent->GetWorld();
this->robot_namespace_ = parent_->GetName ();
if ( !_sdf->HasElement ( "robotNamespace" ) ) {
ROS_INFO_NAMED("joint_state_publisher", "GazeboRosJointStatePublisher Plugin missing <robotNamespace>, defaults to \"%s\"",
this->robot_namespace_.c_str() );
} else {
this->robot_namespace_ = _sdf->GetElement ( "robotNamespace" )->Get<std::string>();
if ( this->robot_namespace_.empty() ) this->robot_namespace_ = parent_->GetName ();
}
if ( !robot_namespace_.empty() ) this->robot_namespace_ += "/";
rosnode_ = boost::shared_ptr<ros::NodeHandle> ( new ros::NodeHandle ( this->robot_namespace_ ) );
if ( !_sdf->HasElement ( "jointName" ) ) {
ROS_ASSERT ( "GazeboRosJointStatePublisher Plugin missing jointNames" );
} else {
sdf::ElementPtr element = _sdf->GetElement ( "jointName" ) ;
std::string joint_names = element->Get<std::string>();
boost::erase_all ( joint_names, " " );
boost::split ( joint_names_, joint_names, boost::is_any_of ( "," ) );
}
this->update_rate_ = 100.0;
if ( !_sdf->HasElement ( "updateRate" ) ) {
ROS_WARN_NAMED("joint_state_publisher", "GazeboRosJointStatePublisher Plugin (ns = %s) missing <updateRate>, defaults to %f",
this->robot_namespace_.c_str(), this->update_rate_ );
} else {
this->update_rate_ = _sdf->GetElement ( "updateRate" )->Get<double>();
}
// Initialize update rate stuff
if ( this->update_rate_ > 0.0 ) {
this->update_period_ = 1.0 / this->update_rate_;
} else {
this->update_period_ = 0.0;
}
#if GAZEBO_MAJOR_VERSION >= 8
last_update_time_ = this->world_->SimTime();
#else
last_update_time_ = this->world_->GetSimTime();
#endif
for ( unsigned int i = 0; i< joint_names_.size(); i++ ) {
joints_.push_back ( this->parent_->GetJoint ( joint_names_[i] ) );
ROS_INFO_NAMED("joint_state_publisher", "GazeboRosJointStatePublisher is going to publish joint: %s", joint_names_[i].c_str() );
}
ROS_INFO_NAMED("joint_state_publisher", "Starting GazeboRosJointStatePublisher Plugin (ns = %s)!, parent name: %s", this->robot_namespace_.c_str(), parent_->GetName ().c_str() );
tf_prefix_ = tf::getPrefixParam ( *rosnode_ );
joint_state_publisher_ = rosnode_->advertise<sensor_msgs::JointState> ( "joint_states",1000 );
#if GAZEBO_MAJOR_VERSION >= 8
last_update_time_ = this->world_->SimTime();
#else
last_update_time_ = this->world_->GetSimTime();
#endif
// Listen to the update event. This event is broadcast every
// simulation iteration.
this->updateConnection = event::Events::ConnectWorldUpdateBegin (
boost::bind ( &GazeboRosJointStatePublisher::OnUpdate, this, _1 ) );
}
void GazeboRosJointStatePublisher::OnUpdate ( const common::UpdateInfo & _info ) {
// Apply a small linear velocity to the model.
#if GAZEBO_MAJOR_VERSION >= 8
common::Time current_time = this->world_->SimTime();
#else
common::Time current_time = this->world_->GetSimTime();
#endif
if (current_time < last_update_time_)
{
ROS_WARN_NAMED("joint_state_publisher", "Negative joint state update time difference detected.");
last_update_time_ = current_time;
}
double seconds_since_last_update = ( current_time - last_update_time_ ).Double();
if ( seconds_since_last_update > update_period_ ) {
publishJointStates();
last_update_time_+= common::Time ( update_period_ );
}
}
void GazeboRosJointStatePublisher::publishJointStates() {
ros::Time current_time = ros::Time::now();
joint_state_.header.stamp = current_time;
joint_state_.name.resize ( joints_.size() );
joint_state_.position.resize ( joints_.size() );
joint_state_.velocity.resize ( joints_.size() );
for ( int i = 0; i < joints_.size(); i++ ) {
physics::JointPtr joint = joints_[i];
double velocity = joint->GetVelocity( 0 );
#if GAZEBO_MAJOR_VERSION >= 8
double position = joint->Position ( 0 );
#else
double position = joint->GetAngle ( 0 ).Radian();
#endif
joint_state_.name[i] = joint->GetName();
joint_state_.position[i] = position;
joint_state_.velocity[i] = velocity;
}
joint_state_publisher_.publish ( joint_state_ );
}