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