Code Monkey home page Code Monkey logo

pid_controller_cpp's Introduction

PID Controller in C++11

Simulator Settings Used

  • Screen Resolution: 640x480
  • Graphics Quality: Fastest

Reflection

A PID controller is relatively simple to implement, the challenging part is choosing proper coefficients (Kp, Ki, Kd) that stabilize the system as a whole.

  • P = Proportional (I like to think of it as the Present)
  • I = Integral (I like to think of it as the past)
  • D = Derivative (I like to think of it as the future)

Each time step, the simulator gives you CTE (cross track error), speed, and steering angle. The CTE is how far the car drifts from the center of the road. The mission was to adjust the coefficients (Kp, Ki, Kd) so that it minimized the CTE.

I've only implemented a controller for the steering angle and kept the speed constant. Hence my controller would calculate the steering angle as follows:

double PID::steering() {
  double steer = -Kp*p_error - Ki*i_error  - Kd*d_error;

  if (steer < MIN_STEER)
    steer = MIN_STEER; //-1

  if (steer > MAX_STEER)
    steer = MAX_STEER; //1

  return steer;
}
  • -Kp*p_error is responsible for keeping track of the present error. It responds in proportion to the CTE.

  • -Ki*i_error is the past, this is responsible for correcting system biases.

  • - Kd*d_error is responsible for keeping track of the future. How will the error change in the future? This is needed mostly to reduce oscillation that is usually caused by having just a P controller with high Kp gain.

Since the simulator calculates the CTE for me, I can handle the rest and compute the needed errors as follows:

void PID::UpdateError(double cte) {
  p_error = cte;
  if (prev_cte == numeric_limits<double>().max())
    prev_cte = cte;

  d_error = cte - prev_cte;
  prev_cte = cte;

  i_error += cte;
}

How the hyperparameters were chosen.

The coefficients where chosen using a manual twiddle algorithm. I first started with just the Kp. I reduced the speed and set Kp such that I had min zig-zag. Then I introduced Kd to reduce the zig-zag even more. At this point, I'd slowly bump the speed and observe the car. If it starts to zig-zag, I'd repeat the process. Ki seems to only make the stability worse, do I left it at zero. This in essentially made my PID controller a PD controller!

Future improvements

  1. Learn how to develop a decent car model to automate the tuning outside of the simulator.
    • I could of automated the twiddle algorithm using the simulator but it was faster to just do twiddle manually.
  2. Improve the PID controller to handle speed.

Dependencies

Basic Build Instructions

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

pid_controller_cpp's People

Contributors

mez avatar

Watchers

 avatar James Cloos 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.