CARLA
ServerSideSensor.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
8 
9 #include "carla/Logging.h"
11 
12 #include <exception>
13 
14 constexpr size_t GBufferTextureCount = 13;
15 
16 namespace carla {
17 namespace client {
18 
20  if (IsAlive() && IsListening()) {
22  "sensor object went out of the scope but the sensor is still alive",
23  "in the simulation:",
24  GetDisplayId());
25  }
26  if (IsListening() && GetEpisode().IsValid()) {
27  try {
28  for (uint32_t i = 1; i != GBufferTextureCount + 1; ++i) {
29  if (listening_mask.test(i))
30  StopGBuffer(i - 1);
31  }
32  Stop();
33  } catch (const std::exception &e) {
34  log_error("exception trying to stop sensor:", GetDisplayId(), ':', e.what());
35  }
36  }
37  }
38 
40  log_debug("calling sensor Listen() ", GetDisplayId());
41  log_debug(GetDisplayId(), ": subscribing to stream");
42  GetEpisode().Lock()->SubscribeToSensor(*this, std::move(callback));
43  listening_mask.set(0);
44  }
45 
47  log_debug("calling sensor Stop() ", GetDisplayId());
48  if (!IsListening()) {
50  "attempting to unsubscribe from stream but sensor wasn't listening:",
51  GetDisplayId());
52  return;
53  }
54  GetEpisode().Lock()->UnSubscribeFromSensor(*this);
55  listening_mask.reset(0);
56  }
57 
58  void ServerSideSensor::ListenToGBuffer(uint32_t GBufferId, CallbackFunctionType callback) {
59  log_debug(GetDisplayId(), ": subscribing to gbuffer stream");
61  if (GetActorDescription().description.id != "sensor.camera.rgb")
62  {
63  log_warning("GBuffer methods are not supported on non-RGB sensors (sensor.camera.rgb).");
64  return;
65  }
66  GetEpisode().Lock()->SubscribeToGBuffer(*this, GBufferId, std::move(callback));
67  listening_mask.set(0);
68  listening_mask.set(GBufferId + 1);
69  }
70 
71  void ServerSideSensor::StopGBuffer(uint32_t GBufferId) {
72  log_debug(GetDisplayId(), ": unsubscribing from gbuffer stream");
74  if (GetActorDescription().description.id != "sensor.camera.rgb")
75  {
76  log_warning("GBuffer methods are not supported on non-RGB sensors (sensor.camera.rgb).");
77  return;
78  }
79  GetEpisode().Lock()->UnSubscribeFromGBuffer(*this, GBufferId);
80  listening_mask.reset(GBufferId + 1);
81  }
82 
84  GetEpisode().Lock()->EnableForROS(*this);
85  }
86 
88  GetEpisode().Lock()->DisableForROS(*this);
89  }
90 
92  return GetEpisode().Lock()->IsEnabledForROS(*this);
93  }
94 
96  log_debug("calling sensor Destroy() ", GetDisplayId());
97  if (IsListening()) {
98  for (uint32_t i = 1; i != GBufferTextureCount + 1; ++i) {
99  if (listening_mask.test(i)) {
100  StopGBuffer(i - 1);
101  }
102  }
103  Stop();
104  }
105  return Actor::Destroy();
106  }
107 
108 } // namespace client
109 } // namespace carla
void Listen(CallbackFunctionType callback) override
Register a callback to be executed each time a new measurement is received.
static void log_error(Args &&... args)
Definition: Logging.h:110
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
void EnableForROS()
Enable this sensor for ROS2 publishing.
void ListenToGBuffer(uint32_t GBufferId, CallbackFunctionType callback)
Listen fr.
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
bool IsListening() const override
Return whether this Sensor instance is currently listening to the associated sensor in the simulator...
static void log_debug(Args &&...)
Definition: Logging.h:75
const std::string & GetDisplayId() const
void DisableForROS()
Disable this sensor for ROS2 publishing.
#define RELEASE_ASSERT(pred)
Definition: Debug.h:84
virtual bool Destroy()
Tell the simulator to destroy this Actor, and return whether the actor was successfully destroyed...
Definition: Actor.cpp:103
static void log_warning(Args &&... args)
Definition: Logging.h:96
bool IsEnabledForROS()
Return if the sensor is publishing for ROS2.
bool Destroy() override
Tell the simulator to destroy this Actor, and return whether the actor was successfully destroyed...
std::function< void(SharedPtr< sensor::SensorData >)> CallbackFunctionType
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
void StopGBuffer(uint32_t GBufferId)
Stop listening for a specific gbuffer stream.
bool IsAlive() const
Definition: client/Actor.h:113
constexpr size_t GBufferTextureCount
void Stop() override
Stop listening for new measurements.
const rpc::Actor & GetActorDescription() const