Implement Model Predictive Control to drive the car around a simulated
- Fit 3rd order polynomial on road waypoints
- Define and set MPC calculations and constraints
- Build a Model Predictive Control that handles a 100 millisecond latency
- Tune parameters for a smooth drive around the simulator track
main.cpp
for calling the model and communicating with the simulatorMPC.cpp
defines the MPC class with code for MPC implementation and steering&throttle outputs
The model
To fit a polynomial curve to the provided waypoints, the model receives the following from the simulator:
ptsx
x-coordinate of the waypoints aheadptsy
y-coordinate of the waypoints aheadpx
coordinate of vehicle's current positionpy
y-coordinate of vehicle's current positionpsi
current orientation angle of vehiclev
current velocity of vehicledelta
current steering angle of the vehiclea
current throttle
Based on the above inputs, the waypoints coordinates are first converted to vehicle coordinates with the following equations:
- ptsx_veh = x * cos(-psi) - y * sin(-psi)
- ptsy_veh = x * sin(-psi) + y * cos(-psi)
Varying weights were used for the following cost variables:
cte_w = 1000
cte costepsi_w = 1000
epsi costv_w = 1
velocity costdelta_w = 5
steering costa_w = 5
acceleration costdelta_change_w = 50
steering rate of change costa_change_w = 5
acceleartion rate of change cost
Tuning the above parameters helped improve the driving behavior. Adjusting cte_w
and epsi_w
values helped the vehicle stay on the center of the road. One important parameter is v_w
, which is needed for the car to constantly move.
With the help of the IPOPT and CPPAD packages, a best fit curve is calculated for optimal trajectory. Note that certain constraints were set before fitting the curve so that the vehicle does not make drastic turns(angle between -25 degrees and 25 degrees) or sudden acceleartions (throttle between -1 and 1).
Latency A latency of 100ms was added for realistic driving condiations. Latency was accounted in the model by predicting where the vehicle would be after 100ms and predict the new state. This allows the vehicle to take action beforehand.
Tuning timesteps and duration I started with 20 timesteps(N) and 0.2s duration(dt) as too large of a prediction horizon(T) would have a hard time coping with the environment changes. I soon found out the N and dt values I started out was too generous as reducing N made the model faster and reducing dT made the model react faster to chaning environments without loss in performance. With a N of 10 and dt of 01.s, the vehicle was able to drive around the track smoothly in quite high velocity.
View the video on Youtube
- Udacity Simulator download
- uWebSocket download
- cmake >= 3.5
- All OSes: click here for installation instructions
- make >= 4.1
- 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
- Windows: recommend using MinGW