Lab Session 2
Introduction
In your first lab session, you built a simple robot and
programmed it to drive in a pre-defined pattern. You then
added some sensors to your robot - it can now detect collisions and
"see" the white lines on the table. In this session
you will use this information to control how the robot behaves.
Detecting Collisions
You should have two micro-switches mounted on the front of your
robot, to detect collisions on the left and right corners. If
not, add them now - see the instructions for Lab
1. The switches should be able to detect collisions over a
wide range of angles. To improve this, you can build a Lego
bumper in front of the switches - the picture below shows a rather
complicated example.

The switches should be connected to two of the digital input ports
(numbered 7 to 15, but not 8) on the Handyboard. You should have tested
that the switches are working, using the function digital(port_number).
If the switches are not working as they should, ask for help!

Testing
The program fragment below will make a robot drive forward until it hits an
obstacle, then reverse a little
and stop, with a message on the display. You can download the
entire program as collision.ic.
void main()
{
printf("Driving\n"); // print message
drive(100); // both
motors on, full speed
while(checkSwitch()==0){ } // do nothing while no collision
ao();
// stop motors immediately
beep();
// make a sound
printf("Collision!\n"); // print message
reverse(50, 0.5); // reverse a
bit and stop
}
Task 1
Modify this program to work with the ports where you have
connected your switches. Also, make it print a message showing on
which side the robot detected a collision. Use this to
test your collision detection - set up your robot to drive towards
the wall of the table at various angles, and make sure that it
detects every collision correctly. If necessary, change the
position of the switches or modify the bumper so your robot detects
collisions properly.
Task 2
Write a program (or modify the one above) to make your robot drive forward until it
hits an obstacle, then reverse, turn away from the obstacle,
and drive forward again. It should behave the same way
when it hits another obstacle. This should continue until you press the
STOP button on the Handyboard - you will need a loop in your program
to make this happen.
That means that if your robot hits
the wall on the front left corner, it should reverse and then
turn right, to get away from the wall. You may have to
experiment a bit to find how far it should reverse and how far it
should turn, so that it does not hit the wall again as soon as it
drives forwards.
To test this, set your robot in any position on the table - it
should be able to drive around continuously, recovering from
collisions as they occur. Try driving it into a corner of the
table - can it find its way out?
Save this program - you may find parts of it useful in Challenge
2, and also in the competition.
Detecting Lines
You should have an optical sensor mounted on your robot, to detect
the white lines on the table surface. If
not, add it now - see the instructions for Lab
1. The sensor should be connected to one of the analogue input ports
(numbered 2, 3 or 6) on the Handyboard. You should have tested
that the sensor is working, using the function
analog(port_number).
If the sensor is not working as it should, ask for help!
The program below will display the value from the
analogue sensor, updated every 0.5 seconds. You can download the program as
line-test.ic.
/* Program to test the optical sensor.
Sensor connected on port 2. */
void main()
{
while(!stop_button()) // keep doing this until STOP is pressed
{
printf("Port 2: %d\n", analog(2)); // print value from port
sleep(0.5); // delay to avoid flickering display
}
}
Task 3
Modify this program to work with the port where you have
connected your optical sensor. Try it with the robot in
various places on the RoboRugby table. Note the range of
values corresponding to black parts of the table, the range of
values corresponding to grey parts, and the range of
values corresponding to white lines and white bumps on the
table. These different ranges of values should not
overlap - if they do, you may need to change the height of your
sensor above the table.
When you are satisfied, choose a threshold
value somewhere between white and grey. By comparing the value from the
sensor with this threshold value, your program should be able to
decide whether the sensor is over a white line or not. If you
cannot find a suitable threshold value, ask for help!
Task 4
Write a program, similar to the one for task 2, to make your robot drive forward until it
"sees" a line on the table, then reverse a little and
turn. To decide whether it is "seeing" a line or
not, you should compare the value from the optical sensor with your
chosen threshold value - any value less than the threshold
will indicate that the sensor is over a line. It would be
useful to hide the detail of this in a function, as with the
switches above.
This process should continue until you press the STOP button, so
that your robot will drive around continuously on the table, without
crossing any lines. You may assume that it will start in one
of the larger areas of the table, so that it has room to move
around.
Challenge 2
In the competition, your robot will have to perform a series of
tasks in sequence. It will have to know when it has done
enough of one task, and then move on to the next task. This
challenge is a simple exercise along those lines, using two of the
tasks which you have already completed.
Starting from a random position on the RoboRugby table, get your robot to drive around the table,
recovering from collisions as in task 2. After exactly 4 collisions, it should start looking for lines,
and stop when it finds one.
That is the basic requirement. If you have time, you should
consider refinements - for example:
beep to let humans know that a collision has been
detected;
print a message on the display to show which part of the program the
robot is in;
show how many collisions have been detected so far;
while looking for a line, respond to a collision;
allow the STOP button to halt the robot and the program at any time;
while looking for a line, stop after 10 seconds if no line is
found...
Report
Add a large comment at the start of your program for Challenge 2.
This should explain who you are, what your program was
supposed to do, and how well it did it. This report can be
quite short, but it must provide enough information so that we can
make sense of your program. The program itself should have
plenty of comments to help with this.
You can work on the report outside the laboratory - any text
editor, such as Notepad, can open the program file and allow you to
add comments. The deadline for submission is 10am on Friday 29
January. One
member of the team can write the report - ideally a different
author each week. The entire team will be assessed on the basis
of this report and program, so you should all check that you are happy with it
before it is submitted.
You can submit your report electronically, to the
Digital Dropbox in Blackboard,
with your team number in the name of the submission. If you prefer, you can print the report and program and submit on
paper (stapled pages please) before you leave the laboratory.
Tidying
Disconnect the white cable from the Handyboard, and make
sure that the Handyboard is switched off. These
two steps are necessary to prevent the Handyboard battery from being
discharged over the next few days. We will charge these
batteries for a few hours before the laboratory session next week,
but there will not be time to re-charge them fully if you let them
discharge completely!
Put all your Lego parts
and your robot into the large plastic box. Make sure the robot
is accessible in the box, so that the Handyboard battery can be
charged later. Check the desk and the floor for stray pieces
of Lego - you may need them for your competition robot!
If you were working at a desktop computer, you can leave the
interface and charging unit on the desk. If you were using a
laptop computer, please put the interface and charging unit and the power supply unit into the box,
to leave the desk clear.
|