ChickBot Pi dance & sing

So, let’s put it all together so we can get our ChickBot moving & grooving at your command.

This is our starter program that your ChickBot and it adds in some little extras to make it super simple for you to say how far it should move or turn. And we give you all the notes we know about so you can get any tune you like playing!

As the program is a bit longer than the others and has more detailed parts to it, it’s best to download it rather than doing lots of copying and pasting.

You need to download two files for the program to work. Download the file main file, ChickBot_Pi_Starter_v1.2.py, as well as this one, pitches.py, and save them into the same folder. ChickBot_Pi_Starter_v1.2.py contains all the code for the program, and pitches.py contains all the notes the program needs to play a tune.

Open up the ChickBot_Pi_Starter_v1.2.py in Geany (either from the Open command on the File menu, or double click the file). Scroll to the very end and take a look at the last section that starts while True: and you’ll see how we’ve made it almost like English to command your ChickBot. The commands you can use are picked out below in orange.

We start out with a version of PlayTune() that can take different tunes you’ve already setup, followed by a DelayS() function which delays in seconds (or fractions of, so you can have a 1.5 second delay if you want). Then we go into our forever loop, starting with a SoundAndLEDAlert() command that plays a tone and flashes the on-board LED.

We’ve got a RunOrShutdown() command that waits for the button to be pressed. There’s some extra code there that figures out how long you’ve held the button down for. If it’s held down for more than 3 seconds, it shuts your Pi down safely.

We have the Delay() command which takes a delay in milliseconds. A millisecond is a 1000th of a second and is a generally useful sort of delay in robotics.

As we have set some calculations and definitions at the top of the program, we can tell ChickBot how far to move in the units of your choice, mm, cm or inches.

This gives us a super useful command of GoForward() – inside the brackets we tell the command how many steps to go, and by using cm or mm or inches, we can say how far to go. Here’s some examples:

GoForward( 10 * cm ) = move forward 10 cm

GoForward( 5 * mm ) = move forward 5 mm

GoForward( 3 * inches ) = move forward 3 inches

I bet you can guess what the backwards command is??? Yup, it’s GoBackwards() with exactly the same options.

Want your ChickBot to stop? That’s what the AllStop() command does. I don’t want to use the word Stop as ‘stop’ is a part of the programming language and if someone forgets the upper case S, the program won’t work as expected.

We have two ways of turning:

The simplest one is the HardTurn() with whether you want it to go Left or Right and how many steps. A hard turn is a turn that’s on the spot – one wheel goes forward and one wheel goes backwards, so ChickBot pivots around its front axle.

The second moves in an arc and is called SoftTurn(), again, you say if you want it to turn Right or Left and how many steps. A soft turn keeps one wheel stopped and the other wheel drives forward. This means it is a bigger turn than a hard turn, something to remember when planning your movements.

The program calculates the number of steps that are needed for various turn sizes, so you have some pre-prepared values of hardTurn360, hardTurn90, softTurn360 and softTurn90. The number in the name refers to the number of degrees of turn. As they are numbers, you can use maths to get different values – so if you want to turn around, a 180° turn, you could use hardTurn360 / 2 or hardTurn90 + hardTurn90. Using softTurn values with the hardTurn() command and vice versa won’t do any harm but won’t make any sense as the calculations can’t be mixed together.

With the new improved PlayTune command you can setup as many tunes as you like and play which ever one you want. The tune (or music) definition is the same but with the option of calling it something more memorable:

Warning!There is no harm in trying stuff out. Using these commands can’t do any damage to your ChickBot unless you drive it off the edge of a table and it falls on the floor. So this isn’t much of a warning really, I just wanted to get your attention.

So, make changes, one at a time, to the loop section and see how you can get your ChickBot moving and singing to your command.

Quick Reference chart

ChickBot commands

Action

Command to use

Beep & flash the LED

SoundAndLEDAlert()

Wait for the button to be pressed

WaitForButtonPress()

Delay in milliSeconds – example shows 500mS = 0.5 seconds

Delay( 500 )

Delay in Seconds – example shows 1.5 seconds

DelayS( 1.5 )

Stop all motors

AllStop()

Go forward – example, 10 cm. Note the *

GoForward( 10 * cm )

Go backward

GoBackward( 10 * cm )

Turn on the spot – example shows 90º to the left

HardTurn( hardTurn90, LEFT )

Turn around a wheel – example shows 90º to the right

SoftTurn( softTurn90, RIGHT )

The soft & hard turn angles can not be mixed up, only use hardTurn90 with a HardTurn. As they are just numbers, you can use maths to calculate other angles – e.g. softTurn90 / 2 would be 45º