Highway Traffic Simulation

This is a project I’ve had in the back of my head for a while. Every time I’m sitting in traffic, I like to observe and think about the cars around me, and I’ve gotten pretty good at predicting what a lot of drivers will do. I also have some theories as to what causes traffic and ways to alleviate traffic.

The first goal of this project is to just simulate a basic highway system and see if it is as stable as my personal observations of traffic.

Second, I want to tweak parameters of each car’s behavior to see if I can replicate things like aggressive drivers, passive drivers, etc.

Third, I want to try some of my theories of things that cause traffic and things that alleviate traffic and have some fun with that

This whole project should be caveated with the statement that I’m not out to make the worlds most accurate simulator. This is more a representation of my observations than a representation of reality, although I will try my best to play by nature’s rules. Naturally, this project will be biased by my desire to make it work the way I imagine it.


The first thing that I want to tackle is making a decent model of a single car. This will include basic longitudinal dynamics for accelerating and maintaining speed, as well as lateral dynamics for changing lanes. For the car, I’m using a control loop frequency of 10Hz, since I believe that’s on the same order that humans make decisions. For each loop of the controller, the physics simulation runs 10 steps, giving a simulation rate of 100Hz (0.01s loop time). This can be refined in the future, but for now the single-car simulation is stable.

Code for the car can be found here: https://github.com/araulinaitis/Car-Simulation

My first iteration of longitudinal control is shown below. The test car was started at rest and given a step-input with a target speed. Most of the tuning for this model was to get the response to look physically reasonable: that is there are no sharp accelerations, no acceleration over 0.25g, and limiting the rate of acceleration change to ~0.18g/s. The goal for this was to bring the total vehicle “zero-to-cruise” time to around 15-20 seconds. I might increase this speed later, but for now it looks like a decent response. Furthermore, I intentionally included some overshoot on the target velocity, since most drivers aren’t perfect.

This was accomplished with a control loop that servos on velocity but affecting acceleration with the basic state model shown below, where x-dot is vehicle acceleration of the vehicle, and u is the control input.

I’m using a basic PID controller (with Integral-windup protection) with hand-tuned parameters to give the above response. Most of the response is dominated by the max acceleration and acceleration slew rate, so the PID dynamics don’t change the response very much.

My next steps for longitudinal control are to think about braking (allowing higher accelerations for emergency-braking) and lateral control for changing lanes

After completing the longitudinal control, I essentially copied it over for lateral control, just changing constants and control parameters. Namely, my maximum acceleration for lateral control is 0.1g/s (as opposed to 0.18g/s for longitudinal control) and maximum acceleration was limited to 0.1g (as opposed to 0.25g for longitudinal control). This, coupled with tuning the PID controller (actually only using PD for lateral control at the moment) gives the response below. The goal for this was to have a lane change take ~4-5 seconds.

Again, I intentionally left a little bit of overshoot both to seem a little more “real,” and also to allow for increased rise time.