CARLA
 
载入中...
搜索中...
未找到
Vehicle.cpp
浏览该文件的文档.
1// Copyright (c) 2019 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/Vehicle.h" // 引入控制和管理车辆的相关类定义
8
9#include "carla/client/ActorList.h" // 引入Actor列表相关的类,通常是用于访问仿真中的所有角色(例如车辆、行人等)
10#include "carla/client/detail/Simulator.h" // 引入Simulator的实现细节,用于模拟环境的控制
11#include "carla/client/TrafficLight.h" // 引入交通信号灯相关的类定义,允许控制和查询交通灯的状态
12#include "carla/Memory.h" // 引入内存管理相关的定义,通常用于资源管理和垃圾回收
13#include "carla/rpc/TrafficLightState.h" // 引入交通灯状态的定义,以便管理交通信号灯的不同状态(如红灯、绿灯等)
14
15#include "carla/trafficmanager/TrafficManager.h" // 引入交通管理器的定义,允许对整个城市中的交通进行全局管理和控制
16
17namespace carla {
18
20
21namespace client {
22
23 // 模板函数:从属性列表中获取“sticky_control”属性的值
24 // 该属性指示是否启用了粘性控制(即控制指令是否会持续应用)
25 template <typename AttributesT>
26 static bool GetControlIsSticky(const AttributesT &attributes) {
27 for (auto &&attribute : attributes) {
28 // 查找属性 ID 为"sticky_control"的属性
29 if (attribute.GetId() == "sticky_control") {
30 return attribute.template As<bool>(); // 返回该属性值(布尔类型)
31 }
32 }
33 // 如果未找到"sticky_control"属性,则默认返回 true
34 return true;
35 }
36 // 车辆类的构造函数
37 // 初始化时获取车辆的“sticky_control”属性,决定是否启用粘性控制
39 : Actor(std::move(init)),
40 _is_control_sticky(GetControlIsSticky(GetAttributes())) {}
41 // 设置车辆的自动驾驶状态
42 // 启用或禁用自动驾驶,并在交通管理器中注册或注销该车辆
43 void Vehicle::SetAutopilot(bool enabled, uint16_t tm_port) {
44 TM tm(GetEpisode(), tm_port);
45 if (enabled) {
46 // 启用自动驾驶,车辆被注册到交通管理器
47 tm.RegisterVehicles({shared_from_this()});
48 } else {
49 // 禁用自动驾驶,车辆从交通管理器中注销
50 tm.UnregisterVehicles({shared_from_this()});
51 }
52 }
53 // 获取车辆的遥测数据
54 // 返回车辆的最新遥测数据,包括速度、位置等
56 return GetEpisode().Lock()->GetVehicleTelemetryData(*this);
57 }
58 // 显示或隐藏车辆的调试遥测信息
59 // 根据`enabled`参数来控制遥测信息是否显示
60 void Vehicle::ShowDebugTelemetry(bool enabled) {
61 GetEpisode().Lock()->ShowVehicleDebugTelemetry(*this, enabled);
62 }
63 // 应用车辆控制指令
64 // 如果控制指令不是粘性的,或者与当前指令不同,才应用新的控制指令
65 void Vehicle::ApplyControl(const Control &control) {
66 // 如果控制指令不是粘性的或者与当前控制指令不同,则应用新的控制指令
67 if (!_is_control_sticky || (control != _control)) {
68 GetEpisode().Lock()->ApplyControlToVehicle(*this, control);
69 _control = control;
70 }
71 }
72 // 应用阿克曼控制指令
73 // 使用阿克曼转向模型来控制车辆的转向
75 GetEpisode().Lock()->ApplyAckermannControlToVehicle(*this, control);
76 }
77 // 获取阿克曼控制器的设置
78 // 返回当前的阿克曼控制器设置,包括转向角度等
80 return GetEpisode().Lock()->GetAckermannControllerSettings(*this);
81 }
82 // 应用新的阿克曼控制器设置
83 // 设置阿克曼控制器的相关参数(如转向参数)
85 GetEpisode().Lock()->ApplyAckermannControllerSettings(*this, settings);
86 }
87 // 应用车辆的物理控制指令
88 // 使用物理控制模型调整车辆的物理行为(例如速度、加速度等)
89 void Vehicle::ApplyPhysicsControl(const PhysicsControl &physics_control) {
90 GetEpisode().Lock()->ApplyPhysicsControlToVehicle(*this, physics_control);
91 }
92 // 打开指定的车辆门
93 // `door_idx`指示需要打开的车辆门(例如:前左门、后右门等)
94 void Vehicle::OpenDoor(const VehicleDoor door_idx) {
95 GetEpisode().Lock()->OpenVehicleDoor(*this, rpc::VehicleDoor(door_idx));
96 }
97 // 关闭指定的车辆门
98 // `door_idx`指示需要关闭的车辆门
99 void Vehicle::CloseDoor(const VehicleDoor door_idx) {
100 GetEpisode().Lock()->CloseVehicleDoor(*this, rpc::VehicleDoor(door_idx));
101 }
102 // 设置车辆的灯光状态
103 // 设置车辆的各类灯光(如远光灯、近光灯等)的状态
104 void Vehicle::SetLightState(const LightState &light_state) {
105 GetEpisode().Lock()->SetLightStateToVehicle(*this, rpc::VehicleLightState(light_state));
106 }
107 // 设置指定轮子的转向角度
108 // `wheel_location`表示轮子的位置,`angle_in_deg`表示转向角度(单位:度)
109 void Vehicle::SetWheelSteerDirection(WheelLocation wheel_location, float angle_in_deg) {
110 GetEpisode().Lock()->SetWheelSteerDirection(*this, wheel_location, angle_in_deg);
111 }
112 // 获取指定轮子的转向角度
113 // 返回指定轮子的当前转向角度(单位:度)
115 return GetEpisode().Lock()->GetWheelSteerAngle(*this, wheel_location);
116 }
117 // 获取车辆当前的控制指令
118 // 返回当前应用于车辆的控制指令(如加速、刹车、转向等)
120 return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.control;
121 }
122 // 获取车辆的物理控制指令
123 // 返回当前车辆的物理控制状态(如速度、加速度等)
125 return GetEpisode().Lock()->GetVehiclePhysicsControl(*this);
126 }
127 // 获取车辆的灯光状态
128 // 返回车辆的灯光状态(如是否开启远光灯、近光灯等)
130 return GetEpisode().Lock()->GetVehicleLightState(*this).GetLightStateEnum();
131 }
132 // 获取车辆的当前速度限制
133 // 返回车辆所受的速度限制(单位:米/秒)
135 return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.speed_limit;
136 }
137 // 获取车辆当前所处交通灯的状态
138 // 返回当前交通灯的状态(例如:红灯、绿灯等)
140 return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.traffic_light_state;
141 }
142 // 判断车辆是否处于交通灯下
143 // 返回 true 如果车辆当前处于交通灯位置
145 return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.has_traffic_light;
146 }
147 // 获取车辆当前所处的交通灯
148 // 返回指向交通灯对象的指针,若没有交通灯则返回 nullptr
150 auto id = GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.traffic_light_id;
151 return boost::static_pointer_cast<TrafficLight>(GetWorld().GetActor(id));
152 }
153 // 启用车辆模拟(CarSim)
154 // 使用指定的模拟文件路径启用车辆模拟
155 void Vehicle::EnableCarSim(std::string simfile_path) {
156 GetEpisode().Lock()->EnableCarSim(*this, simfile_path);// 启动车辆模拟功能
157 }
158 // 设置是否使用车辆模拟的道路
159 // 根据 `enabled` 参数启用或禁用模拟的道路
160 void Vehicle::UseCarSimRoad(bool enabled) {
161 GetEpisode().Lock()->UseCarSimRoad(*this, enabled);
162 }
163 // 启用 Chrono 物理引擎
164 // 设置 Chrono 物理引擎的相关参数(如最大子步数、时间步长等)
165// 启用Chrono 物理引擎,并设置相关参数
167 uint64_t MaxSubsteps,
168 float MaxSubstepDeltaTime,
169 std::string VehicleJSON,
170 std::string PowertrainJSON,
171 std::string TireJSON,
172 std::string BaseJSONPath) {
173 GetEpisode().Lock()->EnableChronoPhysics(*this,
174 MaxSubsteps,
175 MaxSubstepDeltaTime,
176 VehicleJSON,
177 PowertrainJSON,
178 TireJSON,
179 BaseJSONPath);
180 }
181 // 恢复 PhysX 物理引擎
182 // 将车辆的物理引擎恢复为默认的 PhysX 引擎
184 GetEpisode().Lock()->RestorePhysXPhysics(*this);
185 }
186 // 获取车辆的故障状态
187 // 返回车辆的故障状态,包括是否存在故障、故障类型等信息
189 return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.failure_state;
190 }
191
192} // namespace client
193} // namespace carla
用于初始化 Actor 类。只有 ActorFactory 可以创建此对象,因此只有 ActorFactory 可以创建 Actor。
表示模拟中的一个行为体(Actor)。
void SetWheelSteerDirection(WheelLocation wheel_location, float angle_in_deg)
给车辆轮子一个 旋转 (影响汽车的骨骼框架,而不是物理)
Definition Vehicle.cpp:109
TelemetryData GetTelemetryData() const
返回车辆的遥测数据.
Definition Vehicle.cpp:55
void ApplyControl(const Control &control)
应用 控制此车辆.
Definition Vehicle.cpp:65
PhysicsControl GetPhysicsControl() const
返回车辆最后应用的物理控制.
Definition Vehicle.cpp:124
float GetWheelSteerAngle(WheelLocation wheel_location)
从车辆轮子返回一个 旋转
Definition Vehicle.cpp:114
void CloseDoor(const VehicleDoor door_idx)
在车中关门
Definition Vehicle.cpp:99
Vehicle(ActorInitializer init)
Definition Vehicle.cpp:38
LightState GetLightState() const
返回当前车辆的打开灯(LightState).
Definition Vehicle.cpp:129
bool IsAtTrafficLight()
返回交通灯是否正在影响该车辆.
Definition Vehicle.cpp:144
void EnableCarSim(std::string simfile_path)
如果可用,启用CarSim模拟
Definition Vehicle.cpp:155
rpc::TrafficLightState GetTrafficLightState() const
返回当前影响该车辆的交通灯的状态.
Definition Vehicle.cpp:139
void ApplyAckermannControl(const AckermannControl &control)
应用 控制此车辆.
Definition Vehicle.cpp:74
void ApplyPhysicsControl(const PhysicsControl &physics_control)
给此车辆应用物理控制.
Definition Vehicle.cpp:89
rpc::AckermannControllerSettings GetAckermannControllerSettings() const
返回最后应用于车辆的 Ackermann controller 设置.
Definition Vehicle.cpp:79
float GetSpeedLimit() const
返回当前影响该车辆的速度限制.
Definition Vehicle.cpp:134
SharedPtr< TrafficLight > GetTrafficLight() const
检索当前影响该车辆的交通灯.
Definition Vehicle.cpp:149
Control GetControl() const
返回最后应用于车辆的控制.
Definition Vehicle.cpp:119
void UseCarSimRoad(bool enabled)
允许使用CarSim内部道路定义,而不是虚幻引擎的
Definition Vehicle.cpp:160
void ApplyAckermannControllerSettings(const rpc::AckermannControllerSettings &settings)
应用 Ackermann 控制设置给此车辆
Definition Vehicle.cpp:84
void OpenDoor(const VehicleDoor door_idx)
在车中开门
Definition Vehicle.cpp:94
void EnableChronoPhysics(uint64_t MaxSubsteps, float MaxSubstepDeltaTime, std::string VehicleJSON="", std::string PowertrainJSON="", std::string TireJSON="", std::string BaseJSONPath="")
Definition Vehicle.cpp:166
void SetLightState(const LightState &light_state)
给车辆设置一个 LightState.
Definition Vehicle.cpp:104
rpc::VehicleFailureState GetFailureState() const
返回车辆的故障状态
Definition Vehicle.cpp:188
const bool _is_control_sticky
Definition Vehicle.h:197
void SetAutopilot(bool enabled=true, uint16_t tm_port=TM_DEFAULT_PORT)
开关车辆的自动驾驶仪.
Definition Vehicle.cpp:43
void ShowDebugTelemetry(bool enabled=true)
开关车辆的自动驾驶仪.
Definition Vehicle.cpp:60
SharedPtrType Lock() const
与 TryLock 相同,但永远不会返回 nullptr。
定义车辆的物理外观,通过传感器获取
该类通过使用消息传递机制,将交通管理器的各个阶段恰当地整合在一起
void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)
从交通管理器注销车辆。
void RegisterVehicles(const std::vector< ActorPtr > &actor_list)
向交通管理器注册车辆。
static bool GetControlIsSticky(const AttributesT &attributes)
Definition Vehicle.cpp:26
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
boost::shared_ptr< T > SharedPtr
使用这个SharedPtr(boost::shared_ptr)以保持与boost::python的兼容性, 但未来如果可能的话,我们希望能为std::shared_ptr制作一个Python适配器。
Definition Memory.h:19
包含CARLA客户端相关类和函数的命名空间。