Avoid Drop-offs

For the most part, programming your cyber:bot to navigate around the virtual drop-off without going over the simulated edge is a matter of adjusting the if  statements from the script fast_IR_roaming.

First of all, instead of backing up, it will need to go forward 20 ms at a time when it sees objects with both detectors. 

It will also need to turn toward objects instead of away from them, and it will need to turn for more than 20 ms when it sees the drop-off.  375 ms turns seem to work well, but it will be up to you to adjust that value for best performance.

Example Script: avoid_table_edge

  • Use the micro:bit Python Editor to open fast_IR_navigation and change the project name to avoid_table_edge.
  • Update the script so that it matches the one below, and then click Save.
  • Click Send to micro:bit.
  • Disconnect the programming cable, and place the cyber:bot in your electrical tape or paint delimited course.
  • Set the cyber:bot board's PWR switch to position 2.
  • Can your cyber:bot drive around on the poster board without going over the black "edge?"
# avoid_table_edge

from cyberbot import *        
        
def forward():
    bot(18).servo_speed(75)
    bot(19).servo_speed(-75)
    sleep(20)

def backwards():
    bot(18).servo_speed(-75)
    bot(19).servo_speed(75)
    sleep(250)

def right():
    bot(18).servo_speed(75)
    bot(19).servo_speed(None)
    sleep(375)

def left():
    bot(18).servo_speed(None)
    bot(19).servo_speed(-75)
    sleep(375)

while True:
    irL = bot(14, 13).ir_detect(37500)
    irR = bot(1, 2).ir_detect(37500)
    
    if irL == 0 and irR == 0:                                      
        forward()
    elif irL == 1 and irR == 0:                                   
        right()
    elif irL == 0 and irR == 1:                                 
        left()
    else:                                           
        backwards()

How avoid_table_edge Works

The script avoid_table_edge is just the script fast_IR_roaming with a modified if statement in its while True loop.

The condition that used to go forward for 20 ms now backs up for 250 ms.  Likewise, the condition that used to back up now goes forward for 20 ms.  Also, the condition that used to call for a 20 ms right turn now calls for a 375 ms left turn, and the condition that used to call for a 20 ms left turn now calls for a 375 ms right turn.

Let’s look at the two statements side by side:

In response to if irL == 0 and irR == 0, the script fast_IR_roaming backs up because both IR detectors see an obstacle.  In contrast, the script avoid_table_edge goes forward because both IR detectors see the table, which means it’s safe to move forward for another 20 ms.

In response to else if irL == 0 and and irR == 1, the script fast_IR_roaming turns right for 20 ms, taking a step toward avoiding an obstacle on the left, while avoid_table_edge turns away from a drop-off that must be on its right.  

Also, irR == 0 and irL == 1, fast_IR_roaming turns left for 20 ms, taking an incremental step toward avoiding an obstacle on the right while avoid_table_edge has the cyber:bot turning right.

Lastly, any other condition has the fast_IR_roaming driving forward while the avoid_table_edge goes backwards away from the table’s edge.

Try This: Adjusting for the Application

The  turns to avoid the table edge can be adjusted for different applications.  For example, if the cyber:bot is supposed to hug the edge of the table, smaller turns might be useful.  In a contest where the cyber:bot is supposed to push objects out of an area, a larger turn (but not too large) would be better so that it zigzags back and forth across the table.

You can modify the code to make shallower turns by using a smaller sleep function argument.  For example, if you change the 375 in the left()function to 300, it will make shallower left turns.  If you change it to 450, it will make sharper left turns.

  • Modify the script avoid_table_edge so that it closely follows the edge of the simulated drop-off course. Adjust the sleep function arguments in the movement functions to make the cyber:bot execute smaller turns when it sees the drop-off.  How small can you make the turn before it tries to fall off?
  • Experiment with pivoting away from the table edge to make the cyber:bot roam inside the perimeter instead of following the edge.

Your Turn, If you Dare

Drive your cyber:bot on an actual tabletop at your own risk!If you try a tabletop after success with the electrical tape or paint course:

  • Remember to follow the same steps you followed before running the cyber:bot in the electrical tape or paint delimited course!
  • Your cyber:bot may detect you if you are standing in its line of sight.  Its current script has no way to differentiate you from the table below it, so it might try to continue forward and off the edge of the table.  So, stay out of its detector’s line of sight as you spot.
  •  Make sure to be the spotter for your cyber:bot.  It’s best to keep your hand poised so that you are ready to pick it up from above.  Be ready as your cyber:bot roams the tabletop:
  • Always be ready to pick your cyber:bot up from above as it approaches the edge of the table it’s navigating.  If the cyber:bot tries to drive off the edge, pick it up before it takes the plunge.  Otherwise, your cyber:bot might become a Not-Bot!