Faster IR Navigation

The style of pre-programmed maneuvers from the last activity were fine for whiskers, but are unnecessarily slow when using the IR detectors.  With whiskers, the cyber:bot had to make contact and then back up to navigate around obstacles.  With infrared, your cyber:bot will detect most obstacles before it runs into them, and can just find a clear path around the obstacle.

Increase the Sampling Rate to Avoid Collisions

As your cyber:bot navigates, it will execute a series of small turns to avoid an obstacle before it ever runs into it.  With that approach, the cyber:bot never turns further than it has to, and it can neatly find its way around obstacles and successfully navigate more complex courses.  After experimenting with this next script, you’ll likely agree that it’s a much better way for your cyber:bot to roam.

  • Set the cyber:bot board's PWR switch to 0.
  • Use the micro:bit Python Editor to open a previous cyber:bot project, like fast_IR_roaming or cyberbot-template-with-blink.
  • Set the project's name to fast_IR_roaming, update the script so that it matches the one below, and then click Save.
  • Click Send to micro:bit.  
  • Unplug the cyber:bot from USB, put it in an area for roaming, and set the cyber:bot board's PWR switch to 2.
  • Test it with the same obstacles you used with the script roaming_with_IR.  Is your cyber:bot roaming more efficiently?
# fast_IR_roaming

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

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

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

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

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

How fast_IR_roaming works better

Though this script has a similar structure to roaming_with_IR, there are three key changes that improved performance:

  1. The sleep function arguments have been drastically reduced from 250 ms to just 20 ms.  This lets the cyber:bot roam much faster.
  2. The script removes the backwards function when only one IR LED detects something.  Since the cyber:bot is no longer physically touching objects to detect them, it does not need to back up before turning.
  3. The script also performs a one-servo-forward pivot turn instead of rotating left and right using both servos.