Simple Pygame simulator for demonstrating how a particle filter works. Only 2D lidar data is fed to the car in order to make the simulation simple to use, just the x ( distance from the car to the right side of the screen ) and y ( distance to the bottom of the screen ).
To run the simulator, just run the following command in your terminal :
python sim.py
You can control the movement of the car with your W A S D keys. The red box drawn on the screen is the real postion of the car. The bright green box drawn on the car is the postion of the car guessed by the particle filter. You'll also see the particles as the car moves, these particles are coloured according to their weights.
The particles are given a bluer colour if the particle filter is less confident in that guess ( lower weight ) and a greener colour if the particle filter is more confident in that guess ( higher weight ).
The frame rate of the simulator can be adjusted in the sim.py
file by changing the clock.tick(frame_rate)
line.
Barriers are drawn onto the screen in the form of rectangles with pygame.Rect()
. More barriers can be added by changing the barriers
array. Add these barriers as an array of length four with the following format :
[x_pos, y_pos, x_length, y_length]
Note here that the screen has dimensions of (400, 300)
and that in pygame down is the positive direction of the y-axis.
This simulator was originally created with the goal of being a teaching exercise. A lab handout version of the can be found in the exercise
branch. This version has just a very simple baseline particle filter class that does not work. Students can then write the particle filter class themselves without having to worry about the rest of the simulator working in the background.
The velocity of the car is also given some noise as this would happen with real odometry data. This noise is then reduced by a Kalman filter class. This could also be taken out and given as an lab exercise, see kalman-filter-sim