Uploaded by 224332236

FULLTEXT01 (2)

advertisement
Degree Project in Technology
First cycle, 15 credits
Heuristic control of a small
line-following robot
A comparison of different methods
NEO GUMBEL
OMED KHIRZIA
Stockholm, Sweden, 2022
Heuristic control of a small
line-following robot
A comparison of different methods
NEO GUMBEL
OMED KHIRZIA
Date: May 30, 2022
Supervisor: Daniel Frede
Examiner: Martin Edin Grimheden
School of Industrial Engineering and Management
Swedish title: Heuristisk reglering av en liten linjeföljande robot
Swedish subtitle: En jämförelse av olika metoder
TRITA-ITM-EX- 2022:102
© 2022 Neo Gumbel and Omed Khirzia
Abstract | i
Abstract
A line-follower is a robot that follows a line. Line-followers can be used in
industry, mostly for material transportation, but are also raced against each
other in competitions. While line-followers used in industry prioritize smooth
motion and accuracy, competition-style line-followers prioritize speed around
the track. In order for a line-follower to perform well, it needs a robust controlalgorithm. Usually, competition-style line-followers use a manually tuned
PID-algorithm for control. Tuning a PID-controller can be difficult, and is
dependent on the skill of the tuner. There are, however, multiple so called
”heuristic” tuning methods that can give more consistent results, quickly. In
this work, some popular heuristic tuning methods have been investigated for
control of a competition-style line-follower. The methods have been compared
in terms of step-responses and cumulative deviation of the robot from a track.
The results indicate that the purely proportional variant of the Ziegler-Nichols
method gives the best performance for competition-style line-followers. The
”some overshoot” variant of the modified Ziegler-Nichols method gave the
most consistent and accurate results, and could be appropriate for use in
an industrial-scale line-follower. Surprisingly the newest method tested, the
Tyreus-Luyben technique, had a very unstable behaviour, and have thus been
excluded from multiple tests.
Keywords
PID, Ziegler-Nichols, Tyreus-Luyben, Heuristic, Line-follower
ii | Abstract
Sammanfattning | iii
Sammanfattning
En av de enklaste typer av själkörande fordon är en linjeföljande robot.
Dessa kan användas i industrin, som materialtransportörer, men också i
tävlingar där den snabbaste vinner. För att en linjeföljare ska prestera väl
så krävs en robust kontrollalgoritm. De flesta tävlingsinriktade linjeföljare
använder sig av en manuellt inställd PID-algoritm. Att manuellt ställa in
en kontroller kan dock vara svårt, och kan ge inkonsekventa resultat. För
att snabbt ställa in en kontrollalgoritm, med goda och konsekventa resultat,
finns det ett flertal så kallade ”heuristiska” metoder. I det här arbetet har
några heuristiska metoder använts för att kontrollera en liten linjeföljare.
Metoderna har sedan utvärderats utifrån stegsvar och den totala avvikelsen
från en bana under en körning. För en liten linjeföljare så gav den rent
proportionella varianten av Ziegler-Nichols-metoden bäst prestanda. ”Some
overshoot” varianten av den modifierade Ziegler-Nichols metoden hade dock
det jämnaste och mest konsekventa resultatet, och lämpar sig antagligen bäst
för en industriell linjeföljare. Förvånande nog så gav den nyaste metoden,
Tyreus-Luyben, absolut sämst resultat, och kunde i många fall inte ens testas
utan att roboten spårade ur.
Nyckelord
PID, Ziegler-Nichols, Tyreus-Luyben, Heuristik, Linjeföljare
iv | Sammanfattning
Acknowledgments | v
Acknowledgments
We would like to thank our supervisor Daniel Frede for the feedback and
help given to us during our meetings. We’d also like to thank the laboratory
assistants Algot Lindestam and Tore Malmström for all their help and guidance
in our search for solutions.
Stockholm, May 2022
Neo Gumbel
Omed Khirzia
vi | Acknowledgments
Contents | vii
Contents
1
2
Introduction
1.1 Project background . .
1.2 Problem . . . . . . . .
1.3 Purpose . . . . . . . .
1.4 Goals . . . . . . . . .
1.5 Research Methodology
1.6 Delimitations . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Theoretical background
2.1 Components . . . . . . . . . . . .
2.1.1 QTR-8A . . . . . . . . .
2.1.2 Arduino Nano . . . . . . .
2.1.3 L149.6.21 Micro Motors .
2.1.4 DRV8833 motor-controller
2.1.5 Power-source . . . . . . .
2.1.6 Chassi and wheels . . . .
2.2 PID-controller . . . . . . . . . . .
2.3 Tuning methods . . . . . . . . . .
2.3.1 Ziegler-Nichols . . . . . .
2.3.2 Modified Ziegler-Nichols .
2.3.3 Tyreus-Luyben . . . . . .
2.4 Integral Square Error (ISE) . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1
2
2
3
3
3
.
.
.
.
.
.
.
.
.
.
.
.
.
5
6
6
6
6
6
7
7
7
8
8
9
9
10
3
Methods
11
3.1 Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Testing & Data collection . . . . . . . . . . . . . . . . . . . . 12
4
Results and Analysis
15
4.1 Ultimate gain and ultimate period . . . . . . . . . . . . . . . 15
4.2 Step-responses . . . . . . . . . . . . . . . . . . . . . . . . . 15
viii | Contents
4.3
4.4
4.5
ISE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
A comment on Tyreus-Luyben . . . . . . . . . . . . . . . . . 20
5 Discussion
5.1 Ziegler-Nichols PID . . . . . . . . . . . .
5.2 Ziegler-Nichols modified no overshoot . .
5.3 Ziegler-Nichols modified some overshoot
5.4 Ziegler-Nichols P-controller . . . . . . .
5.5 Ziegler-Nichols PI-controller . . . . . . .
5.6 Tyreus-Luyben . . . . . . . . . . . . . .
5.7 Comparison of methods . . . . . . . . . .
5.8 Sources of error . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
21
21
21
22
22
23
23
24
25
6 Conclusions and Future work
27
6.1 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
6.2 Future work . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
References
29
A Drawings
31
B Circuit connections
34
C Step-responses
35
D Arduino Code
46
E Python Code
52
Contents | ix
BRUH
x | List of Figures
List of Figures
2.1
Competition-style line-following robot [5] . . . . . . . . . . .
3.1
3.2
3.3
The assembled line-follower . . . . . . . . . . . . . . . . . . 12
Straight track . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Curvy track . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1
Example step-responses from all variants of the ZieglerNichols method . . . . . . . . . . . . . . . . . . . . . . . . .
Example step-responses from both variants of the modified
Ziegler-Nichols method . . . . . . . . . . . . . . . . . . . . .
Example step-response for the Tyreus-Luyben full PID controller
Average overshoot and standard deviation for the different
controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Average rise time and standard deviation for the different
controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Average settling time and standard deviation for the different
controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Average settling value and standard deviation for the different
controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Average ISE and standard deviation for the different controllers
4.2
4.3
4.4
4.5
4.6
4.7
4.8
5
16
16
17
17
18
18
19
19
List of Figures | xi
BRUH
xii | List of Tables
List of Tables
2.1
2.2
2.3
Ziegler-Nichols tuning parameters [4] . . . . . . . . . . . . . 9
Modified Ziegler-Nichols tuning parameters, only for PID [4] . 9
Tyreus-Luyben tuning parameters [11] . . . . . . . . . . . . . 10
4.1
4.2
Ultimate gain, K0 and ultimate period, T0 . . . . . . . . . . . 15
Average values and standard deviation for the full PID ZieglerNichols and both versions of the modified Ziegler-Nichols . . 20
Average values and standard deviation the P and PI versions
of Ziegler-Nichols, and the full PID version of Tyreus-Luyben 20
4.3
List of Tables | xiii
xiv | List of acronyms and abbreviations
List of acronyms and abbreviations
AGV
Automated Guided Vehicle
DC
Direct Current
IR
ISE
Infrared Radiation
Integral Square Error
P
PCB
PI
PID
PWM
Proportional
Printed Circuit Board
Proportional Integral
Proportional Integral Derivative
Pulse Width Modulation
ZN
Ziegler-Nichols
Introduction | 1
Chapter 1
Introduction
This report details the construction and control of a small line-following robot.
In the following section, the relevant background will be discussed.
1.1
Project background
The last decades has seen a rapid rise in autonomous vehicles. From automatic
vacuum-cleaners that navigate a confined space to fully self-driving cars,
autonomous vehicles are here to stay. Although self-driving cars use a variety
of complex sensors and algorithms, there are a range of simpler autonomous
vehicles. One of the simplest forms of autonomous vehicles is the linefollowing robot. These are found doing various repetitive tasks in industry,
mostly material transportation, and are a type of AGV, or ”Automated Guided
Vehicle” [1]. What all line-followers have in common is that they follow some
sort of line. This line can be visual, like a high contrast line of tape, or a wire
buried in the floor, giving off some sort of signal. Line-followers are also raced
against each other in competitions, where the quickest and most accurate wins.
This report will detail the control of a competition-style line-follower.
Although the principles for an industrial application would be largely the same,
the scale makes the smaller competition-style robots much easier to realize.
Because of the greater relative speed of the robot, a competition style linefollower might even be said to have greater demands on a robust controlalgorithm than an industrial line-follower. Testing controllers on competitionstyle line-followers can thus be beneficial.
Most small scale line-followers seem to employ a PID-algorithm for control.
This is advantageous because the algorithm can be easily tuned manually,
2 | Introduction
which doesn’t require in-depth knowledge of control theory. The manual
tuning process involves changing one of the PID-parameters at a time, and
testing if the robot performs better. There are however several methods used
in industry today to quickly tune PID-controllers through a set of simple
rules. These are called ”heuristic” tuning methods, and are frequently used
in industry today [2].
There are several well known heuristic tuning methods for PID-controllers. A
very popular one is the one proposed by Ziegler and Nichols in 1942, simply
referred to as Ziegler-Nichols. It is often held that this method is sufficient
for most applications [3]. There are however other heuristic methods that
build upon, or claim to outperform, it. Comparing these heuristic methods
is therefore an interesting question.
One of these newer methods is a variation of the standard Ziegler-Nichols
method, the modified Ziegler-Nichols. This method was suggested by Ziegler
and Nichols themselves and provides slightly different tuning rules compared
to the standard method, but uses the same main tuning technique [3].
Another, significantly newer, method is the Tyreus-Luyben technique. It was
first introduced 55 years after Ziegler-Nichols, in 1992. It uses the same
method of tuning as Ziegler-Nichols, but claims to lead to greater stability.
The tuning rules and final controller are thus different [4].
Using a competition-style line-follower to compare methods is advantageous.
Because of the jolting, quick movements, it requires a robust controlalgorithm. Therefore, line-followers can make a great benchmark for the
performance of heuristic tuning-methods, and can be used to draw conclusions
on other similar processes.
1.2
Problem
How do you use heuristic methods to tune a PID-controller, and how do you
measure their performance?
1.3
Purpose
The purpose of this work is to compare three heuristic tuning methods for a
PID-controller, used in a competition-style line-following robot. The robots
Introduction | 3
will be compared in terms of step-responses and cumulative deviation from
the line. This report will explore the different methods used to complete this
task. It will also attempt to draw a conclusion on which method is better, and
if these results can suggest if either of these methods are suitable for other
line-followers, for example those used in industry.
1.4
Goals
The goal of this project is divided into several subgoals.
The subgoals are:
1. Build a working prototype, able to complete the necessary operations,
and collect data
2. Tune the controller using Ziegler-Nichols, and gather data
3. Compare these results to a controller tuned with the modified ZieglerNichols, and the Tyreus-Luyben method
1.5
Research Methodology
The PID-controller will in turn be tuned with the three different methods.
Thereafter, two experiments will be carried out. Firstly, step-responses for the
controllers will be collected and analyzed in terms of rise time, settling time,
and overshoot. Secondly, the robot will be run on a track, and collect data on
the cumulative deviation from the line. This will provide a direct measurement
of how well the different controllers perform in a real-world scenario.
1.6
Delimitations
This report will limit itself to discussing a small scale line-follower, with a
length and width of less than 200 millimeters. The robot will follow a line
of black tape of width 19 millimeters on a white wooden surface. The track
will be less than 2 meters long, and the robot will remain tethered to a powersupply for the duration of the drive. The data will be collected through a cable
connected to a computer.
4 | Introduction
Theoretical background | 5
Chapter 2
Theoretical background
Although a line-follower is a simple robot in concept, the optimisations
possible are almost endless. Generally, the robots found in competitions
follow the design found in Figure 2.1.
Figure 2.1: Competition-style line-following robot [5]
When the robot follows a high contrast line, an IR-sensor is appropriate for
line detection. Simple versions of a bot might only use two sensors, one on
6 | Theoretical background
either side of the line, but more advanced versions use an array of multiple
sensors [6]. The sensors generally sits on an arm that is supported by a Caster
wheel. The arm is attached to a wider base supporting two DC-motors, which
most components sits between, as seen in Figure 2.1. This puts the center of
mass close to the motors, which allows the bot to turn quickly. The bot turns
by varying the speeds of the motors. A sufficiently advanced bot might use a
custom PCB as the body, eliminating cables and greatly slimming it down.
2.1
Components
Some must-haves for a line-follower include a way to detect the line, motors,
and a micro-controller. What follows is a list of components used in this
project.
2.1.1 QTR-8A
A common choice for line-detection is the QTR-8A, which is an IR-sensor
array with an included Arduino library. The array has 8 individual analog
sensors that together gives a score from 0-7000, indicating where the line is
placed under the board. A score of 3500 means the line is right under the
middle of the board.
2.1.2 Arduino Nano
As not much computation is needed to run simple control-algorithms, the
Arduino Nano works well for this kind of application. The small scale of this
microcontroller is also an advantage, making it easy to fit on a small robot. The
Arduino and all subsequent components will be connected through a half-size
breadboard.
2.1.3 L149.6.21 Micro Motors
The L149.6.21 Micro Motors are geared DC motors. This makes them optimal
for finer control, and for testing the robot at lower speeds.
2.1.4 DRV8833 motor-controller
Any motor-controller with PWM-control that can control two motors is
sufficient. In this case, the DRV8833 will be used. It has the advantage of
Theoretical background | 7
being smaller than other common choices, like the L298, making it easier to
fit it on the bot.
2.1.5 Power-source
The line-follower will be run at 10V through cables connected to an external
power-supply.
2.1.6 Chassi and wheels
All components will be attached to a 3D-printed chassi. The wheels will also
be 3D-printed, with O-rings to improve frictions. Please see Chapter 3 for a
more detailed overview of the construction.
2.2
PID-controller
The general form of a PID-controller is
∫ t
de(t)
u(t) = Kp · e(t) + Ki ·
e(τ ) dτ + Kd ·
dt
0
(2.1)
where Kp , Ki , and Kd are the constants that set the behaviour of the controller,
u is the control signal, and e is the error [7].
Generally the above mentioned constants are solved for analytically. Using a
heuristic approach however, the constants are automatically obtained through
a set of rules.
In the case of a line-following robot, the error can be directly measured as the
deviation from the line. The expression will thus look slightly different. Using
the QTR-8A to measure the error, the expression becomes
u(t) = Kp · e + Ki · ecumulative + Kd · ∆e


e = 3500 − S
ecumulative = e + ecumulative


∆e = e − eprevious
(2.2)
Where e is the current measured error, ecumulative is the total measured error
up to now, eprevious is the previous measured error, S is the current score from
the QTR-8A, and 3500 is the reference value aimed for.
8 | Theoretical background
Setting a large Kp leads to a fast response, but a lot of oscillation. To reduce
oscillations, a dampening term should be included. An appropriately selected
Kd makes sure that the loop also reacts to how fast the error changes, reducing
oscillations. To eliminate static error, it is necessary to include the Ki term,
which will make the cumulative error build up (getting more negative or more
positive) for as long as the line is not directly under the sensor. As with a large
Kp , a large Ki has the tendency to cause oscillations. This can however be
compensated for with more derivative action [8].
2.3
Tuning methods
All of the tuning methods presented below uses the same main technique. The
results obtained from this technique is then used with the rules presented by
the three heuristic methods to tune the controller.
The tuning starts by having a regular proportional controller, and increasing its
scaling parameter Kp until the system reaches an oscillatory state with constant
amplitude. The value of the scaling parameter is noted as K0 , also called the
ultimate gain and the period of the oscillation is noted as T0 , also known as
the ultimate period. These new parameters are then scaled by various factors
to obtain a working controller [7].
2.3.1 Ziegler-Nichols
Ziegler-Nichols is noted as being rather aggressive, with a high gain and
overshoot [9]. This is meant to give the controller a better disturbance
rejection, which is a desirable in an application such as a competition-style
line-follower. It might however not be as applicable in scenarios where
oscillations are unwanted. For an industrial line-follower, accuracy and
smooth motion might be preferable over quick reaction.
The standard Ziegler-Nichols offers three different sets of constants for a P, PI,
and PID-controller. These constants can be seen in Table 2.1.
Theoretical background | 9
Controller
Kp
Ki
Kd
P
0.5K0
PI
0.45K0
T0
1.2
PID
0.6K0
T0
2
T0
8
Table 2.1: Ziegler-Nichols tuning parameters [4]
2.3.2 Modified Ziegler-Nichols
For applications that prioritizes smoother motion, Ziegler and Nichols
themselves proposed the modified Ziegler-Nichols method. This method
offers only a PID variant, and is meant to reduce the high overshoot caused
by the standard method. This can be seen in the significantly higher derivative
constant compared to the standard method. The PID-controller comes in
two variants - ”small overshoot” and ”no overshoot”. The constants for the
controllers can be seen in Table 2.2.
Controller
Kp
Ki
Kd
Small overshoot
K0
3
T0
2
T0
3
No overshoot
0.2K0
T0
2
T0
3
Table 2.2: Modified Ziegler-Nichols tuning parameters, only for PID [4]
2.3.3 Tyreus-Luyben
The Tyreus-Luyben method offers a PI and PID variant. The constants for the
controllers can be seen in Table 2.3. It claims to perform much better than
Ziegler-Nichols in a wide variety of settings [10]. Because of the fact that it
is much newer, it is reasonable to assume that it builds upon a more robust
understanding of control theory, and could offer better performance. It is
interesting to note the much larger integral constant compared to the previous
two methods. This has the potential to lead to oscillations, and considering
that the derivative part (thus the ability to correct for oscillations) is rather
small, it is interesting to test the claims of increased stability.
10 | Theoretical background
Controller
Kp
Ki
PI
K0
3.2
2.2T0
PID
K0
2.2
2.2T0
Kd
T0
6.3
Table 2.3: Tyreus-Luyben tuning parameters [11]
2.4
Integral Square Error (ISE)
The ISE is used to measure the total error the line follower accumulates during
its run around the track. This measurement is done by
∫ ∞
x2 dx
(2.3)
0
where ∞ indicates the end of the track and x the error measured by the sensors
[12]. With this method a numerical comparison of the tuning methods can be
made. It is more optimal to achieve a low ISE score, as it indicates a low
amount of deviation from the line.
Methods | 11
Chapter 3
Methods
This chapter will first describe how the robot was constructed. Then it will
discuss how the data was collected and analyzed.
3.1
Construction
A chassi suitable to attach all components to was designed and 3D-printed.
A small breadboard used to connect all cables was attached with glue to the
chassi. The two DC-motors were screwed in place, and the wheels were
attached to the shafts. To prevent the wheels from slipping off, a small amount
of hot glue was added to the end of the shafts. The wheels had a small groove
along their diameter, which allowed for an O-ring to be slipped on. This was
meant to improve grip. For drawings detailing the dimensions of the robot,
please see Appendix A.
The Arduino, QTR-8A, and DRV8833 were all connected to the breadboard.
For the specific connections, please see Appendix B. Both the QTR-8A and
the DRV8833 had to have cables soldered to them according to the provided
instructions. The QTR-8A was then attached to the front of the chassi using
a small amount of hot glue. Finally, a small caster wheel was attached the
bottom of the chassi using a small amount of hot glue. The full design can be
seen in Figure 3.1.
12 | Methods
Figure 3.1: The assembled line-follower
A program was then written for the Arduino that recorded the sensor values
and controlled the motors. Please see Appendix D for the full code.
3.2
Testing & Data collection
The tests involved using black electrical tape on a white wooden surface to
create a high contrasting environment for the QTR-8A. First, a straight line
was created using the tape, see Figure 3.2. After calibration, the line follower
was placed at an offset so that the outermost sensor was directly above the black
tape. This was meant to emulate a step-response. The robot was run at around
40% of its max speed (an analogWrite-value of 100) which corresponds to
around 0.36 m/s. The tests were first performed with only the proportional
controller active. The step response was then used to note when an oscillation
with constant amplitude had occurred, and for what gain. The smallest gain
that lead to the aforementioned oscillation was noted as K0 and the period of
oscillations as T0 . The parameters K0 and T0 were then scaled according to
each tuning methods table, mentioned in Chapter 2. The step-responses of all
controllers were then tested 10 times each. The robot was programmed to run
Methods | 13
for 4 seconds along the track, because of length limitations. The data from the
sensor was sent through a cable to a computer via the serial monitor. After
each run, this data was copied to a text file.
Figure 3.2: Straight track
A new, curvy, track was created using the tape, see Figure 3.3. The track was
then used to record the performance of the controllers. The performance was
based on how well the line follower could adhere to the tape. To measure this,
the ISE was recorded for 10 seconds. That is to say, the difference between
the current value and 3500 was squared and summed for 10 seconds.
Figure 3.3: Curvy track
To process the data and plot it, a Python program was written. The program
was able to import data saved to the previously mentioned text files. First
it took the data from the constant oscillations-experiment, and calculated the
ultimate period. This was done through on the number of intersections of the
assumed average score of 3500. The program then imported the data from
the subsequent experiments, and calculated rise-time, overshoot, and settling
14 | Methods
time. To reduce noise, the program also applied a low-pass filter to the data.
It then took the average and standard deviation of all these results, and printed
them. All values were rounded to the second decimal. For the full program,
please see Appendix E.
Results and Analysis | 15
Chapter 4
Results and Analysis
Here, the most relevant results will be presented. First, the ultimate gain and
ultimate period will be presented. Then, graphs showing examples of the stepresponses of each controller will be presented. Then, graphs detailing the
average rise time, overshoot, and settling time will be presented. Graphs for
the ISE-tests will follow. The results are concluded with tables summarizing
the results from the graphs. For the full results, please see Appendix C. Also
please note that when referencing ”Ziegler-Nichols” or ”Tyreus-Luyben”, if
nothing else is specified it is the full PID-controller that is being discussed.
4.1
Ultimate gain and ultimate period
In Table 4.1 the ultimate gain and ultimate period can be seen. The ultimate
gain is unit-less, and the ultimate period is measured in seconds.
K0 [−]
T0 [s]
0.2
0.4139
Table 4.1: Ultimate gain, K0 and ultimate period, T0
4.2
Step-responses
In Figure 4.1, Figure 4.2, and Figure 4.3, example step-responses from all
methods are presented. In Figure 4.4, Figure 4.5, Figure 4.6, and Figure 4.7 the
average overshoot, rise time, settling time, and settling value of each method
is presented.
16 | Results and Analysis
Figure 4.1: Example step-responses from all variants of the Ziegler-Nichols
method
Figure 4.2: Example step-responses from both variants of the modified
Ziegler-Nichols method
Results and Analysis | 17
Figure 4.3: Example step-response for the Tyreus-Luyben full PID controller
Figure 4.4: Average overshoot and standard deviation for the different
controllers
18 | Results and Analysis
Figure 4.5: Average rise time and standard deviation for the different
controllers
Figure 4.6: Average settling time and standard deviation for the different
controllers
Results and Analysis | 19
Figure 4.7: Average settling value and standard deviation for the different
controllers
4.3
ISE
In Figure 4.8 the average ISE of all methods, excluding the Tyreus-Luyben, is
presented.
Figure 4.8: Average ISE and standard deviation for the different controllers
20 | Results and Analysis
4.4
Tables
In Table 4.2 and Table 4.3 the numerical values of the average overshoot, rise
time, settling time, and settling value for the methods is presented.
Rise Time [s]
Overshoot [%]
Settling Time [s]
Settling value [-]
ISE [-]
ZN
0.2933 ± 0.013
29.07 ± 5.38
1.95 ± 0.29
3530.59 ± 24.43
1287587.79 ± 363142.16
ZN modified no overshoot
0.3578 ± 0.0119
39.03 ± 5.49
1.75 ± 0.95
3532.6 ± 28.97
1358307.9 ± 103701.52
ZN modified some overshoot
0.2849 ± 0.0217
27.6 ± 3.35
1.66 ± 0.64
3535.8 ± 40.17
790714.72 ± 49784.28
Table 4.2: Average values and standard deviation for the full PID ZieglerNichols and both versions of the modified Ziegler-Nichols
Rise Time [s]
Overshoot [%]
Settling Time [s]
Settling value [-]
ISE [-]
ZN P
0.2657 ± 0.0095
13.84 ± 2.26
1.82 ± 0.52
3669.13 ± 52.2
1306393.1 ± 397883.34
ZN PI
0.283 ± 0.0196
44.51 ± 5.26
2.92 ± 0.7
3518.5 ± 20.65
2902842.02 ± 1707300.66
Tyreus-Luyben
0.3151 ± 0.0165
62.15 ± 4.34
3.46 ± 0.52
3491.58 ± 43.21
-
Table 4.3: Average values and standard deviation the P and PI versions of
Ziegler-Nichols, and the full PID version of Tyreus-Luyben
4.5
A comment on Tyreus-Luyben
Because of difficulties testing the Tyreus-Luyben variants, the ISE of the full
PID variant has been left out, and the PI-version completely neglected from
all tests. Please see Chapter 5 for a more detailed explanation.
Discussion | 21
Chapter 5
Discussion
The following chapter will discuss the results of each tuning method, and then
bring up the various sources of error that could have impacted these results.
5.1
Ziegler-Nichols PID
The standard Ziegler-Nichols PID performed reasonably consistently throughout all the tests. It had an overshoot of around 30%, the third lowest out of all
the controllers. The settling value was always close to 3500, but never below
it. Comparing this to the other controllers with an integral part, it performed
similarly. Its ISE was on the lower end, but it performed inconsistently, with
a high standard deviation. This is thought to be due to the higher oscillatory
behaviour of the standard Ziegler-Nichols compared to the other controllers.
The oscillatory behaviour can clearly be seen in the example step-response
in Figure 4.1. What is really interesting is how the full PID variant performs
worse than the P variant in many categories. Especially interesting is how
its ISE is almost completely identical, both in terms of average and standard
deviation. That said the settling value is much closer to 3500 for the full
PID variant than the P variant. This suggests that the testing-track either
didn’t allow the robot to settle properly, or that an accurate settling value don’t
contribute much to a low ISE.
5.2
Ziegler-Nichols modified no overshoot
Interestingly, the method called ”no overshoot” had the third highest overshoot
out of all the controllers. It also had the highest rise time. Although it did
22 | Discussion
have a low average settling time, it had a very large standard deviation. The
settling value was as expected around 3500, but like most other controllers
consistently a little higher. The ISE was fairly consistent with the other
controllers (excluding Ziegler-Nichols PI). This suggests that oscillations do
not contribute much to an increased ISE, as this method is very smooth. The
standard deviation is also very low, suggesting that this is a very consistent
method.
5.3
Ziegler-Nichols modified some overshoot
The ”some overshoot” method had the second lowest overshoot, which was
unexpected. It was also the second most consistent of all the controllers
tested. The rise time was the fourth highest, with a high standard deviation.
The average settling time of this method was the lowest of all the controllers
however, but not by much. Where this method truly shines is when looking at
its ISE. It had a score of around 790000, which is the lowest score of all the
controllers by a wide margin. It also boasts the lowest standard deviation at
around 50000, which is less than half of the second lowest value. This result
was the most surprising, because it was expected that either the ”no overshoot”
version or the newer Tyreus-Luyben would perform the best.
The very low ISE is interesting considering that this method performs quite
similarly to the others in all the other categories. From looking at the example
step-responses, it is obvious that it has very low oscillations, and that it rises
and settles down quickly. It might just be that the combination of accurate
settling value, fast rise time, and low oscillations combine to make the most
consistent and accurate controller.
5.4
Ziegler-Nichols P-controller
This controllers overshoot was the lowest of all controllers tested, which was
rather surprising at first. However, taking the settling value into account
could explain why the results are like this. Because its settling value was
considerably higher than the rest, around 3660, it skewed the calculations of
the overshoot. There was simply less ”distance” between its settling value and
max value. This can probably not account for all of the low overshoot however,
as the settling value is only about 3 % higher than 3500, while the overshoot
is less than half of some of the other controllers. Its rise time was also the
Discussion | 23
lowest and the most consistent of all controllers. This might be because its
proportional value was the highest of all controllers. The settling time was
inline with some of the other controllers tested, at around 1.8 seconds. The
ISE value was rather inconsistent, which might be explained by the higher
oscillatory behaviour.
5.5
Ziegler-Nichols PI-controller
It is not that surprising that the PI-controller had a rather high overshoot,
because of its lack of a derivative factor and relatively high integral factor.
At around 45% it was the second highest of all the controllers. The average
rise time was very quick though, the second lowest compared to the other
controllers. It did however have the second highest standard deviation in this
category. The average settling time was considerably higher than all other
controllers, aside from Tyreus-Luyben. This was to be expected because of
the higher integral part, causing oscillations, and the lack of a derivative part
to handle these. The standard deviation was again the second highest in the
aforementioned category. Its settling value was the second closest to 3500,
only Tyreus-Luyben being closer. It also had the lowest standard deviation of
the controllers tested. That said, looking at the step-responses in Appendix C
indicates that the method didn’t actually quite settle in many cases. Even so,
performing consistently is important.
The average ISE score of this method was a lot higher than all other controllers,
with a very large standard deviation. This was probably caused by the high
oscillatory behaviour. When the controller is unstable, any variation in the
starting value can lead to a large difference in the outcome.
5.6
Tyreus-Luyben
Very surprisingly, Tyreus-Luyben offered almost completely unacceptable
performance. It had the highest overshoot by far, and the highest settling
time. Considering that the program only recorded step-responses for about 4
seconds, it might even be said that the method almost didn’t settle in the given
time frame. This indeed seems to be the case in many of the step-responses,
as the oscillations don’t properly die out in several of them. This once again
shows that for an oscillatory system, small variations in input leads to large
variations in output, sometimes being the difference between stable behaviour
and not.
24 | Discussion
There is no measured ISE for Tyreus-Luyben, because the robot couldn’t drive
the test-track without derailing. Just the same, the PI-version couldn’t even
record step-responses without losing the track. Because of this, it was assumed
to be unstable, and recording any data was not attempted.
It is not known why Tyreus-Luyben worked so poorly. It might simply be
that the robot used in this work was inappropriate for this controller, and
would have worked better with a robot of other dimensions. The culprit
could also potentially be the very high, compared to other controllers, integral
part. As mentioned in Chapter 2 this can lead to greater oscillations, which
it seems to have done. Interestingly, when using the controller Sarif, Kumar
and Rao claims is Tyreus-Luyben, the step-response dramatically improved
[4]. Considering that the integral part, with the values obtained in this work,
is significantly reduced with these values this might not be that surprising.
Because no other source presents the controller in this way though, it is
assumed to be incorrect, and will not be discussed further.
5.7
Comparison of methods
A competition-style line-follower values fast performance. Limited oscillations are not a huge concern, and neither is keeping the line right in the middle
of the sensor. The fastest method, in terms of rise time, is the Ziegler-Nichols
P-controller. This method also has the lowest overshoot, which could mean
that it could take more aggressive curves. That said, an oscillatory controller
seems to have a higher risk of becoming unstable, and if the oscillations grow
too large, the robot will lose track of the line. Also, when travelling straight,
an oscillating robot have to travel further than a robot smoothly following the
track. As both motors have to accelerate and decelerate in turn to keep to the
track, oscillations are detrimental to high speed. It is unclear how much this
would impact a competition-style line-follower however, as there are multiple
styles of track, some with many curves, and some without.
An industrial line-follower prioritizes accuracy and reliability above speed.
Thus, the most interesting measurement is the ISE, settling time, and
overshoot. The method with the lowest ISE by far was the modified ZieglerNichols ”some overshoot” variant. This method also had the lowest standard
deviation of all the controllers in this category. This indicates that it is both
accurate and reliable. The method also, barely, had the lowest settling time,
but with a rather large standard deviation. It also had the second lowest
Discussion | 25
overshoot out of all controllers. This indicates that this controller could be
suitable where high accuracy and smooth operation is necessary.
5.8
Sources of error
There are various things that impact the results in this work. To begin with, it
was difficult to get consistent step-responses. After the robot was calibrated
it was placed so that the sensor edge was aligned with the tape edge. Ideally,
this should make the starting score close to zero, as only the outermost sensors
should detect the line. Multiple times however, the score in the tests were
above 2000. Thus, a score of ”less than 1000” was decided to be a good enough
starting score to keep the result. As can be seen in the step-responses though,
the starting score varies from around 500 up to just below 1000. This can of
course skew the results, making it look like one method behaves much better
than another for the ”same” step-response. It is unclear how much this impacts
the results, but as all the tests are affected somewhat equally, it should still be
possible to draw a conclusion as to the difference between the controllers.
To mitigate this issue, a more accurate way of placing the robot could be
developed.
The same issue as with the step-responses exists for the measurement of the
ISE. Especially for the more oscillatory controllers, the placing of the robot
impacted how they behaved across the track at large. If the robot was placed
slightly too far to one side, oscillations immediately began and were hard for
the robot to dampen. This can be seen in the high standard deviation for all of
the more oscillatory controllers. Even though this might make a good point,
that less oscillatory controllers handle improper placements better, it would
have been better to have more accurate experiments. This issue could, just as
with the step-responses, be mitigated by designing a better way of placing the
robot.
Another source of error is the fact that all data was collected through the serial
port. This meant that a cable had to be attached to the robot at all times,
which weighed it down. To mitigate this, the cable was always held up, but it
is unknown how much this impacted the readings. The cable could also get
tangled on the environment, which would prompt a redo of the test. An easy
way to mitigate this would be to use a wireless card connected to the Arduino
to transmit the data instead.
26 | Discussion
The place at which the tests were being held had slightly inconsistent lighting
conditions across the track, some parts were lit up more than others. It is
unclear how much this affects the IR sensor on the robot, but for future work
a more evenly lit room or a separate strong light source above the track would
help mitigate the errors.
The settling value was consistently slightly above 3500. This indicates that
there could possibly be inconsistencies in the DC motors, one being slightly
stronger than the other. How much this skews the rest of the results is unclear,
but performing other tests that ensures the selected DC motors to be equally
powerful would help mitigate the errors caused.
Another thing to consider is that the line-follower only acts upon the
information given, based on the QTR-8A sensor. If it were to encounter bumps
or other forms of error, it will not be able to compensate for these directly. It is
unclear how much this has impacted the result, as the tests were performed on
a flat and even surface, but some sort of general disturbance rejection should
probably be implemented for real-world use.
Finally, the ultimate gain and ultimate period were only measured once. To
get a more accurate result, these constants could have been measured multiple
times, and averaged. It is unclear how much the lack of multiple tests impacted
the results.
Conclusions and Future work | 27
Chapter 6
Conclusions and Future work
In this chapter, the conclusion of the earlier discussion is presented. Potential
future work is also briefly discussed.
6.1
Conclusions
In a competition-style line-follower, where speed of operation takes precedence, the controller offering the best performance is probably the ZieglerNichols P-controller. With a low rise time and overshoot, and a decent settling
time and ISE, this method is well suited for aggressive curves.
For a line-follower prioritizing smooth and accurate operation however,
the modified Ziegler-Nichols ”some overshoot” variant offers the best
performance. In these experiments it had the lowest and most consistent
ISE, and the lowest, but not as consistent, settling time. It also had a very
respectable overshoot and rise time. This controller could thus be well suited
for an industrial application, where smooth operation is required. As it was
almost always very close to the Ziegler-Nichols P-controller, but provides
significantly smoother and more accurate operation, it might also be well
suited for a competition-style line-follower.
6.2
Future work
A logical continuation of this work would be to scale the line-follower up, in
order to draw conclusions about the appropriate controller for an industrial
robot. This would explore whether the controllers behave differently at lower
relative-speeds. It would also be interesting to see whether this would play a
28 | Conclusions and Future work
role in the implementation of Tyreus-Luyben and if it would have a noticeable
effect.
Another interesting thing to investigate would be how running the robot at
various default speeds impact controller performance. When running the
motors at a high default speed, the speed ”left” for the controller to compensate
for errors will be smaller. When running the robot at a low default speed, the
controller will be able to increase the speed substantially to counteract errors.
How, and to what extent, varying the default speed of the robot could thus be
interesting.
References | 29
References
[1] G. Ullrich et al., “Automated guided vehicle systems,” Springer-Verlag
Berlin Heidelberg, vol. 10, pp. 978–3, 2015. [Page 1.]
[2] S. Skogestad, “Simple analytic rules for model reduction and pid
controller tuning,” Journal of process control, vol. 13, no. 4, pp. 291–
309, 2003. [Page 2.]
[3] M. Shahrokhi and A. Zomorrodi, “Department of chemical & petroleum
engineering,” Shariff University Of Technology,” Comparison of PID
Controller Tuning Methods, 2013. [Page 2.]
[4] B. M. Sarif, D. A. Kumar, and M. V. G. Rao, “Comparison study of
pid controller tuning using classical analytical methods,” International
Journal of Applied Engineering Research, vol. 13, no. 8, pp. 5618–5625,
2018. [Pages xii, 2, 9, and 24.]
[5] Sumozade, “Linecraft fast line follower robot kit 4000rpm - assembled,”
2020. [Online]. Available: https://www.sumozade.com/product/linecra
ft-fast-line-follower-robot-kit-4000rpm-assembled [Pages x and 5.]
[6] L. Wang et al., “Advanced line-follower robot,” Ph.D. dissertation,
Cleveland State University, 2017. [Page 6.]
[7] T. Glad and L. Ljung, Reglerteknik: Grundläggande teori.
teratur AB, 2006. [Pages 7 and 8.]
Studentlit-
[8] ControlAutomation, “Heuristic PID Tuning Procedures - Chapter 33 Process Dynamics and PID Controller Tuning — Control, automation,”
2022. [Online]. Available: https://control.com/textbook/process-d
ynamics-and-pid-controller-tuning/heuristic-pid-tuning-procedures/
[Page 8.]
30 | References
[9] Wikipedia, “Ziegler–Nichols method — Wikipedia, the free
encyclopedia,” 2022. [Online]. Available: http://en.wikipedia.org
/w/index.php?title=Ziegler%E2%80%93Nichols%20method&oldid=1
059796218 [Page 8.]
[10] B. D. Tyreus and W. L. Luyben, “Tuning pi controllers for integrator/dead
time processes,” Industrial & Engineering Chemistry Research, vol. 31,
no. 11, pp. 2625–2628, 1992. [Page 9.]
[11] O. Ibrahim, Z. Yahaya, and N. Saad, “Pid controller response to set-point
change in dc-dc converter control,” vol. 7, pp. 294–302, 06 2016. doi:
10.11591/ijpeds.v7.i2.pp294-302 [Pages xii and 10.]
[12] TheFreeDictionary, “Integral square error,” 2003. [Online]. Available:
https://encyclopedia2.thefreedictionary.com/integral+square+error
[Page 10.]
Appendix A: Drawings | 31
Appendix A
Drawings
Please observe that all dimensions are in millimeters. The first drawing details
the main body of the robot, and the second drawing details the wheels.
32 | Appendix A: Drawings
5
3
77
53
110
90
35
17.5
7.5
3
11.5
Ø3
Ø9
Appendix A: Drawings | 33
6
R2
Ø48
45
3.5
1.75
34 | Appendix B: Circuit connections
Appendix B
Circuit connections
This is a diagram showing the circuit connections of the robot. All ports on
the same horizontal line are connected.
Arduino
A0
A1
A2
A3
A4
A5
A6
A7
D11
D10
D6
D5
VIN
GND
5V
QTR-8A
1
2
3
4
5
6
7
8
GND
VIN
DRV8833
IN1
IN2
IN3
IN4
Motor A, right port
Motor A, left port
Motor B, right port
Motor B, left port
+
-
Power source
Right motor
Left motor
Green cable
Red cable
Green cable
Red cable
10 V
GND
Appendix C: Step-responses | 35
Appendix C
Step-responses
36 | Appendix C: Step-responses
Appendix C: Step-responses | 37
38 | Appendix C: Step-responses
Appendix C: Step-responses | 39
40 | Appendix C: Step-responses
Appendix C: Step-responses | 41
42 | Appendix C: Step-responses
Appendix C: Step-responses | 43
44 | Appendix C: Step-responses
Appendix C: Step-responses | 45
46 | Appendix D: Arduino Code
Appendix D
Arduino Code
/ / SENSOR VARIABLES ===================
# i n c l u d e <QTRSensors . h>
QTRSensors q t r ;
const u i n t 8 _ t SensorCount = 8;
u i n t 1 6 _ t sensorValues [ SensorCount ] ;
int
int
int
int
IN1
IN2
IN3
IN4
=
=
=
=
11;
10;
6;
5;
/ / ====================================
/ / PID VARIABLES ======================
f l o a t l a s t e r r o r = 0;
f l o a t i n t e g r a l e r r o r = 0;
f l o a t d e r i v a t i v e e r r o r = 0;
f l o a t K0 = 0 . 2 ;
f l o a t period = 0.41389473684210526;
Appendix D: Arduino Code | 47
/∗
f l o a t P = K0 ∗ 0 . 6 ; / / S t a n d a r d Z i e g l e r N i c h o l s
f l o a t TI = p e r i o d / 2 ;
f l o a t TD = p e r i o d / 8 ;
f l o a t P = K0 ∗ 0 . 2 ; / / M o d i f i e d Z i e g l e r N i c h o l s − NO o v e r s h o o t
f l o a t TI = p e r i o d / 2 ;
f l o a t TD = p e r i o d / 3 ;
∗/
f l o a t P = K0 / 3 ; / / M o d i f i e d Z i e g l e r N i c h o l s − SMALL o v e r s h o o t
f l o a t TI = p e r i o d / 2 ;
f l o a t TD = p e r i o d / 3 ;
/∗
f l o a t P = K0 ∗ 0 . 5 ; / / ZN o n l y P
f l o a t TI = 0 ;
f l o a t TD = 0 ;
f l o a t P = K0 ∗ 0 . 4 5 ; / / ZN P I
f l o a t TI = p e r i o d / 1 . 2 ;
f l o a t TD = 0 ;
f l o a t P = K0 / 2 . 2 ; / / T y r e u s −Luyben PID
f l o a t TI = 2 . 2 ∗ p e r i o d ;
f l o a t TD = p e r i o d / 6 . 3 ;
f l o a t P = K0 / 3 . 2 ; / / T y r e u s − l u y b e n P I
f l o a t TI = 2 . 2 ∗ p e r i o d ;
f l o a t TD = 0 ;
48 | Appendix D: Arduino Code
∗/
/ / ====================================
/ / MISC ===============================
float
float
float
float
time = 0;
c a l i b r a t i o n t i m e = 5 4 6 0 ; / / Time t o c a l i b r a t e t h e s e n s o r
t;
ISE = 0 ;
/ / ====================================
void setup ( )
{
/ / MOTOR SETUP ======================
pinMode ( IN1 , OUTPUT ) ;
pinMode ( IN2 , OUTPUT ) ;
pinMode ( IN3 , OUTPUT ) ;
pinMode ( IN4 , OUTPUT ) ;
/ / ==================================
/ / SENSOR CONFIG ====================
q t r . setTypeAnalog ( ) ;
q t r . s e t S e n s o r P i n s ( ( c o n s t u i n t 8 _ t [ ] ) { A0 , A1 , A2 , A3 , / /
/ / A4 , A5 , A6 , A7 } , S e n s o r C o u n t ) ;
qtr . setEmitterPin (2);
delay (500);
pinMode ( LED_BUILTIN , OUTPUT ) ;
d i g i t a l W r i t e ( LED_BUILTIN , HIGH ) ;
f o r ( u i n t 1 6 _ t i = 0 ; i < 7 5 ; i ++)
{
Appendix D: Arduino Code | 49
qtr . calibrate ();
}
d i g i t a l W r i t e ( LED_BUILTIN , LOW) ;
S e r i a l . begin (9600);
delay (1000);
/ / ======================================
}
void loop ( )
{
t = millis ();
uint16_t position = qtr . readLineBlack ( sensorValues ) ;
i n t defaultSpeed = 150;
i n t e r r o r = 3500 − p o s i t i o n ;
i n t m o t o r s p e e d = P∗ e r r o r + TD ∗ d e r i v a t i v e e r r o r + TI ∗ i n t e g r a l e
i n t speedA = d e f a u l t S p e e d + m o t o r s p e e d ;
i n t speedB = d e f a u l t S p e e d − m o t o r s p e e d ;
t = ( m i l l i s ( ) − t ) / 1000;
derivativeerror = error − lasterror ;
integralerror = integralerror + error ∗ t ;
lasterror = error ;
ISE = ISE + pow ( e r r o r , 2 ) ∗ t ;
/ / S e r i a l . p r i n t l n ( ISE ) ;
i f ( speedA > 2 5 5 ) {
speedA = 2 5 5 ;
}
50 | Appendix D: Arduino Code
i f ( speedB > 2 5 5 ) {
speedB = 2 5 5 ;
}
i f ( speedB < 0 ) {
speedB = 0 ;
}
i f ( speedA < 0 ) {
speedA = 0 ;
}
i f ( p o s i t i o n == 7000 o r p o s i t i o n == 0 ) {
d i g i t a l W r i t e ( IN1 , LOW) ;
a n a l o g W r i t e ( IN2 , LOW) ;
d i g i t a l W r i t e ( IN4 , LOW) ;
a n a l o g W r i t e ( IN3 , LOW) ;
exit (0);
}
d i g i t a l W r i t e ( IN1 , LOW) ;
a n a l o g W r i t e ( IN2 , speedA ) ;
d i g i t a l W r i t e ( IN4 , LOW) ;
a n a l o g W r i t e ( IN3 , speedB ) ;
time = m i l l i s ( ) ;
/∗
i f ( time − c a l i b r a t i o n t i m e > 4000){
d i g i t a l W r i t e ( IN1 , LOW) ;
a n a l o g W r i t e ( IN2 , LOW) ;
d i g i t a l W r i t e ( IN4 , LOW) ;
a n a l o g W r i t e ( IN3 , LOW) ;
exit (0);
Appendix D: Arduino Code | 51
}
∗/
Serial . print ( position );
Serial . print (”\ t ”);
S e r i a l . p r i n t l n ( time − c a l i b r a t i o n t i m e ) ;
/ ∗ S e r i a l . p r i n t ( speedA ) ;
Serial . print (”\ t ”);
S e r i a l . p r i n t l n ( speedB ) ; ∗ /
/∗ S e r i a l . p r i n t (” Error ” ) ;
Serial . print (” ,”);
Serial . print ( error );
Serial . print (”\ t ”);
Serial . p r i n t (” Derivativeerror ” ) ;
Serial . print (” ,”);
Serial . print ( derivativeerror );
Serial . print (”\ t ”);
Serial . p r i n t (” I n t e g r a l e r r o r ” ) ;
Serial . print (” ,”);
Serial . print ( integralerror );
Serial . print (”\ t ”);
S e r i a l . p r i n t ( ” Time ” ) ;
Serial . print (” ,”);
Serial . println ( t );
∗/
}
52 | Appendix E: Python Code
Appendix E
Python Code
Please note that ”//” is used to indicate a linebreak not present in the actual
code, but necessary for LaTeX formatting.
from m a t p l o t l i b i m p o r t p y p l o t a s p l t
i m p o r t numpy a s np
def importdata ( s u f f i x ) :
position = []
time = [ ]
filename = ”\ data ” + s t r ( suffix ) + ”. txt ”
p a t h = r ”C : \ U s e r s \ Neo \ Documents \ GitHub \KEX\ ZN” + f i l e n a m e
w i t h open ( p a t h , ” r ” ) a s f i l e :
f o r row i n f i l e :
row = row . s p l i t ( )
p o s i t i o n . a p p e n d ( row [ 0 ] )
t i m e . a p p e n d ( row [ 1 ] )
r e t u r n position , time
def t r e a t d a t a ( allpos , a l l t i m e ) :
a = 0.1
for i in range ( len ( a l l t i m e ) ) :
a l l t i m e [ i ] = f l o a t ( a l l t i m e [ i ] ) / 1000
allpos [ i ] = float ( allpos [ i ])
Appendix E: Python Code | 53
i f i +1 == l e n ( a l l t i m e ) :
break
a l l p o s [ i +1] = a ∗ f l o a t ( a l l p o s [ i + 1 ] ) / /
/ / + (1 − a ) ∗ a l l p o s [ i ]
return alltime , allpos
def p l o t d a t a ( alltime , allpos , i ) :
plt . plot ( alltime , allpos )
p l t . x l a b e l ( ” Time [ s e c ] ” )
p l t . ylabel (” P o s i t i o n a l value ”)
plt . grid ()
p l t . axis ([0 , 4 , 0 , 6000])
p l t . t i t l e (” Ziegler −Nichols t e s t ” + s t r ( i +1))
p l t . s a v e f i g ( ”ZN ” + s t r ( i + 1 ) )
def f i t s i n e ( time , pos ) :
s t a r t t i m e = time [0]
endtime = time [ −1]
avg = 3500
crosses = 0
# T r u e = Over 3500
# Low = Under 3500
i f pos [ 0 ] > 3500:
s t a t e = True
else :
s t a t e = False
for i in range ( len ( time ) ) :
i f p o s [ i ] > 3500 and s t a t e i s F a l s e :
s t a t e = True
c r o s s e s += 1
e l i f p o s [ i ] < 3500 and s t a t e i s T r u e :
s t a t e = False
c r o s s e s += 1
54 | Appendix E: Python Code
r e t u r n 2 / ( c r o s s e s / ( endtime − s t a r t t i m e ) )
def Average ( l s t ) :
r e t u r n sum ( l s t ) / l e n ( l s t )
def r i s e t i m e ( time , pos ) :
m a x v a l u e = max ( p o s )
f o r i i n range ( l e n ( pos ) ) :
i f p o s [ i ] >= 0 . 1 ∗ m a x v a l u e :
lowtime = time [ i ]
break
f o r i i n range ( l e n ( pos ) ) :
i f p o s [ i ] >= 0 . 9 ∗ m a x v a l u e :
hightime = time [ i ]
break
r i s e t i m e = hightime − lowtime
return risetime
def o v e r s h o o t ( time , pos ) :
m a x v a l u e = max ( p o s )
l a s t f i f t y = l e n ( p o s ) − 50
avg = A v e r a g e ( p o s [ l a s t f i f t y : ] )
o v e r s h o o t = 100 ∗ ( m a x v a l u e − avg ) / avg
r e t u r n o v e r s h o o t , avg
def s e t t l i n g t i m e ( time , pos ) :
l a s t f i f t y = l e n ( p o s ) − 50
Appendix E: Python Code | 55
avg = A v e r a g e ( p o s [ l a s t f i f t y : ] )
t o p v a l u e = 1 . 0 5 ∗ avg
l o w v a l u e = 0 . 9 5 ∗ avg
inside = False
f o r i i n range ( l e n ( pos ) ) :
i f p o s [ i ] <= t o p v a l u e and p o s [ i ] >= l o w v a l u e :
i f inside is False :
s e t t l i n g = time [ i ]
i n s i d e = True
else :
inside = False
i f i n s i d e i s True :
return settling
else :
r e t u r n max ( t i m e )
d e f main ( ) :
allpos = []
alltime = []
try :
for i in range ( 1 0 ) :
pos , t i m = i m p o r t d a t a ( i + 1 )
a l l p o s . append ( pos )
a l l t i m e . append ( tim )
except :
pass
oversh = [ ]
rise = []
settling = []
avg = [ ]
56 | Appendix E: Python Code
for i in range ( len ( a l l p o s ) ) :
f = p l t . f i g u r e ( i + 1)
alltime [ i ] , allpos [ i ] = treatdata ( allpos [ i ] , alltime [ i ])
# print ( fitsine ( alltime [ i ] , allpos [ i ]))
o v e r , av = o v e r s h o o t ( a l l t i m e [ i ] , a l l p o s [ i ] )
o v e r s h . append ( over )
avg . a p p e n d ( av )
r i s e . append ( r i s e t i m e ( a l l t i m e [ i ] , a l l p o s [ i ] ) )
s e t t l i n g . append ( s e t t l i n g t i m e ( a l l t i m e [ i ] , a l l p o s [ i ] ) )
plotdata ( alltime [ i ] , allpos [ i ] , i )
p r i n t ( ” R i s e t i m e : ” , r o u n d ( np . a v e r a g e ( r i s e ) , 4 ) , / /
/ / ”± ” , r o u n d ( np . s t d ( r i s e ) , 4 ) )
p r i n t ( ” O v e r s h o o t : ” , r o u n d ( np . a v e r a g e ( o v e r s h ) , 2 ) , / /
/ / ”± ” , r o u n d ( np . s t d ( o v e r s h ) , 2 ) )
p r i n t ( ” S e t t l i n g t i m e : ” , r o u n d ( np . a v e r a g e ( s e t t l i n g ) , 2 ) , / /
/ / ”± ” , r o u n d ( np . s t d ( s e t t l i n g ) , 2 ) )
p r i n t ( ” S e t t l i n g v a l u e ” , r o u n d ( np . a v e r a g e ( avg ) , 2 ) , / /
/ / ”± ” , r o u n d ( np . s t d ( avg ) , 2 ) )
p l t . show ( )
main ( )
TRITA-ITM-EX- 2022:102
www.kth.se
Download