I need someone to do paraphrase. Not summery !
Using LEGO Mindstorms EV3 Robotic Kit and Embedded
Programming to Implement a Self-Balancing Robot
Andrew Mikaiel Middle Tennessee State University
Box 19, 1301 E Main St, Murfreesboro, TN 37132 Tel: (001)615-898-2300
Lei Miao
Middle Tennessee State University Box 19, 1301 E Main St, Murfreesboro, TN 37132 Tel: (001)615-898-2256
ABSTRACT
This paper discusses an education project, in which we use LEGO
Mindstorms EV3 and embedded programming to implement a self-
balancing and line-following robot. In particular, we use the open
source ev3dev programming environment to write python
programs to get data from the sensors and control the motors. The
benefit of our approach is that the students can solely focus on
implementing the required PID controller and the corresponding
parameter tuning, without the need of having to do mechanical and
electrical design. We explain in the paper in details about the
system setup, software development, and testing and verification.
The outcomes of this paper can be very helpful to other educational
and research projects that utilize the Lego EV3 robotic kits for
learning and discovery purposes.
Keywords
LEGO Mindstorms EV3; EV3DEV; Embedded Software; PID;
robotics
1. INTRODUCTION Mechatronics Engineering is a newly established engineering
program in the Department of Engineering Technology at Middle
Tennessee State University (MTSU), Murfreesboro, TN, USA. The
curriculum of Mechatronics engineering combines mechanical,
computer, and electrical engineering, along with systems
integration and technical project management.
ENGR4530: Controls and Optimization is one of the required
courses of Mechatronics Engineering at MTSU. In this senior level
course, students study classical feed-back control theory in the s-
domain. Topics include transfer function, first and second order
transient response, block diagram reduction, stability of LTI
systems, root locus methods, PID controller design, etc. To
complement the theoretical aspects of the course, a hands-on course
project is required.
The project had some restriction and requirements that students had
to follow:
1. The project must involve feedback control to a mechatronics system, i.e., the students must use transducers (e.g., sensors)
and actuators (e.g., motors) to regulate the output. A type of
PID control must be used.
2. The project must be demo-able, i.e., it must work. A design without physical implementation or simulation is not
acceptable.
3. A default project topic is provided: build and program a two- wheel self-balancing robot. LEGO and Make block Ultimate
2.0 robotic kits are available.
4. Improving an ongoing project is also allowed; however, the students must show the improvement by implementing a PID
controller. Essentially, the students would need to justify how
the knowledge they learned in this course helps them in that
project; in addition, the students must demo the improvement
in a quantitative way. The students are encouraged to consult
the instructor about your project ideas.
5. The students are encouraged to form teams. Each team should have 1-3 members. In general, more members a team has,
higher project quality is expected.
In this project, we worked on the default project and used a LEGO
Mindstorms EV3 robotic kit (provided by the Engineering
Technology Department at MTSU) to build the self-balancing
robot.
LEGO Mindstorms EV3 is the third-generation robotics kit in
LEGO's Mindstorms line. It is the successor to the second-
generation LEGO Mindstorms NXT 2.0 kit. The "EV" designation
refers to the "evolution" of the Mindstorms product line. "3" refers
to the fact that it is the third generation of computer modules - first
was the RCX and the second was the NXT. It was officially announced on January 4, 2013 and was released in
stores on September 1, 2013. The Education Edition was released
on August 1, 2013. The Education Edition is designed for
classroom use while the Home Edition is the best pick for
individual LEGO fans to use at home. The complete list of
differences between the two editions can be found in [1]. There are
many competitions using this set. Among them are the First LEGO
League and the World Robot Olympiad.
The reason we chose the LEGO kit was that it was very easy to
build the self-balancing robot, as clear and detailed instructions are
available in [2]. Instead of spending a large portion of our time and
efforts to look for parts online, performing electrical and
mechanical design, and building the self-balancing robot from
scratch, we chose to put most of our efforts into the control aspect
of the project, i.e., PID controller design and parameters tuning.
Our decision was made also due to the fact that we only had two
people in the project group.
There are many ways to program the LEGO EV3: one can use the
LEGO programming language MINDSTORM, which is LabVIEW
based and uses blocks and graphical interfaces to program the
LEGO brick. Fig. 1 shows the different components and logical
applications of the LEGO MINDSTORM software. One can surely
use it to connect various components and build customized
programs for different applications. See Fig. 2 for a sample
MINDSTORM program provided in [2] for the self-balancing robot
project.
We could have chosen to use MINDSTORMS EV3 graphical
programming language mentioned above, but we did not like the
idea to having to invest some extra time to learn the language itself
and the functionality of each block. We actually tried using simple
examples, but it was not that easy to implement the PID controller
and also guarantee the self-balancing action. The MINDSTORM
software is mostly targeted for K-12 students and can be the ideal
tool for simple movements and color identification purposes; when
it comes to something as complicated as PID control and the self-
balancing concept, the graphical interfaces makes it more difficult
to program and troubleshoot. Note that the MINDSTORMS
software does provide a PID controller, but its implementation
details are not available; therefore, it is not suitable for a senior
level courses project where students would like to tune the PID
parameters. For the reasons above, we decided to write our own
programs and PID controllers from scratch, using a programming
language such as c/c++ and Python.
Fig.1 Mindstorm LEGO Language
Fig.2 Self-balancing tutorial posted by Robot Square [2]
The second option is ROBOTC [3]. ROBOTC is a cross-robotics-
platform programming language, it is a C-Based Programming
Language for popular educational robotics systems. It is the
premiere robotics programming language for educational robotics
and competitions. However, we had problems obtaining the
language IDE itself: ROBOTC is proprietary and expensive,
compared to MINDSTORM, Python, and c/c++ which are all
completely free.
The third option is to use EV3DEV [4], an embedded Linux
operating system with build-in support for many programming
languages. In particular, EV3DEV is a Debian Linux-based
operating system that runs on several LEGO MINDSTORMS
compatible platforms including the LEGO MINDSTORMS EV3
and Raspberry Pi-powered BrickPi.
We chose option #3 for a few reasons. First, the LEGO brick is very
easy to interact with and does not mind booting a Linux server on
it. Second, since all the programming related libraries are already
installed, there is no need to manually install them using “apt-get”
or “yum”. Third, EV3DEV is completely open-source and free.
Among all the programming languages provided by EV3DEV, we
went ahead and used Python 3.5 as our main programming
language. As part of the Mechatronics program’s curriculum,
Computer Science 1170 is a mandatory course. It is an introductory
course to computer programming, and Python is the main topic in
this course. We thought it will be a good idea to take what we
learned in that course and apply it to this project.
The main contribution of this paper is that we show the LEGO EV3
robotic kit can be used with open-source embedded software
platform EV3DEV to develop complex controls project that
involves motors and sensors. To the best of our knowledge, this is
the first paper that discusses how to use EV3DEV to implement a
self-balancing robot.
The organization of the rest of the paper is as follows: in Section 2,
we discuss the system setup; in Section 3, embedded software
development will be presented; in Section 4, we describe the testing
and verification procedure; finally, we conclude in Section 5.
2. System Setup Before starting programing, one needs to first make sure that the
LEGO kit is assembled properly using the instructions provided in
[2] and the LEGO brick’s battery is fully charged. There is an
option in the LEGO’s interface after you turn it on; it will allow
you to check if all motors and sensors are online or not. In
particular, if you go to settings in the brick itself, you will see an
option that says “component” and once you click it, it will show
you which components are currently connected to the brick and
online.
Before you can move on to the next step you will need a microSD
or microSDHC card with at least 2 GB of space (Note: microSDXC
is not supported). You will also need one of the following ways to
connect the LEGO brick to your computer:
1- USB cable (the one that comes with the kit is preferred) 2- Bluetooth connection. If your computer does not have a
Bluetooth adapter built-in, you can buy an off-the-shelf
USB dongle. You will need to share the network adapter
in your computer with the Bluetooth adapter in order to
allow the LEGO brick to access the Internet.
3- You can use either a USB Wi-Fi dongle or an Ethernet cable to allow the LEGO brick to connect directly to the
Internet.
Once you have the memory card, you will need to download and
burn the ev3dev image onto it, insert it into the memory card slot
on the LEGO brick, and let the LEGO brick boot from it. To
download the image disc and learn how to install it, one can refer
to ev3dev’s website [4] and check the reference page. Note that the
login username and password of EV3DEV are “robot” and
“maker”, respectively.
Note that the LEGO’s kernel version might be dated, and if it is the
case, it may not support the libraries needed for either Python3 or
c++ when using EV3DEV. For this reason, before writing the code
or suspecting any problem with the brick itself, we suggest that the
kernel of the brick is updated. Detailed instructions on how to
perform the update can be found in [5]. Fig.3 shows the kernel
version (4.4.19), followed by the LEGO’s version (15) and the OS
booted into (ev3dev-ev3).
The update process might take hours depending on the speed of the
Internet connection. Sometimes, it may crash in the middle of the
update process. If you cannot update the kernel, you will have to do
it manually, similar to what you did when installing the EV3DEV
image, i.e., you will have to download the upgraded version after
2016, install it on the SD card, and then flash it to the brick.
Fig.3 Kernel version
One more thing, the kernel has some security issues. So if you are
using MS Windows and do not want to install a virtual machine on
your computer, we would suggest that you use MobaXterm [6],
which is a simplified Linux interface allowing you to create and
edit files with ease. If it shows the “Permission Denied” error, right
click on the file that contains the code and change the permission
code to 777 (it will give you full permission of read, write, and
execution).
3. Embedded Software Development
3.1 High Level Design We use a PID controller to maintain the balance of the robot. PID
controllers are very powerful and yet simple controllers that are
widely used in various applications such as industrial process
control [7][8]. Fig. 4 shows the feedback control block diagram of
the PID controller. The input is the desired angle and the plant is
the motors and motor controllers. The actual angle of the robot is
estimated by a gyroscope sensor. The difference between the
desired angle and the measured angle is known as the actuating
signal or error 𝑒(𝑡).
Fig.4 PID feedback control block diagram
The PID controller has three components: P, I, and D.
𝑃 = 𝑘𝑝𝑒(𝑡) is proportional to the error, and it represents the present
time.
𝐼 = ∫ 𝑘𝑖 𝑒(𝜏)𝑑𝜏 𝑡
0 is the integral of the past errors, and it represents
the past.
𝐷 = 𝑘𝑑 𝑑𝑒(𝑡)
𝑑𝑡 is the derivative of the current error with respect to
time, and it is a sort of prediction to the future.
Note that 𝑘𝑝, 𝑘𝑖 , 𝑎𝑛𝑑 𝑘𝑑 are known as proportional, integral, and
derivative gains, respectively, and they need to be tuned carefully.
Combining all three components above, the output of the PID
controller is:
𝑘𝑝 𝑒(𝑡) + ∫ 𝑘𝑖 𝑒(𝜏)𝑑𝜏 𝑡
0
+ 𝑘𝑑 𝑑𝑒(𝑡)
𝑑𝑡
which is the input to the plant. We use PWM (Pulse Width
Modulation) for motor control purposes, and the PID controller’s
output is between -100 and 100: the sign controls the direction of
the motor and the integer value corresponds to the duty cycle of the
pulse signal.
In our program, the PID controller is implemented inside of an
infinite loop, in order to maintain the balance of the robot. The loop
delay was set to 200ms.
3.2 Interfacing with Sensors and Motors
Before worrying about the PID controller or the parameter tuning
process, we first verified that the two motors (left and right) and the
gyroscope sensor work properly. See Fig. 5 for the motors and
sensors the LEGO EV3 robotic kit has.
Fig.5 LEGO Brick along with other components
Essentially, we need the robot to move forward when tilted with a
positive angle and move backward when tilted with a negative
angle. To verify it, we wrote a piece of testing code, which is shown
below.
#!/usr/bin/env python3
# coding: utf-8
# -*- coding: utf-8 -*-
from time import sleep
from ev3dev.ev3 import *
# this line to import ev3dev.ev3 as a variable to you don’t have to
write it every time when using a member of this function
import ev3dev.ev3 as ev3
import logging
gy = ev3.GyroSensor()
A = ev3.LargeMotor('outA')
D = ev3.LargeMotor('outD')
gy.mode = 'GYRO-RATE'
gy.mode = 'GYRO-ANG'
X = int(input(“Enter value: “)) #testing motor movement by
entering your own values.
If x < 0: #negative
A.run_direct(duty_cycle_sp = -X)
D.run_direct(duty_cycle_sp = -X)
If x>0: #positive
A.run_direct(duty_cycle_sp = X)
D.run_direct(duty_cycle_sp = X)
The first 3 lines must be included in any Python code in order to
run it on an LEGO EV3 brick. A and D are variables declared to
indicate connection to the motors (A is Right, and D is Left). X here
is just a simple input which is passed as the duty cycle; later on, it
represents the output of the PID controller.
3.3 PID tuning After running the previous steps successfully, we proceeded with
writing the rest of the code. Specifically, the next task is to tune the
PID parameters: 𝑘𝑝, 𝑘𝑖 and 𝑘𝑑 . This turned out to be the hardest
and the longest procedure in this project. These parameters cannot
be randomly picked; instead, there are systematic ways to tune
them. We used the following steps:
1. Set all gains to zero.
2. Increase 𝑘𝑝 until the robot starts to show steady and large
oscillation with overshoot.
3. Reduce 𝑘𝑝 by 40% and start to increase 𝑘𝑖 until the
oscillation appears again.
4. Increase 𝑘𝑑 until the oscillation is dampened. 5. Use the 𝑘𝑝, 𝑘𝑖 and 𝑘𝑑 values obtained above as the
starting point to continue tune the parameters until the
robot stays balanced
Here are the values we used to keep the robot balanced:
𝑘𝑝 = 30.5
𝑘𝑖 = 5.6
𝑘𝑑 = 0.00007
Note that we did not use 0 degree as the desired reference angle;
instead, we used a small positive degree in PID tuning. It worked
better due to the construction of the LEGO robot.
3.5 Line following
The line following part was not the major focus in this project. As
a result, we spent limited time on it after we got the robot to self-
balance.
There are two approaches to implement the line following feature.
One is to use another PID controller to regulate the diversion from
the line’s color. Since we did not have much time left, we did not
go down this path. In a different method, one can simply take
advantage of the infrared sensor API which contains class members
such as intensity and distance. In turn, these attributes can be used
to compare two different colors. For example, in this project we
used a white solid surface with black lines on it to make it easy for
the infrared sensor to differentiate the two colors.
The idea is to give the robot enough time and space to adjust itself
when it moves away from the black line. What we did was to utilize
an if statement that detects a diversion of 25% from the black color,
i.e., the light intensity reaches 75% black and 25% white. Note that
25% is a lot of diversion from the original route. Once this happens,
the robot will turn towards the other side. The process continues in
a loop so that the robot moves forward along the line.
4. Testing and Verification We spent significant amount of the time on tuning the PID
controller and testing.
We first tested the LEGO EV3 self-balancing robot on a piece of
carpet, since it is easier to keep the robot balanced on surfaces with
higher friction. On carpet, it showed some resistance when
changing directions, but it worked better than using a hard surface
with less friction.
Fig.7 LEGO in action
After seeing success on carpet, we continued our testing on our lab
bench desk. Most of our time was actually spend during this phase.
Because the desk is quite different from the carpet, we had to retune
the PIC controller parameters.
To test the line following feature, we chose to use a piece of
plywood covered with white paper. See Fig. 6 for the test setup.
Then, we drew lines with black markers on the white surface.
During our testing, we noticed that the LEGO brick froze randomly.
After troubleshooting, we realized that sometimes the memory card
did not contact well with the LEGO brick, due to vibration. The
solution was to tape the memory slot on the brick in order to prevent
the memory card from slipping outside. We think LEGO should
resign the memory card slot so that the card itself can be locked into
place.
5. Conclusions In this paper, we discuss a senior level controls course project, in
which a LEGO EV3 robotic kit and the EV3DEV embedded
software development platform are used together to implement a
self-balancing robot. Our results indicate that the open-source
EV3DEV environment is mature enough to handle complex
engineering projects such as the one studied by this paper. We think
that EV3DEV is an excellent programming tool for LEGO EV3
robotic kits and may be very useful in not only upper-division
robotics, controls, AI courses, but also lower-division engineering
education and programming courses. We also believe EV3DEV
and the LEGO EV3 robotic kits can be used in various academic
and industrial research projects.
6. ACKNOWLEDGMENTS We thank the Department of Engineering Technology at Middle
Tennessee State University for making this project possible and
providing the necessary equipment to get the project done.
We thank Hatim Alami, one of the team members who contributed
to the course project.
7. REFERENCES [1] https://www.lego.com/en-us/service/help/products/themes-
sets/lego-education/differences-between-lego-mindstorms-
ev3-home-and-education-editions-408100000007851
[2] “Tutorial: Self-Balancing EV3 Robot.” Robotsquare, 1 July 2014, http://robotsquare.com/2014/07/01/tutorial-ev3-self-
balancing-robot/
[3] http://www.robotc.net/
[4] www.ev3dev.org
[5] “Documentation.” ev3devHome, www.ev3dev.org/docs/getting-started/
[6] https://mobaxterm.mobatek.net/
[7] Yaskawa Electric America. “Introduction to PID Control.” Machine Design, 5 Feb. 2016,
www.machinedesign.com/sensors/introduction-pid-control.
[8] “PID Theory Explained.” PID Theory Explained - National Instruments, www.ni.com/white-paper/3782/en/