One of the major design goals for Rover has been autonomous behavior that avoids collisions. Basically, turn him on, watch him go and NOT run into anything. Here I will describe the main program and the Collision Avoidance (CAV) function.
The entire main program:
#define FIRST_STOP_DX 24
main ()
{
Rover_SetUp(); // all of the initialization code, set up and enable the ISRs
TurretAngle(0); // initialize Rovers eyes to straight ahead
MsDelay(2000); // give Rover some time to settle
while (1) // main loop
{
Rover_CAV ( FIRST_STOP_DX, 30 ); // mover Rover forward until he detects an obstacle (<24″) at 30% of full speed
MsDelay(2000); // some time to think
Rover_Go ( ROVER_REVERSE, 15, 15 ); // backup a little
Wait4Motors(); // wait for the motors to stop
Rover_Search(); // get distance for each of three angles: -45, 0, +45
if ( Obstacles[0] > Obstacles[2] ) Rover_Static_Turn ( -45, 50 ); // turn 45° away from the obstacle
else Rover_Static_Turn ( 45, 50 );
Wait4Motors(); // wait for the motors to stop
MsDelay(1000); // Rover is thinking again!
} // main loop
}
Now for the Collision Avoidance function:
void Rover_CAV ( int Stop_Dx, int Speed )
{ int distance, DirFlag, int i;
CurrentAngle = 0; // current turret angle
DirFlag = 1; // 1 = CW, -1 = CCW
while (1) // stay in loop until obstacle is detected
{
Rover_Go ( ROVER_FORWARD, Speed, 15 ); // go forward for about 1 second (15 pulses)
while ( Motor_Status ) // while motors are running
{
for ( i=-1; i<=1; i++ ) // for each scan angle
{ CurrentAngle = i*SEARCH_ANGLE*DirFlag;
TurretAngle(CurrentAngle); // turn the turret
for ( SRF05ICdone=0; SRF05ICdone<5; )
/* The SRF05 is triggered once every 50msec. Its output is a pulse proportional to the object distance. The pulse is measured by the Input Capture system
of the Rabbit. An interrupt is generated by Input Capture the end of each measurement of the SRF05 sonic sensor. The variable SRF05ICdone is
incremented with each of the interrupts. This for loop has the effect of causing a delay until 5 SARF05 readings have completed. */
{ if ( (distance=GetDistance()) < Stop_Dx ) // if obstacle
{ Rover_Stop(0);
return;
} // if obstacle
} // 50msec per count
} // for each scan angle
DirFlag *= -1; // reverse the scan direction
} // while motors are running
} // stay in loop until obstacle is detected
} // Rover_CAV