Course 7 / Lecture 1:

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

Requirements

Task #1:

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

Task #2:

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

Task #3:

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

Task #4:

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

Task #5:

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


Program code


Figure 7 - Program code that moves the robot and sprite with arrow keys

More information


Next


Copyright © 2000- |Ozeki Ltd | info@ozekirobot.com |
Page: 6106 | 18.223.0.53 | 79.99.42.43 | Login