Mapping and Navigation
Robot navigation is often based on drawing and constantly updating a map of the environment using sensors. In this lecture you will learn how to draw a map on the canvas in SNAP, and how to update the map as information becomes available.
Figure 1 - This lecture teaches to move your robot and the sprite to the same direction
- 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 an entry point to clear the canvas and
set the cursor to the center of the canvas!
First drag the following entry point from the Control menu: 'when green flag clicked'. After that, you have to set the coordinates of the cursor by using the 'go to x:0 y:0' block from the Motion category. You can imagine a coordinate system on the canvas, where the X value defines the horizontal position, the Y value defines the vertical position of the Sprite. So if the X and Y values are set to 0, the cursor moves to the center of the canvas. Insert the 'clear' block, which clears the canvas. This block can be found in the Pen category. These steps are demonstrated in Figure 2.
Figure 2 - Set cursor to center and clear the canvas
Create four entry points to move your robot
forward, backward, right and left!
First add four 'when key pressed' blocks as entry points from the Control category. The first one detects the 'up arrow' key, the second one detects the 'right arrow' key, the third one detects the 'left arrow' key and the last one detects the 'down arrow' key. Let's start with the block that detects the up arrow key. You have to attach the 'Drive [motors] [direction] X secs' block from the Lego motors. In the Drive block select A+D motors, because the large motors of the Ozeki Sniffer connected to the EV3 Brick's A and D ports. You must choose the 'forward' direction and set the time to 0.1 secs. The next three parts are almost the same as the 'up arrow' part. The difference is in the movement direction. Select the right direction according to the arrowkeys like Figure 3 demonstrates. You can partially test the code by starting it and moving the arrow keys. Watch your Sniffer movement.
Figure 3 - Detect arrow keys to control the robot
Upgrade code to move the cursor to the same direction as the robot goes!
Start with the 'point in direction 0' entry point. Add the 'point in direction 0' block from Motion category, because the direction of the cursor must be 0 degree. After that, choose the 'move' block from the same category and set it to 10 steps, so if the up key is pressed, the cursor will draw a 10 steps line in direction 0°. The next 3 part works almost the same as the 'up arrow' part, with only one difference. With the right arrow it turns to 90 degree, with the left arrow 270° and with the down arrow 180° like in Figure 4. You can look on the top right corner to see the red arrow move.
Figure 4 - Move the cursor to the same direction as the robot goes
Create a solution to put the pen up and down using the space key!
Make a 'draw' variable by clicking on the Make a variable button in the Variable category and set its value to 'true' using the 'set [variable] to [value]' block. This variable appears on the top right corner of the GUI. It shows if the pen should draw or not. Please add the 'when [key] key clicked' entry point from the Control category and select 'space' option for its 'key' field. So it actives when the space key of your keyboard is pressed. Then, please add the 'if [condition] else' block from the Control category and attach the green '=' block from the Operators category into the condition field of the 'if else' statement to check if the 'draw' variable is true. If yes, it will pick the pen up due to the 'pen up' block. In the 'else' branch you will do the opposite by putting the pen down and setting the 'draw' variable to true like on Figure 5. Now, you can change the pen's state using the space key.
Figure 5 - You can lift or put down the pen
Test your program to see how to move your robot and
draw the line on the canvas!
You are ready to run the program. Click on the green flag button in the top-right corner of SNAP to start. It will set the cursor to the center and clear the canvas. Press the arrow keys to move the robot and cursor at the same time which will draw a line on the canvas as it moves. If you press the space button, the cursor stops drawing the line on the canvas, but if you press the space button again the cursor continues drawing the line on the canvas as you can see it on Figure 6 below.
Figure 6 - Test the complete program by pressing the green flag
Figure 7 - Program code that moves the robot and sprite with arrow keys
|Download the SNAP program code|
It is capable of moving your robot and the cursor into the same direction at the same time. You can follow the guide easier if you have the code in front of you:
Download Ozeki Snap code for 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
- Map a room's walls using ultrasonic sensor
- Mapping a room with LIDAR
- Outdoor navigation using GPS and mobile network