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
- 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
Make a command block named as forward that has an input parameter as 'cm'
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
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
Edit block to go forward your robot by the given centimeter
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
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
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
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.
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
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
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
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.
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
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
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
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
Figure 14 - The program to move your robot to a specified coordinate
|Download the program code to understand the way of going to a coordinate|
It is easier to follow the guide if you have the codes in front of you:
Download Ozeki Snap code for going to a coordinate
- Mapping and Navigation
- 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