Escaping Corners
When running roaming_with_whiskers, you may have seen your cyber:bot get stuck in a corner. Its left whisker touches a wall so it turns right, then its right whisker touches a wall so it turns left, over and over again, until you rescue your robot.
Track Input to Recognize Patterns
The next example project counts alternate whisker presses to determine if the cyber:bot is stuck in a corner. To do this, the project has to remember what state each whisker was in during the previous contact. If they are opposite, then one is added to a counter. If that counter goes over a threshold that you set, then it is time to turn around out of the corner (and reset that counter). In this way, your cyber:bot “remembers its experience” to be “aware of its predicament” so it can respond appropriately; a primitive example of artificial intelligence.
This project relies on nested if…else statements. The project checks for one condition, then if that condition is true, it checks for another one within the first condition. The project escaping_corners actually uses three if statements nested together.
Example project: Escaping Corners
- Put the cyber:bot board’s power switch in position 0 or 1.
- Enter and flash escaping_corners.
- Put the cyber:bot on the floor in a corner and set the switch to position 2.
- Using obstacles or your hands, alternate right and left whisker presses to simulate being stuck in a corner. After four or five bounces off of alternate walls, your cyber:bot should turn around and leave the corner.