Skip to content

Commit 818b388

Browse files
nkoenigNate Koenig
andauthored
Apply noise to lidar point cloud (#86)
* Apply noise to lidar point cloud Signed-off-by: Nate Koenig <nate@openrobotics.org> * Use ray count instead of range count Signed-off-by: Nate Koenig <nate@openrobotics.org> * Fix range to ray Signed-off-by: Nate Koenig <nate@openrobotics.org> Co-authored-by: Nate Koenig <nate@openrobotics.org>
1 parent f5a57f1 commit 818b388

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

include/ignition/sensors/Lidar.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ namespace ignition
5858
/// \return true if the update was successfull
5959
public: virtual bool Update(const common::Time &_now) override;
6060

61+
/// \brief Apply noise to the laser buffer, if noise has been
62+
/// configured. This should be called before PublishLidarScan if you
63+
/// want the scan data to contain noise.
64+
public: void ApplyNoise();
65+
6166
/// \brief Publish LaserScan message
6267
/// \param[in] _now The current time
6368
/// \return true if the update was successfull

src/GpuLidarSensor.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ using namespace ignition::sensors;
2929
class ignition::sensors::GpuLidarSensorPrivate
3030
{
3131
/// \brief Fill the point cloud packed message
32-
public: void FillPointCloudMsg();
32+
/// \param[in] _laserBuffer Lidar data buffer.
33+
public: void FillPointCloudMsg(const float *_laserBuffer);
3334

3435
/// \brief Rendering camera
3536
public: ignition::rendering::GpuRaysPtr gpuRays;
@@ -232,6 +233,9 @@ bool GpuLidarSensor::Update(const ignition::common::Time &_now)
232233
/// \todo(anyone) It would be nice to remove this copy.
233234
this->dataPtr->gpuRays->Copy(this->laserBuffer);
234235

236+
// Apply noise before publishing the data.
237+
this->ApplyNoise();
238+
235239
this->PublishLidarScan(_now);
236240

237241
if (this->dataPtr->pointPub.HasConnections())
@@ -244,7 +248,7 @@ bool GpuLidarSensor::Update(const ignition::common::Time &_now)
244248

245249
this->dataPtr->pointMsg.set_is_dense(true);
246250

247-
this->dataPtr->FillPointCloudMsg();
251+
this->dataPtr->FillPointCloudMsg(this->laserBuffer);
248252

249253
{
250254
this->AddSequence(this->dataPtr->pointMsg.mutable_header());
@@ -289,7 +293,7 @@ ignition::math::Angle GpuLidarSensor::VFOV() const
289293
}
290294

291295
//////////////////////////////////////////////////
292-
void GpuLidarSensorPrivate::FillPointCloudMsg()
296+
void GpuLidarSensorPrivate::FillPointCloudMsg(const float *_laserBuffer)
293297
{
294298
IGN_PROFILE("GpuLidarSensorPrivate::FillPointCloudMsg");
295299
uint32_t width = this->pointMsg.width();
@@ -322,8 +326,8 @@ void GpuLidarSensorPrivate::FillPointCloudMsg()
322326
{
323327
// Index of current point, and the depth value at that point
324328
auto index = j * width * channels + i * channels;
325-
float depth = this->gpuRays->Data()[index];
326-
float intensity = this->gpuRays->Data()[index + 1];
329+
float depth = _laserBuffer[index];
330+
float intensity = _laserBuffer[index + 1];
327331
uint16_t ring = j;
328332

329333
int fieldIndex = 0;

src/Lidar.cc

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,26 @@ bool Lidar::Update(const ignition::common::Time &/*_now*/)
192192
return false;
193193
}
194194

195+
//////////////////////////////////////////////////
196+
void Lidar::ApplyNoise()
197+
{
198+
if (this->dataPtr->noises.find(LIDAR_NOISE) != this->dataPtr->noises.end())
199+
{
200+
for (unsigned int j = 0; j < this->VerticalRayCount(); ++j)
201+
{
202+
for (unsigned int i = 0; i < this->RayCount(); ++i)
203+
{
204+
int index = j * this->RayCount() + i;
205+
double range = this->laserBuffer[index*3];
206+
range = this->dataPtr->noises[LIDAR_NOISE]->Apply(range);
207+
range = ignition::math::clamp(range,
208+
this->RangeMin(), this->RangeMax());
209+
this->laserBuffer[index*3] = range;
210+
}
211+
}
212+
}
213+
}
214+
195215
//////////////////////////////////////////////////
196216
bool Lidar::PublishLidarScan(const ignition::common::Time &_now)
197217
{
@@ -236,14 +256,6 @@ bool Lidar::PublishLidarScan(const ignition::common::Time &_now)
236256
int index = j * this->RangeCount() + i;
237257
double range = this->laserBuffer[index*3];
238258

239-
if (this->dataPtr->noises.find(LIDAR_NOISE) !=
240-
this->dataPtr->noises.end())
241-
{
242-
range = this->dataPtr->noises[LIDAR_NOISE]->Apply(range);
243-
range = ignition::math::clamp(range,
244-
this->RangeMin(), this->RangeMax());
245-
}
246-
247259
range = ignition::math::isnan(range) ? this->RangeMax() : range;
248260
this->dataPtr->laserMsg.set_ranges(index, range);
249261
this->dataPtr->laserMsg.set_intensities(index,

0 commit comments

Comments
 (0)