[App Inventor x Lego EV3 robot] One touch control

Introduction

This tutorial demonstrates how to control your Lego EV3 robot with one finger dragging on the screen. From the middle of the screen, you drag the small icon forward and robot will go forward, drag icon backward and robot will go backward, so is left and right, etc. You can learn how to transfer touch point's coordinate into robot’s motor speed.  


Notice: You don’t have to write a program for the EV3, just power it on and pair with your Android phone. App Inventor’s EV3 components can talk to Lego EV3 brick with a special protocol called Lego EV3 Direct Command. These commands are actually byte arrays which can be executed directly by Lego EV3 bricks. Lego EV3 components were developed by CAVEDU Education and merged to official AI2 in 2016.


Basic dual-wheel robot platform

Core component of this project:

  1. Lego EV3 intelligent brick, x1

  2. Lego EV3 motor, x2

  3. Wire, x2

  4. rubber tires, x2

  5. Lego parts



Please assemble your robot like Figure 1, if you are not familiar with Lego brick, please check this assembly tutorial (use lesser parts) or Lego Official TRACK3R building tutorial (use far more parts). If your robot looks a little different, that’s fine.  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 a differential drive platform because the robot’s behavior can be easily adjusted according to the motor speed. For instance, the robot will go forward with both motors moving at the same speed and will turn right with the left motor moving a little slower than the right motor.



A-9-1

Figure 1a. dual-wheel robot platform     

C:\Users\allel\AppData\Local\Microsoft\Windows\INetCache\Content.Word\Product_TRACK3R_Square.png

Figure 1b. Use tracks to make your robot cooler (Image source: Lego.com)


Take a look of a real cool Lego robot!




Figure 1c~1e. physical photo of Lego track robot.


Power on your EV3 and pair it with your Android phone (default key: 1234). You can check whether the robot’s EV3 is on with the Bluetooth icon at the upper-left screen corner.


C:\Users\allel\AppData\Local\Microsoft\Windows\INetCache\Content.Word\bt_3.png   C:\Users\allel\AppData\Local\Microsoft\Windows\INetCache\Content.Word\bt_4.png

Figure 1c, 1d. Check EV3 Bluetooth is turned on.

.


App Inventor

Now log into MIT App Inventor site and create a new project.


Designer

In the Designer page of App Inventor platform, please add these components and place them as shown in Figure 2. For details, please refer to Table 1.

For connection, the ListPicker selects a paired Bluetooth device, your Lego EV3 in this case. A Disconnect button closes the Bluetooth connection with Lego EV3.


There is a canvas with a ball component inside, user can drag this ball to drive the robot moving around.

Note: Ball1 is in Canvas1, since we set its Visible property to false, then you can not see it on Designer.



Figure 2. Designer


Table 1.  Required components of this project

Name

Palette

Settings

Description

ListPicker1

User Interface

set Text to "Choose your EV3"

Choose paired Bluetooth device -> our robot

Canvas1

Drawing and animation

set height and width to 320 pixel

area to control the robot

Ball1

Drawing and animation

set X and Y to 160 (center of Canvas1)

set Visible to false

indicator of user finger (touch point)

Button_Disconnect

User Interface

set Text to "Disconnect"

Click to disconnect from EV3 brick.

Ev3Motor_B

LEGO® MINDSTORMS®

set BluetoothClient to BluetoothClient1

set MotorPorts to B

Control and get status of Ev3 Motor.

Ev3Motor_C

LEGO® MINDSTORMS®

set BluetoothClient to BluetoothClient1

set MotorPorts to C

Control and get status of Ev3 Motor.


BluetoothClient1

Connectivity

No need to change

Communication between AI2 and Lego EV3 brick



Blocks

STEP1 Initialize variable for motor power:

This project used several variables:

  • _X: new X coordinate with origin at the canvas center.

  • _Y: new Y coordinate with origin at the canvas center.

  • theta: angle between touch point and the X-axis, derived from _X and _Y.

  • r: distance between touch point and the new origin, derived from _X and _Y.

  • speedL: the final computed result for left motor(connect to Ev3’s C port) speed, , derived from theta and r.

  • speedR: the final computed result for right motor(connect to Ev3’s B port) speed, derived from theta and r.



Figure 3.


STEP2 ListPicker for connecting Lego EV3

We use a ListPicker to show paired devices with your Android device and connect to it by selecting in the Listpicker. This is a better approach and less chance of type error for user to input EV3’s bluetooth address in a TextBox.


Before we click the Listpicker, we must set its elements first(ListPicker1.BeforePicking event). Here we have to set its element to paired devices (BluetoothClient1.AddressAndNames).

Screenshot_2016-04-15-14-50-53-02


Figure 4a. Initialize and what’s inside the ListPicker

 Click to open the ListPicker, we are going to connect (BluetoothClient1.Connect) our paired EV3 by selecting it (ListPicker1.AfterPicking). If connect successfully, Listpicker will be disabled, and Ball1 will show up at the center of Canvas, means user can touch the canvas to drive the robot.


Figure 4b. Select an item to connect to Lego EV3



STEP3 Drag finger to move the robot:

This is the largest code of this project, including three parts:

  1. move origin of coordinated to the center of canvas and calculate theta and r

  2. move the ball component and calculate speedL/speedR

  3. drive motor


Let’s discuss them in detail:

  1. move origin of coordinated to the center of canvas and calculate theta and r

           First take a look of the relationship of these variables. We would like to transfer the current touch point coordinate to motors’ speed. According the figure below:

Figure 5a.



           The origin of canvas is at the top-left corner not the center, so we have to shift the origin: representing by _X and _Y:

  • _X = (currentX - 160 ) /1.6

  • _Y = (currentX - 160 ) /1.6

           

           And for theta and r, we will use trigonometric function to calculate the angle theta.

           which means with larger r with, this will make your robot more controllable and fun.

  • theta: atan (( _Y/_X) ) - 45

  • r: square root of (_X)2 + (_Y)2



Figure 5b.


  1. move the ball component and calculate speedL/speedR

          We want the ball component to follow our finger movement, so use Ball.MoveTo command with x as currentX and y as currentY. And calculate speedL and speedR by the follow equation and show these two values on Screen title.

  • speedL: cos(theta * r)



Figure 5c.


  1. drive motor

          Use Ev3Motor.RotateIndefinitely command to control rightmotor(B) and left motor (C).


Figure 5d.


Finally, our Canvas.Dragged event is finished:


Figure 5e.


STEP4 Finger up to stop the robot

In our case, we wish the robot will stop when our finger is no longer touch the screen. Therefore in Canvas.TouchUp event, we use two Ev3Motor.Stop(set useBrake to true) command to stop two motors and let the ball component return to the canvas center.



Figure 6. Set power variable to slider’s thumb position.


STEP5 Disconnect:

When Button_Disconnect is pressed (Button_Disconnect.Click event), we tell BluetoothClient to disconnect from EV3 brick and set listpicker and ball to original status, ready for the next connection. Like Figure 8.


Figure 7. Disconnect from Bluetooth device


Tips

Once you have paired your device with EV3 robot, then press its power button and check that the EV3’s BT is switched on. Press the [Choose your EV3] listpicker and choose the name of the paired EV3 to connect, you will see the ball appeared when connected successfully. Please click each button to see how robot moves and also drag the slider to adjust motor power.

App starts up like Figure 9, please click Connect ListPicker and select your EV3 (figure 10). Click and connect successfully, you can click the five buttons in the middle of the app to control your robot. Enjoy!


Click Disconnect button to close the Bluetooth connection with EV3,



Figure9a.  robot goes forward with left/right motor speed (51, 56)

Figure 9b. robot turns right with left/right motor speed (-20, 56)

Brainstorming

  1. Build a maze, see if you can drive your robot to conquer the maze.

曾吉弘,
2017年12月4日 上午9:00
v.1