/Model-Predictive-Control

Model Predictive Controller to drive a car in a simulation

Primary LanguageC++MIT LicenseMIT

Model Predictive Control

Model

  • A Kinematic Model is used to represent the car.

State

  • The state of the model has six variables in them:
Variable Description
x Position along the x-axis in the global coordinate system
y Position along the y-axis in the global coordinate system
ψ Orientation angle wrt to the x-axis
v Velocity of the car
cte Cross Track Error - the difference in actual y-axis position and expected
Orientation error - the difference in actual orientation and expected

Actuators

  • There are two actuators for this model.
  • Each of these actuators have their own constraints.
Actuator Constraints Description
δ [-25°, 25°] This is the steering angle. It changes the orientation of the car. Its maximum value can only be 25° clockwise or anti-clockwise.
a [-1, 1] This is acceleration and deceleration. It alters the velocity of the car. It can range between -1 and 1 with negative values denoting deceleration or braking.

Update equations

The update equations for each of the state variables are as follows:

xt+1 = xt + vt * cos(ψt) ∗ dt

yt+1 = yt + vt * sin(ψt) ∗ dt

ψt+1 = ψt + vt / Lf * δt ∗ dt

vt+1 = vt + at ∗ dt

ctet+1 = f(xt) − yt + vt ∗ sin(eψt) ∗ dt

t+1 = ψt - ψdest + (vt / Lf * δt * dt)

Prediction

Timestep length and elapsed duration

  • The prediction part includes calculating actuator values for a specific number of time steps upto a few seconds.
  • These are determined by hyper parameters N and dt.
  • N is the number of time steps and dt is the length of each time step in seconds.
  • N * dt = elapsed duration is the duration up to which we predict actuator values.
  • Choosing a large dt leads to a discretization problem where the actuator values might be very large leading to sudden changes.

Discretization Error

  • Choosing a very small dt will make the actuator values so small that the car won't change orientation or velocity fast enough and will stray from the road.

Very small dt

Fitting the polynomial

  • We fit a 3rd degree polynomial for the waypoints provided. This serves as the reference trajectory.
  • The waypoints are provided with coordinates in the global system.
  • They are transformed into the car's coordinate system before fitting the polynomial.
  • This transformation helps avoid calculations down the line. Since the polynomial is fitted to the transformed waypoints, the predicted points also are in the car's coordinate system.

Latency in transmission

  • There's a 100ms latency in the actuators taking effect on the car.
  • The time step duration is set to 100ms. This way, it becomes easy to model in this latency.
  • To account for this latency we consider the delta0 and a0 values to be two time steps before the current one.
  • This is because it takes 100ms for the change in actuators to happen, and we need another 100ms to observe the state change of the car due to the change in actuators.