-
Notifications
You must be signed in to change notification settings - Fork 772
/
gazebo_ros_gpu_laser.cpp
209 lines (177 loc) · 6.89 KB
/
gazebo_ros_gpu_laser.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright 2013 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
Desc: GazeboRosGpuLaser plugin for simulating ray sensors in Gazebo
Author: Mihai Emanuel Dolha
Date: 29 March 2012
*/
#include <algorithm>
#include <string>
#include <assert.h>
#include <sdf/sdf.hh>
#include <gazebo/physics/World.hh>
#include <gazebo/physics/HingeJoint.hh>
#include <gazebo/sensors/Sensor.hh>
#include <gazebo/common/Exception.hh>
#include <gazebo/sensors/GpuRaySensor.hh>
#include <gazebo/sensors/SensorTypes.hh>
#include <gazebo/transport/transport.hh>
#include <tf/tf.h>
#include <tf/transform_listener.h>
#include "gazebo_plugins/gazebo_ros_gpu_laser.h"
#include <gazebo_plugins/gazebo_ros_utils.h>
namespace gazebo
{
// Register this plugin with the simulator
GZ_REGISTER_SENSOR_PLUGIN(GazeboRosLaser)
////////////////////////////////////////////////////////////////////////////////
// Constructor
GazeboRosLaser::GazeboRosLaser()
{
this->seed = 0;
}
////////////////////////////////////////////////////////////////////////////////
// Destructor
GazeboRosLaser::~GazeboRosLaser()
{
ROS_DEBUG_STREAM_NAMED("gpu_laser","Shutting down GPU Laser");
this->rosnode_->shutdown();
delete this->rosnode_;
ROS_DEBUG_STREAM_NAMED("gpu_laser","Unloaded");
}
////////////////////////////////////////////////////////////////////////////////
// Load the controller
void GazeboRosLaser::Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf)
{
// load plugin
GpuRayPlugin::Load(_parent, this->sdf);
// Get the world name.
std::string worldName = _parent->WorldName();
this->world_ = physics::get_world(worldName);
// save pointers
this->sdf = _sdf;
GAZEBO_SENSORS_USING_DYNAMIC_POINTER_CAST;
this->parent_ray_sensor_ =
dynamic_pointer_cast<sensors::GpuRaySensor>(_parent);
if (!this->parent_ray_sensor_)
gzthrow("GazeboRosLaser controller requires a Ray Sensor as its parent");
this->robot_namespace_ = GetRobotNamespace(_parent, _sdf, "Laser");
if (!this->sdf->HasElement("frameName"))
{
ROS_INFO_NAMED("gpu_laser", "GazeboRosLaser plugin missing <frameName>, defaults to /world");
this->frame_name_ = "/world";
}
else
this->frame_name_ = this->sdf->Get<std::string>("frameName");
if (!this->sdf->HasElement("topicName"))
{
ROS_INFO_NAMED("gpu_laser", "GazeboRosLaser plugin missing <topicName>, defaults to /world");
this->topic_name_ = "/world";
}
else
this->topic_name_ = this->sdf->Get<std::string>("topicName");
this->laser_connect_count_ = 0;
// Make sure the ROS node for Gazebo has already been initialized
if (!ros::isInitialized())
{
ROS_FATAL_STREAM_NAMED("gpu_laser", "A ROS node for Gazebo has not been initialized, unable to load plugin. "
<< "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
return;
}
ROS_INFO_NAMED("gpu_laser", "Starting GazeboRosLaser Plugin (ns = %s)", this->robot_namespace_.c_str() );
// ros callback queue for processing subscription
this->deferred_load_thread_ = boost::thread(
boost::bind(&GazeboRosLaser::LoadThread, this));
}
////////////////////////////////////////////////////////////////////////////////
// Load the controller
void GazeboRosLaser::LoadThread()
{
this->gazebo_node_ = gazebo::transport::NodePtr(new gazebo::transport::Node());
this->gazebo_node_->Init(this->world_name_);
this->pmq.startServiceThread();
this->rosnode_ = new ros::NodeHandle(this->robot_namespace_);
this->tf_prefix_ = tf::getPrefixParam(*this->rosnode_);
if(this->tf_prefix_.empty()) {
this->tf_prefix_ = this->robot_namespace_;
boost::trim_right_if(this->tf_prefix_,boost::is_any_of("/"));
}
ROS_INFO_NAMED("gpu_laser", "GPU Laser Plugin (ns = %s) <tf_prefix_>, set to \"%s\"",
this->robot_namespace_.c_str(), this->tf_prefix_.c_str());
// resolve tf prefix
this->frame_name_ = tf::resolve(this->tf_prefix_, this->frame_name_);
if (this->topic_name_ != "")
{
ros::AdvertiseOptions ao =
ros::AdvertiseOptions::create<sensor_msgs::LaserScan>(
this->topic_name_, 1,
boost::bind(&GazeboRosLaser::LaserConnect, this),
boost::bind(&GazeboRosLaser::LaserDisconnect, this),
ros::VoidPtr(), NULL);
this->pub_ = this->rosnode_->advertise(ao);
this->pub_queue_ = this->pmq.addPub<sensor_msgs::LaserScan>();
}
// Initialize the controller
// sensor generation off by default
this->parent_ray_sensor_->SetActive(false);
ROS_INFO_STREAM_NAMED("gpu_laser","LoadThread function completed");
}
////////////////////////////////////////////////////////////////////////////////
// Increment count
void GazeboRosLaser::LaserConnect()
{
this->laser_connect_count_++;
if (this->laser_connect_count_ == 1)
this->laser_scan_sub_ =
this->gazebo_node_->Subscribe(this->parent_ray_sensor_->Topic(),
&GazeboRosLaser::OnScan, this);
}
////////////////////////////////////////////////////////////////////////////////
// Decrement count
void GazeboRosLaser::LaserDisconnect()
{
this->laser_connect_count_--;
if (this->laser_connect_count_ == 0)
this->laser_scan_sub_.reset();
}
////////////////////////////////////////////////////////////////////////////////
// Convert new Gazebo message to ROS message and publish it
void GazeboRosLaser::OnScan(ConstLaserScanStampedPtr &_msg)
{
// We got a new message from the Gazebo sensor. Stuff a
// corresponding ROS message and publish it.
sensor_msgs::LaserScan laser_msg;
laser_msg.header.stamp = ros::Time(_msg->time().sec(), _msg->time().nsec());
laser_msg.header.frame_id = this->frame_name_;
laser_msg.angle_min = _msg->scan().angle_min();
laser_msg.angle_max = _msg->scan().angle_max();
laser_msg.angle_increment = _msg->scan().angle_step();
laser_msg.time_increment = 0; // instantaneous simulator scan
laser_msg.scan_time = 0; // not sure whether this is correct
laser_msg.range_min = _msg->scan().range_min();
laser_msg.range_max = _msg->scan().range_max();
laser_msg.ranges.resize(_msg->scan().ranges_size());
std::copy(_msg->scan().ranges().begin(),
_msg->scan().ranges().end(),
laser_msg.ranges.begin());
laser_msg.intensities.resize(_msg->scan().intensities_size());
std::copy(_msg->scan().intensities().begin(),
_msg->scan().intensities().end(),
laser_msg.intensities.begin());
this->pub_queue_->push(laser_msg, this->pub_);
}
}