Embedded Thesis
Embedded Thesis
TRINH VI HAO
Lecturer
PhD. Tran Duc Anh Minh
i
ACKNOWLEDGEMENT
I would like to express gratitude to Mr. Minh for teaching and imparting
knowledge and experience so that I could complete the project and, more
importantly, equip myself with the essential knowledge and abilities useful in the
future. In addition, I would want to show my thankfulness to the school's
management as well as the instructors who worked tirelessly to ensure that I
completed the course successfully. I would like to express my gratitude to my
parents, who reared me and provided the opportunities for me to study at Ton Duc
Thang University.
I wish everyone of the teachers on the Board of Directors good health and
many accomplishments in the teaching. I wish you and your family good health,
happiness, and plenty of joy. Wish Ton Duc Thang University will always be the
first and most dependable choice for many parents and students eager to begin their
university careers.
Sincerely thanks!
Trinh Vi Hao
ii
The thesis was carried out at Ton Duc Thang University
Advisor: ........................................................................................
........................................................................................
(Title, full name and signature)
…………………………. ………………………………
iii
THE THESIS HAS BEEN ACCOMPLISHED
AT TON DUC THANG UNIVERSITY
I hereby declare that this thesis was carried out by myself under the
guidance and supervision of ……………………………..; and that the work
contained and the results in this thesis are true and have not been either
submitted anywhere for any previous purpose or published in any other
literature. The data and figures presented in this thesis are for analysis,
comments, and evaluations from various resources by my own work and have
been fully acknowledged in the reference part.
In addition, other comments, reviews and data from other authors, and
organizations used in this thesis have been acknowledged, and explicitly
cited.
I will take full responsibility for any fraud detected in my thesis. Ton
Duc Thang University is unrelated to any copyright infringement caused on
my work (if any).
Trinh Vi Hao
iv
v
TON DUC THANG UNIVERSITY THE SOCIALIST REPUBLIC OF VIETNAM
FACULTY OF ELECTRICAL – Independence – Freedom - Happiness
ELECTRONIC ENGINEERING ----------------------
-------------------
vi
8 System Identification, PID’s controller errors
11/04/2022 PID, Close-loop system fixed. Testing.
9 Building the model base Complete the Report
18/04/2022
10 Final Checking report Submit
25/04/2022
vii
SPEED AND POSITION CONTROL OF DC MOTOR
ABSTRACT
Using Arduino module UNO R3 and L298N Motor Driver module to control
speed and position of unknown parameters DC Motor, the model is built,
monitor and simulate with MATLAB and Simulink. MATLAB and Simulink
System Identification Toolbox and PID tuner is used to configure a PID
Controller. Speed of the motor is controlled by using PWM. The host computer
is where the model is controlled, Arduino board and the host computer
communicating through Serial Communication Protocol. With MATLAB, this
can be done by running the model on “External Mode”, the motor is running
physically and it’s data (Speed and Position) is displayed on the host computer.
viii
CONTENTS
ix
CHAPTER 4.DATA ANALYSIS ................................................................................... 41
REFERENCES................................................................................................................. 44
x
LIST OF FIGURES
xi
3-15 MATLAB FUNCTION BLOCK ............................................................................ 23
3-24 IMPORTED DATA NAMED “Z1” AND “Z2” AND THEIR TIMEPLOT ......29
xii
3-41 SETPOINT 80RPM ................................................................................................. 39
xiii
ABBREVIATIONS
xiv
EMBEDDED SYSTEM THESIS
Page 1
_______________________________________________________________________________
CHAPTER 1. INTRODUCTION
MATLAB and Simulink are potential programs in the field. They interact with
each other so well that is convenient for users such as simulation the model and
save logged data into workspace in MATLAB, and from that data, users can process
any way they want with it. The later versions of MATLAB have a package apps
built for Arduino programming and Simulink, too. Controlling DC motor with
Arduino through MATLAB and Simulink can help building the model, simulate,
control and log the feedback or simulation data to the workspace at the same time.
Choosing the topic will not only having myself to get used to Simulink and
MATLAB but also knowing the fundamental properties as well as working
principles of a DC motor. By saying that I mean, from this model, I can evaluate it
into controlling small RC cars, robots or robot arms with different kinds of motors.
Controlling DC motor with MATLAB has a variety of ways, in this topic, I will
use Data-driven control method. This method is recommended when the plant – DC
motor – parameters, such as, are unknown. Use open loop and sensors to log the
simulation data of the plant to workspace, then use MATLAB built-in toolboxes to
estimate the plant transfer function. Having the transfer function allows us to
configure the parameters of PID controller to put in the closed-loop system. With
setpoint is the input by user, the plant will work as accurate as the transfer function
fitted in.
The digital words are the outputs of the A/D converter, which is Analog-
Digital converter, which input is a Continuous-time signal. In reverse, a D/A
converter is used when digital signal is needed to convert to analog signal.
To describe the digital control system, we need a discrete-time transfer
function which is need z-transform, or discrete-time transfer function is
continuous-time transfer function written in z-plane.
The terminology used in connection with G(s) is also directly applicable in the
case of G(z). The order of G(z) is determined by the denominator polynomial of
G(z), the roots of denominator are called poles, and the roots of numerator are
called zeros. The stability is defined if the poles of the transfer function lie inside
the unit circle.
PIDs output a control signal to a plant from errors feedback from the sensors,
that is, makes the plant work accurately. It can be either only P controller, PI
controller or PID controller, which PID controller is faster than others. A PID
has tunable parameters Kp, Ti, and Td.
2.1.3 PWM
2.2.1 RPM
RPM is rounds of the motor output per minute, it’s calculated by the rated of
change in position divided by the rate of change in time in seconds and multiply
by 60 as there are 60 seconds in 1 minute.
2.2.2 Position
Position monitoring of the motor is simpler than the RPM as it’s calculated
by counting the output PPR and multiply with 360 to gives the position of the
motor in degrees, as 360 is the degree per revolution.
Encoder used for DC motor usually has dual channel outputs, often marked
as Channel A and Channel B, the two channels output pulses in 90 degree phase
difference, one comes after another, channel B comes after channel A if the
motor spins forward or clockwise direction, and channel A comes after channel
B if the motor spins backward or counter-clockwise direction.
2.2.4 Interrupts
For the digital hardware to process the signals from the encoder, interrupt
pins are required. For the UNO, pin 2 and pin 3 are the suitable for this task, for
whenever there is a RISING edge of encoder signal, these pins will detect and
call a subsystem to process the signals, Simulink has a built-in blocks for these
tasks as well.
Like the System Identification Toolbox, the PID Tuner is available when
there’s a transfer function of the model.
3.1.1 Components
Also a popular Motor Driver module, uses IC L298 to drive the motor
Configuration :
- Vs DC 5V ~ 35V
- I(max) 2A
- I(no-load) 10mA
- Control Pins INT1, IN2, INT3, INT4
- Motor Output OUT1, OUT2, OUT3, OUT4
- H-Bridge pins ENA, ENB
Note that, with 3 cells used, theoretically, I will have a 11.8V power
supply, however, practically, the voltage it’s only 10.8V measured with
VOM. So the voltage data that logged into MATLAB in Simulink will be
measured by VOM for accuracy.
“Hardware” panel for “Run on Target” mode is now visible. Set “Mode” to
“Run on Board”, and “Monitor & Tune” to run the program and monitor it at the
same time.
System Identification Toolbox required datas from open-loop system to
estimated transfer function. The flowchart of the model is shown below
First, control the model by sliding the PWM “Slider gain” which is also
connected to a constant “255” block in the Simulink model, the “Slider gain”
will decide the percentage from 0 to 1 (100%) of PWM to the “pin ~11”, which
is digital PWM output signal of the Arduino board. As seen from the flowchart,
Pin 2 outputs pulses read from the encoder, which is then processed by the
MATLAB function block to calculate the RPM. In the other branch, PWM
percentage levels (0, 0.25, 0.5, 0.75, 1) is then converted to referenced measured
output voltage at L298N’s motor output terminals. Both RPM and Voltage
outputs are connected to the scope to be logged into the MATLAB workspace.
Theoretically said, now I will build the model in Simulink to run the motor in
open-loop system by getting blocks in “Library Browser” and connected like the
figure below.
The “Direction Cons” block is to set value to “1” for the Motor turns
Clockwise, ad set to “-1” for the Motor turns Counter-clockwise, the Direction
Constant is compared to 0, the ouput of the comparison is 1 if “TRUE” and 0 if
“FALSE” and both “FALSE” or 0 when the Direction constant is 0 to stop the
Motor.
“PWM Constant” is set to 255 as PWM only takes value from 0 – 255, what
control the value of PWM is the slider gain “PWM Control” map the value from
0 to 100%. Now the motor is ready to run in both directions and various speeds.
Add blocks to calculate the speed from encoder. For speed measurement,
only a channel is needed as there’s no need another one to feedback direction.
“External Interrupt” block is needed to read signals from encoder as mentioned
Whenever the function is called, the initial value becomes “1” and output to
the “pulse” output port, while also save the previous values to be added up later
with “Unit Delay” port, which remembers 1 value and save it for the set
sampling time. This is also called the counter-up function commonly used in
Simulink to count pulses from the encoder. The “Digital Clock” is the real
The Function block output DeltaPulse and DeltaTime, these are then divided
and go through the “Pulses to RPM” gain block to output the RPM, visualized
by the Scope and Display.
Now, the voltage applied is needed to log input-output data. However, the
percent of PWM is not proportional to Voltage, so for each percent step 25%
from 0 to 100%, I will measure the output voltage realistically with the VOM
and write a MATLAB Function to comparison PWM from slider gain to output
measured Voltage.
It can be seen that at even 100%, the motor can only runs at 103RPM
compared to it’s specification is 130RPM, this is due to voltage drop on Motor
Driver module. As measured with VOM, with 100% PWM and a 10.8V power
supply, it can only output 9.23V to the Motor, which is ~1.6V dropped.
To deal with this problem, I measured the output voltage to the motor 4
times, at 25%, 50%, 75% and 100% PWM for accurate voltage – speed graph,
this is done by writing a MATLAB function like below.
Additionally, our Simulink model will now look like this figure above.
Now the model is ready to log the data to the workspace. Go to the Scope’s
configuration to get access to “Logging” panel.
Check the “Log data to workspace” then name the “Variable name” freely, the
“Save Format” should be “Structure”.
Here, the Logged Data from scope in Simulink is named Data1 for Datasets
of the first simulation, and Data for the Datasets of the second simulation.
There’s are 2 arrays inside each struct of Data. First array is named
“Data1.signals(1).values” stores “Voltage” and the second array is named
“Data1.signals(2).values” stores “RPM”, the Data2 values are the same.
Assign “x1”,”x2” for Data1 and Data2 “voltage” with command “x1 =
Data1.signals(1).values” and as same as x2. Continuously, for “y1” and “y2” for
Data1 and Data2 “RPM” as “y1 = Data2.signals(2).values”.
The datas are now ready to be put in “System Identification Toolbox”. Import
data in Time-domain time.
3-24 Imported Data named “z1” and “z2” and their Timeplot
Set “z1” as “Working data” and “z2” as “Validation Data”. Set “Estimate”
panel to “Transfer Function Estimation”
From the figure above, it can be seen that the estimated transfer function fit
up to 88%, which is acceptable. Drag “tf1” to “To Workspace” panel to export
the transfer function to Workspace.
Flowchart for the PID tuner is shown below
The constant block can be replaced by step response block just in case. Error
calculation block is where in this case equals to 1 (constant) subtract to the
feedback signal which is output signal of the transfer function.
The PID controller is ready to be tuned. Create a new Simulink model to tune
PID
Use the slider gain in the upper of the panel to tune PID, in this project, I
only use PI controller as PID requires a stronger and faster host-computer.
The PI block is now ready to put in the final model to control DC motor. The
following flowchart for the closed-loop system is illustrated below.
The control signal process the output signal from PI controller and output a
control signal to control the Arduino pins. The gain is to convert PID output to
PWM signal “255/y(t)”.
The Hardware subsystem is where the host computer communicate with the
Arduino, where Pin 11 is where PWM signal input, pin 5 and pin 6 is for
Clockwise and Counter-Clockwise direction control. The MATLAB function is
to compute the control signal, output the PWM (pwr) and direction (fwd, bwd).
The “abs()” function get the absolute value of u, which is PWM signal output.
The “status” gets input from “Toggle” button which “1” for Forward mode, and
“0” for Backward mode.
This block contain the sensor from Encoder, the subsystem inside is as same as
the blocks where I use to measure speed from open-loop system (figure 3-4).
The model is now ready to run. Press “Monitor and Tune” to run the motor.
4.1 Comments
As seen from figure 3-22 to 3-26 , Firstly ,the RPM setpoint is 20RPM, from the
feedback signal, the error display is 0.6 and the actual RPM is 19.39, this
number however fluctuate as the signal is digital, and the transfer function
estimate before only fits 88%. Set the speed from 20 to 100 (max speed) with
step size is 20 and observe through the scope to see how accurate the system is.
Sometime, the errors get negative or positive or even 0 error.
Graphically, figure 3-27, display a whole picture of how the system response to
the control signal. It can be seen that the transfer function estimated from the
beginning affect the whole process, choosing it with high fit percent can
construct a well-response controller.
CHAPTER 5. CONCLUSION
5.1 Conclusion
5.1.1 Conclusion 1
MATLAB and Simulink Toolboxes are helpful if used the right way. From the
unknown plant problem, I can still can find the transfer function of the controller
if the Input/Output data is logged carefully. Finding the best fit transfer function
from varying poles and zeros is quite challenging, however, the higher the
transfer function fits, the higher accuracy it would be in afterward construction.
PID controllers are common used in control system, however, while doing this
project, the PID mode seems to make the simulation to run slower, sometimes,
the motor runs but the host-computer does not show the feedback that is why PI
mode controller is chosen to run this system.
5.1.2 Conclusion 2
With basis learned from embedded system basis, developing the model to
controlling a different kind of motor such as stepper, servo, etc. step by step
constructing machines like 3D printer or CNC resemblance at low-budget. Deep
learning on micro-controllers and coding on MATLAB and Simulink as there
are still a vast amount of fields that MATLAB and Simulink provide that I have
not mentioned.
REFERENCES
English
Arduino S.r.l. (2021). Arduino® UNO R3. Arduino UNO R3 datasheets. [Revised
June 2021]
M. Gopal. (2003). Digital Control and State Variable Methods : Conventional and
Neuro-fuzzy control systems. New Delhi, India: Tata McGraw-Hill.