CARLA
Loading...
Searching...
No Matches
ServerSideSensor.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 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
14constexpr size_t GBufferTextureCount = 13;
15
16namespace carla {
17namespace 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 Send() ", GetDisplayId());
97 if (GetActorDescription().description.id != "sensor.other.v2x_custom")
98 {
99 log_warning("Send methods are not supported on non-V2X sensors (sensor.other.v2x_custom).");
100 return;
101 }
102 GetEpisode().Lock()->Send(*this, data);
103 }
104
106 log_debug("calling sensor Destroy() ", GetDisplayId());
107 if (IsListening()) {
108 for (uint32_t i = 1; i != GBufferTextureCount + 1; ++i) {
109 if (listening_mask.test(i)) {
110 StopGBuffer(i - 1);
111 }
112 }
113 Stop();
114 }
115 return Actor::Destroy();
116 }
117
118} // namespace client
119} // namespace carla
#define RELEASE_ASSERT(pred)
Definition Debug.h:84
constexpr size_t GBufferTextureCount
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
std::function< void(SharedPtr< sensor::SensorData >)> CallbackFunctionType
bool IsEnabledForROS()
Return if the sensor is publishing for ROS2.
void Send(const rpc::CustomV2XBytes &data)
Send a binary message through this sensor.
void EnableForROS()
Enable this sensor for ROS2 publishing.
void ListenToGBuffer(uint32_t GBufferId, CallbackFunctionType callback)
Listen fr.
void Stop() override
Stop listening for new measurements.
void DisableForROS()
Disable this sensor for ROS2 publishing.
bool IsListening() const override
Return whether this Sensor instance is currently listening to the associated sensor in the simulator.
void Listen(CallbackFunctionType callback) override
Register a callback to be executed each time a new measurement is received.
void StopGBuffer(uint32_t GBufferId)
Stop listening for a specific gbuffer stream.
const rpc::Actor & GetActorDescription() const
const std::string & GetDisplayId() const
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
Fixed-size binary payload carried by the custom V2X sensor (sensor.other.v2x_custom).
This file contains definitions of common data structures used in traffic manager.
static void log_error(Args &&... args)
Definition Logging.h:110
static void log_warning(Args &&... args)
Definition Logging.h:96
static void log_debug(Args &&... args)
Definition Logging.h:68