25constexpr std::size_t kErrorBufferMaxLen = 10;
30void TrimDeque(std::deque<T> &q, std::size_t max_len) {
31 while (q.size() > max_len) {
44 float K_P,
float K_I,
float K_D,
float dt)
45 : _vehicle(
std::move(vehicle)),
54 std::cout <<
"Current speed = " << current_speed_kmh <<
'\n';
56 return PidControl(target_speed_kmh, current_speed_kmh);
60 float target_speed_kmh,
float current_speed_kmh) {
61 const float error = target_speed_kmh - current_speed_kmh;
74 const float u = (
_k_p * error) + (
_k_d * de) + (
_k_i * ie);
75 return std::clamp(u, -1.0f, 1.0f);
79 float K_P,
float K_I,
float K_D,
float dt) {
93 float K_P,
float K_I,
float K_D,
float dt)
94 : _vehicle(
std::move(vehicle)),
102 const auto vehicle_transform =
_vehicle->GetTransform();
105 const auto fwd = vehicle_transform.GetForwardVector();
106 const float vx = fwd.x;
107 const float vy = fwd.y;
112 const auto w_tran = waypoint->GetTransform();
113 const auto r_vec = w_tran.GetRightVector();
115 w_tran.location.x +
_offset * r_vec.x,
116 w_tran.location.y +
_offset * r_vec.y,
119 w_loc = waypoint->GetTransform().location;
122 const float wx = w_loc.
x - vehicle_transform.location.x;
123 const float wy = w_loc.
y - vehicle_transform.location.y;
127 const float w_len = std::sqrt(wx * wx + wy * wy);
128 const float v_len = std::sqrt(vx * vx + vy * vy);
129 const float wv_linalg = w_len * v_len;
132 if (wv_linalg == 0.0f) {
136 const float cos_theta = std::clamp((wx * vx + wy * vy) / wv_linalg,
138 dot = std::acos(cos_theta);
142 const float cross_z = vx * wy - vy * wx;
143 if (cross_z < 0.0f) {
148 TrimDeque(
_e_buffer, kErrorBufferMaxLen);
159 const float u = (
_k_p * dot) + (
_k_d * de) + (
_k_i * ie);
160 return std::clamp(u, -1.0f, 1.0f);
168 float K_P,
float K_I,
float K_D,
float dt) {
188 _max_throttle(max_throttle),
189 _max_brake(max_brake),
190 _max_steering(max_steering),
191 _past_steering(vehicle ? vehicle->GetControl().steer : 0.0f),
192 _lon_controller(vehicle, longitudinal.K_P, longitudinal.K_I,
193 longitudinal.K_D, longitudinal.dt),
194 _lat_controller(vehicle, offset, lateral.K_P, lateral.K_I,
195 lateral.K_D, lateral.dt) {}
198 float target_speed_kmh,
204 if (acceleration >= 0.0f) {
206 control.
brake = 0.0f;
220 if (current_steering >= 0.0f) {
226 control.
steer = steering;
void ChangeParameters(float K_P, float K_I, float K_D, float dt)
PIDLateralController(SharedPtr< client::Vehicle > vehicle, float offset=0.0f, float K_P=1.0f, float K_I=0.0f, float K_D=0.0f, float dt=0.03f)
float RunStep(SharedPtr< client::Waypoint > waypoint)
Returns a steering command in the range [-1, 1].
std::deque< float > _e_buffer
SharedPtr< client::Vehicle > _vehicle
void SetOffset(float offset)
float PidControl(float target_speed_kmh, float current_speed_kmh)
SharedPtr< client::Vehicle > _vehicle
std::deque< float > _error_buffer
PIDLongitudinalController(SharedPtr< client::Vehicle > vehicle, float K_P=1.0f, float K_I=0.0f, float K_D=0.0f, float dt=0.03f)
void ChangeParameters(float K_P, float K_I, float K_D, float dt)
float RunStep(float target_speed_kmh, bool debug=false)
Returns the throttle/brake command (clamped to [-1, 1]).
VehiclePIDController(SharedPtr< client::Vehicle > vehicle, const LateralArgs &lateral, const LongitudinalArgs &longitudinal, float offset=0.0f, float max_throttle=0.75f, float max_brake=0.30f, float max_steering=0.80f)
PIDLongitudinalController _lon_controller
void SetOffset(float offset)
void ChangeLateralPID(const LateralArgs &args)
void ChangeLongitudinalPID(const LongitudinalArgs &args)
PIDLateralController _lat_controller
rpc::VehicleControl RunStep(float target_speed_kmh, SharedPtr< client::Waypoint > wp)
Drives towards wp at target_speed_kmh and returns the resulting VehicleControl.
float GetSpeed(SharedPtr< client::Actor > actor)
Compute speed of an actor in km/h.
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr