sgalkin / CarND-T2P4

Udacity CarND PID Controller Project

Home Page:https://www.udacity.com/drive

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PID Controller Project

Udacity - Self-Driving Car NanoDegree CircleCI


Overview

This project implements controlling application for Udacity self-driving car simulator using PID controller mechanism.

The project implements "twiddle" parameters tuning algorithm as well.

Project objective: No tire may leave the drivable portion of the track surface. The car may not pop up onto ledges or roll over any surfaces that would otherwise be considered unsafe (if humans were in the vehicle).

Demo

PID throttle

./pid -p 0.195405 -i 0.004 -d 3.28759 --tKp 0.45372 --tKi 0 --tKd 0.941861

Constant throttle

./pid -p 0.195405 -i 0.004 -d 3.28759 -c 0.4


Usage

Usage:
  pid [options]
Available options:
      --tKd      throttle PID Kd
      --tKi      throttle PID Ki
  -c, --tC       constant throttle
  -i, --sKi      steering angle PID Ki
  -p, --sKp      steering angle PID Kp
      --tKp      throttle PID Kp
  -d, --sKd      steering angle PID Kd
  -t, --twiddle  enable twiddle mode
  -h, --help     print this help screen

Twiddle mode

./pid -t

In order to activate "twiddle" mode the only -t option should be passed via command line.

Operational mode

./pid -p<Kp> -i<Ki> -d<Kd> (-c<C> | --tKp<tp> --tKi<ti> --tKd<kd>)

In order to configure operational mode one might choice between constant throttle (--tC) and throttle PID controller (--tKp, --tKi, and --tKd). Steering angle always controlled by PID (--sKp, --sKi, and --sKd).

Dependencies

Runtime

Tools

Libraries not included into the project

  • uWebSocketIO == v0.13.0
    • Ubuntu/Debian: the repository includes install-ubuntu.sh that can be used to set up and install uWebSocketIO
    • Mac: the repository includes install-mac.sh that can be used to set up and install uWebSocketIO
    • Windows: use either Docker, VMware, or even Windows 10 Bash on Ubuntu

Libraries included into the project

Build

  1. Clone this repo.
  2. mkdir build
  3. cd build
  4. cmake .. -G "Unix Makefiles"
  5. make
  6. make check # -- optional

Protocol

The project uses uWebSocketIO request-response protocol in communicating with the simulator.

INPUT: values provided by the simulator to the c++ program

{
  "cte": "current cross track error",
  "speed": "current speed",
  "steering_angle": "current steering angle"
}

OUTPUT: values provided by the c++ program to the simulator

  • steer - instruct simulator to adjust steering angle and speed
{
  "steering_angle": "steering angle evaluated on current iteration [-1..1]",
  "throttle": "throttle evaluated on current iteration [-1..1]"
}
  • reset - forces simulator to reset state

Model

The project uses set of two PID controllers to update steering angle and throttle.

PID

PID (proportional–integral–derivative) is wildly used control technic. PID controller continuously evaluates difference between current state and reference (desired) state and applies a correction which consists of

  • proportional - the main force to achieve convergence to desired state The plot shows how choice of Kp affects the error over time. Choosing smaller value of Kp (orange line) leads to longer convergence. On other hand higher value of Kp gives an opportunity to converge faster (around step 250), but might lead to bigger error (around step 475).

  • integral - fights against potential systematic bias The plot shows there is a systematic bias presents in the system. Blue line (Ki == 0) oscillates around small positive value. Choosing small positive value (green curve) as Ki allows get rid of it. The large value (orange curve) might increase overall error.

  • derivative - fights against oscillation around desired state Proper choice of the Kd allows controller to reduce oscillation.

Steering Angle Controller

  • Input - pure simulator cross track error value
  • Parameters - Kp, Ki, Kd
  • Output - truncated to be in range [-1 .. 1]

Throttle Controller

  • Input - magnitude of cross track error shifted by pre-defined value
  • Parameters - Kp and Kd (so in fact it's PD-controller)
  • Output - truncated to be in range [-1e-2 .. 1]

Magnitude of the cross track error was taken as an input since there is no difference for the controller if the car on the left, or on the right side of the reference path.

The offset defines minimal cross track error magnitude to start braking.

Ki parameter was dropped to reduce the size of parameter space for tuning. PID uses Ki in order to reduce an effect of systematic bias. Since magnitude is used as the input for the controller there is no sense to keep track of systematic bias. It could only be introduced by poor choice of offset value (for now the assumption is the offset value is selected carefully).

Tuning

Schema for tuning the hyperparameters

  • Initial feasible solution was found. Fortunately, the value from lesson worked as a feasible solution at constant throttle of 0.3
  • Twiddle approach was used to tune feasible solution from (1).
  • Score was calculated from the whole lap

Since twiddling is very time consuming

  • simulator provides one measurement each ~0.03 seconds
  • the lap is ~0.85 miles long
  • simulation time for 1 lap at avg. speed of 45 mph ~60 seconds
  • Each set of parameters need to be simulated

The following adjustment was made in scoring routine to achieve keep-feasible and fail-fast goals

  1. Negative Kp value for both controllers treated as failure
  2. Score was counted only till currently known best score
  3. Velocity below threshold for pre-defined number of measurements treated as failure
  4. Distance below threshold after pre-defined number of measurements treated as failure
  5. Ki parameter for throttle controller was dropped

TODO

  1. Try to use different parameters tuning technic
  2. Try to use PID instead of PD to control throttle
  3. Try different input for throttle controller (ie use current angle and/or speed)
  4. Normalize parameters to be on the same scale before running twiddle

About

Udacity CarND PID Controller Project

https://www.udacity.com/drive


Languages

Language:C++ 99.6%Language:CMake 0.2%Language:Shell 0.1%Language:Gnuplot 0.1%