This poject is the final project assignment of introduction to Self Driving Cars course offered by University of Toronto on Coursera. The "controller2d.py" file contains a controller object. I implemented the controller in the update_controls method.
In this project, I implemented a controller in Python and used it to drive a car autonomously around a track in Carla Simulator. The output of the controller will be the vehicle throttle, brake and steering angle commands. The throttle and brake come from the Longitudinal speed control and the steering comes from the Lateral Control.
For longitudinal control I implemented a PID Controller, This PID controller will take the desired speed as the reference and outputs throttle and brake.
a PID controller consists of three components. First, a pure gain Kp that scales the vehicle acceleration based on the speed error. This ensures that the vehicle is accelerating in the correct direction with the magnitude proportional to the error.
Second, in integral term KI sets up the output based on accumulated past errors. This ensures the steady steed errors are eliminated for ramp referencing.
Finally, the derivative term KD dampens the overshoot caused by the integration term.
To complete the longitudinal control, we must convert the acceleration output from the PID controller into throttle and brake commands. For simplicity, positive outputs will be throttle and negative outputs will correspond to brake.
The basic idea behind a PID controller is to read a sensor, then compute the desired actuator output by calculating proportional, integral, and derivative responses and summing those three components to compute the output.
- The proportional part is easiest to understand: The output of the proportional part is the product of gain and measured error e. Hence, larger proportional gain or error makes for greater output from the proportional part. Setting the proportional gain too high causes a controller to repeatedly overshoot the setpoint, leading to oscillation.
- e = r – y e: error, r: user input reference, y: actual measured output
- Proportional part = Kp * e Kp: input gain for P – Regulator (Proportional Regulator)
# Proportional part
Proportional = kp * e_current
- Think of the integral as a basket in which the loop stores all measured errors in E. Remember that error can be positive or negative, so sometimes error fills the basket (when positive error is added to positive error or negative error is added to negative) and sometimes it empties the basket — as when positive error is added to negative, or vice versa.
- collect all of these errors over time (take integral over time).
- Integral part = Ki * E
where E = E + e(current) * ∆t, the loop stores all measured errors in E
# integral part
self.vars.E = self.vars.E + e_current * delta_t
integral = ki * self.vars.E
- the derivative is looking at the rate of change of the error. The more error changes, the larger the derivative factor becomes.
- Kd have to be less than 1.
- Derivative part = Kd * ((e(current) - e(previous)) / ∆t)
# derivative part
if delta_t == 0:
derivate = 0
else:
derivate = kd * ((e_current - self.vars.e_previous)/delta_t)
u = Proportional + integral + derivate # u : input signal
if u >= 0:
throttle_output = u
brake_output = 0
elif u < 0:
throttle_output = 0
brake_output = -u
The rise time: the time it takes to reach 90% of the reference value.
Th overshoot: the maximum percentage the output exceeds this reference.
The settling time: the time to settle to within 5% of the reference.
The steady state error: The error between te output and the reference at steady state.
For lateral control, I implemented the Stanley controller. The Stanley method is the path tracking approach used by Stanford University’s autonomous vehicle entry in the DARPA Grand Challenge, Stanley. The Stanley method is a nonlinear feedback function of the cross track error, measured from the center of the front axle to the nearest path point.
- e: Cross track error
- θ_c: Heading of the vehicle (Yaw).
- ψ + θ_c: Heading of the path (path Yaw).
- ψ: Heading error.
- δ: Stearing angle.
- v: Current forward speed (meters per second).
- x_c: Current X position (meters)
- y_c: Current Y position (meters)
When e is non-zero, the second term adjusts δ such that the intended trajectory intersects the path tangent at kv(t) units from the front axle.