Course 7 / Lecture 8:

Map a room's walls using ultrasonic sensor

In this lectures you will learn how to map your room's walls using the ultrasonic distance sensor. Your job will be to create a program that follows the walls of the room by moving your robot forward, turning it when necessary and by constantly measuring the the distance to the wall.

Figure 1 - The Sniffer with ultrasonic sensor is capable to detect a rectangle room


Task #1:

Create a program which sets the cursor position

The beginning of the program starts with a 'when green flag clicked' entry point from the Control Menu. Create an initial variable, called broadcast (Figure 2). Drag the 'set broadcast to' block from the Variable menu and set it to number 1. You have to use the 'pen up' block and 'clear' block to clear the canvas. The cursor starts at the center of the canvas, so add a 'go to x:0 y:0' block from the Motion menu. Rotate the cursor towards 0° with the 'point in direction 0' block.

Figure 2 - Start your code by creating a variable and positioning the cursor

Task #2:

Write a program to start the motors on the robot

The robot should detect the wall four times, so add the 'repeat' block from Control menu and write 4 into it (Figure 3). You have to add the 'repeat' block from the Sensing category and a timer variable will also be required to draw the robot's path. It's time to start the motors with a 'Drive [motors] [direction] at [speed]%' block from the Lego motors menu. Here choose both A+D [motors], select the forward [direction] and set the motor [speed] to 15. In task 3# you will see why it is repeated 4 times. The reason is that a rectangle room has 4 walls to be detected with an ultrasonic sensor.

Figure 3 - Reset the timer and start the motor 4 times

Task #3:

Check the distance and turn right if the robot faces a wall

The robot should go forward until the distance between the robot and wall is greater than 8 cm. With a 'wait until [comparison_is_true]' block you can examine the distance (Figure 4). For this please use a '<' condition. Put a 'Distance in cm [connection]' block on it's left side, where the [connection] is the ultrasonic sensor and write '8' to the right side. After starting the motors, draw a path on the canvas by adding a 'move [number] steps' block, where the [number] should be 'timer x 30'. The 'timer' variable represents the path, it increases until the distance is greater than 8 cm. Then you should turn right with the 'Drive [motors] [direction] [time] sec at [speed]%' block to detect the next wall, where you should select both A+B [motors] and right [direction]. Set it to 1 sec and set the motor [speed] to 25, then these blocks will be repeated after detecting each wall. 4 turns and 4 walls will build up the rectangle on the Ozeki 10 GUI (Figure 4, top right corner).

Figure 4 - Turns the robot right after facing a wall

Task #4:

Draw the robot's path on the canvas and mark the walls

Step 1: Create 4 green squares to mark the wall

Create 4 green squares that will mark the rectangle walls on the Snap canvas. Add a 'when I receive [number]' entry point to all 4 squares (Figure 5). Insert '1', '2', '3' and '4' messages into the entry point and select the right one from the squares. Add a 'go to Sprite' block to all of them, so you will be able to reach them. In the last one you have to set the 'sp5_x' variable to the position of the x coordinate. Do this with a simple 'set [sprite] to [position]' block from the Motion menu.

Figure 5 - Create four green squares

Step 2: Put down the pen

You can draw on the canvas by using the 'pen down' block. Then add a 'broadcast [message] and wait' block from the Control menu (Figure 6) and set the [message] parameter to the 'broadcast' value. If the broadcast block is executed, it activates an entry point in 'Step 1', which places the cursor to the actual sprite (one of the green squares). The broadcast block is executed after each wall detection and then the pen is turned 90° clockwise and the 'broadcast' value is also increased by 1. The 'change [variable] by [value]' block is used for this. Finally 'wait 0.3 secs' before re-starting the Lego motors. The task will be repeated 4 times, so you will discover the rectangle room.

Figure 6 - Set the cursor to the actual green square

Step 3: Make a motion block that checks if the last green square has been drawn

Make a block that will connect the first and last Sprite with a pen. Click the 'make a block' button in the Motion menu and add the title text. You have to drag the 'if [condition]' block from the Control menu and add '=' comparison to the 'if' condition part. Place a 'broadcast' variable to the left side and check if it is 5 (Figure 7). The variable will switch to 5 after all 4 green squares are placed.

Figure 7 - Make a DrawLine block which will draw your rectangle in Step 4

Step 4: Finish the block by drawing the lines between all green squares

Drag 'if [condition] else' block into the previous 'if'. You have to add the '<' block to the 'if' condition part, where you have to select the 'sp5_x' variable to the left side and write 0 to the right side (Figure 8). This condition is true, if the 'sp5_x' variable is less than 0, so if its true, the 'if' branch is executed but its false, the 'else' branch is executed. Put the 'pen down' block to the if branch. After that use the 'go to Sprite2' block from the Motion category and the put the 'pen down' from the Pen menu. You need to change x to sp5_x variable's value, so add the 'set x to [value]' block and drag the sp5_x variable. In the else branch, add the 'set x to 0' block and 'go to Sprite2'. Add the 'go to Sprite5' block. It means the cursor first goes to the first point where it detected wall, Sprite2 and draw the line to the last point, Sprite5. This 'Brush drawline' block must run if you detect a wall. In the next task you will see where to place this block to effectively draw these 4 lines.

Figure 8 - This customized function draws the lines on the canvas

Task #5:

Test the program

Drag the 'DrawLine' block to the end of the program. Finally look at the code and test it. Put the robot to the center of the room and click to the green flag in the top-right corner of SNAP. The robot goes forward and if it detectes a wall, it turns right. A green square appears on the canvas after each detection and the cursor draws the robot's path between these green squares (Figure 9).

Figure 9 - The program that maps your room is completed

Program code with the new block

Figure 10 - This program code moves the robot and draws the path

More information


Copyright © 2000- |Ozeki Ltd | |
Page: 6178 | | Login
Thank you for visiting this page