Self-Driving Car Engineer Nanodegree Program
The goal of this project is to build a PID (proportional, integral, derivative) controller for a car and simulate the car around a track. The car has to stay in lane, use the PID control logic to properly negotiate the turns on the road by turning left and right as requried and complete a complete loop.
The simulator sends the value of cte (cross track error), speed and steering_angle. We only make use of the cte and calculate the value of the steering_angle and throttle using two PID controllers.
Kp = 0.3 -> A low value of proportional coefficient was picked because during sharp turns, a small value ensures smoother transition. A large value results in more extreme oscillations.
Ki = 0 -> The integral coefficient just adds up all the CTE values. In this simulator, the CTE goes up a lot during a turn. When the integral control is applied at this stage, the car completely goes out of control due to a large correction in both steer_value and throttle value that are generated by the integral coefficient. So, in this project the integral coefficient was set to 0.
Kd = 0.8 -> A higher value of derivative coefficient was chosen because it helps a lot with smoothing out after making the proportional correction. Because of this, if the vehicle is closer to the center, the steer_value is gradually reduced and the throttle is also reduced.
fabs(throttle_value) has to be used because if there is a negative value, the car can go in reverse.
straight road means low CTE and low steer_value and throttle (speed) values.
In the video, we can see that during a sharp right turn -> stright road -> left turn -> straight scenario, the car velocity goes from 12 mph -> 18 mph -> 10 mph -> 24 mph - 15 mph. This is all due to the CTE feedback from the simualtor.
The final parameters were mostly chosen by trial and error observing the behavior of the car and going through the lectures to understand the contribution of each of the parameters Kp, Kd and Ki. I was able to determine that the car can be tuned without the use of twiddle for this project. I did try to implement some code that roughly tries to mimic twiddle but was not able to notice the differences of using this.
When the road is straight, the CTE is very low and that results in a small steer_value and throttle value. Due to this the car slows down considerably to 2.5 mph.
The car approaches a left curve with 4 mph and as the curve increases, the CTE goes up and the the resulting PD values increase as well. Due to this the steer_value and throttle value increase. The car steers more and also moves faster to upto 12 mph at the peak of the turn.
Similarly for an sharp right turn, the speed increases from 11 mph to 18 mph at the peak and then settles at 16 mph.
pid.h and pid.cpp classes have the main logic and main.cpp has the code to run the web socket by connecting it to the simulator. In the main class, the pid objects are initialized, a new steering_angle and throttle value is calculated and passed back to the simulator. The initial values used are Kp=0.3, Ki=0, Kd = 0.8 for both the PID controllers (line 77,78 in main.cpp) Kp - proportional coefficient Ki - integral coefficient Kd - derivative coefficient
main.cpp lines 80-84 -> update of steer and throttle values main.cpp lines 93-125 -> commented code for twiddle tests pid.cpp lines 13-26 -> initialization pid.cpp lines 28-33 -> update of proportional, integral and differential errors pid.cpp lines 35-39 -> update of steer and throttle using the Kp, Ki and Kd coeffients and cte pid.cpp lines 41-114 -> commented code for twiddle tests
The program was run from the build directory using the command ./pid and starting the simulator with GPU mode enabled. Here is a sample command line during the run.
Finally when everything seems to work, it seems like magic but its been a fun project and I learned a lot.
- cmake >= 3.5
- All OSes: click here for installation instructions
- make >= 4.1(mac, linux), 3.81(Windows)
- Linux: make is installed by default on most Linux distros
- Mac: install Xcode command line tools to get make
- Windows: Click here for installation instructions
- gcc/g++ >= 5.4
- Linux: gcc / g++ is installed by default on most Linux distros
- Mac: same deal as make - [install Xcode command line tools]((https://developer.apple.com/xcode/features/)
- Windows: recommend using MinGW
- uWebSockets
- Run either
./install-mac.sh
or./install-ubuntu.sh
. - If you install from source, checkout to commit
e94b6e1
, i.e.Some function signatures have changed in v0.14.x. See this PR for more details.git clone https://github.com/uWebSockets/uWebSockets cd uWebSockets git checkout e94b6e1
- Run either
- Simulator. You can download these from the project intro page in the classroom.
Fellow students have put together a guide to Windows set-up for the project here if the environment you have set up for the Sensor Fusion projects does not work for this project. There's also an experimental patch for windows in this PR.
- Clone this repo.
- Make a build directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it:
./pid
.
Tips for setting up your environment can be found here
We've purposefully kept editor configuration files out of this repo in order to keep it as simple and environment agnostic as possible. However, we recommend using the following settings:
- indent using spaces
- set tab width to 2 spaces (keeps the matrices in source code aligned)
Please (do your best to) stick to Google's C++ style guide.
Note: regardless of the changes you make, your project must be buildable using cmake and make!
More information is only accessible by people who are already enrolled in Term 2 of CarND. If you are enrolled, see the project page for instructions and the project rubric.
- You don't have to follow this directory structure, but if you do, your work will span all of the .cpp files here. Keep an eye out for TODOs.
Help your fellow students!
We decided to create Makefiles with cmake to keep this project as platform agnostic as possible. Similarly, we omitted IDE profiles in order to we ensure that students don't feel pressured to use one IDE or another.
However! I'd love to help people get up and running with their IDEs of choice. If you've created a profile for an IDE that you think other students would appreciate, we'd love to have you add the requisite profile files and instructions to ide_profiles/. For example if you wanted to add a VS Code profile, you'd add:
- /ide_profiles/vscode/.vscode
- /ide_profiles/vscode/README.md
The README should explain what the profile does, how to take advantage of it, and how to install it.
Frankly, I've never been involved in a project with multiple IDE profiles before. I believe the best way to handle this would be to keep them out of the repo root to avoid clutter. My expectation is that most profiles will include instructions to copy files to a new location to get picked up by the IDE, but that's just a guess.
One last note here: regardless of the IDE used, every submitted project must still be compilable with cmake and make./
A well written README file can enhance your project and portfolio. Develop your abilities to create professional README files by completing this free course.