Controlling 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 movement of a LEGO MINDSTORMS™ EV3 robot.
- Required Hardware
- Working Principle of Robot movement control
- Task 1 - Familiarize with Controlling EV3 using Host model
- Task 2 - Install Simulink Support Package for Raspberry Pi
- Task 3 - Familiarize with Sense HAT Accelerometer
- Task 4 - Set up LEGO MINDSTORMS EV3 robot
- Task 4 - Configure Network Connection
- Task 6 - Run models on Raspberry Pi hardware and LEGO MINDSTORMS EV3 robot
- Other Things to Try
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:
- Humidity sensor
- Pressure sensor
- 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, we will read the Accelrometer on-board Sense HAT. We will determine the motor speeds for the EV3 robot by using the acceleration data read from the Sense HAT Accelrometer. A network connection would be setup between the Raspberry Pi device and the EV3 robot for data exchange.
- LEGO MINDSTORMS EV3 model In this model, we would use the communication blocks of LEGO MINDSTORMS EV3 to recieve the data sent by Raspberry Pi.\ The data received will be used to drive the motors of the EV3 robot.
This example would help you learn the following :
- Set up network connection between Raspberry Pi hardware and LEGO MINDSTORMS EV3 robot
- Configure and run a Simulink model for Raspberry Pi to read the Accelerometer on-board Sense HAT and use the acceleration data to determine the motor speed of the EV3 robot.
- Configure and run a Simulink model for LEGO MINDSTORMS EV3 robot to receive data sent from Raspberry Pi hardware and use it to drive the EV3 robot motors.
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 movement control
In this section, we will discuss how the acceleration data can be used to control the movement of the robot.
The 'pitch' and 'roll' value corresponding to a given position of Sense HAT can be mathematically determined from the accelaration measured from the Accelerometer on-board Sense HAT. We map the 'pitch' and 'roll' movement of the Sense HAT to the movement of the EV3 robot. The 'pitch' would represent the forward and reverse movement of the motor whereas the 'roll' would represent the sideward movement of the robot.
The mathematical equations required to convert the 'pitch' and 'roll' values to linear speed of the robot are realized using a MATLAB function block in the Simulink model for configured 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 will receive this data and use it as input to drive the two motors.
Task 1 - Familiarize with Controlling EV3 using Host model
Host model to control LEGO MINDSTORMS EV3 robot example shows usage of Dashboard widgets in a Simulink model running on a Host machine to control the movement of a EV3 robot. The communication between EV3 and the Host is over UDP.
Complete this example to get familiarity with setting up a communication link between EV3 and a Host model for data exchange.
Task 2 - Install Simulink Support Package for Raspberry Pi
You need Simulink Support Package for Raspberry Pi to run Simulink model on Raspberry Pi hardware.
1. Install Simulink Support Package for Raspberry Pi.
Click the link to Download and Install Simulink Support Package for Raspberry PI
2. Complete the Getting Started with Raspberry Pi Hardware example in Simulink Support Package for Raspberry Pi you just installed.
Task 3 - Familiarize with Sense HAT Accelerometer
Sense HAT Auto-rotate 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 4 - 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. Please 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.
1. Connect Raspberry Pi board to the network with Ethernet cable through Ethernet port. Make sure that both the Raspberry Pi board, Host PC 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 6 - Run 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. Click Deploy to Hardware button of Raspberry Pi model to run the model on Raspberry Pi hardware.
2. Click Deploy to Hardware button of LEGO MINDSTORMS EV3 model to run the model on LEGO MINDSTORMS EV3 hardware.
3. Control the EV3 robot using the Sense HAT connected to the Raspberry Pi hardware.
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.
This example showcased the following
- Accelerometer on Raspberry Pi Sense HAT is used to control the movement of the EV3 robot.
- Raspberry Pi hardware sends the speed for the left and right motor to EV3 through UDP.
- EV3 receives this data and drives the motors.