Course 7 / Lecture 2:

Going to a coordinate

In this lecture you will learn the concept of a coordinate system. You will learn how to use a coordinate system in robot navigation. Your task will be to create a program that will navigate your robot to a certain point in the coordinate system.


Figure 1 - Moving your robot to a coordinate

Requirements

Task #1:

Make a command block named as forward that has an input parameter as 'cm'


Step 1:Create a command block for this sprite only

You will need 4 commands type of blocks to control your robot, these commands are: forward, backward, left and right blocks. You need to navigate to the Control category and click on the 'Make a block' button bottom of the left-side panel. Now you have to name the block you wish to create. The first one should be named as 'forward' like Figure 2 demonstrates. Then you have to choose the 'command' option, after that please click on the 'OK'. Before that, select the 'for this sprite only' option, you can create some sprite in the program but you use this command only this sprite.


Figure 2 - Creating a forward block

Step 2: Create the 'cm' variable in the forward's title

This block will move the robot forward with the specified distance in centimeters. To create that, click on the 'forward' block created in the previous step with right click and select 'edit' option. After that, click on the '+' signal in the Block Editor next to the 'forward' title and write 'cm' (like centimeter) to the field and hit enter. The 'cm' variable should appear after the 'forward' title. You will use it many times, so you just need to drag to copy it.


Figure 3 - Creating cm variable

Task #2:

Edit block to go forward your robot by the given centimeter


Step 1: Add if statement to check the 'cm' value

You should attach the 'if' block from the Control category to the Block Editor and add the green 'not' block from the Operators category to the 'if' statement's condition part. After that please insert a '=' block into the previous one. On the left hand side of this equality, the 'cm' variable should be on the left, and on the right side it should be 0. This condition is true, if the 'cm' variable's value is not zero. Due to this, if you want to move the robot by 0 centimeters forward, the blocks inside the 'if' statement will not be executed as you can see it on Figure 4 below.


Figure 4 - Check the cm's value

Step 2: Drag the Rotate block and set the correct parameters

Inside the 'if' condition, drag the 'Rotate [motors] by [degree] at [speed] %' block from the Lego motors, where the motors are A+D and set the motor speed to 30%. The 26.5-degree rotation of the motors drives the robot one centimeter forward, so you need to take a ' x ' block inside the degree field of the 'Rotate' block, add the 'cm' variable to the left side and write 26.5 to the right side.


Figure 5 - Now, the robot can go forward

Task #3:

Your robot should go backward so create the 'backward' block almost like the 'forward' block.


You must do nearly the same steps for the 'backward' block that you have at the 'forward' block, but only one thing needs to be changed: in the backward block the value of the B should be -26,5. Due to that, the motors of the Sniffer will move backwards, so the robot will move backwards.


Figure 6 - Create a backward block and set it

Task #4:

Its the time that your robot be able to turn left so make a 'left' block and set it that your robot can turn left.


Step 1: Create 'deg' variable next to the left title

Make a block as you do it the previous steps and please choose the 'edit' option of the 'left' block to open the Block Editor. Click on the '+' signal after the 'left' title and add the 'deg' input name like degree of the rotation as you can see it on Fiugre 7 below.


Figure 7 - Make a deg variable

Step 2: Drag the Rotate block

Attach the 'Rotate [motor_1] by [degree_1] and [motor_2] by [degree_2]' block from the Lego motors category. This block is used to rotate the selected two motors with the specified degrees. Please select A motor for 'motor_1' and B motor for 'motor_2'. Then you have to insert a '[value_1] x [value_2]' block found in Operators into both 'degree_1' and 'degree_2' fields. You should rotate A motor by -3.11 degree and B motor by 3.11 degree to turn left the robot by one degree. To implement this, please put the 'deg' variable to the first field of the 'value_1' field of both 'x' blocks. Then you need to enter -3.11 for the first 'x' block and 3.11 for the second one.


Figure 8 - The robot can turn left

Task #5:

Now your robot can go foward and backward, it can turn left but it cannot turn right, so create a block which turn your robot to right. Use the solution of the previous task because it is similar to it.


You need to do the same steps for the 'right' block although in this one you need to enter 3.11 for the first 'x' block and -3.11 for the second one to turn the robot right as you can see on Figure 6 below.


Figure 9 - Create a right block

Task #6:

Create a navigate block where you can add a (x,y) coordinate to control the robot. You should use the previous four blocks to set the navigate block.


Step 1: Make a navigate block and create 'x' and 'y' variables

You need the 'navigate to' block, where you can add a coordinate to control the robot. Make a block and write a title, please create the 'x' and 'y' input name as you can see on the Figure 7 below. Attach the 'if [condition] else ' block from the Control menu, it makes a decision and continues the program to one of between two branches.


Figure 10 - Create x and y variable to control the robot

Step 2: Use the forward and backward blocks to set the y coordinate

Put the 'A > B' block to the 'if' condition part. A is the y variable and B is zero, you should examine the Y's value because if its less than zero the condition is false, so the else branch will execute, but if the condition is true, the Y's value is greater than zero, the if branch will execute. Inside the 'if' statement attach the 'forward' block with an y variable you drag. In the else, please add the 'backward' block and choose the 'abs of' block, because you will get a minus value, but the robot can go reverse with an absolute value as you can see on the Figure 8 below.


Figure 11 - Move your robot in Y direction if it is required to move

Step 3: Set the x coordinate with left and right blocks

This is the last step, you should set the x variable in the 'navigate to' block. Attach the 'if' block and add the 'not X' block to the 'if' condition part. Examine the X's value because if its zero the if statement doesn't execute. After that add 'if [condition] else ' block where examine the X's value is greater than zero. If the condition is true, the if branch will execute, but when the condition is false, the else branch will execute. You have to choose the 'right' block to the if branch and the 'left' block to the else branch. Set the 90 degress in the 'right' and 'left' blocks, so give the value to the 'deg'. Finally attach the 'wait' block and select 0.2 secs. It is essential to wait a bit, so the robot motor can work properly. After that you have to add the 'forward' block with the 'abs of' block too (Figure 9). To sum it up, it can rotate to positive or negative X directions and move X cm-s.


Figure 12 - Move your robot in X direction if it needs to be moved

Task #7:

Test your program with (-5,8) coordinate


You have to add the 'when green flag clicked' from the Control category as an entry point. Thanks to this, the program starts when you click on the green flag icon on the top right corner. Now you are ready test the program. Click on the green flag in the top-right of the SNAP to start the program, but beforeit do not forget to add -5 to 'X' variable and 8 to 'Y' variable. It means the robot first will go forward 8 cm and after that turn left, becuase you add negativ value to X (if you add positiv value to X the robot turns right). Finally, it will go forward 5 cm. It is demonstrated on a coordinate system (Figure 10). You can have fun by retesting it with more values.


Figure 13 - Run the program to move your robot. The coordinate system is only a demonstration


Program code


Figure 14 - The program to move your robot to a specified coordinate

More information


Next


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