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) 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 bump-func.ic.
void main()
{
printf("Driving\n"); // print message
drive(100); // both
motors on, full speed
while(checkBump()==0); // do nothing while no collision
ao();
// stop motors immediately
reverse(50, 0.5); // reverse a
bit and stop
printf("Collision!\n"); // print message
}
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, and the range of
values corresponding to white lines and white bumps on the
table.
These two ranges of values should not overlap. Choose a threshold
value somewhere between them. By comparing the value from the
sensor with the 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 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.
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. (Hint: this program will have a similar structure to
the one in Task 2.)
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 3
collisions, it should start looking for lines, and avoid crossing
them, as in task 4. After detecting 4 lines, it should
stop.
That is the basic requirement. If you have time, you should
consider refinements - for example:
beep to let humans know that a collision or a line has been
detected;
show which task is going on - is the robot looking for walls or
lines?
show how many collisions or lines have been detected so far;
respond to collisions while looking for lines;
allow the STOP button to halt the program at any time...
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 1 February. One
member of the team can write the report - it should be 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.
Submit the file containing your report and program by e-mail, to
roborugby@ee.ucd.ie.
Add the entire file as an attachment - NOT in the body of the
e-mail. Put your team number in the subject line of the
e-mail.
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 disconnect the interface and charging unit
from the computer, and put it and the power supply unit into the box
with everything else.
|