4.1. AI

Overview

Now that we know a basic algorithm used by a line follower we can add optimizations. Let’s suppose that the robot has to follow the line below:

Figure 1. Track

The robot could be:

  • on the line, centered, driving straight (i.e. The middle sensor detects the black line, and the other sensors detect nothing.)
  • on the line, entering a smooth curve or drifting slightly from the center of the line (i.e. The line is detected by a sensor positioned in close proximity to the middle one.)
  • partially on the line due to a sharp curve / drifting / slipping or other sort of error (i.e. Only the first, or the last, sensor detects the line.)
  • completely off the line due to a large error (i.e. None of the sensors detect the line.)
  • at an intersection of lines (i.e. All the sensors detect the line.)

From sensors to algorithm

We will use again read_sensors() function which returns a number representing the sensor positioned above the black line. Sensors are numbered from 0 to 4.

Now let’s split the above cases and let’s improve the previous algorithm.

  • read_sensors() returns 2 which is the middle sensor ⇒ the robot is centered on the line, so it should drive straight
  • If the value returned by read_sensor is 1 or 3 ⇒ the robot has a slightly offset from the middle, so it should take a soft left/right turn.
  • If the value returned by the function is 0 or 4 ⇒ the robot is partially on the track, so it should take a sharp turn to the left/right.
  • If no sensor is able to detect the line ⇒ the robot is off track, so it should try to find the line.
  • If all sensors detect the line ⇒ the robot reached an intersection. A default operation is driving straight.

In pseudocode should look like this:

 
  while(true)
  {
    line_position = read_sensors();
    if (line_position == 2) drive_straight();
    
    /* Small offset from the middle line */
    if(line_position == 1) soft_turn_left();
    if(line_position == 3) soft_turn_right();
    
    /* Larger offset from the middle line */
    if(line_position == 0) turn_left();
    if(line_position == 4) turn right();

    /* No line is detected */
    if(line_position == NONE) try_to_find_the_line();
    
    /* If all sensors detect the black line */
     if(line_position == ALL) drive_straight();
   }

Searching for the black line

There are several ways to get back on the line if the robot lost the track somewhere:

  • Spinning
    • Spinning is the quickest way to get back on track if the line is in the robot’s sensor range.
    • Spinning involves turning a motor forward and the other one backward.
0j850.600.jpg
Figure 2. Just like a spinning top
  • Circling
    • Circling involves turning both motors forward but at different speeds.
    • If the speed on the right motor is lower than the speed of the left motor, the robot draws a circle in a clockwise direction.
    • If the speed on the right motor is higher than the speed of the left motor, the robot draws a circle in a counterclockwise direction.
Figure 3. Circling a point
  • Spiraling
    • The robot could start with different speeds on each motor. Let’s say 80 on the right motor and 40 on the left motor.
    • The robot starts to move and stops after half a second.
    • The speed on the left motor is incremented and on the right motor is decremented.
    • This will draw an outward spiral.
Figure 4. Outward spiral

Resources

roboticsisfun/chapter4/ch4_1_ai.txt · Last modified: 2012/11/25 16:35 by liviu.radoi