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
- Ozeki Sniffer: http://www.ozeki.hu/index.php?owpn=6204
- Ozeki 10 installed: http://www.ozeki.hu/index.php?owpn=6066
- Lego connection installed: http://www.ozeki.hu/index.php?owpn=6067
- Snap basics: http://www.ozeki.hu/index.php?owpn=6180
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
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
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
Draw the robot's path on the canvas and mark the walls
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
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
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
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
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
|The program code maps the room by using an ultrasonic sensor|
It turns your Lego robot 90° right all the time it gets 8 cm close to the wall.
It is easier to follow the guide if you have this small code in front of you:
Download Ozeki Snap code to map your room with an ultrasonic sensor
- Mapping and Navigation
- Going to a coordinate
- Collision detection / sensor feedback based navigation
- Surround sensing using linear distance sensor
- Create a parking assist system
- Homing to a dock using an IR beacon
- Follow me lunch bag
- Mapping a room with LIDAR
- Outdoor navigation using GPS and mobile network