You are on page 1of 22

Introduction to Rescue Programming

for
Robocup Junior
Getting Started

To do this exercise you will need an NXT robot with two light sensors
a few centimetres apart, (see photo)

The motors should be connected to outputs A and C,


and the sensors to inputs 1 and 4.

You will also need to know how to use the basic blocks in the Mindstorms
software, and how to download and run programs on your NXT.

If you don’t know these things you could start with the booklet
Getting Started with Mindstorms and the NXT Robot from
Robocupjunior New Zealand.

2
Line Following

Click on File New and drag a loop block on to the


screen and put two switch blocks inside it.

Click on this switch block and configure it as shown below

If the light sensor is on the white background it reads over 55. If it is on the black line it reads less that 55.

3
Click on the second switch block and configue it as shown below:

Now add motor blocks inside the switch blocks like this:
Set these two motors
to go forwards.

Set these two motors to stop.

4
Call the program “straight black”. Save it and then download it into the NXT.

When you run the program the robot should follow a straight black line reasonably well.

However . . . try it on the curves and observe carefully what happens. See if you can understand why this happens,
and think about how you could solve this problem.

Explanation of basic line following:


If the light sensor reads
more than 55, program
follows the upper path and
switches the motor on.

The program is enclosed


within a loop, which
repeats forever.

If the light sensor reads


If both sensors are over the white background, the robot
less than 55, program
goes forwards. If a sensor goes on the black line, a motor
follows the lower path and
switches until the sensor gets back on the white background.
switches the motor off.
5
Finding the Victim

Once the robot gets to the large green square it has to locate a Coke tin which will be somewhere inside the square, and push
it right out of the square.

This can be done by just sweeping the robot back and forth across the square until the can get pushed out.

Start a new program and place the following blocks:

6
Configure the blocks like this: (Just click on a block to display its configuration panel.)

Motors A and C reverse, full power for 0.6 seconds

Motors A and C forwards, full power, unlimited.

Loop runs
forever.

Motor A turns robot for 0.6 seconds.


Wait until sensor is on white background.

7
Click File Save As, name your search program e.g. search and save it.

When you try out your program you may find that it misses out parts of the seach area. You may need to change the times
that it spends running in reverse, or turning, but with a bit of trial and error you should get it covering the search area pretty
well.

Looking Ahead

8
Transitions
When the robot arrives at a circle or a square, it needs to perform a turn to the left or right, so it needs to know when it
reaches these points.
The robot can tell when it gets to a checkpoint because both the sensor readings drop at the same time. On the black line
only one sensor reading is low at a time.

We use variables to store the values of the left and right sensors. We are now going to create one variable called left and
another variable called right.
Left will store the values of the left sensor, and right will store the values of the right sensor.

Open your “straight black” program.

Click on Edit Define variables and create one variable called left and one called right

Make sure both variables


are set up as Logic
variables.

9
Now select a variable block from the Data palette. Variable block

Insert variable blocks into your program at these points:

10
Set up these two variables as shown:

This means the variable right


will be set as false when the
right sensor is on the white
background.

It will be true when the


right sensor is over the
black line.

11
Set up the other two
variables as shown here:

This means the variable left


will be set as false when the
left sensor is on the white
background.

It will be true when the left


sensor is over the black line.

12
We need to stop going round the loop when we reach a checkpoint on the black line.
When this happens, both the left and the right variables are true.

Add two more variable blocks to the program like this:

Set up these two variables as read variables.


This allows the loop controller to read their
values.

13
To check whether the left and the right variable are both true we use a logic block from the Data palette.

Data
palette

Logic block

Place the logic block in the program like this:

14
Now pass the values of the left and right variables to the logic block like this:

Your mouse pointer will turn into a


wiring tool when you move it near
one of the data hubs

Also, pass the output of the logic block to the loop control point like this.

Now click on the logic block and set it up as an And block.

This means the logic block will only give a true signal if both the left and the right variables are true.

15
Finally, click on the loop

and set it up as a logic loop that keeps repeating until it receives a true logic signal.

The loop will keep repeating and the


robot will keep following the line until
the reading on both sensors
decreases.
This means the robot has reached a
checkpoint and it is time to start a
new action.

16
To see if your progam can detect checkpoints on the course, try stopping the motors and playing a funny sound after the loop
stops.

Motors A and C are stopped.

Your robot should now be able to follow a straight black line until it reaches a checkpoint, and then stop.

At this point please SAVE your “straight black” program again.

Challenge
Can you make the robot do a right turn when it reaches the checkpoint and get it ready to go around the
square or circle that it has reached.

17
Managing Your Progam

The two programs we have written so far, “search” and “straight black” are only part of the solution to the rescue problem,
yet already they cover way more than the size of the computer screen.

We need to simplify things so we can keep track of what we are doing. To do this we use things called My Blocks which allow
us to make our own blocks which contain entire programs. All of the stuff we have done so far will end up looking like this:

Clicking on a My Block will open up the program inside it and allow you to change it.

This is especially useful for Robocupjunior Rescue, because you will not know until you get there how
the rescue field has been set up. You just need to prepare a bunch of different My Blocks which
work on different parts of the field and when you see how the field has been set out, conncect your
blocks in the right order.

The next few pages will show you how to turn your programs into My Blocks.

Let’s start by opening the program “search” which we wrote earlier.

18
Once the program is open, drag your mouse pointer over it so it looks like this:

This will select the entire program. Then click on Edit Make A New My Block

19
When the My Block Builder opens, choose a name for your My Block

. . . and click Next

20
If you wish you can drag an image to put on your My Block icon.

. . . then click Finish

21
You now have a My Block which is a complete program that will search for the victim in the green square.

You can put this block into your program wherever you need it.

Your My Blocks are found on the Custom Palette which you open by clicking here:

When the custom paletter opens you can see you’re my Blocks by
moving your mouse pointer over here:

seek

To use your My Block just drag it onto the pages the same as any other block.
You might like to open your “straight black” program and convert that into a My Block also.

Then see if you can write a program that follows a black line to the search area and finds the victim.

22