Giter Club home page Giter Club logo

carnd-pid-control-project's Introduction

CarND-Controls-PID

Self-Driving Car Engineer Nanodegree Program


Goal

The goal of this project is to implement a PID controller in C++ to control the steering wheel angle of the car such that it is able to drive along the track in the simulator.

PID Control

Sensor measurements regarding the cars position is compared to the position of the desired position, upon which the cross-track-error is derived. The cross-track error (cte) is fed into a PID control loop in order to calculate the necessary control output (the steering wheel angle) that is required to minimize this error.

P component: The proportional component. If the error was cte then a controller consisting only of a proportional component would try to correct the error by applying a control signal of Kp * cte.

D component: Using only a P controller causes the control variable to overshoot the desired set-point. A derivative component which controlling the cte's rate of change can be integrated to counteract the overshooting. This is done by adding a term of Ki * (cte - prev_cte), where prev_cte is the error of the previous timestep.

I component: Using only a PD controller induces a non-zero steady state error (meaning the desired set-point is never reached). To mitigate this an integral component of Ki * sum(cte) is added to the control output in order to eliminate the steady state bias (where sum(cte) is the sum of all previous errors).

Implementation

The implementation of the PID controller is done in the PID.cpp and PID.h file.

In the PID::UpdateError() function the P, I and D error terms are computed based on the cte. Then in the PID::TotalError() function the control output is calculated as the sum of the individual control terms.

double output = -Kp * p_error - Kd * d_error - Ki * i_error;

For the steering wheel controller the control signal is also limited (as per the instructions) to a range of -1 to 1.

The controller is then instantiated in the main.cpp. The control output is passed to the msgJson["steering_angle"] field which is then sent to the simulator.

Tuning

In this project the control parameters Kp, Ki, Kd are tuned manually according to instructions which I have found online.

The main steps are to first tune Kp (setting Ki = 0, Kd = 0). Then the differential portion was added to it until the car was able to drive around the track (with a slight offset to the center). Finally I added the integral component to the controller.

The final parameters that I have found to work were:

Kp = 0.13, Ki = 0.0001, Kd = 1.8.

There are many other ways to tune the controller (Twiddle, Ziegler-Nichols) which can be further explored.

Speed controller

In addition I have also implemented a speed controller. For this I'm setting a target speed based on the steering wheel error. If the absolute value of this error is low, the target speed is set to a higher speed such as 45mph, otherwise it is set to a low value.

The controller parameters for the speed controller are tuned according to the approach described in the previous section and are set to:

Kp = -0.2, Ki = 0, Kd = -0.022

Observations

With the tuned controllers the vehicle is able to complete the lap with an average speed of about 42-43 mph while staying on track. At higher speeds it still shows some slight swinging motion which can probably be mitigated by more careful parameter tuning.

Interestingly when running the simulator on my local machine (macOS) the controller parameters were vastly different. For example if my Kp was even a tiny fraction higher than 0.00004 the car would immediately overshoot the steering and drive off-track. I suspect that there are a number of factors such as the machine/server specifications which influence how the controller works.

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

carnd-pid-control-project's People

Contributors

baumanab avatar citlaligm avatar davidobando avatar domluna avatar htuennermann avatar jieqingshi avatar mvirgo avatar swwelch avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.