Wall Avoidance (miniBloq)

Introduction

Sparki can use its ultrasonic sensor to avoid running into walls. In this lesson you will program Sparki to do just that!  

What You’ll Need

  • Sparki.
    • Some space for it to run around in and some walls. You can also use solid objects like cardboard boxes for your walls. Try to have hard walls (instead of soft things like pillows or cloth) since the ultrasonic range finder thinks those are easier to see.
 

How It Works

Here is the ultrasonic sensor or ultrasonic range finder. Sparki uses it to see what is in front of it in a similar way to how a dolphin or a bat uses echolocation. If you are not familiar with this sensor, please read this previous lesson. Ultrasonic Identify Also, please remember (you can find more information here) that this sensor has a conic detection zone, like this: Ultrasonic Cone Warning_Triangle Please remember to check that the batteries are properly connected (and charged!). And as we are going to use the motors here, please check that the On/Off Switch is on. Another important thing to take care of when playing with the robot’s motors is to be careful not to be working over a table. A fall from that table could permanently damage your Sparki. In this example, we’re going to code something where Sparki will wander with a green light on (using its RGB LED) until it finds a wall shortly ahead of it. Sparki then puts on the red light, moves backward, turns, puts the LED back to green, and then continues on. These are the basic tasks that we want the robot to do in order to advance while avoiding obstacles:
  • Center the ultrasonic sensor (so it points to the front).
  • Change the color of the LED to green.
  • Make Sparki go forward.
  • Sense distance with the rangefinder.
  • When Sparki’s rangefinder finds an obstacle:
    • Make Sparki beep using the buzzer.
    • Change the LED to red.
    • Make Sparki move backward.
    • Make Sparki turn.
 

Coding the Wall Avoidance Program

Now that we know what to do, let’s figure out how to do it using the blocks that miniBloq provides:
  • Center the ultrasonic sensor so it points to the front:
  • Change the color of the LED to green (later in this same lesson, we will see the values needed for this block’s parameters in order to turn the RGB LED green):
  • Make Sparki go forward:
  • Sense distance with the rangefinder:
  • Make Sparki beep:
Wall Avoidance (miniBloq)
  • Change the LED to red (later in this same lesson, we will see the values needed for this block’s parameters, in order to turn the RGB LED red):
  • Make Sparki move backward:
  • Make Sparki turn:
Putting pieces together, we can make chunks of commands which represent what Sparki normally does and what Sparki does when it finds a wall. Normally, we want the robot to:
  • Change the color of the LED to green.
  • Go forward.
  Here is the code (please note that “0” with the move command makes Sparki go forward forever, or until its told otherwise): And the commands for the chunk when it detects the wall or obstacle are:
  • Make the LED red
  • Beep
  • Move backward
  • Turn
Which translated into code reads like: Now let’s look at what that looks like when we put it all together! Remember that we want the robot to make decisions, so we have wrapped large parts of the code inside an if block (equivalent to the  if statements when programming in SparkiDuino), so it only triggers under the right conditions: PickerDefault.png We have also added a delay block before the robot checks the distance again:

BlockDefault.png

This is because Sparki’s brains are much faster than its wheels, so there is no use in checking again until the robot has moved a bit: Wall Avoidance (miniBloq) Now you’ve made Sparki avoid walls and obstacles!  

Related Lessons

To learn more about the ultrasonic range finder, take a look at the following lessons:  

SparkiDuino Programming Environment

This lesson is also available for the SparkiDuino Programming Environment. SparkiDuino will allow you to program your Sparki using standard Arduino syntax.