[App Inventor x Lego EV3 robot] Sensor panel

Introduction

This project will demonstrate how to update Lego EV3’s 3 color, ultrasonic and touch sensor status on your AI2 screen.

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 are talking with 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 brick.

App Inventor Gallery of this project

Hardware

Lego EV3 Robot Sensors

    Lego EV3 Robot has 5 different kinds of sensors, including:

  1. Color Sensor: return light level (0-100) or color code of object ahead.

  2. Ultrasonic Sensor: return distance (cm/inch).

  3. IR Sensor: return IR message from IR controller(text and number).

  4. Touch Sensor: return whether it is pressed or not(logic).

  5. Gyroscopic Sensor: return robot’s rotational motion and changes in its orientation(number). Angle mode measures angles with an accuracy of +/- 3 degrees Gyro mode has a maximum output of 440 degrees/second. Sample rate of 1 kHz.

Figure 1. Lego EV3 core components

Note: There are encoders within EV3’s motors. We’ve included the GetTachoCount function in EV3Motors component. We will discuss this function in later topics.


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.

App Inventor

    This app will update Lego EV3’s sensor value (color sensor, touch sensor and ultrasonic sensor) value on screen.

Designer

    Please add these components and place them as shown in Figure 2. Details please refer to Table 1.

    Figure 2.  Designer

Table 1.  Required components of this project


Name

Palette

Settings

Description

ListPicker1

User Interface

set Text to “Choose your EV3” (empty)


Button_Touch

User Interface

set backgroundColor to light gray


Label1

User Interface

set Text to “Distance (Ultasonic)


TextBox_Ultra

User Interface

set Text to “” (empty)

Show distance by ultrasonic sensor.

Label2

User Interface

set Text to “Light level


TextBox_Light

User Interface

set Text to “” (empty)

Show light intensity by light sensor.

Button_Disconnect

User Interface

set Text to “Disconnect

Click to disconnect from EV3 brick.

Ev3TouchSensor1

LEGO® MINDSTORMS®

set BluetoothClient to BluetoothClient1

Get status of Ev3TouchSensor

Ev3UltrasonicSensor1

LEGO® MINDSTORMS®

set BluetoothClient to BluetoothClient1

Get status of Ev3UltrasonicSensor

Ev3ColorSensor1

LEGO® MINDSTORMS®

set BluetoothClient to BluetoothClient1

Get status of Ev3ColorSensor

BluetoothClient1

Connectivity


Communication between AI2 and Lego EV3 brick

Clock1

Sensors

set timerinterval to 200, means updating 5 times per second.

Get

Blocks

STEP1 Preparation

First we need to set ListPicker1.Elements to BluetoothClient1.AddressAndNames, therefore we can choose the paired EV3 brick in this ListPicker.


In ListPicker1.Elements.AfterPicking event, we connect with what we chose (ListPicker.Selection). If connect successfully, then activate Clock.Timer and set Screen.Title to “Connected”.


Figure 3. Use listpicker to select paired bluetooth device.


STEP2  Show sensor value regularly

In Clock1.Timer event, we are polling sensors status 5 times per second. Then use each sensor component’s related function to get sensor status. For ultrasonic and color sensor, we simply show the value  directly on Textbox. And we have one special button to change its background color according to whether the touch sensor is pressed or not.


Figure 4. Updating sensor value 5 times per second

STEP3   Disconnect

When Button_Disconnect is pressed(Button_Disconnect.Click event), we ask BluetoothClient to disconnect from EV3 brick, disable Clock Timer and set related message on Screen title.


Figure 5. Disconnect from bluetooth device

Play

    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, you should see varying number on your screen. Try to press Lego EV3’s touch sensor to check whether the button has been red or not~

    

ċ
ev3_sensorPanel.aia
(3k)
曾吉弘,
2017年10月1日 下午8:57
Comments