Page 205
Figure 10-2.
Hank, retrofitted with a light sensor
• avoid() is the highest-level behavior. It is triggered by the bumpers and does the standard back-up-and-turn.
Convincing Hank to seek light is surprisingly hard. You can, of course, implement your own algorithm in seek_enlightenment(). It's a neat feature of subsumption architecture that you can
concentrate solely on light-seeking behavior in seek_enlightenment(). The obstacle avoidance behavior is already programmed in a different behavior and will subsume the light-seeking
behavior as necessary.
Without further ado, here's the entire program, LightSeeker.c:
#include "conio.h"
#include "direct-button.h"
#include "direct-motor.h"
#include "direct-sensor.h"
#include "unistd.h"
#include "sys/tm.h"
#define MAX_TASKS 32
pid_t pid[MAX_TASKS];
int task_index;
#define BACK_TIME 500
#define TURN_TIME 800
// Motor commands.
#define COMMAND_NONE -1
#define COMMAND_FORWARD 1