-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathGpuLidarSensor.cc
379 lines (306 loc) · 10.9 KB
/
GpuLidarSensor.cc
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Copyright (C) 2018 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.
*
*/
#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable: 4005)
#pragma warning(disable: 4251)
#endif
#include <ignition/msgs/pointcloud_packed.pb.h>
#ifdef _WIN32
#pragma warning(pop)
#endif
#include <ignition/common/Console.hh>
#include <ignition/common/Profiler.hh>
#include <ignition/msgs/Utility.hh>
#include <ignition/transport/Node.hh>
#include "ignition/sensors/GpuLidarSensor.hh"
#include "ignition/sensors/SensorFactory.hh"
using namespace ignition::sensors;
/// \brief Private data for the GpuLidar class
class ignition::sensors::GpuLidarSensorPrivate
{
/// \brief Fill the point cloud packed message
/// \param[in] _laserBuffer Lidar data buffer.
public: void FillPointCloudMsg(const float *_laserBuffer);
/// \brief Rendering camera
public: ignition::rendering::GpuRaysPtr gpuRays;
/// \brief Connection to the Manager's scene change event.
public: ignition::common::ConnectionPtr sceneChangeConnection;
/// \brief The point cloud message.
public: msgs::PointCloudPacked pointMsg;
/// \brief Transport node.
public: transport::Node node;
/// \brief Publisher for the publish point cloud message.
public: transport::Node::Publisher pointPub;
};
//////////////////////////////////////////////////
GpuLidarSensor::GpuLidarSensor()
: dataPtr(new GpuLidarSensorPrivate())
{
}
//////////////////////////////////////////////////
GpuLidarSensor::~GpuLidarSensor()
{
this->RemoveGpuRays(this->Scene());
this->dataPtr->sceneChangeConnection.reset();
if (this->laserBuffer)
{
delete [] this->laserBuffer;
this->laserBuffer = nullptr;
}
}
/////////////////////////////////////////////////
void GpuLidarSensor::SetScene(ignition::rendering::ScenePtr _scene)
{
std::lock_guard<std::mutex> lock(this->lidarMutex);
// APIs make it possible for the scene pointer to change
if (this->Scene() != _scene)
{
this->RemoveGpuRays(this->Scene());
RenderingSensor::SetScene(_scene);
if (this->initialized)
this->CreateLidar();
}
}
//////////////////////////////////////////////////
void GpuLidarSensor::RemoveGpuRays(
ignition::rendering::ScenePtr _scene)
{
if (_scene)
{
_scene->DestroySensor(this->dataPtr->gpuRays);
}
this->dataPtr->gpuRays.reset();
this->dataPtr->gpuRays = nullptr;
}
//////////////////////////////////////////////////
bool GpuLidarSensor::Load(const sdf::Sensor &_sdf)
{
// Check if this is being loaded via "builtin" or via a plugin
if (!Lidar::Load(_sdf))
{
return false;
}
// Initialize the point message.
// \todo(anyone) The true value in the following function call forces
// the xyz and rgb fields to be aligned to memory boundaries. This is need
// by ROS1: https://github.com/ros/common_msgs/pull/77. Ideally, memory
// alignment should be configured. This same problem is in the
// RgbdCameraSensor.
msgs::InitPointCloudPacked(this->dataPtr->pointMsg, this->Name(), true,
{{"xyz", msgs::PointCloudPacked::Field::FLOAT32},
{"intensity", msgs::PointCloudPacked::Field::FLOAT32},
{"ring", msgs::PointCloudPacked::Field::UINT16}});
if (this->Scene())
this->CreateLidar();
this->dataPtr->sceneChangeConnection =
RenderingEvents::ConnectSceneChangeCallback(
std::bind(&GpuLidarSensor::SetScene, this, std::placeholders::_1));
// Create the point cloud publisher
this->SetTopic(this->Topic() + "/points");
this->dataPtr->pointPub =
this->dataPtr->node.Advertise<ignition::msgs::PointCloudPacked>(
this->Topic());
if (!this->dataPtr->pointPub)
{
ignerr << "Unable to create publisher on topic["
<< this->Topic() << "].\n";
return false;
}
this->initialized = true;
return true;
}
//////////////////////////////////////////////////
bool GpuLidarSensor::Load(sdf::ElementPtr _sdf)
{
sdf::Sensor sdfSensor;
sdfSensor.Load(_sdf);
return this->Load(sdfSensor);
}
//////////////////////////////////////////////////
bool GpuLidarSensor::Init()
{
return this->Sensor::Init();
}
//////////////////////////////////////////////////
bool GpuLidarSensor::CreateLidar()
{
this->dataPtr->gpuRays = this->Scene()->CreateGpuRays(
this->Name());
if (!this->dataPtr->gpuRays)
{
ignerr << "Unable to create gpu laser sensor\n";
return false;
}
this->dataPtr->gpuRays->SetWorldPosition(this->Pose().Pos());
this->dataPtr->gpuRays->SetWorldRotation(this->Pose().Rot());
this->dataPtr->gpuRays->SetNearClipPlane(this->RangeMin());
this->dataPtr->gpuRays->SetFarClipPlane(this->RangeMax());
// Mask ranges outside of min/max to +/- inf, as per REP 117
this->dataPtr->gpuRays->SetClamp(false);
this->dataPtr->gpuRays->SetAngleMin(this->AngleMin().Radian());
this->dataPtr->gpuRays->SetAngleMax(this->AngleMax().Radian());
this->dataPtr->gpuRays->SetVerticalAngleMin(
this->VerticalAngleMin().Radian());
this->dataPtr->gpuRays->SetVerticalAngleMax(
this->VerticalAngleMax().Radian());
this->dataPtr->gpuRays->SetRayCount(this->RayCount());
this->dataPtr->gpuRays->SetVerticalRayCount(
this->VerticalRayCount());
this->Scene()->RootVisual()->AddChild(
this->dataPtr->gpuRays);
// Set the values on the point message.
this->dataPtr->pointMsg.set_width(this->dataPtr->gpuRays->RangeCount());
this->dataPtr->pointMsg.set_height(
this->dataPtr->gpuRays->VerticalRangeCount());
this->dataPtr->pointMsg.set_row_step(
this->dataPtr->pointMsg.point_step() *
this->dataPtr->pointMsg.width());
this->AddSensor(this->dataPtr->gpuRays);
return true;
}
//////////////////////////////////////////////////
bool GpuLidarSensor::Update(const ignition::common::Time &_now)
{
return this->Update(math::secNsecToDuration(_now.sec, _now.nsec));
}
//////////////////////////////////////////////////
bool GpuLidarSensor::Update(const std::chrono::steady_clock::duration &_now)
{
IGN_PROFILE("GpuLidarSensor::Update");
if (!this->initialized)
{
ignerr << "Not initialized, update ignored.\n";
return false;
}
if (!this->dataPtr->gpuRays)
{
ignerr << "GpuRays doesn't exist.\n";
return false;
}
int len = this->dataPtr->gpuRays->RayCount() *
this->dataPtr->gpuRays->VerticalRayCount() * 3;
if (this->laserBuffer == nullptr)
{
this->laserBuffer = new float[len];
}
this->Render();
/// \todo(anyone) It would be nice to remove this copy.
this->dataPtr->gpuRays->Copy(this->laserBuffer);
// Apply noise before publishing the data.
this->ApplyNoise();
this->PublishLidarScan(_now);
if (this->dataPtr->pointPub.HasConnections())
{
// Set the time stamp
*this->dataPtr->pointMsg.mutable_header()->mutable_stamp() =
msgs::Convert(_now);
this->dataPtr->pointMsg.set_is_dense(true);
this->dataPtr->FillPointCloudMsg(this->laserBuffer);
{
this->AddSequence(this->dataPtr->pointMsg.mutable_header());
IGN_PROFILE("GpuLidarSensor::Update Publish point cloud");
this->dataPtr->pointPub.Publish(this->dataPtr->pointMsg);
}
}
return true;
}
/////////////////////////////////////////////////
ignition::common::ConnectionPtr GpuLidarSensor::ConnectNewLidarFrame(
std::function<void(const float *_scan, unsigned int _width,
unsigned int _height, unsigned int _channels,
const std::string &/*_format*/)> _subscriber)
{
return this->dataPtr->gpuRays->ConnectNewGpuRaysFrame(_subscriber);
}
/////////////////////////////////////////////////
ignition::rendering::GpuRaysPtr GpuLidarSensor::GpuRays() const
{
return this->dataPtr->gpuRays;
}
//////////////////////////////////////////////////
bool GpuLidarSensor::IsHorizontal() const
{
return this->dataPtr->gpuRays->IsHorizontal();
}
//////////////////////////////////////////////////
ignition::math::Angle GpuLidarSensor::HFOV() const
{
return this->dataPtr->gpuRays->HFOV();
}
//////////////////////////////////////////////////
ignition::math::Angle GpuLidarSensor::VFOV() const
{
return this->dataPtr->gpuRays->VFOV();
}
//////////////////////////////////////////////////
void GpuLidarSensorPrivate::FillPointCloudMsg(const float *_laserBuffer)
{
IGN_PROFILE("GpuLidarSensorPrivate::FillPointCloudMsg");
uint32_t width = this->pointMsg.width();
uint32_t height = this->pointMsg.height();
unsigned int channels = 3;
float angleStep =
(this->gpuRays->AngleMax() - this->gpuRays->AngleMin()).Radian() /
(this->gpuRays->RangeCount()-1);
float verticleAngleStep = (this->gpuRays->VerticalAngleMax() -
this->gpuRays->VerticalAngleMin()).Radian() /
(this->gpuRays->VerticalRangeCount()-1);
// Angles of ray currently processing, azimuth is horizontal, inclination
// is vertical
float inclination = this->gpuRays->VerticalAngleMin().Radian();
std::string *msgBuffer = this->pointMsg.mutable_data();
msgBuffer->resize(this->pointMsg.row_step() *
this->pointMsg.height());
char *msgBufferIndex = msgBuffer->data();
// Iterate over scan and populate point cloud
for (uint32_t j = 0; j < height; ++j)
{
float azimuth = this->gpuRays->AngleMin().Radian();
for (uint32_t i = 0; i < width; ++i)
{
// Index of current point, and the depth value at that point
auto index = j * width * channels + i * channels;
float depth = _laserBuffer[index];
float intensity = _laserBuffer[index + 1];
uint16_t ring = j;
int fieldIndex = 0;
// Convert spherical coordinates to Cartesian for pointcloud
// See https://en.wikipedia.org/wiki/Spherical_coordinate_system
*reinterpret_cast<float *>(msgBufferIndex +
this->pointMsg.field(fieldIndex++).offset()) =
depth * std::cos(inclination) * std::cos(azimuth);
*reinterpret_cast<float *>(msgBufferIndex +
this->pointMsg.field(fieldIndex++).offset()) =
depth * std::cos(inclination) * std::sin(azimuth);
*reinterpret_cast<float *>(msgBufferIndex +
this->pointMsg.field(fieldIndex++).offset()) =
depth * std::sin(inclination);
// Intensity
*reinterpret_cast<float *>(msgBufferIndex +
this->pointMsg.field(fieldIndex++).offset()) = intensity;
// Ring
*reinterpret_cast<uint16_t *>(msgBufferIndex +
this->pointMsg.field(fieldIndex++).offset()) = ring;
// Move the index to the next point.
msgBufferIndex += this->pointMsg.point_step();
azimuth += angleStep;
}
inclination += verticleAngleStep;
}
}
IGN_SENSORS_REGISTER_SENSOR(GpuLidarSensor)