CARLA
 
载入中...
搜索中...
未找到
ServerSideSensor.cpp
浏览该文件的文档.
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
7#include "carla/client/ServerSideSensor.h" // 导入ServerSideSensor类的头文件
8
9#include "carla/Logging.h" // 导入日志记录相关的头文件
10#include "carla/client/detail/Simulator.h" // 导入Simulator类的头文件
11
12#include <exception> //导入异常处理的头文件
13
14// 定义一个常量,表示GBuffer的纹理数量
15constexpr size_t GBufferTextureCount = 13;
16
17namespace carla {
18namespace client {
19
20 //ServerSideSensor的析构函数
22 // 检查传感器是否仍在运行且处于监听状态
23 if (IsAlive() && IsListening()) {
24 // 打印警告信息:传感器对象超出作用域,但传感器仍在模拟中运行
26 "sensor object went out of the scope but the sensor is still alive",
27 "in the simulation:",
28 GetDisplayId());
29 }
30 // 如果传感器正在监听并且当前的模拟场景有效
31 if (IsListening() && GetEpisode().IsValid()) {
32 try {
33 // 遍历所有GBuffer纹理,如果正在监听,则停止监听
34 for (uint32_t i = 1; i != GBufferTextureCount + 1; ++i) {
35 if (listening_mask.test(i)) // 检查是否正在监听
36 StopGBuffer(i - 1); // 停止该纹理的监听
37 }
38 Stop(); // 停止传感器监听
39 } catch (const std::exception &e) {
40 // 如果发生异常,记录错误信息
41 log_error("exception trying to stop sensor:", GetDisplayId(), ':', e.what());
42 }
43 }
44 }
45 // Listen函数:开始监听传感器数据流
47 log_debug("calling sensor Listen() ", GetDisplayId()); // 打印调试信息,表示调用了Listen方法
48 log_debug(GetDisplayId(), ": subscribing to stream"); // 记录订阅流的消息
49 //锁定当前模拟场景并订阅传感器数据流
50 GetEpisode().Lock()->SubscribeToSensor(*this, std::move(callback));
51 listening_mask.set(0); // 将监听标志的第0位置为true,表示传感器开始监听
52 }
53
54 // stop函数:停止监听传感器数据流
56 log_debug("calling sensor Stop() ", GetDisplayId()); // 打印调试信息,表示调用了Stop方法
57 if (!IsListening()) {
58 // 如果传感器未监听,打印警告信息
60 "attempting to unsubscribe from stream but sensor wasn't listening:",
61 GetDisplayId());
62 return;
63 }
64 // 锁定当前模拟场景并取消传感器的订阅
65 GetEpisode().Lock()->UnSubscribeFromSensor(*this);
66 listening_mask.reset(0); // 将监听标志的第0位置为false,表示停止监听
67 }
68
69 // Send函数:向模拟器发送消息
70 void ServerSideSensor::Send(std::string message) {
71 log_debug("calling sensor Send() ", GetDisplayId()); //记录调试日志,表示调用了send方法
72 // 检查传感器类型是否为 "sensor.other.v2x_custom"
73 if (GetActorDescription().description.id != "sensor.other.v2x_custom")
74 //如果传感器类型不是"sensor.other.v2x_custom",则不支持send方法
75 {
76 log_warning("Send methods are not supported on non-V2x sensors (sensor.other.v2x_custom).");
77 return;
78 }
79 //通过Episode对象向外发送信息
80 GetEpisode().Lock()->Send(*this,message);
81 }
82
83 // ListenToGBuffer函数:开始监听GBuffer纹理流
84 void ServerSideSensor::ListenToGBuffer(uint32_t GBufferId, CallbackFunctionType callback) {
85 log_debug(GetDisplayId(), ": subscribing to gbuffer stream"); //记录订阅GBuffer流的日志
86 // 确保GBufferId在有效范围内
88 // 检查传感器类型是否为 "sensor.camera.rgb"
89 if (GetActorDescription().description.id != "sensor.camera.rgb")
90 // 如果传感器不是"sensor.camera.rgb",则不支持GBuffer方法
91 {
92 log_warning("GBuffer methods are not supported on non-RGB sensors (sensor.camera.rgb).");
93 return;
94 }
95 // 通过Episode对象订阅指定的GBuffer流,并设置回调函数
96 GetEpisode().Lock()->SubscribeToGBuffer(*this, GBufferId, std::move(callback));
97 // 设置监听掩码的第一位,表示开启了GBuffer监听
98 listening_mask.set(0);
99 // 根据GBufferId设置相应的掩码位,标识该GBuffer流被监听
100 listening_mask.set(GBufferId + 1);
101 }
102
103 // StopGBuffer函数:停止监听GBuffer纹理流
104 void ServerSideSensor::StopGBuffer(uint32_t GBufferId) {
105 log_debug(GetDisplayId(), ": unsubscribing from gbuffer stream"); //记录取消订阅GBuffer流的日志
106 RELEASE_ASSERT(GBufferId < GBufferTextureCount); // 确保GBufferId在有效范围内
107 // 检查传感器类型是否为 "sensor.camera.rgb"
108 if (GetActorDescription().description.id != "sensor.camera.rgb")
109 // 如果传感器不是"sensor.camera.rgb",则不支持GBuffer方法
110 {
111 log_warning("GBuffer methods are not supported on non-RGB sensors (sensor.camera.rgb).");
112 return;
113 }
114 // 通过Episode对象取消订阅指定的GBuffer流
115 GetEpisode().Lock()->UnSubscribeFromGBuffer(*this, GBufferId);
116 // 重置掩码的相应位,表示停止监听
117 listening_mask.reset(GBufferId + 1);
118 }
119
120 // EnableForROS函数:使传感器支持ROS通信
122 // 通过Episode对象启用传感器的ROS支持
123 GetEpisode().Lock()->EnableForROS(*this);
124 }
125
126 // DisableForROS函数:禁用传感器对ROS的支持
128 // 通过Episode对象禁用传感器的ROS支持
129 GetEpisode().Lock()->DisableForROS(*this);
130 }
131
132 // IsEnabledForROS函数:检查传感器是否启用了ROS支持
134 // 检查当前传感器是否启用了ROS支持
135 return GetEpisode().Lock()->IsEnabledForROS(*this);
136 }
137
138 // Destroy函数:销毁传感器对象
140 log_debug("calling sensor Destroy() ", GetDisplayId()); // 记录调试日志,表示调用了Destroy方法
141 // 如果传感器正在监听,则停止监听所有GBuffer流
142 if (IsListening()) {
143 for (uint32_t i = 1; i != GBufferTextureCount + 1; ++i) {
144 if (listening_mask.test(i)) { // 检查掩码以确认是否有活跃的GBuffer流
145 StopGBuffer(i - 1); // 停止每个活跃的GBuffer流
146 }
147 }
148 Stop(); // 停止其他监听任务
149 }
150 // 调用基类的Destroy方法,完成销毁
151 return Actor::Destroy();
152 }
153
154} // namespace client
155} // namespace carla
#define RELEASE_ASSERT(pred)
Definition Debug.h:94
constexpr size_t GBufferTextureCount
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
std::function< void(SharedPtr< sensor::SensorData >)> CallbackFunctionType
回调函数的类型别名,用于接收传感器数据。
bool IsEnabledForROS()
如果传感器正在为 ROS2 发布,则返回
void EnableForROS()
启用此传感器以进行 ROS2 发布
void ListenToGBuffer(uint32_t GBufferId, CallbackFunctionType callback)
监听 fr
void Send(std::string message)
通过该传感器发送数据
void Stop() override
停止监听新的测量结果。
void DisableForROS()
禁用此传感器以进行 ROS2 发布
bool IsListening() const override
返回此传感器实例当前是否正在监听模拟器中的相关传感器。
void Listen(CallbackFunctionType callback) override
注册一个 回调,每次收到新的测量值时执行。
void StopGBuffer(uint32_t GBufferId)
停止监听特定的 gbuffer 流。
const rpc::Actor & GetActorDescription() const
const std::string & GetDisplayId() const
SharedPtrType Lock() const
与 TryLock 相同,但永远不会返回 nullptr。
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
static void log_error(Args &&... args)
Definition Logging.h:115
static void log_warning(Args &&... args)
Definition Logging.h:101
static void log_debug(Args &&... args)
Definition Logging.h:71
包含CARLA客户端相关类和函数的命名空间。