Tuesday, 5 February 2019

micro:bit tells the Pi where to go ...

... but in a nice way.

Not long after applying for Pi Wars, we were thinking about the design of the robot - and one question was how to display visually which mode the robot is currently in. Also - it would be nice to have a physical button on the robot to change the mode - just in case the remote controller can't connect for some reason.

Initially the idea was to have a number of LEDs and a button connected to the Raspberry Pi GPIO pins. The LEDs would show the mode using a binary representation and the button would allow the mode to be changed. However one day I looked across at the Dimm robot from BinaryBots we had built but never quite got round to programming and the answer was there. The BBC micro:bit would be perfect for a simple display and input. And after a few months, we've finally put a lid on the robot and had somewhere to mount it (albeit with some velcro for now - whilst we finalise the position).

micro:bit centre stage on top of Sputnik

Programming the micro:bit is dead simple - an 8 year old could do it - and so he did - putting in the animation for the remote control mode - and he'll be adding animations for the other operating modes as well. I put in some very simple serial comms to send a change of mode to the Pi when the 'A' button is pressed, and to receive change of mode information from the Pi so we can change the display graphic. 

But having done that, could we use it for anything else? One thing we wanted to do was have a decent way of turning 90 degree turns for the minimal maze challenge. And the micro:bit just happens to have a built in compass that we can get the heading from. So a few lines of code (or blocks of code in the case of the micro:bit) later and we're sending the compass heading down the serial cable to the Pi, 5 times a second. 

Because the compass heading isn't always reliable, it took a bit of work to come up with some code on the Pi to be able to use the information to turn precisely. 

It's a little slow at the moment - but it does actually work. It's amazing that, with all the motors and electronics in close proximity, the micro:bit is still able to lock onto the Earth's magnetic field and give us an accurate compass heading. 

Here's Sputnik in action turning 90 degrees with a little help from its new friend:

Thing's learned this month:

  • Programming a robot would be very easy if you could rely on the sensors 100%. A lot of the code written seems to be telling the robot to take the sensor readings with a pinch of salt Otherwise one dodgy reading and it would suddenly veer off in one direction or the other and crash into a wall. 
  • Having the garden as the only place to build the robot chassis using power tools is not ideal in deep snow and ice. (Hence in the picture you can see where I've marked out the holes to drill for the Pi Noon attachment - but not got round to actually doing the drilling yet). 

No comments:

Post a Comment