Using Multitasking to Monitor Sensors
Read Chapter 12 of the Interactive C Manual. It discusses IC's facilities for multitasking. IC enables you to create several quasi-parallel processes. You can designate any procedure as a process and start it using start_process. By default, each process gets allotted 5 milliseconds. Using the optional parameters of start_process, you can change the default values, though you may not need to for most situations. A built-in scheduler assigns the cpu to an active task in round robin fashion.
Multitasking comes in handy when you want to separate the Perceptual Processing components in S-R agents. You can designate one or more perceptual processing functions and have them running as separate processes. Results of the processes are communicated to Action Functions using global variables. As an example, say you have a light sensor and you would like to detect whether it sees dark, based on a threshold value, THRESH. A calibrate function is used to set the threshold value. The skeletal program may look as follows:
#define LightSensor 3 /* Light Sensor is in analog port#3 */
int THRESH = 0;
int SeesDark = 0;
void monitor_sensors() {
while (1) {
SeesDark = analog(LightSensor) > THRESH;
}
} /* monitor_sensors */
void Calibrate() {
/* Calibrates the Light Sensor */
THRESH = ...
} /* Calibrate */
void main() {
Calibrate(); /* Calibrate the light sensor */
start_process(monitor_sensors());
while(1) {
/* Action Function here */
}
} /* main */
As you can see from above, first the threshold is set using Calibrate. Next, the monitor_sensors process is started, followed by the action selection loop. It is actually better to define the Action Function itself as a process. It may be defined as follows:
void ActionFunction() {
while(1) {
/* do your action selection here */
...
}
} /* ActionFunction */
Then, main simply does the following:
void main() {
Calibrate(); /* Calibrate the light sensor */
start_process(monitor_sensors());
start_process(ActionFunction());
} /* main */
From now on, try and use the above ideas in your S-R agents.
Exercises for this week
In the next lab session, you will demonstrate three simple creatures. All the creatures can use the same vehicle base. You will need two touch sensors, two light sensors, and either an infrared sensor or a bend sensor (whisker).
Creature#1: Dogged: the obstacle avoider
Dogged has two touch sensors: one in the front, and one in the back. When Dogged is started, it runs either forward or backward. Dogged changes direction every time either bumper gets pressed. Let loose, the vehicle will run across the floor until it hits something. When it does, the bumper will be pressed and it will reverse direction and run away. It will continue until it hits something in the other direction. It reverses and runs back to the first obstacle. In this way, it will fall into a pattern of running very quickly back and forth between two objects over and over again.
Creature#2: Insecure: the wall follower
Insecure has a whisker protruding on its left side. The amount of bend in the whisker can be registered and is used as a threshold (set using the knob) to detect walls. Insecure has exactly one motor running at any time. When the threshold on the whisker is not exceeded, the right motor runs, otherwise, the left motor runs. When Insecure is put down in an open area, the right motor will be driving, and it will turn in circles. Imagine, however, that it is put down so that there is a wall close to its left. The right motor will drive, turning the vehicle left and forwards, until the whisker touches the wall. Once this happens, the left motor will drive and the right motor will stop. Insecure will now move forwards and right. Soon it will have turned far enough right that the whisker will no longer be touching the wall, and it will start turning back. Insecure slowly edges its way along walls and around bases of pillars.
Note: I will wire up the whiskers and get them to you by thursday. You may also try and accomplish the same behaviors using the infrared sensors.
Creature#3: Driven: the light seeker
Driven is our standard light seeking vehicle. It has two light sensors mounted on its front. When the left sensor is receiving more light than the right side, the vehicle turns left. When the right sensor receives more light than the left sensor, it turns right. Again, this is accomplished by turning only one motor at any given time. Driven moves towards a bright light by successive right and left turns. It slowly wiggles its way towards light sources.
Collecting Reactions
Once completed, you should invite some friends to observe the three creatures and record their reactions for discussion.
Backgroud: These creatures were designed by David Hogg, Fred Martin, and Mitchel Resnick of the MIT Media Laboratory. They are inspired by a set of thought experiments designed by Valentino Braitenberg in his book, Vehicles: Experiments in Synthetic Psuchology, MIT Press, 1984. In the book, Braitenberg describes a set of increasingly complex vehicles built from simple mechanical and electronic components. Each of the imaginary vehicles mimics intelligent behavior in some way and each is given a name that corresponds to the behavior it imitates: Fear, Love, Values, Logic, etc.Braitenberg uses these thought experiments to explore psychological ideas and the nature of intelligence. Intricate behaviors seem to emerge from interactions of simple component parts. Hogg et al used specialized electronic LEGO bricks to build these creatures. For more details, see their paper Braitenberg Creatures.