Course 7 / Lecture 9:

# Mapping a room with LIDAR

Laser distance measuring devices used on a rotation base are called Lidars. They are used in self driving cars, robot vacuums and in many other robotics equipment. In this lecture you will learn how Lidars work, and you will build one using an ultrasonic distance sensor.

## Instruction video

Lecture video - how to mapping a room with your robot

## Requirements Figure 1 - Lego Lidar

## Start programming with set the cursor's position

First drag the 'when green flag clicked' entry point. After that, you need to add the the 'pen up' block to cannot draw lines. Make a 'x' variable in Variable menu and set it to zero. Add the 'point in direction 0' block from Motion category and put x variable into this block. The direction of the cursor must be 0°. Finally, put the 'clear' block from Pen menu to clear the canvas. Figure 2 - Start programming

## Create a loop, which examine when the robot turns 360°

Add the 'Forever' block and inside it put the 'if [condition] else' block from Control menu. You should examine the x variable because if the cursor turn 360°, the x is equal to 360, the program stops. Drag the '=' block from the Operators menu to the 'if' condition part, where the left side is the x variable and on the right side is it 360 as you can see it on Figure 3 below. Figure 3 - Examine the X variable

## Write a code to check the distance with using Ultrasonic Sensor and save that coordinate

You have to choose the 'move step' block from the Motion menu and drag into the '/' block from the Operators menu where the left side is 'Distance in cm' which select from the Lego menu and on the right side is 1.5. Don't forget to select the Ultrasonic Sensor, because it will measure the distance. It's necessary to divide the distance with 1.5 because that way the distance is appear correnctly on the canvas. Make two variables called 'x1' and 'y1' in the Variables menu. Drag the 'set x1 to' and the 'set y1 to' block from to Variables menu and put into the 'x position' block to the first block and put the 'y position' block to the second block like in Figure 4 below. Figure 4 - Check the distance and save it

## Upgrade to start the motor on the robot to map the room

Get ready for a newer distance checking, but first set the cursor position to (0,0), so add the 'go to x:0 y:0' block from the Motion category. You have to change the x variable by 5 degrees, please drag the 'change x by 5' block and set it to the cursor point in direction. Before that, drag the 'Start motor [motor] at [speed]%' from the Lego motors category, where use the A motor and set the motor speed to -3,4. Finally, put the 'wait' block where you should select 0,11776 secs because it's necessery for the cursor and the robot to turn 5° at the same time. (Figure 5) Figure 5 - Start the motor to turn 360°

## Examine the distance with using Ultrasonic Sensor continuously until the robot turns and save the measurements

You must do for this task as you did in the Task #5 but only one thing needs to be changed: In this task you have to make two variable called 'x2' and 'y2' variables. Go to the Variables menu and select the 'Make variable' to create the variables. Now you need to check the distance as you did in the Task #5. You have to add the 'set x2 to' and 'set y2 to' blocks and put them into the 'x position' and 'y position' block, so you save the coordinate of distance to x2 and y2 variables. (Figure 6) Figure 6 - Set x2 and y2 variables

## Modify your program to connect the measurements with using pen

Now, the cursor places in the coordinate of x2y2 position. You have to add the 'pen down' block from the Pen menu to draw on the canvas. The cursor need to draw the line to coordinate of x1y1 so add the 'go to x:[A] y:[B]' block from the Motion menu, where A is the x1 variable and B is the y1 variable. After that, cursor goes to the starting position with using the 'go to x:0 y:0' block, but before that you add the 'pen up' block as you can see it on Figure 7 below. Figure 7 - Draw line

## Save the first measurement's position

Drag an 'if [condition] else' block from the Control menu to the code as you can see it on Figure 8. In the condition part you have to add the ' < ' block, which examine the x variable's value because this block is executed if the x is greater than zero degree. Make a variable called 'first_y', it use to save the first measurement's y coordinate. Add the 'set first_y' to' block and put into the 'y position' block from the Motion menu. You have to drag the 'pen down' block to draw the line between the x1y1 and x2y2 points, so add the 'go to x: [A] y:[B]' block, where A is x2 variable and B is y2 variable. Figure 8 - Draw line between first and last measurements

## Stop the Lidar robot and connect the first coordiante and the last coordinate with using the cursor and the pen

You have to stop the robot so add the 'Stop motor [motor]' from the Lego motors menu, where select the A motor. Draw line between the last coordinate and the first coordinate, so drag the 'go to x:[A] y:[B]' block where A is the x2 variable and B is the y2 variable. This is the last coordinate. Put the pen down and draw the line to the first coordinate, so add the 'go to x:[A] y[B]' block again and set zero to A and set the first_y variable to B. After that, cursor draw line but it have to go the starting position so put the pen up and set the cursor position to (0,0); Finally, put the 'stop all' block to stops the program as you can see it in the Figure 9 below. Figure 9 - Complete program

## Program code Figure 10 - Program code