CARLA
 
载入中...
搜索中...
未找到
CarlaWheeledVehicle.h
浏览该文件的文档.
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#pragma once
8
9#include "WheeledVehicle.h"
10
21#include "WheeledVehicleMovementComponent4W.h"
23#include "VehicleAnimInstance.h"
24#include "PhysicsEngine/PhysicsConstraintComponent.h"
26
27
28#include "FoliageInstancedStaticMeshComponent.h"
29#include "CoreMinimal.h"
30
31//-----CARSIM--------------------------------
32#ifdef WITH_CARSIM
33#include "CarSimMovementComponent.h"
34#endif
35//-------------------------------------------
36
37#include <utility>
38
40#include "CarlaWheeledVehicle.generated.h"
41
42class UBoxComponent;
43
44UENUM()
45enum class EVehicleWheelLocation : uint8 {
46
47 FL_Wheel = 0,
48 FR_Wheel = 1,
49 BL_Wheel = 2,
50 BR_Wheel = 3,
51 ML_Wheel = 4,
52 MR_Wheel = 5,
53 //Use for bikes and bicycles
54 Front_Wheel = 0,
55 Back_Wheel = 1,
56};
57
58/// Type of door to open/close
59// When adding new door types, make sure that All is the last one.
60UENUM(BlueprintType)
61enum class EVehicleDoor : uint8 {
62 FL = 0,
63 FR = 1,
64 RL = 2,
65 RR = 3,
66 Hood = 4,
67 Trunk = 5,
68 All = 6
69};
70
71/// Base class for CARLA wheeled vehicles.
72UCLASS()
73class CARLA_API ACarlaWheeledVehicle : public AWheeledVehicle
74{
75 GENERATED_BODY()
76
77 // ===========================================================================
78 /// @name Constructor and destructor
79 // ===========================================================================
80 /// @{
81public:
82
83 ACarlaWheeledVehicle(const FObjectInitializer &ObjectInitializer);
84
86
87 /// @}
88 // ===========================================================================
89 /// @name Get functions
90 // ===========================================================================
91 /// @{
92public:
93
94 /// Vehicle control currently applied to this vehicle.
95 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
96 const FVehicleControl &GetVehicleControl() const
97 {
98 return LastAppliedControl;
99 }
100
101 /// Vehicle Ackermann control currently applied to this vehicle.
102 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
103 const FVehicleAckermannControl &GetVehicleAckermannControl() const
104 {
105 return LastAppliedAckermannControl;
106 }
107
108 /// Transform of the vehicle. Location is shifted so it matches center of the
109 /// vehicle bounds rather than the actor's location.
110 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
111 FTransform GetVehicleTransform() const
112 {
113 return GetActorTransform();
114 }
115
116 /// Forward speed in cm/s. Might be negative if goes backwards.
117 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
118 float GetVehicleForwardSpeed() const;
119
120 /// Orientation vector of the vehicle, pointing forward.
121 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
122 FVector GetVehicleOrientation() const;
123
124 /// Active gear of the vehicle.
125 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
126 int32 GetVehicleCurrentGear() const;
127
128 /// Transform of the vehicle's bounding box relative to the vehicle.
129 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
130 FTransform GetVehicleBoundingBoxTransform() const;
131
132 /// Extent of the vehicle's bounding box.
133 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
134 FVector GetVehicleBoundingBoxExtent() const;
135
136 /// Get vehicle's bounding box component.
137 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
138 UBoxComponent *GetVehicleBoundingBox() const
139 {
140 return VehicleBounds;
141 }
142
143 /// Get the maximum angle at which the front wheel can steer.
144 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
145 float GetMaximumSteerAngle() const;
146
147 /// @}
148 // ===========================================================================
149 /// @name AI debug state
150 // ===========================================================================
151 /// @{
152public:
153
154 /// @todo This function should be private to AWheeledVehicleAIController.
156 {
157 State = InState;
158 }
159
160 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
161 ECarlaWheeledVehicleState GetAIVehicleState() const
162 {
163 return State;
164 }
165
166 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
167 FVehiclePhysicsControl GetVehiclePhysicsControl() const;
168
169 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
170 FAckermannControllerSettings GetAckermannControllerSettings() const {
171 return AckermannController.GetSettings();
172 }
173
174 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
175 void RestoreVehiclePhysicsControl();
176
177 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
178 FVehicleLightState GetVehicleLightState() const;
179
180 void ApplyVehiclePhysicsControl(const FVehiclePhysicsControl &PhysicsControl);
181
182 void ApplyAckermannControllerSettings(const FAckermannControllerSettings &AckermannControllerSettings) {
183 return AckermannController.ApplySettings(AckermannControllerSettings);
184 }
185
186 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
187 void SetSimulatePhysics(bool enabled);
188
189 void SetWheelCollision(UWheeledVehicleMovementComponent4W *Vehicle4W, const FVehiclePhysicsControl &PhysicsControl);
190
191 void SetWheelCollisionNW(UWheeledVehicleMovementComponentNW *VehicleNW, const FVehiclePhysicsControl &PhysicsControl);
192
193 void SetVehicleLightState(const FVehicleLightState &LightState);
194
195 void SetFailureState(const carla::rpc::VehicleFailureState &FailureState);
196
197 UFUNCTION(BlueprintNativeEvent)
198 bool IsTwoWheeledVehicle();
199 virtual bool IsTwoWheeledVehicle_Implementation() {
200 return false;
201 }
202
203 /// @}
204 // ===========================================================================
205 /// @name Vehicle input control
206 // ===========================================================================
207 /// @{
208public:
209
210 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
211 void ApplyVehicleControl(const FVehicleControl &Control, EVehicleInputPriority Priority)
212 {
213 if (bAckermannControlActive) {
214 AckermannController.Reset();
215 }
216 bAckermannControlActive = false;
217
218 if (InputControl.Priority <= Priority)
219 {
220 InputControl.Control = Control;
221 InputControl.Priority = Priority;
222 }
223 }
224
225 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
226 void ApplyVehicleAckermannControl(const FVehicleAckermannControl &AckermannControl, EVehicleInputPriority Priority)
227 {
228 bAckermannControlActive = true;
229 LastAppliedAckermannControl = AckermannControl;
230 AckermannController.SetTargetPoint(AckermannControl);
231 }
232
234 {
235 return bAckermannControlActive;
236 }
237
238 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
239 void ActivateVelocityControl(const FVector &Velocity);
240
241 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
242 void DeactivateVelocityControl();
243
244 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
245 FVehicleTelemetryData GetVehicleTelemetryData() const;
246
247 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
248 void ShowDebugTelemetry(bool Enabled);
249
250 /// @todo This function should be private to AWheeledVehicleAIController.
251 void FlushVehicleControl();
252
253 /// @}
254 // ===========================================================================
255 /// @name DEPRECATED Set functions
256 // ===========================================================================
257 /// @{
258public:
259
260 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
261 void SetThrottleInput(float Value);
262
263 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
264 void SetSteeringInput(float Value);
265
266 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
267 void SetBrakeInput(float Value);
268
269 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
270 void SetReverse(bool Value);
271
272 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
273 void ToggleReverse()
274 {
275 SetReverse(!LastAppliedControl.bReverse);
276 }
277
278 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
279 void SetHandbrakeInput(bool Value);
280
281 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
282 void HoldHandbrake()
283 {
284 SetHandbrakeInput(true);
285 }
286
287 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
288 void ReleaseHandbrake()
289 {
290 SetHandbrakeInput(false);
291 }
292
293 TArray<float> GetWheelsFrictionScale();
294
295 void SetWheelsFrictionScale(TArray<float> &WheelsFrictionScale);
296
297 void SetCarlaMovementComponent(UBaseCarlaMovementComponent* MoementComponent);
298
299 template<typename T = UBaseCarlaMovementComponent>
301 {
302 return Cast<T>(BaseMovementComponent);
303 }
304
305 /// @}
306 // ===========================================================================
307 /// @name Overriden from AActor
308 // ===========================================================================
309 /// @{
310
311protected:
312
313 virtual void BeginPlay() override;
314 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);
315
316 UFUNCTION(BlueprintImplementableEvent)
317 void RefreshLightState(const FVehicleLightState &VehicleLightState);
318
319 UFUNCTION(BlueprintCallable, CallInEditor)
320 void AdjustVehicleBounds();
321
322 UPROPERTY(Category="Door Animation", EditAnywhere, BlueprintReadWrite)
323 TArray<FName> ConstraintComponentNames;
324
325 UPROPERTY(Category="Door Animation", EditAnywhere, BlueprintReadWrite)
326 float DoorOpenStrength = 100.0f;
327
328 UFUNCTION(BlueprintCallable, CallInEditor)
329 void ResetConstraints();
330
331private:
332
333 /// Current state of the vehicle controller (for debugging purposes).
334 UPROPERTY(Category = "AI Controller", VisibleAnywhere)
336
337 UPROPERTY(Category = "CARLA Wheeled Vehicle", EditAnywhere)
338 UVehicleVelocityControl* VelocityControl;
339
340 struct
341 {
345 }
347
351
352 bool bAckermannControlActive = false;
354
355 float RolloverBehaviorForce = 0.35;
356 int RolloverBehaviorTracker = 0;
357 float RolloverFlagTime = 5.0f;
358
360
361public:
362 UPROPERTY(Category = "CARLA Wheeled Vehicle", EditDefaultsOnly)
363 float DetectionSize { 750.0f };
364
365 UPROPERTY(Category = "CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly)
366 FBox FoliageBoundingBox;
367
368 UPROPERTY(Category = "CARLA Wheeled Vehicle", EditAnywhere)
369 UBoxComponent *VehicleBounds;
370
371 UFUNCTION()
372 FBox GetDetectionBox() const;
373
374 UFUNCTION()
375 float GetDetectionSize() const;
376
377 UFUNCTION()
378 void UpdateDetectionBox();
379
380 UFUNCTION()
381 const TArray<int32> GetFoliageInstancesCloseToVehicle(const UInstancedStaticMeshComponent* Component) const;
382
383 UFUNCTION(BlueprintCallable)
384 void DrawFoliageBoundingBox() const;
385
386 UFUNCTION()
387 FBoxSphereBounds GetBoxSphereBounds() const;
388
389 UFUNCTION()
390 bool IsInVehicleRange(const FVector& Location) const;
391
392 /// Set the rotation of the car wheels indicated by the user
393 /// 0 = FL_VehicleWheel, 1 = FR_VehicleWheel, 2 = BL_VehicleWheel, 3 = BR_VehicleWheel
394 /// NOTE : This is purely aesthetic. It will not modify the physics of the car in any way
395 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
396 void SetWheelSteerDirection(EVehicleWheelLocation WheelLocation, float AngleInDeg);
397
398 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
399 float GetWheelSteerAngle(EVehicleWheelLocation WheelLocation);
400
401 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
402 void OpenDoor(const EVehicleDoor DoorIdx);
403
404 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
405 void CloseDoor(const EVehicleDoor DoorIdx);
406
407 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
408 void OpenDoorPhys(const EVehicleDoor DoorIdx);
409
410 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
411 void CloseDoorPhys(const EVehicleDoor DoorIdx);
412
413 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
414 void RecordDoorChange(const EVehicleDoor DoorIdx, const bool bIsOpen);
415
416 virtual FVector GetVelocity() const override;
417
418//-----CARSIM--------------------------------
419 UPROPERTY(Category="CARLA Wheeled Vehicle", EditAnywhere)
420 float CarSimOriginOffset = 150.f;
421//-------------------------------------------
422
423 UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere)
424 bool bIsNWVehicle = false;
425
426 void SetRolloverFlag();
427
428 carla::rpc::VehicleFailureState GetFailureState() const;
429
430 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
431 static FRotator GetPhysicsConstraintAngle(UPhysicsConstraintComponent* Component);
432 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
433 static void SetPhysicsConstraintAngle(
434 UPhysicsConstraintComponent*Component, const FRotator &NewAngle);
435
436private:
437
438 UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere)
439 bool bPhysicsEnabled = true;
440
441 // Small workarround to allow optional CarSim plugin usage
442 UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
443 UBaseCarlaMovementComponent * BaseMovementComponent = nullptr;
444
445 UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
446 TArray<UPhysicsConstraintComponent*> ConstraintsComponents;
447
448 UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
449 TMap<UPhysicsConstraintComponent*, UPrimitiveComponent*> ConstraintDoor;
450
451 // container of the initial transform of the door, used to reset its position
452 UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
453 TMap<UPrimitiveComponent*, FTransform> DoorComponentsTransform;
454
455 UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
456 TMap<UPrimitiveComponent*, UPhysicsConstraintComponent*> CollisionDisableConstraints;
457
458 /// Rollovers tend to have too much angular velocity, resulting in the vehicle doing a full 360º flip.
459 /// This function progressively reduces the vehicle's angular velocity so that it ends up upside down instead.
460 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
461 void ApplyRolloverBehavior();
462
463 void CheckRollover(const float roll, const std::pair<float, float> threshold_roll);
464
465 void AddReferenceToManager();
466 void RemoveReferenceToManager();
467
468
469 FTimerHandle TimerHandler;
470public:
471 float SpeedAnim { 0.0f };
472 float RotationAnim { 0.0f };
473
474 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
475 float GetSpeedAnim() const { return SpeedAnim; }
476
477 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
478 void SetSpeedAnim(float Speed) { SpeedAnim = Speed; }
479
480 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
481 float GetRotationAnim() const { return RotationAnim; }
482
483 UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
484 void SetRotationAnim(float Rotation) { RotationAnim = Rotation; }
485};
ECarlaWheeledVehicleState
State of a ACarlaWheeledVehicle, to be displayed in editor for debugging purposes.
EVehicleWheelLocation
EVehicleDoor
Type of door to open/close
EVehicleInputPriority
Base class for CARLA wheeled vehicles.
FVehicleLightState LightState
void SetAIVehicleState(ECarlaWheeledVehicleState InState)
T * GetCarlaMovementComponent() const
FVehicleControl LastAppliedControl
FVehicleAckermannControl LastAppliedAckermannControl
struct ACarlaWheeledVehicle::@1 InputControl
FVehiclePhysicsControl LastPhysicsControl
bool IsAckermannControlActive() const
void ApplyAckermannControllerSettings(const FAckermannControllerSettings &AckermannControllerSettings)
FAckermannController AckermannController
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133