MATLAB Answers


Help with balancing GyroBoy (Lego Mindstorms EV3) using MATLAB

Asked by Mohammad Sarim on 22 Oct 2014
Latest activity Edited by Sandip
on 3 Feb 2015
Hello all,
I am trying to program a self-balancing two-wheeled robot (GyroBoy) using Lego Mindstorms EV3. I have programmed the PID controller in MATLAB to balance it. I tried to program it by taking some help from an online available code which works perfectly well to balance it. (Code available here in the form of Lego EV3 software program). The problem is that the output of my PID, which is the input to the motors, has an unbound value. The EV3 motors take an input in the range [-100, 100]. Hence, when ever my PID output is more than 100, it runs the motor at 100% power. So, I am unable to balance the robot. I have tried scaling the PID output, but then the value is too low to be able to balance it. Can someone guide me in any direction regarding how to balance a GyroBoy using MATLAB? I am using the EV3 hardware support in MATLAB R2014b.


Sign in to comment.

1 Answer

Answer by Jon Boerner on 23 Oct 2014

Hi Mohammad,
When you say you have programmed the PID controller, using MATLAB, what exactly do you mean? Are you still using the Mindstorms environment to program the EV3, or are you using a MATLAB/Simulink Support Package? Also, how are you developing your PID loop?
Lastly, have you seen this example already?
Not exactly what you are trying to do, but fairly similar in terms of setup.
Lastly, scaling the PID probably is not the best thing to do. You would want to re-tune it with different levels of aggressiveness and confirm that the motor effort does not exceed 100 (in this case). Just directly scaling the output allows the poles of the closed-loop system to become unstable, even if the original PID controller was stable.


Hi Jon,
First of all, I am using the MATLAB support package included in the MATLAB R2014b. I am using the Gyro Sensor to get the inclination of the robot with the vertical and implementing PID control over that to keep it at the stable position (zero degrees). I am estimating the rate of change of the Gyro angle through Kalman filter. This serves as the differential error in my PID loop. (The rate of change of Gyro angle can be found directly using the MATLAB support package, but it doesn't work and messes up the calibration of the gyro sensor).
I already looked at the example, but I was unable to test it since I am unable to connect the EV3 to MATLAB using Bluetooth (tried a few times), and I don't have a WiFi dongle. The Simulink doesn't connect through USB.
I also noticed that scaling is not an option, and I have been trying for weeks now to tweak the parameters of the PID loop, but have been unsuccessful.
I was wondering has no one tried balancing a two-wheeled robot using MATLAB?
Hi Mohammad,
I am also facing a similar problem regrading the connection to EV3 over Bluetooth from Matlab R2014b. I have also included the support package in MATLAB R2014b, but no matter what I do, I am not able to connect to EV3 over Bluetooth.
I am also getting the following error:
myev3 = legoev3('Bluetooth','COM21') Error using error Unable to load a message catalog 'legoev3io:build'. Please check the file location and format.
Error in legoev3 (line 157) error(message('legoev3io:build:CommInvalidType'));
I was wandering what to do ?

Sign in to comment.