AverageBot Devblog 7 – Nearing Completion!

AverageBot Devblog 7Line sensors finally sorted this week!

As we get closer to the Pi Wars competition on 5th December, I’ve been putting in the hours to get AverageBot ready for action. To recap on my progress so far, check out my other Pi Wars devblogs here.

With the physical build mostly complete, key progress this week has been completion and submission of my initial code (a competition challenge itself), including those pesky line followers. I’ve also written and tested some code for the three point turn challenge.

Let’s show you what I’ve been up to, and what’s left to do…

Line Followers

I finally got round to wiring and coding the line followers on my robot, after months of putting it off. It was pretty much as I expected – a pain to set up the hardware, and a bit of a pain to code – but totally necessary for the Line Follower course.

First I tackled the hardware. Wiring seemed straight forward – three pins to connect – Power, Ground and Signal (GPIO).

Using all three line following sensors created a new problem – a lack of 3.3V connections. The PiRoCon motor controller does have a few of these dotted around the board, but just for the fun of it I decided to make a little pin extender out of an old ProtoCam protoype I had in my drawer:

ProtoCam board recycling

Look…a cheeky plug of my ProtoCam board range!

Custom GPIO duplicator

Yes the pins are bent. Yes it doesn’t look pretty. Yes it works…and that’s all that matters!

Next problem: the sensors are meant to work at 5V but at that voltage I couldn’t get the high/low signal to change on a simple test script. After an hour of swearing at these components, I decided to try them at 3.3V and they worked instantly (hence my need for more 3.3V pins mentioned above).

Line sensors

All three line sensors working, gotta love that red glow!

With the sensors functioning, I then had to work out how to code them. Getting the high/low to register is one thing – but making a robot follow a track is a considerably larger beast!

Fellow Twitterer @sweenig suggested I try the Raspberry Pi Guy’s tutorial over on YouTube which gave me the fundamental understanding I needed to kick off my code. Here’s the video:

It’s a lot simpler than you’d think, but it gets a lot more complicated when you attempt tracks with tighter turns and sharp angles. So far I have the following code (below), which works on a basic oval but seems to freak out with anything complicated.

You’ll notice that I have a variable called ‘LastDir’. This is to track the last left or right sensor that my robot detected, to say “If the left or right sensor saw the line last, and then suddenly no line is seen by any sensor, it’s probably best to try that last direction again”.

It’s an attempt at pulling the robot back to the track on tight turns where it might lose itself. It doesn’t work all that well…but the theory is bang on the money!

    def linefollow(): ### PROGRAM 4 ###
    ## Line sensor: http://4tronix.co.uk/store/index.php?rt=product/product&product_id=144
    ## Logic:
    ##     When seeing white (off of the line) = Low logic
    ##     When seeing black (on the line) = High logic
        print "LineFollow script started"
        LLeft = ""
        LRight = ""
        LMiddle = ""
        LastDir = 0