
Up the garden path is one of the challenges we have been dreading, as it can only be completed autonomously, and neither of us having much experience with line following code (as it didn’t really feature much in PiWars 2019)
The Secret Sauce
We would be using visionDock to complete the challenge and get maximum points, we needed to come up with an algorithm using solely the camera to navigate the line, and choose the correct route through the many dead ends helpfully placed up the path ?
With the Pixy Cam 2 (visionContainer) attached we were able to use the inbuild microprocessor to identify the line and return the start and end coordinates to the main code.
Attempt 1 – Line positioning
Initially, we decided to look at the line positioning to determine the course of the robot. The frame of the camera is split into effectively a graph, with 78 points on the X axis, and 51 points on the y axis.

Because of this, we decided to base the course of the robot off of position of the line, so using the following strategy:
- if x was in the first quarter of the screen, turn +45°
- if x was in the last quarter of the screen, turn -45°
- if x was in the second quarter turn +25°
- if x was in the third quarter turn -25°
Upon running this, we found it to be incredibly choppy, with the robot really struggling to find the centre point, and shaking from side to side. It also didn’t have enough time to react to the corners so was getting lost very quickly
Attempt 2 – Trigonometry
Back when I was in school, I remember countless times working out the unknown angle of a triangle and thinking…. when will I ever need to use this. Well it turns out Pi Wars!
We went back to the drawing board and decided we needed to be more though with our calculations and could use trigonometry to determine the angle of the line. I’ve tried my best to explain it below, however this is a very useful video which does a lot better job than me!

Using the above line which has a start position of (52,42) and an end position of (18,12) we can easily work out the length of the sides by finding the difference between the 2 points. So the length of y is 30 (42-12) and the length of y is 34 (52 – 18)
We then have 2 known lengths which will help us to find the missing angle as denoted by θ. Because we know both the opposite and adjacent angles, we can use the the tragicomic identify of tan.

So in this instance, we can work out θ by the following

As the difference on x is greater than the difference in y, we are able to determine the angle based on x axis, however for our drive code we need to base it off the y axis, which is very simply a case of subtracting 41° from 90°. This then left us with the angle of 49°… Simple right?

With this all written, we then went on our first test run, hoping for first time success… however…
After much head scratching we eventually worked out why!
Attempt 3 – Returning to a center point!
It soon became apparent although the robot was turning to follow the angle of the line, trying to correct itself, the line was quickly falling out the frame of the camera. This was because we weren’t basing the angle off the center point of the robot. To do this, we needed to base x1 and y1 off the bottom centre of the camera (39,51) and then work the angle from the furthest point of the line, so using our points from before, it can be worked out by the following

The maths, is exactly the same as before, the only difference is we are basing off the center point as apposed to the start and finish of the line, in the aim to adjust the robot to bring the line to the center.
The only issue we then had to solve was reaction times. With the camera being angled forward (as to determine junctions) it tended to turn too soon, doing the course, just not really following the line. To counteract this, we added a 3 frame buffer to the code (essentially lagging the camera) so that it followed the correct path.
With all of the above, we were very pleased with the results. the robot was following the lines smoothly, and in the event it lost sight, it reversed back until it locked on again! We were ready to film our challenge video!
6,167 total views, 2 views today
This is really interesting – it’s almost exactly the same steps as we went through for our robot – Nanny McPi.
I calculated the angles using a centre point below the camera frame for ours to try and get closer to the actual centre of the robot. I also took into account both the direction of the line and the angle from the centre point. This way if the line is to the right, but is angled left we don’t need to turn quite so much.
Also – if we lose the line – which we always do on the curves – we remember where the line was last time we saw it and just keep turning that way until we find it again. That worked really well for us.
Looking forward to seeing the challenge runs next weekend.