Control LEGO MINDSTORMS EV3 Robot Using Raspberry Pi Sense HAT
This example shows how to read the Accelerometer on Raspberry Pi™ Sense HAT and use this data to control the motion of a LEGO® MINDSTORMS® EV3 robot.
The Raspberry Pi Sense HAT is an add-on board for Raspberry Pi hardware. It has an 8X8 RGB LED Matrix, a five-position joystick and includes the following sensors:
IMU sensor (Accelerometer, Gyroscope, and Magnetometer)
LEGO MINDSTORMS EV3 is a Linux based educational robot kit developed by the LEGO group. The Simulink® Support Package for LEGO MINDSTORMS EV3 Hardware enables you to create and run Simulink models on LEGO MINDSTORMS EV3 hardware. The support package includes a library of Simulink blocks for configuring and accessing LEGO MINDSTORMS EV3 sensors and actuators.
We will develop two Simulink models:
Raspberry Pi model In this model, the data read by the Accelerometer On-board Sense HAT data is used to determine the motor speeds for EV3 robot. A network connection is setup between the Raspberry Pi device and the EV3 robot for data exchange.
LEGO MINDSTORMS EV3 model In this model, the data sent by the Raspberry Pi device is used to drive the two motors of the EV3 robot.
You will learn how to:
Set up network connection between Raspberry Pi hardware and LEGO MINDSTORMS EV3 robot
Configure and run a Simulink model for Raspberry Pi hardware to send UDP packets to LEGO MINDSTORMS EV3 robot
Configure and run a Simulink model for LEGO MINDSTORMS EV3 robot to receive UDP packets from Raspberry Pi hardware.
We recommend completing Getting Started with Simulink Support Package for Raspberry Pi Hardware example.
To run this example you will need the following hardware:
Raspberry Pi hardware
Raspberry Pi Sense HAT
LEGO MINDSTORMS EV3
Two LEGO MINDSTORMS EV3 Large Motors
EV3 Wi-Fi Dongle
Working Principle of Robot motion control
In this section, we will discuss about how the acceleration data is used to control the motion of the robot.
From the Accelerometer data, we can mathematically determine the value of 'pitch' and 'roll' for a given position of the Sense HAT. We can map the 'pitch' and 'roll' movement of the Sense HAT to the motion of the EV3 robot. The 'pitch' represents the forward and reverse motion of the motor whereas the 'roll' represents the sideward motion of the robot.
The mathematical equations required to convert the 'pitch' and 'roll' values to linear speed of the motors are realized using a MATLAB function block in the Simulink model for Raspberry Pi.
A communication link for data exchange is set up between Raspberry Pi and the EV3 brick. The linear motor speed derived from the pitch and roll values is sent over UDP from Raspberry Pi to EV3. EV3 receives this data and uses it as an input to drive the two motors.
Task 1 - Familiarize with Sense HAT Accelerometer (Recommended)
Auto-Rotate Image Displayed on Raspberry Pi Sense HAT LED matrix example shows usage of the Accelerometer on the Raspberry Pi Sense HAT. The acceleration data is used to auto-rotate an image displayed on the LED matrix of Sense HAT.
Complete this example to get familiarity with the usage of Accelerometer.
Task 2 - Install Simulink Support Package for LEGO MINDSTORMS EV3
You need Simulink Support Package for LEGO MINDSTORMS EV3 to run Simulink model on LEGO MINDSTORMS EV3 hardware.
1. Install Simulink Support Package for LEGO MINDSTORMS EV3.
Click link below to
2. (Recommended) Complete the Getting Started with LEGO MINDSTORMS EV3 Hardware example in Simulink Support Package for LEGO MINDSTORMS EV3 you just installed.
Task 3 - Set Up LEGO MINDSTORMS EV3 robot
In this task, we 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 - Configure Network Connection
In this task, you will set up network connection between Raspberry Pi board and LEGO MINDSTORMS EV3. The communication protocol used in this example is UDP. You can alternatively use TCP/IP protocol. TCP/IP send/receive blocks are available in the library for LEGO MINDSTORMS EV3 and Raspberry Pi.
1. Connect Raspberry Pi board to the network with Ethernet cable through Ethernet port. Make sure that both the Raspberry Pi board and the EV3 robot are connected to the same network.
2. Verify the connection between your Raspberry Pi board and LEGO MINDSTORMS EV3.
Execute the following command on the MATLAB command prompt:
r = raspberrypi
This command returns an object with IP address info for the Raspberry Pi board.
Run command system(r, 'sudo ping [EV3_IP_Address] -c 10') with the LEGO MINDSTORMS EV3 IP address found in step 2 of Task 3 to verify the connection. e.g.
system(r, 'sudo ping 192.168.1.2 -c 10')
3. After verifying the connection, open the Raspberry Pi model and update Send to EV3 block with the IP address of your EV3 hardware.
Task 5 - Run Simulink Models on Raspberry Pi Hardware and LEGO MINDSTORMS EV3 Robot
In this task, we will run the models that we have developed on the respective hardware.
1. On the Hardware tab of the Raspberry Pi Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start to run this Simulink model on your Raspberry Pi hardware.
2. On the Hardware tab of the LEGO MINDSTORMS EV3 Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start to run this Simulink model on your LEGO MINDSTORMS EV3 hardware.
3. Control the EV3 robot using the Sense HAT connected to the Raspberry Pi hardware.
Other Things to Try
1. 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.
2. Develop a model to control the EV3 robot using the Joystick on Sense HAT. Use the Joystick block in the Sense HAT library to read the state of the Joystick on-board Sense HAT.