CARLA
AckermannController.h
Go to the documentation of this file.
1 // Copyright (c) 2022 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 
11 #include "Vehicle/VehicleControl.h"
12 
13 // Forward declaration
15 
16 class PID
17 {
18  public:
19  PID() = default;
20  PID(float Kp, float Ki, float Kd): Kp(Kp), Ki(Ki), Kd(Kd) {}
21  ~PID() = default;
22 
23  void SetTargetPoint(float Point)
24  {
25  SetPoint = Point;
26  }
27 
28  float Run(float Input, float DeltaTime)
29  {
30  float Error = SetPoint - Input;
31 
32  Proportional = Kp * Error;
33  Integral += Ki * Error * DeltaTime;
34  // Avoid integral windup
35  Integral = FMath::Clamp(Integral, MinOutput, MaxOutput);
36  Derivative = (-Kd * (Input - LastInput)) / DeltaTime;
37 
38  float Out = Proportional + Integral + Derivative;
39  Out = FMath::Clamp(Out, MinOutput, MaxOutput);
40 
41  // Keep track of the state
42  LastError = Out;
43  LastInput = Input;
44 
45  return Out;
46  }
47 
48  void Reset()
49  {
50  Proportional = 0.0f;
51  Integral = 0.0f;
52  Integral = FMath::Clamp(Integral, MinOutput, MaxOutput);
53  Derivative = 0.0f;
54 
55  LastError = 0.0f;
56  LastInput = 0.0f;
57  }
58 
59  public:
60  float Kp = 0.0f;
61  float Ki = 0.0f;
62  float Kd = 0.0f;
63 
64  private:
65  float SetPoint;
66 
67  // Out Limits
68  float MinOutput = -1.0f;
69  float MaxOutput = 1.0f;
70 
71  // Internal state.
72  float Proportional = 0.0f;
73  float Integral = 0.0f;
74  float Derivative = 0.0f;
75 
76  float LastError = 0.0f;
77  float LastInput = 0.0f;
78 };
79 
80 
82 {
83 public:
84 
85  FAckermannController() = default;
87 
88 public:
89 
90  FAckermannControllerSettings GetSettings() const;
91  void ApplySettings(const FAckermannControllerSettings& Settings);
92 
93  void UpdateVehicleState(const ACarlaWheeledVehicle* Vehicle);
94  void UpdateVehiclePhysics(const ACarlaWheeledVehicle* Vehicle);
95 
96  void SetTargetPoint(const FVehicleAckermannControl& AckermannControl);
97  void Reset();
98 
99  void RunLoop(FVehicleControl& Control);
100 
101 private:
102 
103  // Lateral Control
104  void RunControlSteering();
105 
106  // Longitudinal Control
107  bool RunControlFullStop();
108  void RunControlReverse();
109  void RunControlSpeed();
110  void RunControlAcceleration();
111  void UpdateVehicleControlCommand();
112 
113 private:
114 
115  PID SpeedController = PID(0.15f, 0.0f, 0.25f);
116  PID AccelerationController = PID(0.01f, 0.0f, 0.01f);
117 
119 
120  // Target point after aplying restrictions
121  float TargetSteer = 0.0;
122  float TargetSteerSpeed = 0.0;
123  float TargetSpeed = 0.0;
124  float TargetAcceleration = 0.0;
125  float TargetJerk = 0.0;
126 
127  // Restricitions parameters
128  float MaxAccel = 3.0f; // [m/s2]
129  float MaxDecel = 8.0f; // [m/s2]
130 
131  // Control parameters
132  float Steer = 0.0f;
133  float Throttle = 0.0f;
134  float Brake = 0.0f;
135  bool bReverse = false;
136 
137  // Internal control parameters
138  float SpeedControlAccelDelta = 0.0f;
139  float SpeedControlAccelTarget = 0.0f;
140 
141  float AccelControlPedalDelta = 0.0f;
142  float AccelControlPedalTarget = 0.0f;
143 
144  // Simulation state
145  float DeltaTime = 0.0f; // [s]
146 
147  // Vehicle state
148  float VehicleMaxSteering = 0.0f; // [rad]
149 
150  float VehicleSteer = 0.0f; // [rad]
151  float VehicleSpeed = 0.0f; // [m/s]
152  float VehicleAcceleration = 0.0f; // [m/s2]
153 
154  float LastVehicleSpeed = 0.0f; // [m/s]
155  float LastVehicleAcceleration = 0.0f; // [m/s2]
156 };
float SetPoint
float MaxOutput
float LastInput
PID()=default
PID(float Kp, float Ki, float Kd)
float LastError
float MinOutput
void Reset()
float Integral
float Derivative
void SetTargetPoint(float Point)
~PID()=default
FVehicleAckermannControl UserTargetPoint
float Proportional
float Run(float Input, float DeltaTime)
Base class for CARLA wheeled vehicles.