Tutorial Details:
Difficulty Level:
Intermediate
Topics Covered: Using
events and watchers in relation to the touch sensor.
Assumed Knowledge:
The Basics, My First Program, Get Moving, Lighting
Up (parts 1, 2 & 3), Making Noise.
Written By: BILL LANE
Print Version
There's not much point programming a robot if it can't make
some decisions based on external events. One of the most common
events when we set our Spybot loose in the world is that it'll
probably run into something. Then you'll most likely trun
it around with your foot so it can keep going. But it would
be much better if the Spybot could get itself out of trouble.
So that's exactly what we're going to do. To do this we need
to define the event and create something called a watcher
to keep an eye out for that event. But the first step is to
declare the sensor so we can use it. You'll find that the
sensor is declared in spybot.h. But so we understand the process
we'll declare it ourselves. The declaration looks like this:
sensor bumper on 1
This is very similar in format to declaring a motor, but
instead of output we use
sensor. Then we give the
sensor a name and tell it we're talking about sensor 1 (sensor
2 is the light sensor). So now we can use the touch sensor.
The next task is to define an event for that sensor. For
the touch sensor the relevant event is called pressed.
There are other types of events that the Spybot is capable
of detecting relating to the light sensor, the VLL and targets.
But we'll need to look at them another day. For now here's
the event declaration:
event BumpEvent when bumper.pressed
We start with the event
keyword and then we provide a name for the event. Here it's
called BumpEvent. We could
call this anything but it will make life easier if we give
it a logical name. Because we're going to need to use it again
soon. Next comes when followed
by the objects name (here it's bumper
which we declared before) abd finally the event itself.
That's all the declarations we'll need. The next step is
to define the watcher.
The watcher is a task
in the same way that main
is a task. As such it
is given it's own code block after the main command block.
To create the watcher we need to give it it's own name, then
tell it the name of the event we want it to watch and then
provide some commands for it to execute when this event occurs.
Here's an example:
watcher hit monitor BumpEvent{
reverse [left right]
}
So we've called our watcher
hit and it's going to monitor
BumpEvent that we declared earlier. When the watcher
is triggered then it will tell the Spybot to
reverse direction. This obviously assumes that we've
declared both motors. That'll do for a start. But we'll want
to add a few more commands here or our Spybot will just drive
backwards until it hits something (an event it won't be able
to detect). But we'll come back to that in a moment. Right
now we have one more essential job if we want this thing to
work.
We've created the watcher
but we haven't turned it on. As you'll remember the Spybot
starts by executing the main
task. But if there is nothing there the program will just
close. Therefore we need to place a start
statement inside the main
task to turn on the watcher.
I'll also start the robot moving so the watcher has something
to watch. Here's the main task:
main
{
fd [left right] on [left right] //move forward
start hit //start the
watcher for front sensor
}
Great! It'll work now. But we do want to do some more work
on that watcher. What
I've done is to get it to move backwards for 1 second and
then to turn and continue forward. I've also used some of
the pre-programmed sound and light sequences to provide some
feedback. So here's the completed code:
program sensorTest
{
//output ports
output right on 1 //define
motor 1 as right
output left on 2 //define
motor 2 as left
//sensors
sensor bumper on 1 //define
sensor 1 as bumper
event BumpEvent when bumper.pressed
//create bumpEvent to watch for sensor being pressed
main
{
fd [left right] on [left right] //move forward
start hit //start the
watcher for front sensor
}
watcher hit monitor BumpEvent{
//define watcher for bumpEvent
display 6 //that's ledAlarm
sound 14 //that's sndShocked
bk [left right] //change
motor direction
on [left right] for 100
//move backwards for 1 sec
dir right left //turn
for 1 sec
on [left right] for 100
fd[left right] //move
forward
on[left right]
clear sound
clear display
}
}
You'll notice that at the end of the watcher I've turned
off the sound and light sequences to indicate a return to
normal running. Although the watcher command block has finished.
The watcher continues to run in the background. So the next
time your Spybot hits something it will run this block of
commands again.
There's obviously a lot more we could do with this program.
We could make the main task more interesting by getting the
spybot to scan left and right randomly, we could add some
light and sounds relevant to these actions. We could add another
event and watcher couple to respond to light events. But that's
all getting a bit beyond the range of this tutorial. |