An interesting thing about these IR detectors is that their outputs are just like the whiskers. When no object is detected, the output is high; when an object is detected, the output is low. You could modify the script roaming_with_whiskers so that it works with the IR detectors with just a few changes. Here are the steps it takes:
irL = bot(14, 13).ir_detect(37500) irR = bot(1, 2).ir_detect(37500)
if irL == 0 and irR == 0: backwards() right() elif irL == 1 and irR == 0: backwards() left() elif irL == 0 and irR == 1: backwards() right() else: forward()
# roaming_with_IR 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(250) def right(): bot(18).servo_speed(75) bot(19).servo_speed(75) sleep(250) def left(): bot(18).servo_speed(-75) bot(19).servo_speed(-75) sleep(250) while True: irL = bot(14, 13).ir_detect(37500) irR = bot(1, 2).ir_detect(37500) if irL == 0 and irR == 0: backwards() right() elif irL == 1 and irR == 0: backwards() left() elif irL == 0 and irR == 1: backwards() right() else: forward()
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.
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.
# 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()
Though this script has a similar structure to roaming_with_IR, there are three key changes that improved performance: