This tutorial demonstrates how to control your Lego EV3 robot with buttons. There are five buttons to make robot moving forward, backward, right-turn, left-turn and stop. You can also drag the slider to adjust motor power.
Notice: You don’t have to write any program for EV3, just power it on and pair with your Android phone. App Inventor’s EV3 components can talk with Lego EV3 brick with a special protocol called Lego EV3 Direct Command. These commands are actually byte arrays that can be executed directly by Lego EV3 brick. Lego EV3 components are developed by CAVEDU Education and merged to official AI2 in 2016.
App Inventor Gallery of this project.
Basic dual-wheel robot platformPlease assemble your robot like Figure 1. If your robot is somehow different, don’t worry, just make sure the two motors (attach to port B and C) are on the opposite side of robot. This kind of robot base is called differential drive platform. Because robot’s behavior can be easily adjusted according to the motor speed. For instance, robot will go forward with both motors moving at same speed; and will turn right with left motor moving a little slower than right motor.
This robot needs no sensors. Power on your EV3 and pair it with you Android phone (default key: 1234). You can check whether robot’s EV3 is on by the Bluetooth icon at the upper-left screen corner.
Please add these components and place them as shown in Figure 2. Details please refer to Table 1.
For connection, a Listpicker is used to select paired Bluetooth device, your Lego EV3 in this case. A Disconnect button to close the Bluetooth connection with Lego EV3.
You can see there are five buttons to make robot move forward, backward, turn left, turn right and stop. We also add a slider to adjust motor power (0-100). Please add more interesting moves to your robot.
- ListPicker1 (User Interface). set Text to “Choose your EV3”
- Label1 (User Interface). set Text to “Slide to adjust motor power”
- Slider1 (User Interface). set min to 0, max to 100
- TableArrangement1
- Button_Forward (User Interface). set Text to “Forward” Click to make robot go forward
- Button_Backward (User Interface). set Text to “Backward” Click to make robot go backward
- Button_Left (User Interface). set Text to “Left” Click to make robot turn left
- Button_Right (User Interface). set Text to “Right” Click to make robot turn right
- Button_Stop (User Interface). set Text to “Stop” Click to make robot stop
- Button_Disconnect (User Interface). set Text to “Disconnect” Click to disconnect from EV3 brick
- Ev3Motor1 (LEGO® MINDSTORMS®). set BluetoothClient to BluetoothClient1
- Control and get status of Ev3 Motor, MotorPorts is BC
- BluetoothClient1 (Connectivity). Communication between AI2 and Lego EV3 brick
First we use a variable (power) to represent motor power, with initial value 100. We will set it’s value in Slider.position changed event to further control motor power.
In ListPickerConnect.BeforePicking event, we have to set its element to BluetoothClient1.AddressAndNames. After that we can connect(BluetoothClient1.Connect) our paired EV3 in ListPickerConnect.AfterPicking event. If connect sucessfully, Listpicker will be set to unable, but other button will be set to enabled for user to operate. (Figure 3).
Except for Button Stop, other four buttons will call Ev3Motors1.RotateSyncInDuration command when clicked, with different turnRatio and power. Take Button_Forward for instance, we have these parameters: power variable, 2000(ms), 0 (means two motor with same speed and true (break when execution complete).
Notice that everytime you click the button will let robot do something for 2 seconds, you can modify the millisecond field or change to RotateSyncIndefinitely command. It will let your robot be more gentle.
As for Button_Backward, we simply multiply variable power with -1 to make motors turn in the opposite direction (same power). Your robot will then move backward (Figure 5).
Button_Left and Button_Right are almost the same idea, but this time we set turnRatio to -200 (turn left) and 200 (turn right). You can set turnRatio to higher value(max is 100) to make a sharper turn.
Finally click Button_Stop to stop the robot, you can use RatateSync command with 0 power to achieve the same effect. (Figure 6).
In slider.positionChanged event, we set power variable to slider’s thumb position, therefore the motor power is changed as well (0-100).
When Button_Disconnect is pressed(Button_Disconnect.Click event), we ask BluetoothClient to disconnect from EV3 brick, set buttons and Listpicker to original status, waiting for next connection. Like Figure 7.
You must pair with your EV3 robot first, then press its power button and check EV3’s BT is switched on. Press the [Choose your EV3] Listpicker and choose the name of the paired EV3, then all the control button will be enabled to click. Please click each button to see how robot moves and also drag the slider to adjust motor power. You can build a maze and see who is the best maze conqueror in the class!!
Connect your EV3App starts up like Figure 9, please click Connect Listpicker and select your EV3 (figure 10). Click and connect successfully, you can click five buttons in the middle of the app to control your robot. Enjoy.
Click Disconnect button to close Bluetooth connection with EV3.
Comments
Please log in or sign up to comment.