-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathSensors.cc
616 lines (518 loc) · 20 KB
/
Sensors.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
* 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.
*
*/
#include "Sensors.hh"
#include <map>
#include <set>
#include <unordered_map>
#include <utility>
#include <vector>
#include <ignition/common/Profiler.hh>
#include <ignition/plugin/Register.hh>
#include <sdf/Sensor.hh>
#include <ignition/math/Helpers.hh>
#include <ignition/rendering/Scene.hh>
#include <ignition/sensors/CameraSensor.hh>
#include <ignition/sensors/RenderingSensor.hh>
#include <ignition/sensors/ThermalCameraSensor.hh>
#include <ignition/sensors/Manager.hh>
#include "ignition/gazebo/components/Atmosphere.hh"
#include "ignition/gazebo/components/Camera.hh"
#include "ignition/gazebo/components/DepthCamera.hh"
#include "ignition/gazebo/components/GpuLidar.hh"
#include "ignition/gazebo/components/RenderEngineServerPlugin.hh"
#include "ignition/gazebo/components/RgbdCamera.hh"
#include "ignition/gazebo/components/ThermalCamera.hh"
#include "ignition/gazebo/components/World.hh"
#include "ignition/gazebo/Events.hh"
#include "ignition/gazebo/EntityComponentManager.hh"
#include "ignition/gazebo/rendering/Events.hh"
#include "ignition/gazebo/rendering/RenderUtil.hh"
using namespace ignition;
using namespace gazebo;
using namespace systems;
// Private data class.
class ignition::gazebo::systems::SensorsPrivate
{
/// \brief Sensor manager object. This manages the lifecycle of the
/// instantiated sensors.
public: sensors::Manager sensorManager;
/// \brief used to store whether rendering objects have been created.
public: bool initialized = false;
/// \brief Main rendering interface
public: RenderUtil renderUtil;
/// \brief Unique set of sensor ids
public: std::set<sensors::SensorId> sensorIds;
/// \brief rendering scene to be managed by the scene manager and used to
/// generate sensor data
public: rendering::ScenePtr scene;
/// \brief Temperature used by thermal camera. Defaults to temperature at
/// sea level
public: double ambientTemperature = 288.15;
/// \brief Temperature gradient with respect to increasing altitude at sea
/// level in units of K/m.
public: double ambientTemperatureGradient = -0.0065;
/// \brief Keep track of cameras, in case we need to handle stereo cameras.
/// Key: Camera's parent scoped name
/// Value: Pointer to camera
// TODO(anyone) Remove element when sensor is deleted
public: std::map<std::string, sensors::CameraSensor *> cameras;
/// \brief Maps gazebo entity to its matching sensor ID
///
/// Useful for detecting when a sensor Entity has been deleted and trigger
/// the destruction of the corresponding ignition::sensors Sensor object
public: std::unordered_map<Entity, sensors::SensorId> entityToIdMap;
/// \brief Flag to indicate if worker threads are running
public: std::atomic<bool> running { false };
/// \brief Flag to signal if initialization should occur
public: bool doInit { false };
/// \brief Flag to signal if rendering update is needed
public: bool updateAvailable { false };
/// \brief Thread that rendering will occur in
public: std::thread renderThread;
/// \brief Mutex to protect rendering data
public: std::mutex renderMutex;
/// \brief Condition variable to signal rendering thread
///
/// This variable is used to block/unblock operations in the rendering
/// thread. For a more detailed explanation on the flow refer to the
/// documentation on RenderThread.
public: std::condition_variable renderCv;
/// \brief Connection to events::Stop event, used to stop thread
public: ignition::common::ConnectionPtr stopConn;
/// \brief Update time for the next rendering iteration
public: std::chrono::steady_clock::duration updateTime;
/// \brief Sensors to include in the next rendering iteration
public: std::vector<sensors::RenderingSensor *> activeSensors;
/// \brief Mutex to protect sensorMask
public: std::mutex sensorMaskMutex;
/// \brief Mask sensor updates for sensors currently being rendered
public: std::map<sensors::SensorId,
std::chrono::steady_clock::duration> sensorMask;
/// \brief Pointer to the event manager
public: EventManager *eventManager{nullptr};
/// \brief Wait for initialization to happen
private: void WaitForInit();
/// \brief Run one rendering iteration
private: void RunOnce();
/// \brief Top level function for the rendering thread
///
/// This function captures all of the behavior of the rendering thread.
/// The behavior is captured in two phases: initialization and steady state.
///
/// When the thread is first started, it waits on renderCv until the
/// prerequisites for initialization are met, and the `doInit` flag is set.
/// In order for initialization to proceed, rendering sensors must be
/// available in the EntityComponentManager.
///
/// When doInit is set, and renderCv is notified, initialization
/// is performed (creating the render context and scene). During
/// initialization, execution is blocked for the caller of PostUpdate.
/// When initialization is complete, PostUpdate will be notified via
/// renderCv and execution will continue.
///
/// Once in steady state, a rendering operation is triggered by setting
/// updateAvailable to true, and notifying via the renderCv.
/// The rendering operation is done in `RunOnce`.
///
/// The caller of PostUpdate will not be blocked if there is no
/// rendering operation currently ongoing. Rendering will occur
/// asyncronously.
//
/// The caller of PostUpdate will be blocked if there is a rendering
/// operation currently ongoing, until that completes.
private: void RenderThread();
/// \brief Launch the rendering thread
public: void Run();
/// \brief Stop the rendering thread
public: void Stop();
};
//////////////////////////////////////////////////
void SensorsPrivate::WaitForInit()
{
while (!this->initialized && this->running)
{
igndbg << "Waiting for init" << std::endl;
std::unique_lock<std::mutex> lock(this->renderMutex);
// Wait to be ready for initialization or stopped running.
// We need rendering sensors to be available to initialize.
this->renderCv.wait(lock, [this]()
{
return this->doInit || !this->running;
});
if (this->doInit)
{
// Only initialize if there are rendering sensors
igndbg << "Initializing render context" << std::endl;
this->renderUtil.Init();
this->scene = this->renderUtil.Scene();
this->initialized = true;
}
this->updateAvailable = false;
this->renderCv.notify_one();
}
igndbg << "Rendering Thread initialized" << std::endl;
}
//////////////////////////////////////////////////
void SensorsPrivate::RunOnce()
{
std::unique_lock<std::mutex> lock(this->renderMutex);
this->renderCv.wait(lock, [this]()
{
return !this->running || this->updateAvailable;
});
if (!this->running)
return;
if (!this->scene)
return;
IGN_PROFILE("SensorsPrivate::RunOnce");
{
IGN_PROFILE("Update");
this->renderUtil.Update();
}
if (!this->activeSensors.empty())
{
this->sensorMaskMutex.lock();
// Check the active sensors against masked sensors.
//
// The internal state of a rendering sensor is not updated until the
// rendering operation is complete, which can leave us in a position
// where the sensor is falsely indicating that an update is needed.
//
// To prevent this, add sensors that are currently being rendered to
// a mask. Sensors are removed from the mask when 90% of the update
// delta has passed, which will allow rendering to proceed.
for (const auto & sensor : this->activeSensors)
{
// 90% of update delta (1/UpdateRate());
auto delta = std::chrono::duration_cast< std::chrono::milliseconds>(
std::chrono::duration< double >(0.9 / sensor->UpdateRate()));
this->sensorMask[sensor->Id()] = this->updateTime + delta;
}
this->sensorMaskMutex.unlock();
{
IGN_PROFILE("PreRender");
this->eventManager->Emit<events::PreRender>();
// Update the scene graph manually to improve performance
// We only need to do this once per frame It is important to call
// sensors::RenderingSensor::SetManualSceneUpdate and set it to true
// so we don't waste cycles doing one scene graph update per sensor
this->scene->PreRender();
}
{
// publish data
IGN_PROFILE("RunOnce");
this->sensorManager.RunOnce(this->updateTime);
this->eventManager->Emit<events::PostRender>();
}
this->activeSensors.clear();
}
this->updateAvailable = false;
lock.unlock();
this->renderCv.notify_one();
}
//////////////////////////////////////////////////
void SensorsPrivate::RenderThread()
{
IGN_PROFILE_THREAD_NAME("RenderThread");
igndbg << "SensorsPrivate::RenderThread started" << std::endl;
// We have to wait for rendering sensors to be available
this->WaitForInit();
while (this->running)
{
this->RunOnce();
}
// clean up before exiting
for (const auto id : this->sensorIds)
this->sensorManager.Remove(id);
igndbg << "SensorsPrivate::RenderThread stopped" << std::endl;
}
//////////////////////////////////////////////////
void SensorsPrivate::Run()
{
igndbg << "SensorsPrivate::Run" << std::endl;
this->running = true;
this->renderThread = std::thread(&SensorsPrivate::RenderThread, this);
}
//////////////////////////////////////////////////
void SensorsPrivate::Stop()
{
igndbg << "SensorsPrivate::Stop" << std::endl;
std::unique_lock<std::mutex> lock(this->renderMutex);
this->running = false;
if (this->stopConn)
{
// Clear connection to stop additional incoming events.
this->stopConn.reset();
}
lock.unlock();
this->renderCv.notify_all();
if (this->renderThread.joinable())
{
this->renderThread.join();
}
}
//////////////////////////////////////////////////
void Sensors::RemoveSensor(const Entity &_entity)
{
auto idIter = this->dataPtr->entityToIdMap.find(_entity);
if (idIter != this->dataPtr->entityToIdMap.end())
{
// Remove from active sensors as well
// Locking mutex to make sure the vector is not being changed while
// the rendering thread is iterating over it
{
std::unique_lock<std::mutex> lock(this->dataPtr->sensorMaskMutex);
sensors::Sensor *s = this->dataPtr->sensorManager.Sensor(idIter->second);
auto rs = dynamic_cast<sensors::RenderingSensor *>(s);
auto activeSensorIt = std::find(this->dataPtr->activeSensors.begin(),
this->dataPtr->activeSensors.end(), rs);
if (activeSensorIt != this->dataPtr->activeSensors.end())
{
this->dataPtr->activeSensors.erase(activeSensorIt);
}
}
this->dataPtr->sensorIds.erase(idIter->second);
this->dataPtr->sensorManager.Remove(idIter->second);
this->dataPtr->entityToIdMap.erase(idIter);
}
}
//////////////////////////////////////////////////
Sensors::Sensors() : System(), dataPtr(std::make_unique<SensorsPrivate>())
{
}
//////////////////////////////////////////////////
Sensors::~Sensors()
{
this->dataPtr->Stop();
}
//////////////////////////////////////////////////
void Sensors::Configure(const Entity &/*_id*/,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm,
EventManager &_eventMgr)
{
igndbg << "Configuring Sensors system" << std::endl;
// Setup rendering
std::string engineName =
_sdf->Get<std::string>("render_engine", "ogre2").first;
this->dataPtr->renderUtil.SetEngineName(engineName);
this->dataPtr->renderUtil.SetEnableSensors(true,
std::bind(&Sensors::CreateSensor, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
this->dataPtr->renderUtil.SetRemoveSensorCb(
std::bind(&Sensors::RemoveSensor, this, std::placeholders::_1));
// parse sensor-specific data
auto worldEntity = _ecm.EntityByComponents(components::World());
if (kNullEntity != worldEntity)
{
// temperature used by thermal camera
auto atmosphere = _ecm.Component<components::Atmosphere>(worldEntity);
if (atmosphere)
{
auto atmosphereSdf = atmosphere->Data();
this->dataPtr->ambientTemperature = atmosphereSdf.Temperature().Kelvin();
this->dataPtr->ambientTemperatureGradient =
atmosphereSdf.TemperatureGradient();
}
// Set render engine if specified from command line
auto renderEngineServerComp =
_ecm.Component<components::RenderEngineServerPlugin>(worldEntity);
if (renderEngineServerComp && !renderEngineServerComp->Data().empty())
{
this->dataPtr->renderUtil.SetEngineName(renderEngineServerComp->Data());
}
}
this->dataPtr->eventManager = &_eventMgr;
this->dataPtr->stopConn = _eventMgr.Connect<events::Stop>(
std::bind(&SensorsPrivate::Stop, this->dataPtr.get()));
// Kick off worker thread
this->dataPtr->Run();
}
//////////////////////////////////////////////////
void Sensors::Update(const UpdateInfo &_info,
EntityComponentManager &_ecm)
{
IGN_PROFILE("Sensors::Update");
if (this->dataPtr->running && this->dataPtr->initialized)
{
this->dataPtr->renderUtil.UpdateECM(_info, _ecm);
}
}
//////////////////////////////////////////////////
void Sensors::PostUpdate(const UpdateInfo &_info,
const EntityComponentManager &_ecm)
{
IGN_PROFILE("Sensors::PostUpdate");
// \TODO(anyone) Support rewind
if (_info.dt < std::chrono::steady_clock::duration::zero())
{
ignwarn << "Detected jump back in time ["
<< std::chrono::duration_cast<std::chrono::seconds>(_info.dt).count()
<< "s]. System may not work properly." << std::endl;
}
if (!this->dataPtr->initialized &&
(_ecm.HasComponentType(components::Camera::typeId) ||
_ecm.HasComponentType(components::DepthCamera::typeId) ||
_ecm.HasComponentType(components::GpuLidar::typeId) ||
_ecm.HasComponentType(components::RgbdCamera::typeId) ||
_ecm.HasComponentType(components::ThermalCamera::typeId)))
{
igndbg << "Initialization needed" << std::endl;
std::unique_lock<std::mutex> lock(this->dataPtr->renderMutex);
this->dataPtr->doInit = true;
this->dataPtr->renderCv.notify_one();
}
if (this->dataPtr->running && this->dataPtr->initialized)
{
this->dataPtr->renderUtil.UpdateFromECM(_info, _ecm);
auto time = math::durationToSecNsec(_info.simTime);
auto t = math::secNsecToDuration(time.first, time.second);
std::vector<sensors::RenderingSensor *> activeSensors;
this->dataPtr->sensorMaskMutex.lock();
for (auto id : this->dataPtr->sensorIds)
{
sensors::Sensor *s = this->dataPtr->sensorManager.Sensor(id);
auto rs = dynamic_cast<sensors::RenderingSensor *>(s);
auto it = this->dataPtr->sensorMask.find(id);
if (it != this->dataPtr->sensorMask.end())
{
if (it->second <= t)
{
this->dataPtr->sensorMask.erase(it);
}
else
{
continue;
}
}
if (rs && rs->NextDataUpdateTime() <= t)
{
activeSensors.push_back(rs);
}
}
this->dataPtr->sensorMaskMutex.unlock();
if (!activeSensors.empty() ||
this->dataPtr->renderUtil.PendingSensors() > 0)
{
std::unique_lock<std::mutex> lock(this->dataPtr->renderMutex);
this->dataPtr->renderCv.wait(lock, [this] {
return !this->dataPtr->running || !this->dataPtr->updateAvailable; });
if (!this->dataPtr->running)
{
return;
}
this->dataPtr->activeSensors = std::move(activeSensors);
this->dataPtr->updateTime = t;
this->dataPtr->updateAvailable = true;
this->dataPtr->renderCv.notify_one();
}
}
}
//////////////////////////////////////////////////
std::string Sensors::CreateSensor(const Entity &_entity,
const sdf::Sensor &_sdf, const std::string &_parentName)
{
if (_sdf.Type() == sdf::SensorType::NONE)
{
ignerr << "Unable to create sensor. SDF sensor type is NONE." << std::endl;
return std::string();
}
// Create within ign-sensors
auto sensorId = this->dataPtr->sensorManager.CreateSensor(_sdf);
auto sensor = this->dataPtr->sensorManager.Sensor(sensorId);
// Add to sensorID -> entity map
this->dataPtr->entityToIdMap.insert({_entity, sensorId});
if (nullptr == sensor || sensors::NO_SENSOR == sensor->Id())
{
ignerr << "Failed to create sensor [" << _sdf.Name()
<< "]" << std::endl;
return std::string();
}
this->dataPtr->sensorIds.insert(sensorId);
// Set the scene so it can create the rendering sensor
auto renderingSensor =
dynamic_cast<sensors::RenderingSensor *>(sensor);
renderingSensor->SetScene(this->dataPtr->scene);
renderingSensor->SetParent(_parentName);
renderingSensor->SetManualSceneUpdate(true);
// Special case for stereo cameras
auto cameraSensor = dynamic_cast<sensors::CameraSensor *>(sensor);
if (nullptr != cameraSensor)
{
// Parent
auto parent = cameraSensor->Parent();
// If parent has other camera children, set the baseline.
// For stereo pairs, the baseline for the left camera is zero, and for the
// right camera it's the distance between them.
// For more than 2 cameras, the first camera's baseline is zero and the
// others have the distance between them.
if (this->dataPtr->cameras.find(parent) !=
this->dataPtr->cameras.end())
{
// TODO(anyone) This is safe because we're not removing sensors
// First camera added to the parent link
auto leftCamera = this->dataPtr->cameras[parent];
auto rightCamera = cameraSensor;
// If cameras have right / left topic, use that to decide which is which
if (leftCamera->Topic().find("right") != std::string::npos &&
rightCamera->Topic().find("left") != std::string::npos)
{
std::swap(rightCamera, leftCamera);
}
// Camera sensor's Y axis is orthogonal to the optical axis
auto baseline = abs(rightCamera->Pose().Pos().Y() -
leftCamera->Pose().Pos().Y());
rightCamera->SetBaseline(baseline);
}
else
{
this->dataPtr->cameras[parent] = cameraSensor;
}
}
// Sensor-specific settings
auto thermalSensor = dynamic_cast<sensors::ThermalCameraSensor *>(sensor);
if (nullptr != thermalSensor)
{
thermalSensor->SetAmbientTemperature(this->dataPtr->ambientTemperature);
// temperature gradient is in kelvin per meter - typically change in
// temperature over change in altitude. However the implementation of
// thermal sensor in ign-sensors varies temperature for all objects in its
// view. So we will do an approximation based on camera view's vertical
// distance.
auto camSdf = _sdf.CameraSensor();
double farClip = camSdf->FarClip();
double angle = camSdf->HorizontalFov().Radian();
double aspect = camSdf->ImageWidth() / camSdf->ImageHeight();
double vfov = 2.0 * atan(tan(angle / 2.0) / aspect);
double height = tan(vfov / 2.0) * farClip * 2.0;
double tempRange =
std::fabs(this->dataPtr->ambientTemperatureGradient * height);
thermalSensor->SetAmbientTemperatureRange(tempRange);
ignmsg << "Setting ambient temperature to "
<< this->dataPtr->ambientTemperature << " Kelvin and gradient to "
<< this->dataPtr->ambientTemperatureGradient << " K/m. "
<< "The resulting temperature range is: " << tempRange
<< " Kelvin." << std::endl;
}
return sensor->Name();
}
IGNITION_ADD_PLUGIN(Sensors, System,
Sensors::ISystemConfigure,
Sensors::ISystemUpdate,
Sensors::ISystemPostUpdate
)
IGNITION_ADD_PLUGIN_ALIAS(Sensors, "ignition::gazebo::systems::Sensors")