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