Commented Robot Demo Program (Excerpts)

Processes are startet in the main program and run concurrently. The multi-tasking operating system switches every 5 ms between tasks (time slices). Each robot behavior may be modeled as a process, with priorities among other processes.
void main()
{ sleep(0.75);				/* short pause before driving */
  alert_tune();				/* play a melody */
  start_process(printer());		/* LCD output */
  start_process(motor_control());	/* motor control */
  start_process(cruise());		/* drive straight */
  start_process(photo());		/* read light sensors */
  start_process(ir());			/* read infrared sensors */
  start_process(check_sound());		/* listen for noise */
  start_process(check_bump());		/* read contact sensors */
}

Process cruise becomes active, if none of the sensor processes report activity. cruise drives the robot straight for max. 1 second.
int cruise()				/* default activity */
{ while(1) {				/* endless loop for process */
    cruise_command = FORWARD;		/* robot moves straight */
    cruise_active = 1;
    wait(1000);				/* drive 1 second */
  }
}

Process photo reads the light sensors. If a light source has been recognized, photo will change the robot's course to approach the light.
int photo()				/* follow a light source */
{ int lpc, rpc, delta;	
  while (1) {				/* endless loop for process */
    lpc = analog(1) + photo_cal;	/* read sensors */
    rpc = analog(0);
    delta = rpc - lpc;			    /* search light */
    if ( abs(delta) > photo_dead_zone ) {   /* difference > threshold */
      if (delta>0) photo_command=LEFT_ARC;  /* left light => left curve */
        else photo_command = RIGHT_ARC;	    /* else right curve */
      photo_active = 1;			    /* found a light source */
    }
    else photo_active = 0;		/* inactive */
    defer();				/* wake up next process */
  }
}

Process motor_contol arbitrates the vehicle control according to fixed priorities, in case more than one control process becomes active. The process evaluates global variables, set by the individual behavior processes. E.g. if bumpers and infra-red sensors are active at the same time, the bumper process gets control over the robot.
void motor_control()
{  while (1) {
     if (bump_active)
        move(bump_command);
      else if (ir_active)
        move(ir_command);
      else if (photo_active)
        drive(50.0, (float) del_out * p_gain);
      else if (cruise_active)
        move(cruise_command);
      else
        move(STOP);			/* no command => STOP */
      defer();				/* hand over processor */
    }
}

tml">Thomas Bräunl
Last modified: Wed Aug 08 15:26:39 1995