INTRODUCTION
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!
Requirements
Before starting this challenge, we recommend that you 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.
SOLUTION
A possible solution to this challenge is the following, although there are many others. Which one is yours?
async function main(){ var Robobo = require('./lib/robobo'); robobo = new Robobo('10.113.36.163'); await robobo.connect(); var closeIRValue = 120, mediumIRValue = 30, farIRValue = 10, speed = 20; robobo.setLedColorTo('all','off'); robobo.moveWheels(speed,speed); while ((robobo.readIRSensor('Front-C') < farIRValue)&&(robobo.readIRSensor('Front-RR') < farIRValue)&&(robobo.readIRSensor('Front-LL') < farIRValue)) { await robobo.update(); } robobo.setLedColorTo('all','green'); while ((robobo.readIRSensor('Front-C') < mediumIRValue)&&(robobo.readIRSensor('Front-RR') < mediumIRValue)&&(robobo.readIRSensor('Front-LL') < mediumIRValue)) { await robobo.update(); } robobo.setLedColorTo('all','magenta'); while ((robobo.readIRSensor('Front-C') < closeIRValue)&&(robobo.readIRSensor('Front-RR') < closeIRValue)&&(robobo.readIRSensor('Front-LL') < closeIRValue)) { await robobo.update(); } robobo.stopMotors(); robobo.setLedColorTo('all','red'); await robobo.pause(2); robobo.setLedColorTo('all','blue'); } main()
ADDITIONAL CHALLENGE
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.