CarND-Controls-PID

Self-Driving Car Engineer Nanodegree Program


PID Parameter Tuning

This project is to use PID controller to drive a car. There are P, I, D and error paramters.

double kp = 0.225;  // proportional coefficient
double ki = 0.00001; // integral coefficient
double kd = 3.0; // differential coefficient

Overall the simulator gave data input by uWebSokcets, the key data from simulator is cte.

CTE: Cross Track Error, basically can be understand as the distance of the car to the road track.

Note: There is also the steering angle from the simulator, but it is not been used.

For P.I.D each, the error is calculated as bellow (see PID.cpp # updateError(cte))

d_error = cte - p_error;
p_error = cte;
i_error += cte;

The calculate a total error for each round of incoming message from the simulator. (see PID.cpp # totalError())

// Using: -tau_p * CTE - tau_d * diff_CTE - tau_i * int_CTE
//return -Kp * p_error - Kd * d_error - Ki * i_error;
return Kp * p_error + Kd * d_error + Ki * i_error;

Note: all these algorithms are from the class session.

Another parameter used in the implementation is speed "throttle". To make sure the car can reach some speed, and also not out of track, use bellow algorithm to tune the throttle

double throttle = 0.3;
double abs_cte = fabs(cte);
if (abs_cte <= 0.01) {
    throttle = 0.5;
} else if (abs_cte <= 0.4) {
    throttle = 0.4;
} else {
    throttle = 0.3;
}

This project is to find the appropriate values for three P.I.D. coefficients. And the final total error is calculated with them.

P: If the proportional coefficient is too high, the car starts to oscillate and it cannot converge.
I: Integral coefficient represents the sum of all errors. If it is too high, the car starts moving in circles.
D: Differential coefficient needs to be little big to detect rapid oscillations in the CTE.

All the values of the coefficients are manually and based on many round of tryings, it is little empirical. Bellow values guarantees the car can finish the whole track successfully with an average speed of 40 miles/h.

double kp = 0.225;  // proportional coefficient
double ki = 0.00001; // integral coefficient
double kd = 3.0; // differential coefficient

Here is one video show the PID drive the car on the whole track.

PID driving Video

Warn: From the video, we can see in some curve road, the car oscillations little, although it does not drive off the track, this algorithms still need lot to improve. So this algorithms is NOT safe for real driving.

Improvements

  1. As show in the video, there is still little oscillations during the driving, especially in the curve road
  2. The twiddle algorithm is not included in this implementation, so far only can reach speed at about 40-45 mph.

Dependencies

There's an experimental patch for windows in this PR

Basic Build Instructions

  1. Clone this repo.
  2. Make a build directory: mkdir build && cd build
  3. Compile: cmake .. && make
  4. Run it: ./pid.

In my implementation, I use XCode as IDE. After clone the project from github, at the root of the project folder, run following to generate a XCode project.

cmake -G "Xcode" .