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 
12 #include "Vehicle/VehicleControl.h"
16 #include "VehicleVelocityControl.h"
17 #include "WheeledVehicleMovementComponent4W.h"
18 #include "VehicleAnimInstance.h"
19 #include "PhysicsEngine/PhysicsConstraintComponent.h"
21 
22 #include "CoreMinimal.h"
23 
24 //-----CARSIM--------------------------------
25 #ifdef WITH_CARSIM
26 #include "CarSimMovementComponent.h"
27 #endif
28 //-------------------------------------------
29 
30 #include "CarlaWheeledVehicle.generated.h"
31 
32 class UBoxComponent;
33 
34 UENUM()
35 enum class EVehicleWheelLocation : uint8 {
36 
37  FL_Wheel = 0,
38  FR_Wheel = 1,
39  BL_Wheel = 2,
40  BR_Wheel = 3,
41  //Use for bikes and bicycles
42  Front_Wheel = 0,
43  Back_Wheel = 1,
44 };
45 
46 /// Type of door to open/close
47 // When adding new door types, make sure that All is the last one.
48 UENUM(BlueprintType)
49 enum class EVehicleDoor : uint8 {
50  FL = 0,
51  FR = 1,
52  RL = 2,
53  RR = 3,
54  Hood = 4,
55  Trunk = 5,
56  All = 6
57 };
58 
59 /// Base class for CARLA wheeled vehicles.
60 UCLASS()
61 class CARLA_API ACarlaWheeledVehicle : public AWheeledVehicle
62 {
63  GENERATED_BODY()
64 
65  // ===========================================================================
66  /// @name Constructor and destructor
67  // ===========================================================================
68  /// @{
69 public:
70 
71  ACarlaWheeledVehicle(const FObjectInitializer &ObjectInitializer);
72 
74 
75  /// @}
76  // ===========================================================================
77  /// @name Get functions
78  // ===========================================================================
79  /// @{
80 public:
81 
82  /// Vehicle control currently applied to this vehicle.
83  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
84  const FVehicleControl &GetVehicleControl() const
85  {
86  return LastAppliedControl;
87  }
88 
89  /// Transform of the vehicle. Location is shifted so it matches center of the
90  /// vehicle bounds rather than the actor's location.
91  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
92  FTransform GetVehicleTransform() const
93  {
94  return GetActorTransform();
95  }
96 
97  /// Forward speed in cm/s. Might be negative if goes backwards.
98  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
99  float GetVehicleForwardSpeed() const;
100 
101  /// Orientation vector of the vehicle, pointing forward.
102  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
103  FVector GetVehicleOrientation() const;
104 
105  /// Active gear of the vehicle.
106  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
107  int32 GetVehicleCurrentGear() const;
108 
109  /// Transform of the vehicle's bounding box relative to the vehicle.
110  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
111  FTransform GetVehicleBoundingBoxTransform() const;
112 
113  /// Extent of the vehicle's bounding box.
114  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
115  FVector GetVehicleBoundingBoxExtent() const;
116 
117  /// Get vehicle's bounding box component.
118  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
119  UBoxComponent *GetVehicleBoundingBox() const
120  {
121  return VehicleBounds;
122  }
123 
124  /// Get the maximum angle at which the front wheel can steer.
125  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
126  float GetMaximumSteerAngle() const;
127 
128  /// @}
129  // ===========================================================================
130  /// @name AI debug state
131  // ===========================================================================
132  /// @{
133 public:
134 
135  /// @todo This function should be private to AWheeledVehicleAIController.
137  {
138  State = InState;
139  }
140 
141  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
142  ECarlaWheeledVehicleState GetAIVehicleState() const
143  {
144  return State;
145  }
146 
147  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
148  FVehiclePhysicsControl GetVehiclePhysicsControl() const;
149 
150  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
151  void RestoreVehiclePhysicsControl();
152 
153  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
154  FVehicleLightState GetVehicleLightState() const;
155 
156  void ApplyVehiclePhysicsControl(const FVehiclePhysicsControl &PhysicsControl);
157 
158  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
159  void SetSimulatePhysics(bool enabled);
160 
161  void SetWheelCollision(UWheeledVehicleMovementComponent4W *Vehicle4W, const FVehiclePhysicsControl &PhysicsControl);
162 
163  void SetVehicleLightState(const FVehicleLightState &LightState);
164 
165  UFUNCTION(BlueprintNativeEvent)
166  bool IsTwoWheeledVehicle();
168  return false;
169  }
170 
171  /// @}
172  // ===========================================================================
173  /// @name Vehicle input control
174  // ===========================================================================
175  /// @{
176 public:
177 
178  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
179  void ApplyVehicleControl(const FVehicleControl &Control, EVehicleInputPriority Priority)
180  {
181  if (InputControl.Priority <= Priority)
182  {
183  InputControl.Control = Control;
184  InputControl.Priority = Priority;
185  }
186  }
187 
188  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
189  void ActivateVelocityControl(const FVector &Velocity);
190 
191  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
192  void DeactivateVelocityControl();
193 
194  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
195  void ShowDebugTelemetry(bool Enabled);
196 
197  /// @todo This function should be private to AWheeledVehicleAIController.
198  void FlushVehicleControl();
199 
200  /// @}
201  // ===========================================================================
202  /// @name DEPRECATED Set functions
203  // ===========================================================================
204  /// @{
205 public:
206 
207  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
208  void SetThrottleInput(float Value);
209 
210  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
211  void SetSteeringInput(float Value);
212 
213  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
214  void SetBrakeInput(float Value);
215 
216  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
217  void SetReverse(bool Value);
218 
219  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
220  void ToggleReverse()
221  {
222  SetReverse(!LastAppliedControl.bReverse);
223  }
224 
225  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
226  void SetHandbrakeInput(bool Value);
227 
228  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
229  void HoldHandbrake()
230  {
231  SetHandbrakeInput(true);
232  }
233 
234  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
235  void ReleaseHandbrake()
236  {
237  SetHandbrakeInput(false);
238  }
239 
240  TArray<float> GetWheelsFrictionScale();
241 
242  void SetWheelsFrictionScale(TArray<float> &WheelsFrictionScale);
243 
244  void SetCarlaMovementComponent(UBaseCarlaMovementComponent* MoementComponent);
245 
246  template<typename T = UBaseCarlaMovementComponent>
248  {
249  return Cast<T>(BaseMovementComponent);
250  }
251 
252  /// @}
253  // ===========================================================================
254  /// @name Overriden from AActor
255  // ===========================================================================
256  /// @{
257 
258 protected:
259 
260  virtual void BeginPlay() override;
261  virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);
262 
263  UFUNCTION(BlueprintImplementableEvent)
264  void RefreshLightState(const FVehicleLightState &VehicleLightState);
265 
266  UFUNCTION(BlueprintCallable, CallInEditor)
267  void AdjustVehicleBounds();
268 
269  UPROPERTY(Category="Door Animation", EditAnywhere, BlueprintReadWrite)
270  TArray<FName> ConstraintComponentNames;
271 
272  UPROPERTY(Category="Door Animation", EditAnywhere, BlueprintReadWrite)
273  float DoorOpenStrength = 100.0f;
274 
275  UFUNCTION(BlueprintCallable, CallInEditor)
276  void ResetConstraints();
277 
278 private:
279 
280  /// Current state of the vehicle controller (for debugging purposes).
281  UPROPERTY(Category = "AI Controller", VisibleAnywhere)
283 
284  UPROPERTY(Category = "CARLA Wheeled Vehicle", EditAnywhere)
285  UBoxComponent *VehicleBounds;
286 
287  UPROPERTY(Category = "CARLA Wheeled Vehicle", EditAnywhere)
288  UVehicleVelocityControl* VelocityControl;
289 
290  struct
291  {
295  }
296  InputControl;
297 
300 
301 public:
302 
303  /// Set the rotation of the car wheels indicated by the user
304  /// 0 = FL_VehicleWheel, 1 = FR_VehicleWheel, 2 = BL_VehicleWheel, 3 = BR_VehicleWheel
305  /// NOTE : This is purely aesthetic. It will not modify the physics of the car in any way
306  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
307  void SetWheelSteerDirection(EVehicleWheelLocation WheelLocation, float AngleInDeg);
308 
309  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
310  float GetWheelSteerAngle(EVehicleWheelLocation WheelLocation);
311 
312  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
313  void OpenDoor(const EVehicleDoor DoorIdx);
314 
315  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
316  void CloseDoor(const EVehicleDoor DoorIdx);
317 
318  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
319  void OpenDoorPhys(const EVehicleDoor DoorIdx);
320 
321  UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
322  void CloseDoorPhys(const EVehicleDoor DoorIdx);
323 
324  virtual FVector GetVelocity() const override;
325 
326 //-----CARSIM--------------------------------
327  UPROPERTY(Category="CARLA Wheeled Vehicle", EditAnywhere)
328  float CarSimOriginOffset = 150.f;
329 //-------------------------------------------
330 
331 private:
332 
333  UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere)
334  bool bPhysicsEnabled = true;
335 
336  // Small workarround to allow optional CarSim plugin usage
337  UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
338  UBaseCarlaMovementComponent * BaseMovementComponent = nullptr;
339 
340  UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
341  TArray<UPhysicsConstraintComponent*> ConstraintsComponents;
342 
343  UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
344  TMap<UPhysicsConstraintComponent*, UPrimitiveComponent*> ConstraintDoor;
345 
346  // container of the initial transform of the door, used to reset its position
347  UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
348  TMap<UPrimitiveComponent*, FTransform> DoorComponentsTransform;
349 
350  UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
351  TMap<UPrimitiveComponent*, UPhysicsConstraintComponent*> CollisionDisableConstraints;
352 
353 };
void SetAIVehicleState(ECarlaWheeledVehicleState InState)
T * GetCarlaMovementComponent() const
FVehicleLightState LightState
ECarlaWheeledVehicleState
State of a ACarlaWheeledVehicle, to be displayed in editor for debugging purposes.
virtual bool IsTwoWheeledVehicle_Implementation()
FVehicleControl LastAppliedControl
EVehicleInputPriority
EVehicleWheelLocation
FVehiclePhysicsControl LastPhysicsControl
EVehicleDoor
Type of door to open/close.
Base class for CARLA wheeled vehicles.