Previously, we planned three functions for the robot (line following, turning, and skipping missing segments). However, we now face situations with two simultaneous turns, where only one is correct. A closer inspection reveals two details that will help us plan our solution:
- After a wrong turn, the correct path is always 180° in the opposite direction.
- There are crossroads with two incorrect paths, which the robot must be able to account for.
With this in mind, we make the following changes to the previous program:
- The robot does not turn immediately after detecting a turn; instead, it remembers the turn’s direction while continuing forward. This ensures that the robot always moves forward when facing a crossroads.
-
After detecting a turn, it resets the relative position of one motor to help determine if it has passed a crossroads or if it needs to turn immediately due to no path forward.
- After each turn or successful segment skip, the robot clears any previously detected turns from memory.
- If skipping a missing segment fails to find a line and no turns are detected, the robot assumes it took a wrong turn, makes a 180° turn, and goes in the opposite direction. Since the wrong turn always ends near the crossroads, this approach ensures recovery.
- If skipping a missing segment fails to find a line and turns are detected, the robot returns to the crossroads, where the motor position was reset, and makes a turn.
- All turns have two sets of commands: one for immediate turns and one that returns to the crossroads before turning.
These changes add complexity, so let's review how this program behaves in different scenarios. First, let’s start with a simple turn:
- The robot detects a turn but attempts to continue forward.
- When it finds no path forward, it initiates a turn.
- The turn command detects a low motor position and executes the immediate turn sequence.
- The robot resets its memory of turns and resumes following the line.
Next, let’s see what the robot does when it encounters a "T" section and makes the wrong turn:
- The robot detects turns in both directions but continues forward.
- When it finds no path forward, it initiates a turn.
- The turn command detects a low motor position and executes the immediate turn sequence.
- The robot resets its memory of turns and resumes following the line.
- Upon reaching the end of the path, it attempts to skip a missing segment.
- After the skip, it finds no line and no turns since they were reset, so it makes a 180° turn and resumes following the line.
Finally, let’s see what the robot does when it encounters a crossroads with two incorrect paths:
- The robot detects turns in both directions but continues forward.
- Upon reaching the end of the path, it attempts to skip a missing segment.
- After the skip, it finds no line but detects previously identified turns, prompting it to turn.
- The turn command detects a high motor position and executes the correction turn sequence.
- The robot resets its memory of turns and resumes following the line.
- Upon reaching the end of the second path, it again attempts to skip a missing segment.
- After the skip, it finds no line and no turns since they were reset, so it makes a 180° turn and resumes following the line.
In each scenario, the robot initially takes the wrong path but ultimately finds the correct one. This program’s simplicity—relying on a few rules rather than memory-based path tracking—demonstrates how basic logic can address complex challenges.