CARLA
 
载入中...
搜索中...
未找到
TrafficManagerClient.h
浏览该文件的文档.
1// Copyright (c) 2020 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#pragma once
8
9#include "carla/trafficmanager/Constants.h"// 引入常量定义
10#include "carla/rpc/Actor.h"// 引入Actor类的定义
11
12#include <rpc/client.h>// 引入RPC客户端库
13
14namespace carla {
15namespace traffic_manager {
16 /**
17 * 使用常量命名空间中的TM_TIMEOUT和TM_DEFAULT_PORT。
18 */
21
22/**
23 * @class TrafficManagerClient
24 * @brief 提供与TrafficManagerServer的RPC通信功能。
25 */
27
28public:
29 /**
30 * @brief 拷贝构造函数,默认实现。
31 */
33 /**
34 * @brief 移动构造函数,默认实现。
35 */
37 /**
38 * @brief 拷贝赋值运算符,默认实现。
39 */
41 /**
42 * @brief 移动赋值运算符,默认实现。
43 */
45
46 /**
47 * @brief 参数化构造函数,用于初始化连接参数。
48 * @param _host 服务器主机名或IP地址。
49 * @param _port 服务器端口号。
50 */
52 const std::string &_host,// 服务器主机名或IP地址
53 const uint16_t &_port)// 服务器端口号
54 : tmhost(_host),// 初始化成员变量tmhost
55 tmport(_port) {// 初始化成员变量tmport
56
57 /**
58 * 创建RPC客户端实例。
59 * 如果_client尚未创建,则创建一个新的rpc::client实例,并设置超时时间。
60 */
61 if(!_client) {
62 _client = new ::rpc::client(tmhost, tmport);// 创建RPC客户端实例
63 _client->set_timeout(TM_TIMEOUT);// 设置超时时间为TM_TIMEOUT
64 }
65 }
66
67 /// 析构函数,用于清理资源
69 if(_client) {
70 delete _client;// 如果_client存在,则删除它
71 _client = nullptr;// 将_client指针置为空,避免野指针
72 }
73 };
74
75 /// 设置服务器详细信息。
76/// @param _host 服务器主机名或IP地址。
77/// @param _port 服务器端口号
78 void setServerDetails(const std::string &_host, const uint16_t &_port) {
79 tmhost = _host;// 设置主机名
80 tmport = _port;// 设置端口号
81 }
82
83 /// 获取服务器详细信息。
84/// @param _host 用于存储服务器主机名的字符串引用。
85/// @param _port 用于存储服务器端口号的无符号短整型引用
86 void getServerDetails(std::string &_host, uint16_t &_port) {
87 _host = tmhost;// 返回主机名
88 _port = tmport;// 返回端口号
89 }
90
91 /// 通过RPC客户端向远程交通管理服务器注册车辆。
92/// @param actor_list 要注册的车辆列表。
93 void RegisterVehicle(const std::vector<carla::rpc::Actor> &actor_list) {
94 DEBUG_ASSERT(_client != nullptr);// 断言_client不为空
95 _client->call("register_vehicle", std::move(actor_list));// 调用RPC方法注册车辆
96 }
97
98 /// 通过RPC客户端从远程交通管理服务器注销车辆。
99/// @param actor_list 要注销的车辆列表。
100 void UnregisterVehicle(const std::vector<carla::rpc::Actor> &actor_list) {
101 DEBUG_ASSERT(_client != nullptr);// 断言_client不为空
102 _client->call("unregister_vehicle", std::move(actor_list));// 调用RPC方法注销车辆
103 }
104
105 /// 设置车辆相对于限速的速度百分比差异。
106/// 如果小于0,则表示百分比增加。
107/// @param _actor 要设置速度差异的车辆。
108/// @param percentage 速度百分比差异。
109 void SetPercentageSpeedDifference(const carla::rpc::Actor &_actor, const float percentage) {
110 DEBUG_ASSERT(_client != nullptr);// 断言_client不为空
111 _client->call("set_percentage_speed_difference", std::move(_actor), percentage);// 调用RPC方法设置速度差异
112 }
113
114 /// 设置车辆相对于中心线的车道偏移量。
115/// 正值表示向右偏移,负值表示向左偏移。
116/// @param _actor 要设置车道偏移量的车辆。
117/// @param offset 车道偏移量。
118 void SetLaneOffset(const carla::rpc::Actor &_actor, const float offset) {
119 DEBUG_ASSERT(_client != nullptr);// 断言_client不为空
120 _client->call("set_lane_offset", std::move(_actor), offset);// 调用RPC方法设置车道偏移量
121 }
122
123 /// 设置车辆的精确期望速度。
124/// @param _actor 要设置期望速度的车辆。
125/// @param value 期望速度值。
126 void SetDesiredSpeed(const carla::rpc::Actor &_actor, const float value) {
127 DEBUG_ASSERT(_client != nullptr);// 断言_client不为空
128 _client->call("set_desired_speed", std::move(_actor), value);// 调用RPC方法设置期望速度
129 }
130
131 /// 设置全局相对于限速的速度百分比差异。
132/// 如果小于0,则表示百分比增加。
133/// @param percentage 全局速度百分比差异。
134 void SetGlobalPercentageSpeedDifference(const float percentage) {
135 DEBUG_ASSERT(_client != nullptr);// 断言_client不为空
136 _client->call("set_global_percentage_speed_difference", percentage);// 调用RPC方法设置全局速度差异
137 }
138
139 /// 设置全局车道偏移量,相对于中心线。
140/// 正值表示向右偏移,负值表示向左偏移。
141/// @param offset 车道偏移量。
142 void SetGlobalLaneOffset(const float offset) {
143 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
144 _client->call("set_global_lane_offset", offset);// 调用RPC方法设置全局车道偏移量
145 }
146
147 /// 设置车辆灯光的自动管理。
148/// @param _actor 要设置灯光管理的车辆。
149/// @param do_update 是否启用灯光自动管理。
150 void SetUpdateVehicleLights(const carla::rpc::Actor &_actor, const bool do_update) {
151 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
152 _client->call("update_vehicle_lights", std::move(_actor), do_update);// 调用RPC方法设置车辆灯光自动管理
153 }
154
155 /// 设置车辆间的碰撞检测规则。
156/// @param reference_actor 参考车辆。
157/// @param other_actor 另一辆参与碰撞检测的车辆。
158/// @param detect_collision 是否启用碰撞检测。
159 void SetCollisionDetection(const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision) {
160 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
161 _client->call("set_collision_detection", reference_actor, other_actor, detect_collision);// 调用RPC方法设置碰撞检测规则
162 }
163
164 /// 强制车辆换道。
165/// @param actor 要强制换道的车辆。
166/// @param direction 换道方向,true表示向左,false表示向右。
167 void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction) {
168 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
169 _client->call("set_force_lane_change", actor, direction);// 调用RPC方法强制车辆换道
170 }
171
172 /// 启用/禁用车辆的自动换道功能。
173/// @param actor 要设置自动换道功能的车辆。
174/// @param enable 是否启用自动换道。
175 void SetAutoLaneChange(const carla::rpc::Actor &actor, const bool enable) {
176 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
177 _client->call("set_auto_lane_change", actor, enable);// 调用RPC方法设置自动换道功能
178 }
179
180 /// 设置车辆与前车应保持的距离。
181/// @param actor 要设置距离的车辆。
182/// @param distance 应保持的距离。
183 void SetDistanceToLeadingVehicle(const carla::rpc::Actor &actor, const float distance) {
184 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
185 _client->call("set_distance_to_leading_vehicle", actor, distance);// 调用RPC方法设置与前车的距离
186 }
187
188 /// 设置车辆忽略行人的碰撞概率。
189/// @param actor 要设置碰撞概率的车辆。
190/// @param percentage 忽略行人的碰撞概率(百分比)。
191 void SetPercentageIgnoreWalkers(const carla::rpc::Actor &actor, const float percentage) {
192 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
193 _client->call("set_percentage_ignore_walkers", actor, percentage); // 调用RPC方法设置忽略行人的碰撞概率
194 }
195
196 /// 设置车辆忽略其他车辆的碰撞概率。
197/// @param actor 要设置碰撞概率的车辆。
198/// @param percentage 忽略其他车辆的碰撞概率(百分比)。
199 void SetPercentageIgnoreVehicles(const carla::rpc::Actor &actor, const float percentage) {
200 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
201 _client->call("set_percentage_ignore_vehicles", actor, percentage);// 调用RPC方法设置忽略其他车辆的碰撞概率
202 }
203
204 /// 设置车辆闯红灯的概率。
205/// @param actor 要设置闯红灯概率的车辆。
206/// @param percentage 闯红灯的概率(百分比)。
207 void SetPercentageRunningLight(const carla::rpc::Actor &actor, const float percentage) {
208 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
209 _client->call("set_percentage_running_light", actor, percentage);// 调用RPC方法设置闯红灯的概率
210 }
211
212 /// 设置车辆无视任何交通标志的概率。
213/// @param actor 要设置无视交通标志概率的车辆。
214/// @param percentage 无视交通标志的概率(百分比)。
215 void SetPercentageRunningSign(const carla::rpc::Actor &actor, const float percentage) {
216 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
217 _client->call("set_percentage_running_sign", actor, percentage);// 调用RPC方法设置无视交通标志的概率
218 }
219
220 /// 将交通管理器切换为同步执行模式。
221/// @param mode 是否启用同步模式。
222 void SetSynchronousMode(const bool mode) {
223 DEBUG_ASSERT(_client != nullptr);// 断言_client指针不为空
224 _client->call("set_synchronous_mode", mode);// 调用RPC方法设置同步执行模式
225 }
226
227 /// 设置同步执行模式下的超时时间(以毫秒为单位)
229 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空。
230 _client->call("set_synchronous_mode_timeout_in_milisecond", time);/// 调用_client的call方法设置超时时间
231 }
232
233 /// 提供同步滴答(tick)操作
235 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
236 return _client->call("synchronous_tick").as<bool>();/// 调用_client的call方法执行同步滴答,并返回结果
237 }
238
239 /// 检查远程交通管理器(TM)是否存活
241 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
242 _client->call("health_check_remote_TM");/// 调用_client的call方法检查远程交通管理器状态
243 }
244
245 /// 设置全局领航车辆应保持的距离
246 void SetGlobalDistanceToLeadingVehicle(const float distance) {
247 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
248 _client->call("set_global_distance_to_leading_vehicle",distance); /// 调用_client的call方法设置全局领航距离
249 }
250
251 /// 设置车辆保持在右侧车道的百分比
252 void SetKeepRightPercentage(const carla::rpc::Actor &actor, const float percentage) {
253 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
254 _client->call("keep_right_rule_percentage", actor, percentage);/// 调用_client的call方法设置保持右侧车道的百分比
255 }
256
257 /// 设置车辆随机进行左车道变换的百分比
258 void SetRandomLeftLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage) {
259 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
260 _client->call("random_left_lanechange_percentage", actor, percentage);/// 调用_client的call方法设置随机左车道变换的百分比
261 }
262
263 /// 设置车辆随机进行右车道变换的百分比
264 void SetRandomRightLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage) {
265 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
266 _client->call("random_right_lanechange_percentage", actor, percentage);/// 调用_client的call方法设置随机右车道变换的百分比
267 }
268
269 /// 设置混合物理模式
270 void SetHybridPhysicsMode(const bool mode_switch) {
271 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
272 _client->call("set_hybrid_physics_mode", mode_switch);/// 调用_client的call方法设置混合物理模式
273 }
274
275 /// 设置混合物理模式的半径
276 void SetHybridPhysicsRadius(const float radius) {
277 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
278 _client->call("set_hybrid_physics_radius", radius);/// 调用_client的call方法设置混合物理模式的半径
279 }
280
281 /// 设置随机化种子
282 void SetRandomDeviceSeed(const uint64_t seed) {
283 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
284 _client->call("set_random_device_seed", seed);/// 调用_client的call方法设置随机化种子
285 }
286
287 /// 设置Open Street Map模式
288 void SetOSMMode(const bool mode_switch) {
289 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
290 _client->call("set_osm_mode", mode_switch);/// 调用_client的call方法设置Open Street Map模式
291 }
292
293 /// 设置自定义路径
294 void SetCustomPath(const carla::rpc::Actor &actor, const Path path, const bool empty_buffer) {
295 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
296 _client->call("set_path", actor, path, empty_buffer);/// 调用_client的call方法设置自定义路径
297 }
298
299 /// 移除一系列点(路径)
300 void RemoveUploadPath(const ActorId &actor_id, const bool remove_path) {
301 DEBUG_ASSERT(_client != nullptr);/// 断言_client指针不为空
302 _client->call("remove_custom_path", actor_id, remove_path);/// 调用_client的call方法移除自定义路径
303 }
304
305 /// 更新已设置的点列表的方法
306 void UpdateUploadPath(const ActorId &actor_id, const Path path) {
307 DEBUG_ASSERT(_client != nullptr);/// 断言_client不为nullptr
308 _client->call("update_custom_path", actor_id, path);/// 调用_client的call方法,传入"update_custom_path"指令、actor_id和path
309 }
310
311 /// 设置我们自己的导入路线的方法
312 void SetImportedRoute(const carla::rpc::Actor &actor, const Route route, const bool empty_buffer) {
313 DEBUG_ASSERT(_client != nullptr);/// 断言_client不为nullptr
314 _client->call("set_imported_route", actor, route, empty_buffer);/// 调用_client的call方法,传入"set_imported_route"指令、actor、route和empty_buffer
315 }
316
317 /// 移除路线的方法
318 void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path) {
319 DEBUG_ASSERT(_client != nullptr);/// 断言_client不为nullptr
320 _client->call("remove_imported_route", actor_id, remove_path);/// 调用_client的call方法,传入"remove_imported_route"指令、actor_id和remove_path。
321 }
322
323 /// 更新已设置的导入路线的方法
324 void UpdateImportedRoute(const ActorId &actor_id, const Route route) {
325 DEBUG_ASSERT(_client != nullptr);/// 断言_client不为nullptr
326 _client->call("update_imported_route", actor_id, route);/// 调用_client的call方法,传入"update_imported_route"指令、actor_id和route
327 }
328
329 /// 设置休眠车辆的自动重生模式的方法
330 void SetRespawnDormantVehicles(const bool mode_switch) {
331 DEBUG_ASSERT(_client != nullptr);/// 断言_client不为nullptr
332 _client->call("set_respawn_dormant_vehicles", mode_switch);/// 调用_client的call方法,传入"set_respawn_dormant_vehicles"指令和mode_switch
333 }
334
335 /// 设置重生车辆的边界的方法
336 void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound) {
337 DEBUG_ASSERT(_client != nullptr);/// 断言_client不为nullptr
338 _client->call("set_boundaries_respawn_dormant_vehicles", lower_bound, upper_bound);/// 调用_client的call方法,传入"set_boundaries_respawn_dormant_vehicles"指令、lower_bound和upper_bound
339 }
340
341 /// 设置最大边界的方法
342 void SetMaxBoundaries(const float lower, const float upper) {
343 DEBUG_ASSERT(_client != nullptr);/// 断言_client不为nullptr。
344 _client->call("set_max_boundaries", lower, upper);/// 调用_client的call方法,传入"set_max_boundaries"指令、lower和upper
345 }
346
347 /// 获取车辆下一个动作的方法
348 Action GetNextAction(const ActorId &actor_id) {
349 DEBUG_ASSERT(_client != nullptr);/// 断言_client不为nullptr
350 _client->call("get_next_action", actor_id);/// 调用_client的call方法,传入"get_next_action"指令和actor_id
351 return Action();/// 返回Action对象
352 }
353
354 /// 获取车辆动作缓冲区的方法
356 DEBUG_ASSERT(_client != nullptr);/// 断言_client不为nullptr
357 _client->call("get_all_actions", actor_id);/// 调用_client的call方法,传入"get_all_actions"指令和actor_id
358 return ActionBuffer();/// 返回ActionBuffer对象
359 }
360 /// 关闭的方法
361 void ShutDown() {/// 断言_client不为nullptr
362 DEBUG_ASSERT(_client != nullptr);
363 _client->call("shut_down");/// 调用_client的call方法,传入"shut_down"指令
364 }
365
366private:
367
368 /// RPC客户端
369 ::rpc::client *_client = nullptr;
370
371 /// 服务器端口和主机
372 std::string tmhost;
373 uint16_t tmport;
374};
375
376} // namespace traffic_manager
377} // namespace carla
#define DEBUG_ASSERT(predicate)
Definition Debug.h:68
提供与TrafficManagerServer的RPC通信功能。
void SetGlobalLaneOffset(const float offset)
设置全局车道偏移量,相对于中心线。
Action GetNextAction(const ActorId &actor_id)
获取车辆下一个动作的方法
void HealthCheckRemoteTM()
检查远程交通管理器(TM)是否存活
TrafficManagerClient & operator=(TrafficManagerClient &&)=default
移动赋值运算符,默认实现。
void SetPercentageIgnoreWalkers(const carla::rpc::Actor &actor, const float percentage)
设置车辆忽略行人的碰撞概率。
void SetDesiredSpeed(const carla::rpc::Actor &_actor, const float value)
设置车辆的精确期望速度。
void SetGlobalPercentageSpeedDifference(const float percentage)
设置全局相对于限速的速度百分比差异。
TrafficManagerClient(TrafficManagerClient &&)=default
移动构造函数,默认实现。
void SetMaxBoundaries(const float lower, const float upper)
设置最大边界的方法
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
设置重生车辆的边界的方法
TrafficManagerClient & operator=(const TrafficManagerClient &)=default
拷贝赋值运算符,默认实现。
void SetRandomDeviceSeed(const uint64_t seed)
设置随机化种子
void SetKeepRightPercentage(const carla::rpc::Actor &actor, const float percentage)
设置车辆保持在右侧车道的百分比
void SetSynchronousMode(const bool mode)
将交通管理器切换为同步执行模式。
TrafficManagerClient(const TrafficManagerClient &)=default
拷贝构造函数,默认实现。
ActionBuffer GetActionBuffer(const ActorId &actor_id)
获取车辆动作缓冲区的方法
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
移除一系列点(路径)
void SetPercentageRunningLight(const carla::rpc::Actor &actor, const float percentage)
设置车辆闯红灯的概率。
void RegisterVehicle(const std::vector< carla::rpc::Actor > &actor_list)
通过RPC客户端向远程交通管理服务器注册车辆。
void UnregisterVehicle(const std::vector< carla::rpc::Actor > &actor_list)
通过RPC客户端从远程交通管理服务器注销车辆。
void SetOSMMode(const bool mode_switch)
设置Open Street Map模式
void getServerDetails(std::string &_host, uint16_t &_port)
获取服务器详细信息。
void SetPercentageSpeedDifference(const carla::rpc::Actor &_actor, const float percentage)
设置车辆相对于限速的速度百分比差异。
void SetCustomPath(const carla::rpc::Actor &actor, const Path path, const bool empty_buffer)
设置自定义路径
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
更新已设置的导入路线的方法
void SetUpdateVehicleLights(const carla::rpc::Actor &_actor, const bool do_update)
设置车辆灯光的自动管理。
void SetRandomRightLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage)
设置车辆随机进行右车道变换的百分比
void SetPercentageIgnoreVehicles(const carla::rpc::Actor &actor, const float percentage)
设置车辆忽略其他车辆的碰撞概率。
void SetForceLaneChange(const carla::rpc::Actor &actor, const bool direction)
强制车辆换道。
void SetDistanceToLeadingVehicle(const carla::rpc::Actor &actor, const float distance)
设置车辆与前车应保持的距离。
void SetLaneOffset(const carla::rpc::Actor &_actor, const float offset)
设置车辆相对于中心线的车道偏移量。
void SetSynchronousModeTimeOutInMiliSecond(const double time)
设置同步执行模式下的超时时间(以毫秒为单位)
void SetAutoLaneChange(const carla::rpc::Actor &actor, const bool enable)
启用/禁用车辆的自动换道功能。
void SetPercentageRunningSign(const carla::rpc::Actor &actor, const float percentage)
设置车辆无视任何交通标志的概率。
void setServerDetails(const std::string &_host, const uint16_t &_port)
设置服务器详细信息。
void SetRandomLeftLaneChangePercentage(const carla::rpc::Actor &actor, const float percentage)
设置车辆随机进行左车道变换的百分比
void SetImportedRoute(const carla::rpc::Actor &actor, const Route route, const bool empty_buffer)
设置我们自己的导入路线的方法
void SetHybridPhysicsRadius(const float radius)
设置混合物理模式的半径
void SetGlobalDistanceToLeadingVehicle(const float distance)
设置全局领航车辆应保持的距离
TrafficManagerClient(const std::string &_host, const uint16_t &_port)
参数化构造函数,用于初始化连接参数。
void UpdateUploadPath(const ActorId &actor_id, const Path path)
更新已设置的点列表的方法
void SetHybridPhysicsMode(const bool mode_switch)
设置混合物理模式
~TrafficManagerClient()
析构函数,用于清理资源
bool SynchronousTick()
提供同步滴答(tick)操作
void SetCollisionDetection(const carla::rpc::Actor &reference_actor, const carla::rpc::Actor &other_actor, const bool detect_collision)
设置车辆间的碰撞检测规则。
void SetRespawnDormantVehicles(const bool mode_switch)
设置休眠车辆的自动重生模式的方法
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
移除路线的方法
std::string tmhost
服务器端口和主机
static const unsigned short TM_DEFAULT_PORT
Definition Constants.h:22
std::vector< uint8_t > Route
路线类型,由一系列地理位置组成
std::vector< cg::Location > Path
参与者的唯一标识符类型
std::vector< Action > ActionBuffer
动作缓冲区类型别名。
std::pair< RoadOption, WaypointPtr > Action
动作类型别名。
carla::ActorId ActorId
参与者的智能指针类型
CARLA模拟器的主命名空间。
Definition Carla.cpp:139