Robots explore their environment, but they must be careful. They may find obstacles in their way or reach the edge of a precipice. If they learn to detect dangers and avoid them they won’t be damaged.
In this tutorial, you will learn how to detect obstacles and make Robobo stop before crashing. You will also learn how to turn on/off the LEDs and change their color like a light signal.
The challenge is the creation of an Anti-Shock Robobo!
Before starting this challenge, we recommend you to read the following sections of the Robobo programming manual:
challenge: anti-shock ROBOBO
Creating a program that makes Robobo go straight until it detects an obstacle.
Through the front infrared sensors, Robobo will detect the obstacle and, as it advances, it will change the color of the LEDs to indicate the distance to the obstacle (green if far away, yellow at medium distance, and red when near). In addition, when it is close to the obstacle, it will stop to avoid the collision.
A possible solution to this challenge is the following, although there are many others. Which one is yours?
from Robobo import Robobo from utils.LED import LED from utils.Color import Color from utils.IR import IR robobo = Robobo('10.113.36.163') robobo.connect() closeIRValue = 120 mediumIRValue = 30 farIRValue = 10 speed = 20 robobo.setLedColorTo(LED.All, Color.OFF) robobo.moveWheels(speed, speed) while (robobo.readIRSensor(IR.FrontC) < farIRValue)and(robobo.readIRSensor(IR.FrontRR) < farIRValue)and(robobo.readIRSensor(IR.FrontLL) < farIRValue): robobo.wait(0.01) robobo.setLedColorTo(LED.All, Color.GREEN) while (robobo.readIRSensor(IR.FrontC) < mediumIRValue)and(robobo.readIRSensor(IR.FrontRR) < mediumIRValue)and(robobo.readIRSensor(IR.FrontLL) < mediumIRValue): robobo.wait(0.01) robobo.setLedColorTo(LED.All, Color.MAGENTA) while (robobo.readIRSensor(IR.FrontC) < closeIRValue)and(robobo.readIRSensor(IR.FrontRR) < closeIRValue)and(robobo.readIRSensor(IR.FrontLL) < closeIRValue): robobo.wait(0.01) robobo.stopMotors() robobo.setLedColorTo(LED.All, Color.RED) robobo.wait(2) robobo.setLedColorTo(LED.All, Color.BLUE)
When you have completed the “Anti-shock Robobo” challenge, we propose you a new one that uses the same sensors, but in a slightly different way, shall we try?
Put Robobo on a table and try to prevent it from falling to the ground. Perform this challenge with care, if Robobo falls it can break. Use a slow speed for the tests of your program and stay alert in case you have to catch it to prevent it from falling while you debug your program.
Note that the infrared sensors Front-L, Front-R, Back-R and Back-L are inclined towards the floor, so these are the ones to use in this new challenge.