what is robotics and how can i start doing it? adrian ......what is robotics and how can i start...

12
What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly technological world in which all sorts of strange new words, like coding, artificial intelligence and the Internet of Things, have come into the our vocabulary, but which are pretty hard for ordinary mortals to understand. I have chosen to use the “robotics” word simply because it is something which has been around for quite a long time and for which most of us have some images, such as from film/TV, space exploration, car production lines and remote controlled gadgets such as an electronic lawn mower – see this video. Robotics is just one field of computer controlled systems which are designed to do something practical. Fortunately there is now a range of small and cheap consumer devices we can use for ourselves to build our own systems. Here are images of Arduino, BBC micro:bit, Crumble and Raspberry Pi. They can each be programmed using a computer and then be disconnected to carry out tasks on their own – known as `autonomous systems’. I will stick to the BBC micro:bit here, not just because it is cheap, powerful and robust. Many people of a certain age will have learned to program using a BBC microcomputer and its built-in BASIC programming language. Nowadays many of the younger generation have learned to program in a system called Scratch, developed by MIT. This is based on an earlier text language called Logo, and has similar forward, back, left and right commands, called turtle geometry, to produce designs on a computer display. The key feature is that you don’t type commands in using the QWERTY keyboard. You use your mouse to drag graphical blocks into an editor screen where you compose your program. Below is a picture captured from a Scratch screen. The menu for blocks is in the middle. The program editor is on the right. The output display is on the left.

Upload: others

Post on 14-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

What is robotics and how can I start doing it? Adrian Oldknow November 2018

We all know that we live in an increasingly technological world in which all sorts of

strange new words, like coding, artificial intelligence and the Internet of Things,

have come into the our vocabulary, but which are pretty hard for ordinary mortals

to understand. I have chosen to use the “robotics” word simply because it is

something which has been around for quite a long time and for which most of us

have some images, such as from film/TV, space exploration, car production lines

and remote controlled gadgets such as an electronic lawn mower – see this video.

Robotics is just one field of computer controlled systems which

are designed to do something practical. Fortunately there is now

a range of small and cheap consumer devices we can use for

ourselves to build our own systems. Here are images of Arduino,

BBC micro:bit, Crumble and Raspberry Pi. They can each be

programmed using a computer and then be disconnected to

carry out tasks on their own – known as `autonomous systems’.

I will stick to the BBC micro:bit here, not just because it is cheap, powerful and robust. Many

people of a certain age will have learned to program using a BBC microcomputer and its built-in

BASIC programming language. Nowadays many of the younger generation have learned to

program in a system called Scratch, developed by MIT. This is based on an earlier text language

called Logo, and has similar forward, back, left and right commands, called turtle geometry, to

produce designs on a computer display. The key feature is that you don’t type commands in using

the QWERTY keyboard. You use your mouse to drag graphical blocks into an editor screen where

you compose your program. Below is a picture captured from a Scratch screen. The menu for

blocks is in the middle. The program editor is on the right. The output display is on the left.

Page 2: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

While you can write programs for the micro:bit in languages such as Python, JavaScript and C+,

there is a much easier and friendlier system developed by Microsoft Research, called MakeCode.

You can run this from an Internet browser using this link. You can install it as a stand-alone App for

Windows 10 from this link. You don’t even need an actual micro:bit to start writing code for

micro:bits, as the display includes a graphical simulator. Let’s have a look at a simple starting point.

The first thing to notice in the program editor window on the right is that there isn’t just a single

program; there are 6 separate blocks of code. Think about all the clever electronic systems in a

modern car. When you switch the engine on, a whole load of things get displayed on the

Page 3: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

dashboard. You have a number of manual controls you can use, for example to change gear, to

indicate which way you want to signal a turn, to put the radio on, or to wash the windscreen. But

the car also has sensors which detect if you have a seat with someone sitting in who is not using a

seatbelt, or to see if it’s getting dark outside and then to turn on the lights, or if there is moisture

on the windscreen and then to turn on the

wipers.

The `Basic’ menu is shown in blue. That is

where the blocks for commands like `on start’,

`show icon’ and `show string’ can be found

and dragged into the editor. The `show icon’

block has an arrow which can be used to select a choice of icons from an

extensive menu. The `show string’ block has a blank area in which you can type the letter or

message you want to have displayed. The purple `Input’ menu is where commands

like `on button A pressed’ and `on

shake’ can be found. The possible

inputs include the sensors which tell

the micro:bit if either or both buttons

are pressed, the built-in sensors on the board for light, temperature, direction,

acceleration and magnetic field, and external sensors connected to any of 20 or so

`pins’ which can provide information such as humidity, current

and sound. The `shake’ and `tilt’ values are examples of

`gestures’ which the micro:bit can sense from its built-in

accelerometers. If you tilt the micro:bit left, then a left arrow is

displayed, called `West’. When

you choose the `Basic’ menu, it

displays 3 dots and `more’ to

show there are additional

commands available. That is

where the `clear screen’ and `show arrow’ commands are to

be found. The `on shake’ block simulates a 6-sided dice. This

uses the `pick random’ block found in the `Math’ menu.

If you make any mistakes you can drag loose boxes into the menu area in the middle, which turns

into a trash can. Have a go at entering this project yourself, and check out, using the simulator, that

all works as expected. Remember to give your project a name and to save it. I have used the

`Share’ icon at the top of the screen to publish so that others, like you, can access the project

without building up the program. All you need to do is to click on this link! (or type it into a

browser: https://makecode.microbit.org/_ipyTM3XC20vz ). Now use `Home’ and `New’ to build

your own demo project starting from scratch!

Page 4: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

Now you have met some of the basic ideas and principles of programming (aka

coding) in Microsoft’s MakeCode block editor, we can move to creating

something useful. My first example will be to produce a micro:bit driven

metronome to beat time to help musicians play through a score. An original one,

like that shown, was driven by a clockwork motor making an arm moving side to

side and producing a click for each beat. The rate at which it beats is controlled

by moving a sliding weight up or down the arm. Instead of using a clockwork

motor we can use a small and cheap electronic motor called a servo. The one

shown here costs about £3 and is available from suppliers such as

Kitronik. It has 3 wires. One needs to connected to the GND pin and

another to the 3V pin. The third one can be connected to one of the

other 3 pins P0, P1 and P2. I’ll use P1. We need to send this a number

between 0 and 180 to represent the angle to which the servo motor’s

arm needs to point. To build the actual device you can use crocodile

clips or just wind stripped wire through the holes. But as soon as we

introduce a servo command, MakeCode’s simulator will fix one up for us!

In this short program we introduce some more

useful features of MakeCode. In the `on start’ block

we use a `servo write pin’ command. This comes

from a menu called `Pins’ which gets revealed when

you click on `Advanced’. Use the arrow to change

the pin to P1 and type in 90 in place of 180. This

will cause the simulator to display an animated

picture of a servo motor, wired in to P1, 3V and

GND. It also causes the simulated `arm’ to point in

the downwards position. The `on button A pressed’ blocks animates a slow swing of the

Page 5: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

metronome’s arm from 0° to 180°. This introduces the idea of a counted loop, as well as that of a

variable. The green `for index from 0 to 20’ loop creates a variable called `index’. The loop is

repeated 21 times with `index’ changing through 0, 1, 2, 3, …, 19, 20. We use the `Variables’ menu

to create our own variable called `angle’. This is given the value of `index’ multiplied by 9. So

`angle’ changes through 0, 9, 18, 27, …, 171, 180. The `multiply’ block is found in the `Math’ menu.

The value of `angle’ is written to pin P1 to control the angle of the servo’s `arm’. After each small

change of 9°, the program pauses for 200 milliseconds. A millisecond, written ms, is a thousandth

of a second, so we are waiting 200 thousandths, or 0.2 seconds, between movements.

Now that we can perform a single swing of the pendulum, we can build in some control to switch

the pendulum on and off. We could just use a button to do this, but we can also use one of the

micro:bit’s sensors to do it. The next version of the program brings in some more important

aspects of MakeCode. The variable `On’ is first created, and then given the value 0. The idea is that

`On’ can just be either 0, standing for `false’, or 1, standing for `true’. When `On’ is 0 we want the

pendulum to be still, and when it is 1 we want it to make a swing. The `on shake’ block just

reverses the state of `On’, so we have built a simple on/off switch using one of the micro:bit’s

gestures. The `forever’ block contains an `if’ command from the `Logic’ menu. The program has

been tidied up a bit by introducing the idea of a `function’. The `Function’ block is found in the

`Advanced’ menus, in dark blue. We have created the `Swing’ function to hold the loop that we

previously activated with the A button.

Servos are usually found in things like robotic hands and arms where we

need precise control. Here we just want to swing a pendulum from side

to side, so we can simplify the movement in the `Swing’ function. I f we

adjust the `pause’ command to 500 instead of 1000 in the `forever’

block, we have created a simple metronome which

just beats once per second, or 60 beats per minute.

Before looking at ways to adjust this, we can introduce some sound and

Page 6: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

light as well! The commands for making sounds

are in the `Music’ menu in orange. By inserting a

`play tone’ block in the `Swing’ function, the

MakeCode simulator wires up a stereo

headphone adaptor using the P0 and GND pins!

Because your computer has its own built-in

speakers, when you run the program on your

computer this command makes a short beep using them. You can adjust the

tone and duration to whatever you like.

As well as making sounds, we can also add light effects to our

metronome using special kinds of LEDs with variable colours

and brightness. Common names for these are `neopixels’ and

`zip strips’. The one shown is from Kitronik at £3.30.

The strip needs to be connected to the GND and 3V pins for power, and to one of

the other pins, such as P2, for control. In order to make the neopixels work with

the micro:bit we need some additional programming blocks, which are

conveniently available with MakeCode! The last item in the `Advanced’ list is

`Extensions’. Click on this and you will some of those available, such as `neopixel’.

When you install this, MakeCode adds the `Neopixel’ menu to the middle bar. In

`on start’ we can tell MakeCode how many LEDs there are, and which pin to use.

In `Swing’ we can change the strip from red to green etc. The simulator now

also shows the zip strip attached to pins P2, 3V and GND. So now we have the

full set of physical outputs from our micro:bit to the hi-tech metronome.

We just need to be able to adjust the rate at which the metronome beats.

That is where the buttons A and B come into play. We will introduce a variable, called `Beat’ which

will start with a set value, such as 60 beats per minute, and use A and B to adjust this up or down.

Page 7: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

Here is the tidied up code. We now have 6 blocks in our project. The `on start’ block sets up the

information we need to start. The `forever’ block is the heart of the system, starting and stopping

the metronome. It uses the `Swing’ function to do the work of swinging the pendulum, sounding

the buzzer and flashing the coloured lights. There are 3 event blocks which control the functioning

of the metronome. The `on shake’ block uses the accelerometers to make an on/off switch. The

`on button’ block changes the clock’s beat rate up or down, by 5 beats per minute at a time. There

is just one little bit of tricky maths involved. One minute is 60000 milliseconds. So the time taken

for each beat is worked out by dividing this by the value of `Beat’. Our `Swing’ function has two

pauses, one after swinging to the right, and another after swinging to the left. So these must each

take one half of 60000÷Beat, or 30000÷Beat each. So the `Gap’ between each of the two swings is

given by `Gap’ = 30000 ÷ `Beat’ milliseconds.

So we have now built our own smart electronic metronome, which has manual control for the beat

rate, and sensed control for turning on and off. The final step in this project is to bring in remote

control using a second micro:bit. Micro:bits can connect to each other, and to other devices, using

either serial connections through a wire to a USB port, or wirelessly using Bluetooth, or by radio.

We will use the radio commands from the `Radio’ menu to send messages between micro:bits.

Page 8: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

Using Radio commands is a bit weird at first. We don’t have to have different programs for the

transmitting and receiving micro:bits. It just works out which does what by knowing which

micro:bit issued a `radio send’ command, and which sensed an incoming `radio received’ message.

So the `on start’ block runs on each micro:bit as soon as they are powered up. Let’s call the

micro:bits T and R for Transmitter and Receiver. When you press button A on micro:bit T, it

increases its own value for the variable `Beat’ and displays it on its own led screen. But it also uses

`radio send number’ to transmit the new value of `Beat’ to any other micro:bit

programmed to be listening to the radio group called `1’. Both T and R are

listening in on the group, so whichever picks up a number through its `on radio

received’ block, that one will do something with the incoming value. So when

T hears an incoming number called `Signal’, it updates its own value of `Beat’.

So micro:bit T can now remotely control the beat rate of micro:bit R, which is

built into the metronome.

In order to be able to use gestures, such as `shake’, on T to control R we need

to be a bit more precise about how we send the messages. Instead of just

sensing a simple number, like 60, we can send a label and a number together,

using `radio send value’ instead of `radio send number’. So now R can work

out for itself whether to change the beat rate or to switch itself on or off!

In the `on radio received’ we want to test whether the received `name’ is a

“B” or not. In order to enter the text “B” rather than a number you need to

use the `” “’ block from the `Text’ menu in `Advanced’.

Now we are ready to put all the bits together. My first attempt had the servos

running on both the T and R micro:bits, so we do have to take care that the

`Swing’ function only applies to the R unit. This can be cured by having

another variable, `Receive’, which is originally set to 0 on both T and R.

Page 9: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

By having a `set Receive to 1’ command inside the `on radio received’ block, we can put an extra `if’

block in `forever’ so that only the R unit runs the `Swing’ function.

You can open the completed file from this link: https://makecode.microbit.org/_0jc4k9Yh80FP .

Up to this point, we have been doing the development using the simulator alone, just like real

product development takes place in industry! Now you can build your own working metronome,

with or without remote control.

Now the world is your oyster. Using just these basic ideas in MakeCode you design, make and test

as many autonomous and remotely controlled gizmos as your imagination can create. Here are

some ideas to get you started. Can you make some traffic lights or level crossing gates using more

servos and/or neopixels?

Page 10: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

You can use the Pins menu to experiment with reading from, and writing to, the various pins like P0

or P1. The program above simulates reading a value from an external sensor attached to P0. An

analog sensor, such as a light dependent resistor LDR, will return an analog value on a continuous

scale from 0 to 1023. The simulator detects when a pin is being read from, and allows you to adjust

the input value by dragging the shaded area around P0 up or down. The program detects whether

the value read is greater than 500, and if so it writes a 1 to pin P1, otherwise it writes a zero. So the

micro:bit is using the sensor attached to pin P0 to switch whatever is attached to pin P1 on or off.

Here is a variation in which P1 has a neopixel attached which shows red or green according to the

input on P0. Of course you could make a warning sound as well, but this will use pin P0, so you will

need to read the signal from P2 instead of P0.

Of course we could make the analog input on pin P0 control a servo as well, or instead. Here is a

way we can use the micro:bit to create a meter, using a servo connected to pin P1 to control the

needle. The input can take a value between 0 and 1023, and the servo can, typically, take an angle

between 0 and 180. So if we just divide the input by 10 we can get a reasonable change in angle.

Page 11: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

As before, you can adjust the input on P0 in the simulator by dragging the shaded area up or down,

which immediately moves the needle on pin P1. If the input on P0 was from an external

temperature or light sensor, you could use the servo to adjust the opening of a window or door. If

it was from a moisture sensor you could use it to drive pump to make an automatic watering

system. Could you make a fire sprinkler attached to a smoke sensor? Of course we could use an

on-board sensor instead. Here is one using the light sensor, which returns a value between 0 and

255, so we need to adjust the arithmetic a little. Again you can drag the symbol at the top left of

the simulated micro:bit to adjust the input light level.

Page 12: What is robotics and how can I start doing it? Adrian ......What is robotics and how can I start doing it? Adrian Oldknow November 2018 We all know that we live in an increasingly

We can use a similar approach to build a compass using the `compass heading’ input. You might

need to adjust the maths a bit if you want the compass needle driven by the servo to point in the

same direction as the simulated compass needle.

The great thing about using a single servo, is that they are cheap to buy, and do not draw much

power, so that they can be powered just using the 3V supply provided by the micro:bit. If you need

more servos, or heavier duty ones, or want to drive motors, you will probably need to give them a

separate power supply, and then use the micro:bit just to send control signals to the devices. But

that’s enough for now, and we’ll explore these and other developments in the next instalment.

Happy tinkering!