KevsRobots Learning Platform
By Kevin McAleer, 5 Minutes
To prevent collisions with obstacles, use bot.distance to measure the distance between the object and your bot. If the measured distance is less than a threshold (for example 15 cm), stop the bot, back up and turn to maintain a safe distance from the obstacle.
The ultrasonic rangefinder works by sending out sound waves and measuring how long they take to bounce back. The bot.distance property returns the distance in centimeters to the nearest object.
The code:
from burgerbot import Burgerbot
bot = Burgerbot()
while True: # Infinite loop - runs forever (note: capital T in True!)
distance = bot.distance # Get current distance in cm
if distance <= 15: # Object closer than 15cm
print(f"Too close! Distance: {distance}cm")
bot.stop()
bot.backward(0.5) # Back up
bot.turn_left(0.5) # Turn away
else:
print(f"Path clear. Distance: {distance}cm")
bot.forward(0.1) # Keep going, checking 10 times a second
How it works:
while True: creates an infinite loop that runs continuouslybot.distance gets the current distance reading from the rangefinderforward(0.1) means the robot checks the distance ten times a secondWhy short bursts? Checking often allows the robot to respond more quickly to obstacles, making navigation smoother and safer.
bot.distance returns 0 or very large numbers (like 400)Why: Incorrect wiring prevents the sensor from sending or receiving ultrasonic signals
Why: Sound waves need time and space to separate transmission from echo
Why: Soft materials (like fabric) absorb sound waves, and angled surfaces bounce them away. Colour doesn’t matter to sound
while True: with a capital T. Python is case-sensitive!Why: while true: will cause a NameError because true is not defined in Python (it’s True)
random.uniform(0.3, 1.0) from the random moduleReady to experiment with object avoidance? Try these challenges:
Adjust the detection threshold: Change if distance <= 15: to if distance <= 30:. How does the robot’s behavior change? What about distance <= 5?
Smarter turning: Turn left or right at random after reversing. Hint: import random and random.choice([bot.turn_left, bot.turn_right])(0.5).
Variable speed: Make the robot slow down as it approaches objects. Use the distance value to calculate motor speed.
Obstacle counter: Keep track of how many objects the robot has avoided. Print this count to the console.
Advanced Challenge: Create a robot that maintains exactly 10cm from a wall as it drives parallel to it. You’ll need to constantly adjust direction based on distance readings!
To follow an object, flip the idea around. Measure the distance, then move forward if the object is too far away, backward if it’s too close, and stop when it’s just right. Hold your hand in front of BurgerBot and it will follow it like a pet!
from burgerbot import Burgerbot
bot = Burgerbot()
bot.speed = 0.4
TARGET = 20 # the distance we want to keep, in cm
MARGIN = 5 # how close is 'close enough'
while True:
distance = bot.distance
if distance > TARGET + MARGIN:
bot.forward(0.1)
elif distance < TARGET - MARGIN:
bot.backward(0.1)
else:
bot.stop()
The MARGIN gives the robot a ‘just right’ zone. Without it, the robot would jitter back and forth around the exact target distance.
Quick quiz 1 of 2
In the avoidance code, why do we call bot.forward(0.1) rather than bot.forward(2)?
forward() is waiting, the robot isn’t checking the distance. Short waits keep it responsive.Quick quiz 2 of 2
What is MARGIN for in the object-following code?
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments