Linux Format

Micro:bit robots

Les Pounder shows us how to use two micro:bits and some MicroPytho­n code to build radio-controlled robots for our own robot battles!

- Les Pounder is a (gentlemen) hacker/maker who loves tinkering with Raspberry Pi, Arduino and micro:bit. He trains teachers for the Raspberry Pi Foundation and writes up his adventures at http://bigl.es.

Les Pounder shows us how to use two BBC micro:bits and some MicroPytho­n to build radio-controlled robots for our own wireless robot battles!

We introduced MicroPytho­n, in a previous tutorial [see Tutorials, p88, LXF221], by learning how to create our own IronMan- inspired light glove that reacted to input, in the form of coordinate data, taken from an accelerome­ter built into the micro:bit. For this tutorial, we shall build something even better!

One of the most popular projects for beginner hardware hackers is creating a robot. We’ve seen many different versions, typically using various models of the Raspberry Pi as the brains of the robot attached to many different motor control boards and robot chassis. But since the release of the micro:bit, we’re seeing even more robot kits and accessorie­s come to the market. Since the micro:bit doesn’t come with Wi-Fi and there’s no support for Bluetooth using Micro Python, how can we control a robot? The answer is that we have a simple radio system in the BBC micro:bit that can be used to send brief messages or data.

In this tutorial, we’ll introduce the following steps to building and programmin­g a robot: Building a robot chassis from a kit. Controllin­g motors using a driver control board.

Making a controller unit that detects user input and interprets movement.

Establishi­ng radio communicat­ion from the controller to the robot.

Configurin­g the robot ‘brain’ to receives radio signals and react accordingl­y.

Prepping for the project

For this tutorial, you’ll need to own two micro:bits (after all, you need two sides to have a war), a USB battery pack, two USB-to-micro USB leads, Kitronik motor driver board and a robot kit. You can find robot kits for a cheap price on eBay and you’ll need one that includes, two motors, chassis, a pack of four AA batteries, two wheels and a trolley caster (with front wheel balance).

Our robot will be controlled by a micro:bit steering wheel. This wheel will read data from the built-in accelerome­ter and communicat­e the informatio­n to another micro:bit attached to our robot. We will code all of this project in MicroPytho­n, using the Mu applicatio­n. Released by the Python Software Foundation to enable anyone to use the leaner implementa­tion of Python 3, MicroPytho­n was originally created by Damien George.

Using MicroPytho­n, two micro:bits, a motor driver board and a cheap robot chassis we’ll take our first steps with MicroPytho­n robotics with the micro:bit. We covered installing Mu, the MicroPytho­n editor in the previous issue, but here is a quick reminder. Installing Mu on a Linux machine is trivial, all you need to do is download the applicatio­n from https://s3-us-west-2.amazonaws.com/ardublockl­ybuilds/microbit/linux/mu-2016-11-06_11_36_15 then navigate to your Downloads folder. Right-click on the applicatio­n and select Properties then go to Permission­s and change the permission­s so that the file can executed as an applicatio­n. Now you can double-click on the applicatio­n and it will open the Mu editor.

We’ve broken the tutorial down into sections to make the whole process easier to follow, so let’s start building a robot!

Our project uses two micro:bits, the ‘brains’ of the robot and a controller unit used to send the commands over a radio connection. We shall start by coding the controller. So in Mu make sure you click on ‘New’ to start with a new blank document. Also remember to save often: from microbit import * import radio

Coding the controller

We first import two libraries, the entire micro:bit library, which gives us access to the sensors and display present on the board, and import radio , a library that enables short range radio communicat­ion between micro:bits. In order to use the radio we first must turn it on, this must be done for every micro:bit that we wish to use in the project. ( Seealso Micro:bitRadio,above,formoreinf­ormation): radio.on()

We now scroll a message across the LED matrix of our controller micro:bit. This message advises the user on how to control the robot. We also set the scrolling speed using a delay. This delay will control how quickly the matrix is updated. The default is 150ms, but we have reduced it to 50ms as 150ms was too slow. display.scroll("Turn the wheel to drive the robot",delay=50)

In order to constantly check the driver’s input, we need to use an infinite loop, which in MicroPytho­n is while True:

Inside of the loop we need to determine the driver’s input. This is collected by checking the status of the accelerome­ter. In the previous issue, we gathered and used the individual X,Y and Z co-ordinates generated by moving the micro:bit to control our WS2812B LEDs. But for this project we can simplify the accelerome­ter data by using ‘gestures’. The micro:bit can determine 11 gestures, such as up, down, left, right and shake etc. But it can also measure accelerati­on forces, measured in G: gesture = accelerome­ter.current_gesture() print(gesture) Here we create a variable called gesture and store the current gesture. We also print the contents of our gesture variable to the Python shell, REPL, for debugging purposes.

We have the gesture stored in a variable and now we shall test to see if the gesture matches one of those required to control our robot. For this we shall use an if...elseif test: if gesture == "left": This first test is to see if the controller has been turned left. So we check the contents of our gesture variable against the hard-coded value "left" .

So if the gesture is left, we need to provide the driver with feedback to say that this has been acknowledg­ed: display.show(Image.ARROW_W) radio.send('left')

Here we’ve updated the LED matrix on the controller micro:bit to show the direction of travel. The display class contains lots of images that we can show. So to illustrate that we are turning left we update the display to show an arrow pointing ‘west’, which is the left-hand side of the micro:bit. The radio function handles communicat­ing with our robot brain and it sends a string of data over the radio to our awaiting robot brain. With the string being the direction that we wish the robot to travel.

But what if the gesture was right? Well, here we use an else if, known as elif in Python. Elif will be checked if the first condition is False, or previous elif conditions also returned False. We go down the elif until one returns as True. So if we gestured to go right then the following code is run: elif gesture == "right": display.show(Image.ARROW_E) radio.send('right') We repeat this for tilting the micro:bit up and down: elif gesture == "up": display.show(Image.ARROW_N) radio.send('forward') elif gesture == "down": display.show(Image.ARROW_S) radio.send('reverse') Our final elif condition to test is our emergency brake. Just in case we need it we can press the ‘B’ button on our micro:bit to stop the robot. This only works if the board is held flat! elif button_b.was_pressed():

display.show(Image.SURPRISED)

radio.send('brakes')

So that is all of the code for our controller. Make sure that you save the code to your computer. Now plug in your micro:bit and click on ‘Flash’ to upload the code to your micro:bit. After a few seconds, you will see the instructio­ns scrolling across the LED matrix. Now hold the micro:bit like a steering wheel and you will see an Up arrow appear, indicating that the radio unit is sending the command forward to our robot. Turn the miro:bit left and right, now turn it upside down for reverse gear. Finally, hold the micro:bit flat and press B to apply the brake.

To create a more ‘realistic’ feel for steering the robot we used an old circular frame, which also transports cables/ wires through the post, to create a makeshift steering wheel. With a few cable ties to hold the micro:bit and our USB battery securely in place, we were ready to go for a drive.

Building the robot

With the controller complete, we now move on to creating our robot, and our first task is to build the robot a body. Our robot uses a common robot chassis found in an online auction house for roughly £10. There was a little soldering involved, namely the terminal connection­s on each motor. This is a simple task but if you can’t solder or don’t have the kit, pop along to your local makerspace/Raspberry Jam or LUG meeting for help.

The wires from your motors are connected to a Kitronik motor driver board. Each motor is connected to its own terminal. Looking at our robot from the front, Motor 1 is on the left of our chassis and Motor 2 is on the right. Both of the wires from one motor are connected to the two terminals for Motor1 on the driver board. Do the same for your second motor ensuring that it connects to Motor 2. Also ensure that the terminals grip the wire snugly. Now connect your AA battery box to the power terminal, and check that you match the polarity. Typically the AA battery box has red and black wires, where red is ‘+’ and black is ‘-’. Don’t put the batteries in just yet. Now secure the battery box, motors and driver board to your chassis using the screws in the kit or for a quick hack use cable ties. For now, keep the robot brain micro:bit out of the robot chassis as we need to program it.

Ensure that you are using the micro:bit intended to be the brain of our robot car. Don’t use the robot controller micro:bit that we flashed earlier. Connect your micro:bit to your computer and load the Mu applicatio­n. Create a new file and remember to save often. We start coding the robot by importing the same libraries as we did for the controller,

namely microbit and radio. We also ensure that the radio is turned on ready to receive a signal: from microbit import * import radio radio.on()

To signify that the robot is loading and preparing for use we’ll create a short animation using a for loop. For loops are used to iterate a set number of times, e.g. in this for loop we’ll set a range of 3 which will cause the loop to iterate three times: for i in range(3):

What will happen inside the loop is that we shall display a small heart on the LED matrix, wait half a second, or 500ms using Micro Python’s sleep function, to ensure that the image is visible to the user: display.show(Image.HEART_SMALL) sleep(500)

Then we swap the image for that of a larger heart and repeat the same sleep to create a ‘beating’ effect on the LED matrix. display.show(Image.HEART) sleep(500)

Coding the robot

With the robot startup code written, we now move on to the main loop that’s responsibl­e for continuall­y checking for radio signals. Again, we use an infinite loop to contain the code necessary: while True:

Inside the loop, we create a new variable called incoming this will store the incoming radio messages, sent by our controller micro:bit. incoming = radio.receive() In order to interpret and process the commands being sent over the radio, our robot needs to first understand what to do for a specific command, and here we once again use an if...elif conditiona­l test. We check that the value of our incoming variable is the same as a hardcoded value, for example ‘left' : if incoming == ‘left': So if the instructio­n was to turn left, then we update the LED matrix of our robot brain micro:bit to show the direction of travel. In this case, we update to show an arrow pointing east, as when looking at the robot head on, the arrow pointing east is also pointing in a left direction.

display.show(Image.ARROW_E)

To control the motors, we need to control the GPIO pins that they are connected to via the motor driver board. As you can see, next to the terminals on the motor driver board there are ‘P’ numbers. These are the GPIO pins used for the terminals. To drive the motor in one direction one pin must be high, and the other low. In other words, only one pin can be on and the other must be off. To reverse the direction of the motor we need to reverse that polarity: pin8.write_digital(0) pin12.write_digital(1) pin16.write_digital(0) pin0.write_digital(1)

Motor control

To turn the robot left we need to tell one motor to go forward, and the other to go backwards. For our example, motor one is the left motor (looking from the front of the robot) and it’s connected to pins 12 and 8. Our right motor is connected to motor two which is controlled by pins 16 and 0. So to turn the robot left, motor one needs to go forwards and motor two backwards. We do this by sending power to the respective GPIO pins using write_digital .

The code to turn the motors right is a reverse of what we set for turning left. Showing that the circuit can easily switch polarity on the fly: elif incoming == ‘right': display.show(Image.ARROW_W) pin8.write_digital(1) pin12.write_digital(0) pin16.write_digital(1) pin0.write_digital(0) Here is the code for moving the robot forwards: elif incoming == ‘forward': display.show(Image.ARROW_N) pin12.write_digital(1) pin8.write_digital(0) pin16.write_digital(1) pin0.write_digital(0) Here is the code for reversing our robot: elif incoming == ‘reverse': display.show(Image.ARROW_S) pin12.write_digital(0) pin8.write_digital(1) pin16.write_digital(0) pin0.write_digital(1) Our last condition to test is our emergency brake. This will set all of the GPIO pins connected to the motors to off: elif incoming == ‘brakes': display.show(Image.SURPRISED) pin12.write_digital(0) pin8.write_digital(0) pin16.write_digital(0) pin0.write_digital(0) Save the code and click on ‘Flash’ to upload the code to your micro:bit. Now remove the microbit from your computer and place it in your motor driver board’s slot. Once you insert the batteries, you should see the heartbeat animation start. Now power up your controller; we used a USB phone battery for ours. After the scrolling instructio­ns, you will be able to drive your robot around the room. If your robot behaves a little differentl­y then you may need to swap the wires from each motor to their motor terminals.

 ??  ?? The finished robot car is really simple to make thanks largely to an off-the-shelf chassis and a motor driver built for the micro:bit.
The finished robot car is really simple to make thanks largely to an off-the-shelf chassis and a motor driver built for the micro:bit.
 ??  ?? Mu is the Micro Python editor for the micro:bit. It offers a simple to use interface that handles code suggestion­s and indentatio­n for the user.
Mu is the Micro Python editor for the micro:bit. It offers a simple to use interface that handles code suggestion­s and indentatio­n for the user.
 ??  ??
 ??  ?? Our controller is a micro:bit held in place on a DIY steering wheel using BluTack. Turning the micro:bit as you would controllin­g a car will successful­ly control the robot.
Our controller is a micro:bit held in place on a DIY steering wheel using BluTack. Turning the micro:bit as you would controllin­g a car will successful­ly control the robot.
 ??  ?? Here we used a USB power pack to power our micro:bit, but you can also use the official 3V AAA power pack.
Here we used a USB power pack to power our micro:bit, but you can also use the official 3V AAA power pack.
 ??  ?? Our robot car has a brain and a heart! The heart animation indicates that the robot is ready for action.
Our robot car has a brain and a heart! The heart animation indicates that the robot is ready for action.
 ??  ?? Our chassis has all manner of holes for attaching components, but the motor pylons can only go in this area due to the careful balance needed for our robot.
Our chassis has all manner of holes for attaching components, but the motor pylons can only go in this area due to the careful balance needed for our robot.
 ??  ?? Motor 1 is the motor on the left of this picture, and Motor 2 is on the right. You can also see the wires routed through a space in the chassis to the motors.
Motor 1 is the motor on the left of this picture, and Motor 2 is on the right. You can also see the wires routed through a space in the chassis to the motors.

Newspapers in English

Newspapers from Australia