This exercise is intended to help students to understand how to communicate with the LEGO MINDSTORMS EV3 brick using MATLAB.
- Build a four-wheeled vehicle with two large motors, which has a sonic sensor at the front.
- Program the vehicle in such a way that it drives forward with 50% engine power until it is 30 cm away from an obstacle. Then it drives backwards with 50% engine power for two seconds.
- Read the rotation of the motors and the distance data of the sonic sensor. Display it graphically over time (in seconds).
- Discuss possible lags in the response of the vehicle to the MATLAB commands and between the sensor data and the motors.
Solution
- First you have to build the vehicle according to the LEGO building instructions. The building instructions are in a LXF file which can be opened with the free LEGO Digital Designer software, which is available for Computers running macOS or Windows. After launching the software you can use the view menue to switch from the construction mode to the building instructions mode.
- Your MATLAB script creates connections to the motors and the sonic sensor, then uses a WHILE loop to move the vehicle until the distance is smaller than 0.3 meters. Then the script uses another WHILE loop to move backwards for 2 seconds. The functions TIC and TOC are used to measure time. The function PLOTYY is used to display both distance and rotation over time in a single graphics.
LEGO building instructions and MATLAB script to run the experiment
Download the building instructions and MATLAB script. The LXF file contains the building instructions to be used with the free LEGO Digital Designer software available for macOS and Windows.