Skip to content

Udacity's Self-Driving Car Nano Degree - Project 8: PID Controller

License

Notifications You must be signed in to change notification settings

apf99/PID-Control-Project

Repository files navigation

PID Control Project

####Udacity's Self-Driving Car Engineer Nanodegree Program


##Overview

The idea behind this project is to implement a PID controller as described in the lectures and to use it to control the steering of a simulated vehicle in order to allow it to successully drive a windy track. This is achieved by tuning the controller's hyperparameters based upon using the vehicle's cross-track error (CTE) as an input. We separately track the CTE, its derivative and its integral and use all three in determing a control output in order to minimize the collective error.

Additionally, I implemented a PID controller to control the vehicles speed via throttle control because I noticed that the vehicle's ability to follow the centerline of the road tended to be a strong function of the vehicle's speed. This controller noticeably improved the performance of the vehicle by allowing it to speed up on the easy parts of the track and slow down on the tricky parts.

##Results

In tuning the PID controller, I staarted with the steering controller while keeping the throttle setting fixed to 30%. I discovered that the first order of business was setting tuning the P control while setting both I and D to zero initially.

Increasing the P-constant cause the vehicle to oscilate its position about the centerline of the road. I picked a value that cause the vehicle to be responsive - so much so that the vehicle ran of the track at some point to a dynamic instability caused by the wild steering oscillations about the centerline.

![Screenshot 2019-05-13 13.26.19](Images/Screenshot 2019-05-13 13.26.19.png)

Next, I tuned the D-constant as a quick test showed me the this had a great effect on damping out the oscillations. With a little manual tuning, I was very quickly able to get the vehicle to drive well around the track.

![Screenshot 2019-05-13 13.26.31](Images/Screenshot 2019-05-13 13.26.31.png)

At this point, I increased the I-constant very slightly because I found that this made the vehicle sit slightly closer to the centerline of the road. This is really just a slight effect, but tends to be more noticeable after long, straight portions of the road. Setting too high an I-constant caused the car to oscillate wildly.

![Screenshot 2019-05-13 13.26.40](Images/Screenshot 2019-05-13 13.26.40.png)

I have included in the repository video of the tuning processing using P-only control, then PD control and then the final PID controller. These are available for download in the VideoClips directory.

The car was driving well at this point, however, at some sections of the track, on the sharp bends, it was getting a little too close to the edge of the road. Consueqently, I decided to add a PID controller for the throttle control. This worked exactly the same way as the steering control EXCEPT FOR the fact that we used the absolute value of the CTE in the case of throttle control. This is due to the fact that the throttle should be reduced regardless of whether the CTE is positive or negative. There is a symmetry that applies to throttle control that does not apply to steering control which is directional. This is show in the following differences in the implementation of each controller (lines 65-66 and 68-69 in main.cpp).

	s_pid.UpdateError(cte);
	double steer_value = -s_pid.TotalError();  
	t_pid.UpdateError(fabs(cte));
	double throttle_value = 0.3 - t_pid.TotalError();

The reference throttle setting was set at 30%, but it was found that with a little more aggressive tuning could handle throttle settings of 50% although the car drove wildly! Possibly using something like Twiddle to fine tuned the PID parameters would have helped a little, but I found that everything worked pretty well.

The constant for the throttle controller were tuning very quickly manually in a similar fashion to what was done for the steering controller. FIrst a fairly aggressive P-value was found. This immediately had a pronounced effect by reducing the speed aggressively on the sharp cornered sections of the track and aggressively speeding up on the straight portions. This was tempered by adding a little D-control. It was found that no I-control was needed for the throttle so this was left at 0.

Good final values for the PID constants for the steering and throttle controllers were found to be:

Steering PID Constants: [0.1, 0.0005, 0.35]

Throttle PID Constants: [0.25, 0.0, 0.005]


##Appendix

Dependencies

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.

Basic Build Instructions

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

Tips for setting up your environment can be found here

Editor Settings

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)

Code Style

Please (do your best to) stick to Google's C++ style guide.

Project Instructions and Rubric

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.

Hints!

  • 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.

Call for IDE Profiles Pull Requests

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./

How to write a README

A well written README file can enhance your project and portfolio. Develop your abilities to create professional README files by completing this free course.

About

Udacity's Self-Driving Car Nano Degree - Project 8: PID Controller

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages