MATLAB Examples

Controlling LEGO MINDSTORMS EV3 robot from Host computer

This example shows how to create a Simulink model to communicate between the Host PC and the LEGO MINDSTORMS EV3 robot. The Simulink model running on the Host PC will exchange data with the EV3 brick and control the motion of the EV3 robot.

Contents

Introduction

In this example, you will establish a communication link between the Host PC and the EV3 robot. You will create a Simulink model on the host comprising of Dashboard widgets and communication blocks to generate the motor speed. On the EV3 robot, you will run a Simulink model which will receive and use the data sent from the host to drive the motors of the EV3 robot.

In the host model, you will use the following dashboard widgets:

  • Slider switch - Start/Stop the robot
  • Knob - Specify the speed of the robot (0 - 100%)
  • Toggle switch - Forward/Reverse motion of the robot
  • Rotary switch - Direction of the robot.

The data exchange takes place over UDP between the host and the EV3 brick.

Prerequisites

We recommend completing Getting Started with LEGO MINDSTORMS EV3 Hardware and Communicating with LEGO MINDSTORMS EV3 Hardware examples.

Required Hardware

  • LEGO MINDSTORMS EV3 Brick
  • Two LEGO MINDSTORMS EV3 Large Motors
  • EV3 Wi-Fi Dongle

Task 1 - Develop Simulink model for Host PC

In this task you will develop a Simulink model which will generate the motor speed for the EV3 robot. The model will exchange data with the EV3 robot using UDP communication block.

1. Create a new Simulink model.

2. From the Simulink library browser, add Slider switch, Knob, Toggle switch, and Rotary switch from the Dashboard library.

3. Associate these Dashboard widgets to individual constant blocks.

4. Create a sub-system and implement the logic to generate speed values for the left and right motor. A sample logic is implemented in the pre-configured example model.

5. Add a UDP send block from the Instrument Control Toolbox library. The values of speed for the two motors will form the input to this block.

6. Save your model.

A pre-configured model is provided for reference.

Task 2 - Develop Simulink model for LEGO MINDSTORMS EV3

In this task you will develop a Simulink model which will run on the EV3 brick and control the motion of the robot.

1. Create a new Simulink model.

2. In the Simulink library browser, navigate to the Simulink Support package for LEGO MINDSTORMS EV3 library. This library consists of the driver blocks for the EV3 hardware.

3. From the EV3 library, add a UDP receive block to the Simulink model. Add two motor blocks and configure them for Port A (left motor) and Port B (right motor). The speed for each motor is received on the UDP receive block. Connect the output of the UDP receive block to the input of the two motors. Additionally, you can add two display blocks from the EV3 library to view the speed of the motors on the EV3 screen.

4. Save your model.

A pre-configured model is provided for reference.

Task 3 - Set up LEGO MINDSTORMS EV3 robot

In this task, you will build a EV3 robot and connect the EV3 brick to the same network as the Host PC.

1. Build a two-wheeled robot. You can build a robot similar to the one described in the printed building instructions in the education core set.

2. Set up a connection between EV3 brick and your host machine. Refer to Task 1 and 2 in Getting Started with LEGO MINDSTORMS EV3 Hardware example.

Task 4 - Update the Host control model

In this task you will update the host control model with the IP address of the LEGO MINDSTORMS EV3 hardware.

1. Open the Host control model. Update UDP Send to EV3 block with the IP address of your EV3 hardware.

Task 5 - Run models on LEGO MINDSTORMS EV3 hardware and Host PC

In this task you will Deploy and run the models developed on their respective hardware devices.

1. Click Run button of Host control model to run this model in simulation mode on your Host PC.

2. In your Simulink model for EV3, click Tools > Run on Target Hardware > Prepare to Run... .

3. In Configuration Parameters, set the Target hardware parameter to LEGO MINDSTORMS EV3.

4. Update Host to Target Connection parameters with the ones found in step 2 of Task 3.

5. Click OK.

6. In your Simulink model, click the Deploy to Hardware button on the toolbar. The model will now be deployed to the LEGO MINDSTORMS EV3 hardware.

7. Control the EV3 robot using the Dashboard widgets in the Host control model.

8. Look at the EV3 brick LCD while you tune the dashboard widgets. The LCD will display left and right motor speeds.

Other Things to Try

Improve the control system on the EV3 robot. Try incorporating a PID controller.

Refer to EV3 drive closed loop example and improve the control algorithm on the EV3 robot.

Summary

In this example, you controlled the motion of an EV3 robot using a Simulink model running on the Host PC You learned the following:

  • Create a communication link between the EV3 robot and the Host PC to exchange data.
  • Create a Simulink model on the Host comprising of dashboard widgets, which send the speed for the motors of the EV3 robot over UDP.
  • With Simulink and Simulink Support Package for LEGO MINDSTORMS EV3 Hardware, you can implement simple to complex robot control algorithms.
  • A way to implement a simple control algorithm for the LEGO MINDSTORMS EV3 Hardware which receives data over UDP and drives the robot.