Prerequisite Knowledge
This lab does not assume any previous knowledge of event processing, It does require basic knowledge of boolean conditions, if and while statements, and how to control the movements of the Turtle.
Events are the driving force behind moving around in any interactive environment. We human beings are constantly reacting to what we sense in our environment, adjusting our behavior accordingly. Why shouldn't our robots behave in the same way?
Unfortunately, our Turtle doesn't have eyes to sense its environment with. But it does have bump sensors and various buttons that can be pressed. This lab will introduce you to the events that the Turtle can register. To process these events we will use boolean conditions to control the actions of if and while statements.
This lab does not assume any previous knowledge of event processing, It does require basic knowledge of boolean conditions, if and while statements, and how to control the movements of the Turtle.
There are many ways that a robot can sense and react to things in the world, such as bumping into something or having people press its buttons. These things are called "events" and our Turtle software queues them up in a waiting line so that it can deal with each in turn, in the order in which they happened.
You can take the next event out of the Turtle's event queue by using its nextEvent() method. It returns an integer that is one of the following named values:
Events remain in the event queue until your program removes them using the nextEvent method. However, if your program is ever in a situation where it no longer cares about any of the events that have been recorded (say it's starting over), you can clear them all by calling the clearEvents() method.
You can use event information to control the actions of if and loop statements. For instance, to make a Turtle move forward if the next event is the pressing of the run button we code the following if statement:
if ( Turtle.nextEvent() == Turtle.RUN ) {
Turtle.forward(500);
}
If you want to do one of two possible actions, you can use the if...else statement:
if ( Turtle.nextEvent() == Turtle.LEFT ) {
Turtle.left(45);
} else {
Turtle.right(45);
}
If you want to check an event variable for more than one value, it's best to save it in a variable:
int e = Turtle.nextEvent();
if ( e == Turtle.RUN ) {
Turtle.forward(500);
} else if ( e == Turtle.LEFT ) {
Turtle.backward(300);
}
Using events to control loops is done similarly. The following code fragment moves the Turtle forward until its left bumper is pressed:
Turtle.forward();
int event = Turtle.nextEvent();
while ( event != Turtle.LEFT ){
event = Turtle.nextEvent();
}
Turtle.stop();
Implement the last example in the previous section to have the Turtle move forward until its run button is pressed. When the run button is pressed the Turtle should then spin 360 degrees and stop.
Extend you solution to task one so that if the Turtle encounters an obstacle (i.e., a bump sensor is pressed), the Turtle stops, backs up a bit, turns 90 degrees in the opposite direction and then continues forward in that new direction. This will require the use of an if statement within the while loop. The Turtle should still spin when its Run button is pressed.
| Tip: | Having your bot back up a small distance before the turn will make sure it clears itself away from the wall before it turns so that it won't scrape the wall during its turn. If you don't do this your bot may not turn properly due to the bump sensor scaping the wall during the turn. |
Now we will add the use of View and Prgm buttons to control our Turtle's actions. Make your Turtle run in such a way that if you were to hit either the left bumper or the View button the Turtle would act as though the left bumper had been pressed in Task Two. Same goes for the right bumper and the Prgm button. Do this using boolean operators to allow for mutiple relational tests in your if statements
Now you design a program that will have your Turtle search for the exit from a randomly constructed maze. How should the Turtle act so that it finds the exit quickly? We will have a little contest and see whose Turtle can exit the maze most quickly.
In this lab you worked with the following concepts: