Course 4 / Lecture 5:

Use a gamepad to control your Lego robot

In this lecture you will learn to use a gamepad for robot control. This lecture is based on information you have learnt in Lecture 4. You will apply your knowledge to control motors by gamepad events. Turning a real life robot around, making it move and stop as you wish is a bit more tricky then operating the cursor on the canvas. You will enjoy this lecture, and once you complete all the tasks, you will be more equipped with knowledge on how to add great control input sources to your machines.


Figure 1 - Move your Dozer with a gamepad

Requirements

Instruction video

Lecture video - How you can control the Dozer using your gamepad

Task #1:

Connect the gamepad to Ozeki 10 and configure it!


STEP 1: Install the Gamepad connection

In this lecture, a simple gamepad is used. To use it in Ozeki Snap, you have to connect it to Ozeki 10 in a similar way like the Lego Brick. At first connect the gamepad to your computer. Open your browser, navigate to http://localhost:9505 then login to Ozeki 10 and open Control Panel from the Desktop. Click on the Create new Connection button. On the right-side Connection details panel, select the Sensor option then select the Browser sensor icon, finally choose the Gamepad as you can see on Figure 2. Please type a name for the connection and click 'Ok' to create it.


Figure 2 - Installing the Gamepad connection

STEP 2: Open the gamepad connection in the Control Panel

You will see the gamepad connection in the connection list (Figure 3). It has two connections added: your gamepad connection, which is a 'container' connection that has one or more 'physical' gamepad connections next to it. Test your physical gamepad connection by clicking on the 'Details' button to open its configuration GUI and trying out the buttons through Ozeki 10's Control Panel.


Figure 3 - Gamepad connections

STEP 3: Test the gamepad by pressing its buttons

Now you can test and configure the gamepad. Try to press some buttons and see how it displays (Figure 4). You will use 6 buttons: 4 buttons for moving the robot forward, backward, right and left and 2 for opening and closing the gipper. You can choose any button. In this lecture the following buttons are used: BTN8, 9, 12, 13, 14 and 15. BTN1 and 3 for changing the gripper state, the rest is for moving the robot. Choose your buttons you want to use and write them down somewhere. Note that the numbers in this lecture may not be the same as your gamepad button numbers!


Figure 4 - Pressing BTN12

Task #2:

Create the robot moving parts of the program!

At first, you need to detect somehow if the requested key pressed on the gamepad. You can do this by using the 'gamepad [gamepad] button [button] pressed?' block from Gamepad category. This block returns true if the selected button is pressed on the selected gamepad. Now, you need to use this block to create the entry points. To do this, please place four 'when [condition]' control blocks into the Scipts field then insert the 'gamepad' blocks into their 'condition' fields. Due to these, the 'when' blocks will be activated when the 'gamepad' blocks in their 'condition' fields return true, so when you press the corresponding button. Then you need to attach the 'Drive' blocks to drive the robot forward, backward, right and left. Your next task is to wait until the right button is released and then stop the motors of the robot. To do this, you only need to use a 'wait until', 'not' and 'gamepad' blocks. It is demonstrated on Figure 5. The 'wait until' block will wait until the right button on the gamepad is released. Then there is a 'Stop motor' block to stop the robot.


Figure 5 - The robot moving parts

Task #3:

Create the gripper moving parts of the program!

This task will be similar to the previous one. You need to create another two entry points for controlling the gripper of the Dozer. So place two 'when [condition]' control blocks into the Scripts field then insert two 'gamepad [gamepad] button [button] pressed?' blocks into their 'condition' fields. You need to select your connected gamepad's connection for their 'gamepad' fields and choose BTN3 and BTN1 for the 'button' fields. Due to this, these two entry point blocks will be actived when the buttons indentified as BTN3 and BTN1 are pressed. Compared to the previous task, the difference is that you have to use 'Start motor' blocks instead of 'Drive' blocks. You need to drive B motor forward to open the gripper and backward to close it. The second part of the program is similar to the previous one in Task 2. Look at Figure 6 for help.


Figure 6 - The gripper moving parts

Task #4:

Use broadcast event to simplify the code!

You can use only one part to detect if all of the buttons on the gamepad are released and to stop the motors of the Ozeki Dozer. So you can avoid unnecessary duplication in the SNAP code. Let's create the part which will replace the duplicates. At first, place a 'when I receive [message]' control block and create a new message called 'stop_motors'. In this part, you need to detect if all the buttons are released. If so, you need to stop all motors. Implementing it is simpler than you think! There is a 'gamepad [gamepad] no button pressed?' block found in Gamepad category that returns true if all the buttons are relesed. So you just need to put this block into the 'wait until' block and attach it to the 'when I receive' one. Finally, you should place a 'Stop motor' block at the end of the part. This part is ready. The last step is to modify the other parts to broadcast here. So you need to replace the last two blocks of every parts with 'broadcast [stop_motors] and wait' blocks as you can see on Figure 7. There is nothing left to try it! Have fun!


Figure 7 - The complete program that uses brandcast


Program code

Program code
Figure 8 - Program code to control the robot using gamepad keys

More information


Next


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