-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathSensors.cc
935 lines (799 loc) · 30.1 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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
/*
* 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 <unordered_set>
#include <utility>
#include <vector>
#include <gz/common/Profiler.hh>
#include <gz/plugin/Register.hh>
#include <sdf/Sensor.hh>
#include <gz/math/Helpers.hh>
#include <gz/rendering/Scene.hh>
#include <gz/sensors/BoundingBoxCameraSensor.hh>
#include <gz/sensors/CameraSensor.hh>
#include <gz/sensors/DepthCameraSensor.hh>
#include <gz/sensors/GpuLidarSensor.hh>
#include <gz/sensors/RenderingSensor.hh>
#include <gz/sensors/RgbdCameraSensor.hh>
#include <gz/sensors/ThermalCameraSensor.hh>
#include <gz/sensors/SegmentationCameraSensor.hh>
#include <gz/sensors/WideAngleCameraSensor.hh>
#include <gz/sensors/Manager.hh>
#include "gz/sim/components/Atmosphere.hh"
#include "gz/sim/components/BatterySoC.hh"
#include "gz/sim/components/BoundingBoxCamera.hh"
#include "gz/sim/components/Camera.hh"
#include "gz/sim/components/DepthCamera.hh"
#include "gz/sim/components/GpuLidar.hh"
#include "gz/sim/components/ParentEntity.hh"
#include "gz/sim/components/RenderEngineServerHeadless.hh"
#include "gz/sim/components/RenderEngineServerPlugin.hh"
#include "gz/sim/components/RgbdCamera.hh"
#include "gz/sim/components/SegmentationCamera.hh"
#include "gz/sim/components/ThermalCamera.hh"
#include "gz/sim/components/WideAngleCamera.hh"
#include "gz/sim/components/World.hh"
#include "gz/sim/Events.hh"
#include "gz/sim/EntityComponentManager.hh"
#include "gz/sim/rendering/Events.hh"
#include "gz/sim/rendering/RenderUtil.hh"
using namespace gz;
using namespace sim;
using namespace systems;
// Private data class.
class gz::sim::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
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 gz::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 Flag to signal if a rendering update must be done
public: std::atomic<bool> forceUpdate { 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: common::ConnectionPtr stopConn;
/// \brief Connection to events::ForceRender event, used to force rendering
public: common::ConnectionPtr forceRenderConn;
/// \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();
/// \brief Force rendering thread to render
public: void ForceRender();
/// \brief Update battery state of sensors in model
/// \param[in] _ecm Entity component manager
public: void UpdateBatteryState(const EntityComponentManager &_ecm);
/// \brief Use to optionally set the background color.
public: std::optional<math::Color> backgroundColor;
/// \brief Use to optionally set the ambient light.
public: std::optional<math::Color> ambientLight;
/// \brief A map between model entity ids in the ECM to its battery state
/// True means has charge, false means drained
public: std::unordered_map<Entity, bool> modelBatteryState;
/// \brief A map between model entity ids in the ECM to whether its battery
/// state has changed.
/// True means has charge, false means drained
public: std::unordered_map<Entity, bool> modelBatteryStateChanged;
/// \brief A map of sensor ids to their active state
public: std::map<sensors::SensorId, bool> sensorStateChanged;
/// \brief Disable sensors if parent model's battery is drained
/// Affects sensors that are in nested models
public: bool disableOnDrainedBattery = false;
/// \brief Mutex to protect access to sensorStateChanged
public: std::mutex sensorStateMutex;
};
//////////////////////////////////////////////////
void SensorsPrivate::WaitForInit()
{
while (!this->initialized && this->running)
{
gzdbg << "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
gzdbg << "Initializing render context" << std::endl;
if (this->backgroundColor)
this->renderUtil.SetBackgroundColor(*this->backgroundColor);
if (this->ambientLight)
this->renderUtil.SetAmbientLight(*this->ambientLight);
#ifndef __APPLE__
this->renderUtil.Init();
#else
// On macOS the render engine must be initialised on the main thread.
// See Sensors::Update.
#endif
this->scene = this->renderUtil.Scene();
this->scene->SetCameraPassCountPerGpuFlush(6u);
this->initialized = true;
}
this->updateAvailable = false;
this->renderCv.notify_one();
}
gzdbg << "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;
GZ_PROFILE("SensorsPrivate::RunOnce");
{
GZ_PROFILE("Update");
this->renderUtil.Update();
}
if (!this->activeSensors.empty() || this->forceUpdate)
{
// disable sensors that are out of battery or re-enable sensors that are
// being charged
if (this->disableOnDrainedBattery)
{
std::unique_lock<std::mutex> lock2(this->sensorStateMutex);
for (const auto &sensorIt : this->sensorStateChanged)
{
sensors::Sensor *s =
this->sensorManager.Sensor(sensorIt.first);
if (s)
{
s->SetActive(sensorIt.second);
}
}
this->sensorStateChanged.clear();
}
// 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.
{
std::unique_lock<std::mutex> lockMask(this->sensorMaskMutex);
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;
}
}
{
GZ_PROFILE("PreRender");
this->eventManager->Emit<events::PreRender>();
this->scene->SetTime(this->updateTime);
// 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();
}
// disable sensors that have no subscribers to prevent doing unnecessary
// work
std::unordered_set<sensors::RenderingSensor *> tmpDisabledSensors;
this->sensorMaskMutex.lock();
for (auto id : this->sensorIds)
{
sensors::Sensor *s = this->sensorManager.Sensor(id);
auto rs = dynamic_cast<sensors::RenderingSensor *>(s);
if (rs->IsActive() && !rs->HasConnections())
{
rs->SetActive(false);
tmpDisabledSensors.insert(rs);
}
}
this->sensorMaskMutex.unlock();
{
// publish data
GZ_PROFILE("RunOnce");
this->sensorManager.RunOnce(this->updateTime);
this->eventManager->Emit<events::Render>();
}
// re-enble sensors
for (auto &rs : tmpDisabledSensors)
{
rs->SetActive(true);
}
{
GZ_PROFILE("PostRender");
// 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->PostRender();
this->eventManager->Emit<events::PostRender>();
}
this->activeSensors.clear();
}
this->updateAvailable = false;
this->forceUpdate = false;
lock.unlock();
this->renderCv.notify_one();
}
//////////////////////////////////////////////////
void SensorsPrivate::RenderThread()
{
GZ_PROFILE_THREAD_NAME("RenderThread");
gzdbg << "SensorsPrivate::RenderThread started" << std::endl;
// We have to wait for rendering sensors to be available
this->WaitForInit();
while (this->running)
{
this->RunOnce();
}
this->eventManager->Emit<events::RenderTeardown>();
// clean up before exiting
for (const auto id : this->sensorIds)
this->sensorManager.Remove(id);
this->renderUtil.Destroy();
gzdbg << "SensorsPrivate::RenderThread stopped" << std::endl;
}
//////////////////////////////////////////////////
void SensorsPrivate::Run()
{
gzdbg << "SensorsPrivate::Run" << std::endl;
this->running = true;
this->renderThread = std::thread(&SensorsPrivate::RenderThread, this);
}
//////////////////////////////////////////////////
void SensorsPrivate::Stop()
{
gzdbg << "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 SensorsPrivate::ForceRender()
{
this->forceUpdate = true;
}
//////////////////////////////////////////////////
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);
}
}
// update cameras list
for (auto &it : this->dataPtr->cameras)
{
if (it.second->Id() == idIter->second)
{
this->dataPtr->cameras.erase(it.first);
break;
}
}
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)
{
gzdbg << "Configuring Sensors system" << std::endl;
// Setup rendering
std::string engineName =
_sdf->Get<std::string>("render_engine", "ogre2").first;
// get whether or not to disable sensor when model battery is drained
this->dataPtr->disableOnDrainedBattery =
_sdf->Get<bool>("disable_on_drained_battery",
this->dataPtr-> disableOnDrainedBattery).first;
// Get the background color, if specified.
if (_sdf->HasElement("background_color"))
this->dataPtr->backgroundColor = _sdf->Get<math::Color>("background_color");
// Get the ambient light, if specified.
if (_sdf->HasElement("ambient_light"))
this->dataPtr->ambientLight = _sdf->Get<math::Color>("ambient_light");
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));
this->dataPtr->renderUtil.SetEventManager(&_eventMgr);
// 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());
}
// Set headless mode if specified from command line
auto renderEngineServerHeadlessComp =
_ecm.Component<components::RenderEngineServerHeadless>(worldEntity);
if (renderEngineServerHeadlessComp)
{
this->dataPtr->renderUtil.SetHeadlessRendering(
renderEngineServerHeadlessComp->Data());
}
}
this->dataPtr->eventManager = &_eventMgr;
this->dataPtr->stopConn = this->dataPtr->eventManager->Connect<events::Stop>(
std::bind(&SensorsPrivate::Stop, this->dataPtr.get()));
this->dataPtr->forceRenderConn = _eventMgr.Connect<events::ForceRender>(
std::bind(&SensorsPrivate::ForceRender, this->dataPtr.get()));
// Kick off worker thread
this->dataPtr->Run();
}
//////////////////////////////////////////////////
void Sensors::Reset(const UpdateInfo &_info, EntityComponentManager &)
{
GZ_PROFILE("Sensors::Reset");
if (this->dataPtr->running && this->dataPtr->initialized)
{
gzdbg << "Resetting Sensors\n";
{
std::unique_lock<std::mutex> lock(this->dataPtr->sensorMaskMutex);
this->dataPtr->sensorMask.clear();
}
for (auto id : this->dataPtr->sensorIds)
{
sensors::Sensor *s = this->dataPtr->sensorManager.Sensor(id);
if (nullptr == s)
{
gzwarn << "Sensor removed before reset: " << id << "\n";
continue;
}
s->SetNextDataUpdateTime(_info.simTime);
}
}
}
//////////////////////////////////////////////////
void Sensors::Update(const UpdateInfo &_info,
EntityComponentManager &_ecm)
{
GZ_PROFILE("Sensors::Update");
std::unique_lock<std::mutex> lock(this->dataPtr->renderMutex);
#ifdef __APPLE__
// On macOS the render engine must be initialised on the main thread.
if (!this->dataPtr->initialized &&
(_ecm.HasComponentType(components::BoundingBoxCamera::typeId) ||
_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) ||
_ecm.HasComponentType(components::SegmentationCamera::typeId) ||
_ecm.HasComponentType(components::WideAngleCamera::typeId)))
{
igndbg << "Initialization needed" << std::endl;
this->dataPtr->renderUtil.Init();
}
#endif
if (this->dataPtr->running && this->dataPtr->initialized)
{
this->dataPtr->renderUtil.UpdateECM(_info, _ecm);
}
}
//////////////////////////////////////////////////
void Sensors::PostUpdate(const UpdateInfo &_info,
const EntityComponentManager &_ecm)
{
GZ_PROFILE("Sensors::PostUpdate");
{
std::unique_lock<std::mutex> lock(this->dataPtr->renderMutex);
if (!this->dataPtr->initialized &&
(this->dataPtr->forceUpdate ||
_ecm.HasComponentType(components::BoundingBoxCamera::typeId) ||
_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) ||
_ecm.HasComponentType(components::SegmentationCamera::typeId) ||
_ecm.HasComponentType(components::WideAngleCamera::typeId)))
{
gzdbg << "Initialization needed" << std::endl;
this->dataPtr->doInit = true;
this->dataPtr->renderCv.notify_one();
}
}
if (this->dataPtr->running && this->dataPtr->initialized)
{
this->dataPtr->renderUtil.UpdateFromECM(_info, _ecm);
std::vector<sensors::RenderingSensor *> activeSensors;
{
std::unique_lock<std::mutex> lk(this->dataPtr->sensorMaskMutex);
for (auto id : this->dataPtr->sensorIds)
{
sensors::Sensor *s = this->dataPtr->sensorManager.Sensor(id);
if (nullptr == s)
{
continue;
}
auto rs = dynamic_cast<sensors::RenderingSensor *>(s);
if (nullptr == rs)
{
continue;
}
auto it = this->dataPtr->sensorMask.find(id);
if (it != this->dataPtr->sensorMask.end())
{
if (it->second <= _info.simTime)
{
this->dataPtr->sensorMask.erase(it);
}
else
{
continue;
}
}
if (rs && rs->NextDataUpdateTime() <= _info.simTime)
{
activeSensors.push_back(rs);
}
}
}
if (!activeSensors.empty() ||
this->dataPtr->renderUtil.PendingSensors() > 0 ||
this->dataPtr->forceUpdate)
{
if (this->dataPtr->disableOnDrainedBattery)
this->dataPtr->UpdateBatteryState(_ecm);
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 = _info.simTime;
this->dataPtr->updateAvailable = true;
this->dataPtr->renderCv.notify_one();
}
}
}
//////////////////////////////////////////////////
void SensorsPrivate::UpdateBatteryState(const EntityComponentManager &_ecm)
{
// Battery state
_ecm.Each<components::BatterySoC>(
[&](const Entity & _entity, const components::BatterySoC *_bat)
{
bool hasCharge = _bat->Data() > 0;
auto stateIt =
this->modelBatteryState.find(_ecm.ParentEntity(_entity));
if (stateIt != this->modelBatteryState.end())
{
// detect a change in battery charge state
if (stateIt->second != hasCharge)
{
this->modelBatteryStateChanged[_ecm.ParentEntity(_entity)] =
hasCharge;
}
}
this->modelBatteryState[_ecm.ParentEntity(_entity)] = hasCharge;
return true;
});
// disable sensor if parent model is out of battery or re-enable sensor
// if battery is charging
for (const auto & modelIt : this->modelBatteryStateChanged)
{
// check if sensor is part of this model
for (const auto & sensorIt : this->entityToIdMap)
{
// parent link
auto parentLinkComp =
_ecm.Component<components::ParentEntity>(sensorIt.first);
if (!parentLinkComp)
continue;
// parent model
auto parentModelComp = _ecm.Component<components::ParentEntity>(
parentLinkComp->Data());
if (!parentModelComp)
continue;
// keep going up the tree in case sensor is in a nested model
while (parentModelComp)
{
auto parentEnt = parentModelComp->Data();
if (parentEnt == modelIt.first)
{
std::unique_lock<std::mutex> lock(this->sensorStateMutex);
// sensor is part of model - update its active state
this->sensorStateChanged[sensorIt.second] = modelIt.second;
break;
}
parentModelComp = _ecm.Component<components::ParentEntity>(parentEnt);
}
}
}
this->modelBatteryStateChanged.clear();
}
//////////////////////////////////////////////////
std::string Sensors::CreateSensor(const Entity &_entity,
const sdf::Sensor &_sdf, const std::string &_parentName)
{
if (_sdf.Type() == sdf::SensorType::NONE)
{
gzerr << "Unable to create sensor. SDF sensor type is NONE." << std::endl;
return std::string();
}
// Create within gz-sensors
sensors::Sensor *sensor{nullptr};
if (_sdf.Type() == sdf::SensorType::CAMERA)
{
sensor = this->dataPtr->sensorManager.CreateSensor<
sensors::CameraSensor>(_sdf);
}
else if (_sdf.Type() == sdf::SensorType::DEPTH_CAMERA)
{
sensor = this->dataPtr->sensorManager.CreateSensor<
sensors::DepthCameraSensor>(_sdf);
}
else if (_sdf.Type() == sdf::SensorType::GPU_LIDAR)
{
sensor = this->dataPtr->sensorManager.CreateSensor<
sensors::GpuLidarSensor>(_sdf);
}
else if (_sdf.Type() == sdf::SensorType::RGBD_CAMERA)
{
sensor = this->dataPtr->sensorManager.CreateSensor<
sensors::RgbdCameraSensor>(_sdf);
}
else if (_sdf.Type() == sdf::SensorType::THERMAL_CAMERA)
{
sensor = this->dataPtr->sensorManager.CreateSensor<
sensors::ThermalCameraSensor>(_sdf);
}
else if (_sdf.Type() == sdf::SensorType::BOUNDINGBOX_CAMERA)
{
sensor = this->dataPtr->sensorManager.CreateSensor<
sensors::BoundingBoxCameraSensor>(_sdf);
}
else if (_sdf.Type() == sdf::SensorType::SEGMENTATION_CAMERA)
{
sensor = this->dataPtr->sensorManager.CreateSensor<
sensors::SegmentationCameraSensor>(_sdf);
}
else if (_sdf.Type() == sdf::SensorType::WIDE_ANGLE_CAMERA)
{
sensor = this->dataPtr->sensorManager.CreateSensor<
sensors::WideAngleCameraSensor>(_sdf);
}
if (nullptr == sensor)
{
gzerr << "Failed to create sensor [" << _sdf.Name()
<< "]." << std::endl;
return std::string();
}
// Store sensor ID
auto sensorId = sensor->Id();
this->dataPtr->entityToIdMap.insert({_entity, sensorId});
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 gz-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);
gzmsg << "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();
}
GZ_ADD_PLUGIN(Sensors, System,
Sensors::ISystemConfigure,
Sensors::ISystemReset,
Sensors::ISystemUpdate,
Sensors::ISystemPostUpdate
)
GZ_ADD_PLUGIN_ALIAS(Sensors, "gz::sim::systems::Sensors")
// TODO(CH3): Deprecated, remove on version 8
GZ_ADD_PLUGIN_ALIAS(Sensors, "ignition::gazebo::systems::Sensors")