OZEKI Robot

Course 4 / Lecture 3:

# Move your Lego robot from the keyboard

Control your real-life robot from the keyboard. After completing Lecture 2 you will find it easy to control your robot's movement from the keyboard. In this lecture your task will be to write a robot control code. You will find challenges in turning the robot by exactly 90 degrees, and it will require you to do calculations to make the robot go forward the exact distance you wish. This is a great lecture.

Figure 1 - Move your lego robot from the keyboard

## Name the components needed for keyboard control of the robot! Explain how these components are connected!

Figure 2 - Components diagram

## Create an entry point to start the robot forward by pressing the up arrow key!

Let's start with creating the entry point. As you learnt before, these type of blocks can be found in Control category. Now you just have to find out exactly which block you need to use. To detect the key presses, the 'when [key] key pressed' is required. So please place this block into the Scrips field then select the 'up arrow' option for its 'key' field. Due to this, the blocks attached to it will be executed when the up arrow key on your keyboard is pressed. Finally, to drive the Dozer robot forward, you just need to attach a 'Drive [motors] [direction] at [power] %' block from Lego motors category to the previously placed control block and set its fields like on Figure 3. At this point, your robot will go forward if you press the up arrow key. You can stop it by double clicking on the 'stop motor [all]' block in Lego motors category.

Figure 3 - Program code to move your robot forward

## Upgrade the code to stop the robot if the key is released!

The previously created program only starts the Dozer forward, but it never stops the moving robot. So you need to upgrade the code to be able to stop the robot's motors if the up key is released. The 'key [key] pressed?' predicate type of block found in Sensing category returns true, if the selected key is pressed on your keyboard. It is fine, but you should check if the selected key is released. To do this, you only need to put this block into a green 'not' block found in Operators. This block will return true if the block in it returns false. So after selecting the 'up arrow' option for the 'key' field of the 'key [key] pressed?' block, the 'not' block will return true if the up arrow key is released. Using this information, you can put this 'not' block into a 'wait until' block which will cause the program to wait till you release the up arrow key. So after this block, please attach a 'Stop motor [A+D]' block to stop the Dozer's motors like on Figure 4. In summary, when you press the up arrow key, the robot will start moving forward then the execution will wait until you release the up arrow key due to the 'wait until' block. If the key is released, the program will continue the execution with the 'Stop motor' block and your robot will stop.

Figure 4 - Program code to move your robot forward then stop it

## Create entry points for movig the robot to other directions!

In the previous task, you wrote code for driving the robot forward and stopping it depening on the up arrow key's state. Now your task is to create three other parts like the first one to drive the robot backward, left and right. You just need to copy the first entry point and the attached blocks to it and modify their fields to create the new parts. Look at Figure 5 for help!

Figure 5 - The code to control the robot from your keyboard

## Use broadcast event to simplify the code!

If you look closely at the blocks, it is apparent that the last two blocks at the end of the parts are nearly the same. The only difference is the value of the 'key' field of the blue 'key [key] pressed?' blocks. So you can lift these blocks into a separate part, to which the program broadcasts from the other parts. So you need to copy these two block into a new part and make some changes on them. At first, you need to change the key field of the 'key [key] pressed?' block to 'any key'. Due to this modification, the 'not' block will return true if all the keys on your keyboard are released. Thanks to 'wait until' block, the execution is wait until this condition is met. There is only one step to do is to create the broadcasting events. To create it, place a 'when I receive [message]' block to the Scripts field and attach the two blocks to it. Then you need to create a new message by clicking on its message field and selecting the 'new message' option. Type 'stop_motors' into the textbox then click on 'Ok'. This message is automatically selected for the message field of this control block. Now you need to delete the 'wait until' and 'Drive' blocks at the end of the other parts and insert a 'broadcast [message] and wait' block instead of. Finally, select the 'stop_motors' for its message field as can be seen on Figure 6. Turn on the 'Visible stepping' next to the project name and try to control your robot!

Figure 6 - The simplified code that uses broadcast event

## Create entry points for moving the Dozer's gripper!

Your task is to open the Dozer's gripper by pressing the 'o' key and close it by pressing the 'c' key on your keyboard. So as you did before create two entry points using a 'when [key] key pressed' control blocks. For turning the gripper, you need to use 'Start motor' blocks instead of the 'Drive' ones because these blocks are capable to select only the B port the gripper motor connected. The last step is to insert a 'broadcas [stop_motors] and wait' blocks to stop the moving gripper if the key is released. But before you doing this, you should make a change on the 'Stop motor' block. Please select the 'all' option insted of 'A+D' to be able to stop the gripper motor too like on Figure 7. You have successfully reached the end of this lesson!

Figure 7 - Program code to moving the gripper too

## Program code

Figure 8 - Program code to control your robot from keyboard