Some of the challenges in PiWars can be attempted autonomously. This is a good way to flex our programming muscles and gain extra points.

The challenge we're focusing on right now is the Eco Disaster challenge.
https://piwars.org/2020-competition/challenges/eco-disaster/

Briefly, you need to pick up red barrels and put them in the yellow zone and pick up (pr push) green barrels and place them in the blue zone. There are 12 barrels, 6 of each colour (we think!)

So how did we go about this in the code?

Simulation

First we setup a simulation to test our code. This is our favourite way of testing code. It looks cool and saves time. It also helps with debugging. You can see what the computer was thinking during the process and where it goes wrong!

Here's a sped up simulation of our first attempt at this challenge:

The top down view was created in pygame, the front 3d view was made in VTK.

Algorithm

The robot scans 180 degrees looking for barrels with the camera using openCV to detect blobs of red or green colours. When a barrel is detected the robot lines up with it and uses the laser distance metre to accurately measure how far it is. It knows how far it's turned because it has an on-board gyroscope. We use the gyroscope to keep track of where we are.

We pick the nearest barrel and navigate to it and grab it. Because we know where all the other barrels are, we can create a map. With that map we draw a ring around each barrel which we say the robot shouldn't enter, using a library called shapely. Shapely merges barrels together that are too close for the robot to go between.

We use the outline of those shapes to create a visibility graph using pyvisgraph. This allows you to plot the shortest path between two points while avoiding the keep-out areas. It actually creates a network and we use Dijkstra's algorithm!
We tell the robot to follow that path.

Once we've delivered the barrel we back off a little bit and repeat the process until there are no unsorted barrels

Potential problems

The simulation is obviously just that - a simulation! It's not properly simulating turns and movements of the robot. There is some error built into the simulation but not a lot.

Knocking over barrels still might happen.

Overall, we're still pretty happy with our chances of scoring high on this challenge. We're going to try and recreate this in the real world!