Final Thesis Paper Auto

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 93

1

ACKNOWLEDGEMENT
2

ABSTRACT

In this thesis, a control system design to stabilize the ball and beam system,
which is a non-linear system, is observed. The main purpose of this project is to
control the ball at a desired position on the beam by using a suitable controller, either
by PID or LQR controller. First, a mathematical model for the ball and beam system
is developed, and then, based on that model, an effective controller for the system is
designed. The system consists of a ball and beam mechanism, a dc servo motor and a
dc motor with gearbox, a position sensitive detector (PSD) sensor and an Arduino
controller. The open-loop unstable system is stabilized by feeding back the actual
position of the ball, measured from position sensor, into the controller which
compares the actual position with the desired position set by the user, and outputs a
control voltage signal to the dc servo motor based on the programmed control
algorithm. The torque generated by the motor drives the beam, and the rotation of the
beam changes the ball position until it reaches the desired set point value. The control
methods used are Proportional-Integral-Derivative (PID) control and Linear Quadratic
Regulator (LQR) control. The control design for the controller is implemented as
much as possible to minimize the error, that is, the difference between the desired
position and the actual position of the ball on the beam. In this project, a graphical
user interface (GUI) is also provided to allow the user to open the Simulink model in
order to start and stop the system as well as to set the desired ball position and plot the
experimental results through that GUI platform. The software used to develop
programs, block diagrams and graphical user interface is Matlab/Simulink with
Arduino I/O interface.
iii

TABLE OF CONTENT
Title Page

ACKNOWLEDGEMENT..........................................................................................i
ABSTRACT...............................................................................................................ii
LIST OF FIGURES..................................................................................................vi
LIST OF TABLES..................................................................................................viii

CHAPTER 1...................................................................................................................1

INTRODUCTION......................................................................................................1

1.1 Introduction to Ball and Beam Control System...............................................1


1.2 Introduction to Graphical User Interface.........................................................3
1.3 Aim and Objectives..........................................................................................4
1.4 Implementation................................................................................................5
1.5 Outlines of Thesis............................................................................................5

CHAPTER 2...................................................................................................................6

BACKGROUND RESEARCH..................................................................................6

2.1 Brief History of Ball and Beam System...........................................................6


2.2 Ball and Beam System Configuration..............................................................8
2.3 Control Methods for Ball and Beam Control System....................................10

2.3.1 Types of PID Controller..........................................................................10


2.3.2 PID Tuning Methods...............................................................................12
2.3.3 LQR Control Method..............................................................................14

2.4 Graphical User Interface (GUI).....................................................................16

2.4.1 UI Layout................................................................................................16
2.4.2 UI Programming......................................................................................16
2.4.3 Graphical User Interface for Ball and Beam Control System.................17

2.5 Summary........................................................................................................17

CHAPTER 3.................................................................................................................18

DESIGN AND CALCULATION............................................................................18

3.1 Calculation of Transfer Function...................................................................18


iv

3.1.1 Motor Transfer Function.........................................................................18


3.1.2 Ball and Beam Transfer Function...........................................................21
3.1.3 Selecting Moment of Inertia of the Ball..................................................23
3.1.4 Relation between Beam Angle and Motor Angle...................................24

3.2 Calculation of State Space Equation..............................................................25


3.3 Tuning of PID Gains by Pole Assignment Method.......................................27
3.4 Selecting Weight Matrices for LQR Gain Matrix..........................................29

3.4.1 Inserting Scale Factor N to Eliminate Steady-state Error.......................30

3.5 Angle Detection Unit for Motor Position.......................................................31


3.6 Equations of Distance Sensor to Find Ball Position......................................32
3.7 Summary........................................................................................................35

CHAPTER 4.................................................................................................................36

EXPERIMENT AND RESULTS............................................................................36

4.1 Construction of Experimental Setup..............................................................36


4.2 Sensor Calibration for Position Sensor..........................................................37

4.2.1 Output Voltage Measurement of PSD Sensor.........................................38


4.2.2 Identification of Sensor Parameters........................................................39
4.2.3 Ball Position Detection Part....................................................................40

4.3 Identification of Motor Parameters................................................................41


4.4 Experimental Results for PID Control Experiment.......................................44

4.4.1 Results for Single Set Point PID Control Experiment............................44


4.4.2 Results for Two Set Points PID Control Experiment..............................46

4.5 Experimental Results for LQR Control Experiment......................................47


4.6 Root Locus and Frequency Response of Ball and Beam Control System.....49
4.7 Graphical User Interface for Ball and Beam Control System........................51
4.8 Errors and Limitations in PID and LQR Control Experiment.......................52
4.9 Summary........................................................................................................53

CHAPTER 5.................................................................................................................54

DISCUSSION AND CONCLUSION......................................................................54

5.1 Discussion......................................................................................................54
v

5.2 Conclusion......................................................................................................55
5.3 Future Work...................................................................................................56

REFERENCES.........................................................................................................57
APPENDICES..........................................................................................................60
APPENDIX A..........................................................................................................61
APPENDIX B..........................................................................................................65
APPENCIX C...........................................................................................................67
APPENDIX D..........................................................................................................72
vi

LIST OF FIGURES

Figure Page

Figure 1.1. PID Control System Block Diagram...........................................................2


Figure 1.2. LQR Control System Block Diagram..........................................................3
Figure 1.3. Graphic User Interface for Ball and Beam Control System........................4
Figure 2.1. Ball and Beam Systems: (a) Hirsch’s System (1999), (b) Lieberman’s
System (2004), (c) Arroyo’s System (2005), (d) Quanser’s System (2006)..................7
Figure 2.2. Acrome myControl Ball and Beam System.................................................8
Figure 2.3. Two Basic Configurations for Ball and Beam System Model: (a) Beam
Supported at Center (b) Beam Supported at Both Ends.................................................9
Figure 2.4. Ball and Beam System Configuration.........................................................9
Figure 2.5. PID Controller...........................................................................................10
Figure 2.6. PI-D Controller..........................................................................................11
Figure 2.7. I-PD Controller..........................................................................................11
Figure 2.8. I-PD Controller with Feed-forward Term..................................................12
Figure 2.9. LQR Controller..........................................................................................15
Figure 3.1. Circuit Diagram for Armature Controlled DC Motor................................18
Figure 3.2. Free Body Diagram for Forces Acting on the Ball....................................21
Figure 3.3. Ball and Beam Diagram.............................................................................24
Figure 3.4. Closed-Loop Control System with I-PD+FF Controller...........................27
Figure 3.5. LQR Controller with Scale Factor N.........................................................31
Figure 4.1. Ball and Beam Experimental Setup...........................................................37
Figure 4.2. Sensor Calibration Process........................................................................37
Figure 4.3. PSD Sensor................................................................................................38
Figure 4.4. Graph of Output Voltage versus Distance.................................................39
Figure 4.5. Fitting Curve for Recorded Data Values...................................................40
Figure 4.6. Block Diagram for Position Detection Part...............................................40
Figure 4.7. Block Diagram for Motor Parameter Identification Process.....................41
Figure 4.8. Output Waveform during Experiment.......................................................42
Figure 4.9. Plot of 8 Repetitions and Average Value..................................................42
Figure 4.10. Comparison Plot of Model Response and Actual Response...................43
Figure 4.11. Circuit Diagram for PID Control Experiment.........................................44
vii

Figure 4.12. Block Diagram for Single Set Point PID Control Experiment...............45
Figure 4.13. Single Set Point PID Control Experimental Result.................................45
Figure 4.14. Two Set Points PID Control Experimental Result..................................46
Figure 4.15. Circuit Diagram for LQR Control Experiment........................................47
Figure 4.16. Block Diagram for LQR Control Experiment.........................................47
Figure 4.17. LQR Control Experimental Results.........................................................48
Figure 4.18. Root Locus of Closed Loop System........................................................49
Figure 4.19. Nyquist Plot of Closed Loop System.......................................................50
Figure 4.20. Bode Diagram of Closed Loop System...................................................50
Figure 4.21. Graphical User Interface for Ball and Beam Control System.................51
Figure 4.22. Initialization Time before Control Process in PID Control.....................52
Figure 4.23. Deviation of Output Response in PID Control Experiment....................53
Figure 4.24. Deviation of Output Response in LQR Control Experiment...................53
Figure B.1. HG Servo Sub-system...............................................................................65
Figure B.2. RC Servo Sub-system...............................................................................65
Figure B.3. Ball Position Detection Block Diagram....................................................66
Figure B.4. Block Diagram for I-PD+FF Controller....................................................66
viii

LIST OF TABLES

Table Page

Table 2.1. Characteristics of PID Gains.......................................................................13


Table 2.2. PID Gain Tuning Equation by Limit Sensitivity Method...........................13
Table 4.1. Electrical Components used in Experiment................................................36
Table 4.2. Recorded Data Values for Voltage Measurement and Distances...............39
1

CHAPTER 1
INTRODUCTION

1.1 Introduction to Ball and Beam Control System


Ball and beam system is the most basic system to study both classical and
modern control theories. It consists of a beam on which a ball is placed and the ball
which rolls freely along the beam. The system itself is an open loop unstable system,
which has two degree of freedom: the ball rolling up and down along the beam and
the beam rotates through an axis according to a pivot point. Many different types of
controllers are designed to control the position of the ball at a desired reference point,
rejecting the disturbances such as a push from a finger. The ball and beam system
mainly consists of a ball and beam mechanism, a position sensor, a DC motor and a
controller. The control input signal is derived by feeding back the position of the ball
by a sensor and compared with the desired reference position. The controller outputs a
control voltage signal to the DC motor and the torque generated by the motor drives
the beam to rotate to a desired angle which, in turn, changes the ball position to a
desired point.
The ball and beam control system can be found in most university control labs
because it is a very simple system to understand both classical control theory such as
PID control method and modern control theory such as LQR control method. Also,
the ball and beam system works on the same principle as real control problems such
as vertical position control of aerospace and airplanes as well as horizontal stabilizing
an airplane during landing and in turbulent airflow. Since these systems are very
dangerous to test practically in real world, the models of ball and beam system are
constructed in laboratories and are studied to understand the prescribed real world
problems. Moreover, control theories for ball and beam control system can be applied
to most systems which are related to balancing and control process. Therefore, the
ball and beam control system has been studied since many years ago, and is still
2

studying nowadays by many researchers because it is really a very useful theory for
control system approach.
3

Some ball and beam control systems use only the ball position as the feedback
signal and the motor control voltage is derived based on that information. In such
system, only the ball position is controlled. This type of control system is used in PID
control of this experiment. Some systems take information from both ball position
sensor and the motor angular position sensor. In such systems, both the ball position
and the motor angular position are controlled. LQR control system of this experiment
used this type of control system. The block diagrams for both PID and LQR control
systems are shown in Figure 1 .1 and Figure 1 .2.

Figure 1.1. PID Control System Block Diagram

In the ball and beam control system in this experiment, a ball and beam
mechanism made of Tayami tool kit, a position detective infrared sensor, an Arduino
controller, an RC servo motor and a dc motor with high power gearbox is used. Two
types of control methods, proportional integral derivative (PID) control and linear
quadratic regulator (LQR) control, are observed. PID control is in the form of
proportional derivative type PID controller with feed-forward term called I-PD+FF
controller. LQR control is implemented by inserting a scale factor to eliminate steady-
state error. RC servo is used in PID control experiment while high power gearbox dc
motor is used for LQR control experiment. In LQR control, both ball position and
motor angular position are controlled but in PID control, only the ball position is
controlled since built-in position control circuit inside RC servo provide angular
position control of the motor. The software used to develop programs and block
diagrams as well as to create graphical user interface is Matlab/Simulink linked with
Arduino I/O interface library.
4

Figure 1.2. LQR Control System Block Diagram

1.2 Introduction to Graphical User Interface


In some system, a graphical user interface, abbreviated as GUI, is used to
provide a user-friendly system application. In GUI, different types of components can
be defined by the user as desired to create a user-defined GUI window which works in
accordance with the callback functions related to the defined components. GUI
provide a more convenient interface between the user and the system because it
displays a window containing buttons, input boxes or instruction texts, instead of
program codes, so that the users can easily understand how to interact with the
system. Therefore, in some ball and beam control systems, a graphical user interface
is applied to input gain values and reference position, to display the output response
plot, as well as to show the ball and beam system animation.
In this ball and beam control system, a graphical user interface, which uses
static text component to display instructions, a slider to input reference ball position,
pushbutton components to open Simulink block diagrams and plot commands, radio
button to choose whether PID or LQR experiment is to be carried out, as well as an
axes component to show ball and beam system model and output response plot, is
created. An example of GUI window for ball and beam system used in this
experiment is shown in Figure 1 .3. From this GUI platform, users can set and reset
the desired reference position, open the Simulink model to start and stop the
experiment and plot the output result from Simulink scope. Since a description is
expressed for how to use the GUI control panel, users can interact easily with the
5

control experiment even without understanding the program codes and Simulink
diagrams.

Figure 1.3. Graphic User Interface for Ball and Beam Control System

1.3 Aim and Objectives


The main aim of this thesis is to model a ball and beam system and to control
the ball position at a desired reference point with the use of a suitable controller, a
PID controller or an LQR controller.
The objectives of the thesis are as described below:
 To study how to model a ball and beam control system in order to demonstrate
an own system based on gained knowledge and theories
 To learn PID and LQR control methods for ball and beam control system
 To develop programs and block diagrams with Matlab/Simulink linked with
Arduino for the control system design
 To understand how the combination of hardware and software works
 To carry out experiments using PID controller and LQR controller
 To compare the theoretical results and the practical experiment results
 To make the experiment more convenient for users by creating a graphic user
interface (GUI)
6

 To improve functions and options of GUI window in order to achieve a


convenient and user-friendly ball and beam GUI control panel

1.4 Implementation
First, mathematical modeling of the system, including calculation of transfer
function and state equations, is done. Then, mechanical and electronic components to
create ball and beam experimental setup are assembled. To control the ball at desired
position, the ball position feedback information from the position detective sensor is
important to be exact so that the control input can be correctly determined by the
controller. To achieve this fact, the sensor calibration is carefully carried out to obtain
exact position information. After that, the position of the ball is measured to test the
exactness of the position sensor. Also, motor parameters, namely gain K and time
constant T, which are to be used in LQR control experiment, are determined by step
response method. After all the preparation required for the experiment is done, Matlab
programs and Simulink block diagrams for PID and LQR control methods are
developed and the experiment is carried out. Frequency response plot of the system is
also observed. Moreover, the components are selected and associated callback
functions are defined to create a desired GUI window. Finally, PID and LQR control
experiment are carried out through created graphical user interface.

1.5 Outlines of Thesis


This thesis is composed of five chapters, the contents of which are briefly
summarized as described below.

 Chapter 1 represents the introduction section which explains an overview of


what this thesis is about. The aim and objectives as well as the implementation
of the thesis are included in this section.
 Chapter 2 provides the literature review about the ball and beam system and
background theories to be used in this thesis.
 Chapter 3 describes the calculation of transfer function and state space
equations, tuning of PID gains, finding LQR gain matrix, as well as computing
PSD sensor equations and calculating parameters to find ball position.
 Chapter 4 includes tests and experiment results. Sensor calibration and motor
parameter identification are also carried out in this section.
7

 Chapter 5 is discussion and conclusion section in which the results obtained


from experiment are discussed and conclusion is made on the whole thesis
8

CHAPTER 2
BACKGROUND RESEARCH

2.1 Brief History of Ball and Beam System


The ball and beam system had previously been built and experimented by
many organizations, which were by means of PD control, PID control or LQR control.
A brief history of those researches is summarized as follows. Hirsch (1999) built the
‘Ball on Beam System’ in which an ultrasonic sensor was used to detect the ball
position and was controlled by a PD controller. The motor with a gearbox was driven
with a high power op-amp circuit and the beam angle was measured by a
potentiometer. Lieberman (2004) built a system called ‘A Robotic Ball Balancing
Beam’ which used a resistive position sensor to measure the ball position and a
potentiometer to measure beam angle. Lieberman’s system included a compensation
process to design the controller. Arroyo (2005) built the system named ‘Ball on
Balancing Beam’. That system was controlled by a PD controller as in Hirsch’s
system but Arroyo’s system only controlled the ball position and not the tilt angle of
the beam. It employed a resistive wire sensor placed along the beam to detect ball
position and the signals from the sensor were processed in a Digital Signal Processor
(DSP). The motor used in that system is a DC motor with reducing gear.
Ambalavanar, Moinuddin and Malyshev (2006) from the Department of
Electrical Engineering at Lakehead University built the ‘Ball and Beam Balancer’
which consisted of a resistive wire position sensor, DC motor with integrated gearbox
and a digital encoder. Unlike the above systems, the system was controlled by an
LQR controller. All of the prescribed systems had a configuration such that the beam
was supported at its center and the motor was attached to the beam center. Quanser
(2006) introduced a commercial product named ‘Ball and Beam Module’ which had a
different configuration such that the beam was supported at one end and was rotated
through the other end as pivot point. Position sensing was by means of resistive wire
and the system was controlled either by a PID controller or a state space controller.
9

(a) (b)

(c) (d)

Figure 2.4. Ball and Beam Systems: (a) Hirsch’s System (1999), (b) Lieberman’s
System (2004), (c) Arroyo’s System (2005), (d) Quanser’s System (2006)

Later, to improve the system performance, display screen such as Liquid


Crystal Display (LCD) or Graphic User Interface (GUI) were applied to achieve user-
friendly system Application. Sitki from Kirklareli University and Hilmi from Trakya
University of Turkey created a ball and beam control system with LCD display as
control panel in 2013. Mohd Fuaad Rahmat, Herman Wahid and Norhaliza
Abdul Wahab (2010) tested Simulation in Matlab\Simulink for PID controller, LQR
controller and neutral network controller and displayed the gain values, output results
and ball and beam animation using a GUI window. Nowadays, ball and beam
commercial products or experimental kits are available which readily provide ball and
beam experimental setup including servo motor and position sensors, as well as
controller unit with open source program and Simulink diagram. Quanser (2006) is
such type of product. Moreover, some products even contain GUI for the system such
as the system produced by Acrome Automation Control Robotics Mechatronics
(2013) which provide P, PD, PID and Fuzzy logic controllers. Figure 2 .5 shows
Acrome myControl Ball and Beam Control System with its GUI window.
10

Figure 2.5. Acrome myControl Ball and Beam System

2.2 Ball and Beam System Configuration


The ball and beam system can be modeled in two basic configurations as
described below.

1) Beam supported at its center with motor attached at that point


2) Beam supported on both sides by two lever arms, one as pivot and the other at
which motor is attached

In the first configuration, the beam rotates through its central axis, and since
the motor is directly attached to it, the motor output shaft angle and the beam angle
can be considered to have the same value. So, the mathematical model of this type of
system is simple because the ratio between beam angle and motor angle is not taken
into account while calculating transfer function of the system. This type of
configuration is the one used in the systems of Hirsch (1999), Lieberman (2004) and
Arroyo (2005).
The second configuration is the one used in ball and beam system of Quanser
(2006) called ‘Ball and Beam Module’. In this type, the beam rotates through the
pivot point located at one end of the beam, being driven by the motor attached at the
other end. This can be said to have a more complex system configuration because the
ratio between beam angle and motor angle is required to consider for modeling the
system and calculating its transfer function, based on the point at which the lever arm
is attached to the beam.
The two basic configurations for ball and beam system are shown in Figure 2
.6, in which the first configuration shows the beam supported at center and the second
configuration represents the beam supported at each end of the beam.
11

(a) (b)
Figure 2.6. Two Basic Configurations for Ball and Beam System Model: (a) Beam
Supported at Center (b) Beam Supported at Both Ends

In this thesis, a configuration, which can be thought of as the combination of


the above two configurations, is used. The beam is supported at its center and rotates
through its central axis, but the motor is attached at a certain distance from the pivot
point on the beam with a lever arm. Therefore, the ratio between beam angle and
motor angle is required to consider while modeling the ball and beam system. The
system configuration used in this experiment is described in Figure 2 .7. The ratio
between beam angle (α) and motor angle (θ) is defined as the ratio of d/L where d is
the offset distance between the motor shaft and the lever arm attached point to the
motor arm while L is the distance between the beam center and the lever arm attached
point to the beam.

Figure 2.7. Ball and Beam System Configuration


12

2.3 Control Methods for Ball and Beam Control System


The control methods used in this experiment include Proportional-Integral-
Derivative (PID) control, which is a conventional control method, and Linear
Quadratic Regulator (LQR) control, which is a modern control method. In PID control
method, the transfer function of the system is used to find the PID gains, while in
LQR control method, the state equations of the system are used to calculate the LQR
gain. The gain values for both PID and LQR control systems are determined in such a
way to set the ball at the desired position with minimum error, that is, the difference
between the target value and the actual value, as well as to reduce percent overshoot
and settling time.

2.3.1 Types of PID Controller


PID controller can be in many different forms such as P control, PI control,
PD control or PID control. It can also be in the form of PI-D controller, which is a
derivative type PID control, and I-PD controller, which is a proportional derivative
type PID controller.

Figure 2.8. PID Controller

PID controller, PI-D controller and I-PD controller have the same disturbance
effect but the target value response characteristics are different. In PID control, the
difference between target value and actual output value is multiply by proportional,
integral and differential elements, the sum of which becomes the control input to the
controlled object. Therefore, the deviation changes in the same manner as the target
value. When the target value changes in a step-like manner, the deviation also
changes with the step-like manner, and since the slope of the step is infinite, the
differentiator will generate very large impulse-like signal. This can pose a sudden
load to the actuator, and can cause residual vibration by exciting the resonance
characteristics of the controlled object. To avoid these problems, the PI-D controller
and the I-PD controller, which are typed as Two-Degree-of-Freedom control system.
13

Figure 2.9. PI-D Controller

In PI-D control, the input to the differential term is only the actual output, and
not the difference between target value and actual output as in the case of PID control.
Therefore, when a step input is applied as target value, there is no differentiation for
the step input which can avoid impulse-like control input. However, the deviation of
the actual value from the target value is fed to the proportional and integral element,
so when the target value is changed in a stepwise manner, the control input also
changes with a stepwise manner. Therefore, it is necessary to eliminate step-like
change of the control input. This problem can be solved a method in which only the
integrator acting on the deviation between the actual output and the target input. This
type of controller is called I-PD controller as shown in Figure 2 .10.

Figure 2.10. I-PD Controller

Although I-PD controller can avoid impulse-like as well as step-like change of


the control input, the response of I-PD control is slower compared to PID and PI-D
control. Therefore, to improve the target value response, a feed-forward term K FF is
added to I-PD controller. This can offset the closed-loop system slow poles so that the
convergence to the target value will become faster, which can improve the response
speed. Therefore, in this ball and beam control system, proportional derivative type
PID controller with feed-forward term, which is called I-PD+FF controller as shown
in Figure 2 .11, is used.
14

Figure 2.11. I-PD Controller with Feed-forward Term

2.3.2 PID Tuning Methods


Different kinds of PID tuning methods can be used to tune proportional gain
(KP), integral gain (KI) and derivative gain (KD), which can make the controller to give
the best control action on the controlled object or system.
The PID controller has a transfer function as given below:

(2.1)

The output response in time domain for PID controller is:

(2.2)

In equation ( 2 .1) and ( 2 .2), each term, other than the sum of all terms,
separately represents proportional, integral and derivative parts of the controller.
Therefore tuning of PID gains can be performed for all types of PID controllers
whether they are in the form of P control, PD control, PI control, PID control or PI-D
and I-PD control.
Among different PID tuning methods, manual tuning, limit sensitivity method
and pole assignment methods are studied and the most suitable method for the system
is selected. In manual tuning method, PID gains are determined by trial-and-error with
minimal analytic analysis using step responses obtained by simulation or by actual
testing on the system. The values of PID gains are adjusted and decided based on their
characteristics to obtain a system with desired specifications. The effects of PID gains
on the system output response is described in Table 2 .1.
15

Table 2.1. Characteristics of PID Gains

PID Gain Rise Time Settling Time Percent Overshoot Steady State Error
Decrease Small Change Increase Decrease

Decrease Increase Increase Eliminate

Small Change Decrease Decrease No Impact

In limit sensitivity method, ultimate gain (KC) and ultimate period (TC) are first
determined from the system response under defined condition, and then, PID gains are
calculated by the defined equations as described in . This method is the same as the
closed loop concept of Ziegler-Nichols PID tuning.
The following steps described how to perform the limit sensitivity method:

 Gain KP is increased until the output is in sustained vibration. This gain on the
border of instability is defined as ultimate gain, KC
 The period of sustained vibration, termed as TC, is taken from system response
 Calculate the gain values using the defined equations
 Apply some fine-tuning so that output response can be in desired conditions

Table 2.2. PID Gain Tuning Equation by Limit Sensitivity Method

Controller Type Transfer Function

P control - -

PI control -

PID control

In pole assignment method, PID gains are designed to achieve the desired
properties which will make the closed-loop poles in a desired stable region. This is
16

why this process is known as pole assignment method. PID gains are calculated by
equating the roots of closed loop control system to the desired poles. Therefore,
unlike manual tuning and limit sensitivity methods, this method uses not only the
transfer function of the controller but it also uses the transfer function of the
controlled object to calculate the required PID gains.
The advantages of pole assignment method over manual tuning and limit
sensitivity method is that the PID gains can directly obtained from the transfer
function of the closed loop system. There is no need to take data from the system
response, as in the case of limit sensitivity method, which can include some error
according to inexact reading of data. Also, the time required to tune PID gains is
reduced since it is not required to repeat the control process in order to determine the
gains based on the system response, as in the case of manual tuning method.
Moreover, no adjustment is required to carry out manually. Since the gain values are
calculated based on the desired poles, the system response can be obtained with the
desired characteristics. Hence, pole assignment method is considered to be the best
tuning method among the three methods that have been studied. Therefore, in this ball
and beam control system, pole assignment method is selected to tune the PID gains.

2.3.3 LQR Control Method


In Linear Quadratic Regulator (LQR) control method, the gain matrix which
minimizes the performance index (J) is to be calculated. In this system, this gain
matrix will be assigned as ‘F’. To use LQR control method, it is required that the
system is state controllable. Therefore before calculating the value of gain F, the
controllability of the system is to be tested by controllability matrix P C, which is an
nxn matrix, as given in equation (PC = [B AB A2 B … An-1B] ( 2 .3 ), in
which A is the state matrix and B is the input matrix of the system. It is stated that the
system is controllable if the determinant of PC has a non-zero value.

PC = [B AB A2 B … An-1B] (2.3)

If the system is state controllable, then, LQR gain F is determined by the


following procedure:
17

 To determine weighting matrices Q and R


 To solve Riccati equation for matrix P
 To find optimal gain matrix F

Figure 2.12. LQR Controller

The performance index is defined by equation ( 2 .4)


given below.

(2.4)
In this equation, Q and R represent the weighting matrices where Q is the state
weighting matrix and R is the control weighting matrix. To design LQR controller, it
is important to choose the values of Q and R. Since R is a control weighting matrix, if
control input u is a scalar, R becomes a scalar. In this system, the control input is the

voltage to the motor ( ). The state weighting matrix Q is usually chosen as a


diagonal matrix. The element along its diagonal is correspond to the state variables, in
this system, ball position ( ), ball speed ( ), motor angular position ( ) and motor

angular velocity ( ), respectively.


Consider R to be a scalar r and Q to be diagonal matrix with elements q 1, q2, q3
and q4 along its diagonal, then, equation ( 2 .4) becomes:

(2.5)
18

(2.6)

In above equation the integral terms and are


called integral square errors. When the integral square error is small, the error area
response will become small. If the error area of a response of specific state variable is
wanted to be small, the weight corresponds to that state variable should be increased.
In this ball and beam control system, it is desired to minimize the error of ball
position, then the weightq 1 , which is correspond with the ball position state variable x
should increase. Also, to make the control input v a smaller, the value of r should be
increased. The values for Q and R are selected and adjusted until the system response
contains as less error as possible.
After determining weighting matrices Q and R, Riccati equation given in
equation ( 2 .7) is solved to obtain a positive definite symmetric unique solution P.
Finally, the LQR gain matrix F is calculated by using equation ( 2 .8).

(2.7)

(2.8)

2.4 Graphical User Interface (GUI)


Graphical user interface can provide a more convenient interface between the
system and the user. Creating GUI window in Matlab Guide consists of two parts:

 UI layout
 UI programming

2.4.1 UI Layout
User interface layout provides a set of tools for creating the user interface. By
using the layout editor, GUI can be easily created by clicking and dragging
components such as the buttons, sliders, pop up menu, static text and edit text, etc. By
saving the created UI layout, Matlab produces two files, namely, .fig file, which is a
19

figure of created GUI window, and .m file, in which callbacks functions associated to
the used components are to be programmed.

2.4.2 UI Programming
User interface programming includes a program file containing callback
functions that control how UIs behave. m.file for UI programming is automatically
created upon saving the UI layout. It contains callback functions for each component
used in UI layout. Only the programmers have to do is to write codes in callback
functions which can make the program act in the desired way.

2.4.3 Graphical User Interface for Ball and Beam Control System
The GUI used in this ball and beam control system includes a ball and beam
model to display the ball and beam system configuration which uses “patch”
command to create each ball and beam component. A description box is provided to
instruct the users how to use the GUI control panel in terms of static text components.
A ball position slider together with a Set Position and Reset buttons are created to
select and set/reset the ball position into the Simulink block diagram. To do this
function the value from slider is taken by “get” command and is set into the Simulink
blocks by “set_param” command. It also contains a radio button panel to select
experiment method and Open Simulink button to open Simulink model of the selected
method. The selected method is checked by using “switch/case” command and the
associated Simulink block is opened by “open_system” command. But, GUI only
opens the Simulink block diagram and start/stop function is done in the Simulink
model. It also contains an axis together with Plot Output and Clear Axes buttons to
plot the output response and clear it. Here, the data are first logged into the
workspace from the scope of the Simulink block, then, is imported to GUI by
“evalin” (evaluate expression in work space) command and is plotted and clears
simply by “plot” and “cla” commands.

2.5 Summary
In this chapter, first, a brief history of ball and beam control system is
described. The two basic configurations of ball and beam control system are
explained and a configuration which is used in this thesis is discussed. Then, PID and
20

LQR control methods for the ball and beam control system is considered. This
includes types of PID controller, PID tuning method as well as LQR gain and
weighting matrices. PID and LQR control methods are explained with associated
equations to find the gain values for both PID and LQR control. Also, a brief
explanation about graphical user interface (GUI) is expressed together with how GUI
is created for this ball and beam control system.
21

CHAPTER 3
DESIGN AND CALCULATION

3.1 Calculation of Transfer Function


In mathematical modeling of the ball and beam system, calculation of transfer
function consists of two parts:

1) Motor transfer function


2) Ball and beam model transfer function

3.1.1 Motor Transfer Function


The motor transfer function, which is a transfer function of motor input
voltage to its angle of rotation, can be calculated by the armature-controlled dc motor
circuit shown in Figure 3 .13.

V a (t )

Figure 3.13. Circuit Diagram for Armature Controlled DC


Motor
From the given circuit diagram,

(3.9)

Taking Laplace transform,


22

(3.10)
23

In equation ( 3 .10), = Armature voltage

= Resistance

= Inductance

= Armature Current
Vb(s) = Back electromotive force

Again, the back electromotive force is proportional to the motor speed.

11
(3. )

(3.12)

where, = Back electromotive force constant

= Motor angular speed

= Motor angular position

Therefore, equation ( 3 .10) becomes:

(3.13)

The motor torque (Tm) is:

(3.14)

The load torque (TL) is:

(3.15)

Taking Laplace transform,


24

Again, the disturbance torque, Td(s) is:

(3.16)

Assuming no disturbance torque, so Td(s) = 0, then,

(3.17)

where, = Total load inertia


= Total load friction

= Motor torque constant

By equating the two equations for I(s), equations ( 3 .13) and


( 3 .17),

(3.18)

In small DC motor, the inductance La is negligible, then,


25

(3.19)

Let (a constant), then,

Again, put and , so,

(3.20)

This is the motor transfer function, where K is the gain and T is the time
constant of the dc motor. The values of K and T are to be calculated by step response
method in experiment section of next chapter.

3.1.2 Ball and Beam Transfer Function


The transfer function of beam tilt to the ball position is defined as the ball and
beam transfer function, and can be obtained by using Newtonian Method in which the
calculation is made by means of force and torque equations. There are two forces
acting on the ball, namely, the translational force due to gravity (Ft) and the rotational
force due to torque (Fr). Consider the free body diagram of the system shown in the
Figure 3 .14.
26

Figure 3.14. Free Body Diagram for Forces Acting on the Ball
The force summation equation for the forces acting in the inclined x-direction
is:

(3.21)

where, Ft = translational force


Fr = rotational force
m = mass of the ball
g = gravity
α = beam angle
The torque equation is:

(3.22)

where, Fr = rotational force


R = radius of the ball
Jb = moment of inertia of the ball
ä = angular acceleration

But, the angular acceleration is:

(3.23)
27

(3.24)

where, = second derivative of the position

Again, the translational force can be expressed as:

(3.25)

Therefore, by substituting the values of Fr and Ft in equation


( 3 .21),

For the small beam angle, using the small angle approximation, , then

(3.26)

Taking Laplace transform,

(3.27)

Let (3.28)

Then, the transfer function of the ball and beam system can be written as:

(3.29)
28

3.1.3 Selecting Moment of Inertia of the Ball


The value of moment of inertia of the ball (Jb) can have different values for
different types of balls:

 for solid sphere ball,

 for spherical shell (hollow) ball,

So, according to equation Let ( 3 .28),

(for solid ball) (3.30)

(for hollow ball) (3.31)

In this ball and beam control system, a hollow steel ball is used. Therefore,
equation (for hollow ball) ( 3 .31) is selected. Also, the beam
length is 42 cm and the ball does not move more than a few tens of centimeters, so the
unit of position is set to ‘cm’. Therefore, to match with the unit of ‘cm’, the value of
Kb is to be multiplied by 100 which is conversion from meter to centimeter. Then, the
value of Kb becomes (3/5) (100g).

3.1.4 Relation between Beam Angle and Motor Angle


Since the beam is pivoted at its center and the motor is attached to the beam at
a certain distance from the beam center, the ratio between the beam angle and the
motor angle has to be calculated. This ratio is considered based on the distance
between pivot point (beam center) and lever arm attach point on the beam, related to
the length of the motor arm.
29

Figure 3.15. Ball and Beam Diagram

According to Figure 3 .15, the distance between beam center and


lever arm attach point is ‘L’ and the motor arm length is ‘d’. Therefore, the ratio
between beam angle (α) and motor angle (θ), assigned as Kθ, can be calculated by the
following equation.

(3.32)
In ball and beam control system, the value of Kθ is used to calculate the
required motor angle which makes the ball position to be at the desired set point. To
get the motor angle, the beam angle, which is determined by the controller, is multiply
by 1/Kθ.

3.2 Calculation of State Space Equation


In order to control the ball and beam system by state feedback controller, the
transfer function alone is not sufficient. It is required to find the state space equations
of the system, which a matrix representation of differential equations. Therefore,
differential equations are first determined from the calculated transfer function and
then converted to the state equations.

Taking the motor transfer function from equation


( 3 .20),
30

(3.33)

Taking inverse Laplace transform,

(3.34)

Taking the ball and beam transfer function from equation


( 3 .29),

(3.35)

Taking inverse Laplace transform,

(3.36)

Taking the ratio between beam angle and motor angle from equation
( 3 .32),

(3.37)

Therefore, equation ( 3 .36) becomes,

(3.38)

Now, the state variables are defined as follows:

(ball position)

(ball speed)
31

(motor angular position)

(motor angular speed)

Differentiating the state variables and substituting the values from


equations ( 3 .34 ) and ( 3 .38 ), the
following state equations are obtained.

(3.39)

(3.40)

(3.41)

(3.42)

Let the input ‘u’ be the input voltage to the motor ‘va’ and the output ‘y’ be the
ball position ‘x1’. Then, the state equation of the system is:

(3.43)

(3.44)

The output equation of the system is:

(3.45)

(3.46)

Now, the matrices A, B, C, and D are obtained by using state space


representation equations given in ( 3 .47 ) and
32

( 3 .48 ( 3 .48 ) given


below.

(3.47)

(3.48)

, , ,

3.3 Tuning of PID Gains by Pole Assignment Method

For I-PD+FF controller used in this ball and beam control system, the PID
gains and the feed-forward gain are tuned by using pole assignment method. First, the
block diagram for the closed loop control system is considered. Then, the closed loop
transfer function is calculated and the roots of closed loop system are equated with the
desired poles to obtain the system response with desired characteristics. This can also
be done by comparing the characteristic equations of the closed loop control system
and the desired poles. The detail calculation of the gain values using pole assignment
method is explained below.
From equation ( 3 .29 ), the transfer function

of the ball and beam system is . Therefore, the block diagram of the closed loop
control system using I-PD+FF controller becomes as shown in Figure 3 .16.

Figure 3.16. Closed-Loop Control System with I-PD+FF Controller


33

From Figure 3 .16, the closed loop transfer function of the system is:

(3.49)

The characteristic equation of the system is:

(3.50)

Let the poles be p1, p2, p3 with damping ratio ( ) and natural frequency ( ).

(3.51)

(3.52)

(3.53)

Then, the characteristic equation becomes:

(3.54)

By comparing equations ( 3 .50) and ( 3 .54), the PID


gain values are obtained:

(3.55)

(3.56)

(3.57)

To find the feed-forward gain , substitute characteristic equation of third-


order system, equation ( 3 .58) , in denominator of the closed-
loop transfer function in equation ( 3 .49).

(3.58)
34

Since feed-forward gain is intended to offset the slow pole, , set

the value of so that the slow pole is eliminated from the system.

(3.59)

Now, all the gain values , , and are obtained. The values of

damping ratio, , and natural frequency, , as well as the value of is to be selected


in order to obtain the system response with desired characteristics. Here, is the third
pole of the closed loop system and not to be complicated with the beam angle from
the calculation of ball and beam transfer function.

In this ball and beam control system with PID control method, the values of

and are selected as 0.6 and 1.5 respectively. These values are mostly used in
control systems because they can provide good system response. With these values, a
system response with a settling time about 4.44 seconds, and percent overshoot of
9.48% is obtained. The value of is set as 0.5. This settling time and percent
overshoot is obtained in response of system model but may have some deviation in

actual experiment. The values of , , and obtained are as follows:

(3.60)

(3.61)

(3.62)

(3.63)

3.4 Selecting Weight Matrices for LQR Gain Matrix


In LQR control system, the state weighting matrix Q is defined as a diagonal
matrix with elements q1, q2, q3 and q4 along its diagonal, and the control weighting
matrix R is defined as a scalar value r, as explained in LQR control method section
35

described in previous chapter. The weights q1, q2, q3 and q4 are corresponding to the
state variables: ball position, ball velocity, motor angular position and motor angular
velocity. In order to find the LQR gain matrix F, these weighting matrices have to be
selected to yield the best system response.
Since the ball and beam control system is a position control system, the goal is
to reduce the error between the desired ball position and actual ball position. In other
words, it is desired to reduce the error of state variable x1, the ball position. Moreover,
the control input u, the voltage input to the motor is desired to be smaller. Based on
these facts, the state weighting matrices are selected by the following two criteria:

 Increase q1 to reduce the error of ball position state variable x1


 Increase r to make the control input u smaller

The values of Q and R obtained for this ball and beam control system is:

(3.64)

(3.65)

The LQR gain matrix F is calculated using Matlab command ‘lqr(sys,Q,R)’,


where sys is the state space system obtained by state matrices A,B,C and D. This
Matlab command automatically calculates the optimal gain matrix which minimizes
the cost function J as explained in LQR control method section of previous chapter.
The value of LQR gain matrix F is described in equation ( 3 .66).

(3.66)

3.4.1 Inserting Scale Factor N to Eliminate Steady-state Error


It is considered that the LQR gain matrix alone cannot give the best system
response, so, a scale factor N which has a constant gain value is inserted after the
reference input to eliminate the steady-state error of the system. The value of constant
gain N is calculated by a user-defined function ‘rscale.m’, which is described in
36

Appendix C. This function computes gain N by using state matrices and LQR gain
matrix F. The value of gain N as obtained by using Matlab function
‘rscale(A,B,C,D,F)’ is described in equation ( 3 .67).

(3.67)

Figure 3.17. LQR Controller with Scale Factor N

3.5 Angle Detection Unit for Motor Position

In LQR control experiment, the motor angle is detected by using a


potentiometer. So, the equation to convert potentiometer output voltage to motor shaft
angle is required to find out. The number of teeth of gear on the output shaft of high

power gear box motor ( ) is 40, and the number of teeth of gear on the
potentiometer knob ( ) is 36. Therefore, the ratio between the potentiometer output

angle ( ) and the motor angle ( ) with respect to the teeth ratio is:

(3.68)

(3.69)

The actual measured output sensitivity of the potentiometer is 50 deg per 1 V.

Therefore, the value of potentiometer constant ( ) can be written as:


37

(3.70)

Also, the relation between potentiometer output voltage (Vpot) and


potentiometer output angle (θpot) is:

(3.71)

(3.72)

Finally, by substituting equation ( 3 .72) in equation


( 3 .69) , the equation to convert potentiometer output
voltage to motor angle is obtained. Since the potentiometer output is an analog signal,
in order to know the potentiometer output voltage, the output value is multiply by
5/1023 to convert an analog value of 0 to 1023 to a voltage of 0 to 5V.

(3.73)

3.6 Equations of Distance Sensor to Find Ball Position


The distance sensor measures the ball position by producing a voltage that is
proportional to the distance. However, there may be some noises and error which can
affect the sensor reading so that voltage may not be proportional to distance as
considered before. Therefore, the characteristics of the sensor are to be calculated in
order to obtain an exact ball position. The relation between output voltage (Vo) and
distance (D) is first considered as:

(3.74)

constant

But, when the noises and error which may contain in sensor reading are taken
into account, the relationship between the output voltage and the distance is no longer
the same as the one described above in equation
38

( 3 .74 ). Therefore, it is approximated by equation


( 3 .75) given below.

(3.75)

where, x = the output voltage measured in volt


y = the distance measured in cm
a, b, and c are sensor parameters
Therefore, it is required to find the parameters, a, b, and c, which can make
measured data to be as exact as possible. These values are calculated by using the
least square method. First, rearrange equation ( 3 .75) as
described in equation ( 3 .76).

(3.76)

The approximate equation given above is the relation of x and y for a single
measurement. So, if N number of measurements is made, then x and y is represented
as data pairs (xi , yi) where ( i = 1,2,3,…..,N ).

Assign a formula error to the data pairs of (xi , yi) and applied to equation
( 3 .76).

(3.77)

This can be written as:

(3.78)

where X1 = c, X2 = -b, X3 = a + bc
39

Convert equation ( 3 .78) to matrix form as expressed


in equation .

(3.79)

Let

(3.80)

The sum of squares is defined in equation


( 3 .81) and the values of a,b, and c which minimize
that sum is to be computed:

(3.81)

(3.82)

Here, the term is a scalar and does not change when transposed,
therefore it can be written as:

(3.83)
40

Now, the value of minimum X is to be determined. This is called the least


squares solution. When the above equation of sum of squares, denoted as J, is
minimized, the following condition must be satisfied.

(3.84)

This is considered according to the nature of the curve for output voltage and
distance which has a decreasing slope, therefore, the value of X obtained by equating
the partial differentiation of the J equation to zero gives the minimum value for X.

(3.85)
Equation ( 3 .85) is solved by the partial differential formulas
given in equation ( 3 .86) and equation

( 3 .87) in which M is considered as and N is

considered as . Then, as a result, equation ( 3 .88) is


obtained.

(3.86)

(3.87)

(3.88)
Now, equation ( 3 .88) is equated with zero so that the
minimum value of X denoted by X opt can be found.

(3.89)

(3.90)
41

Since the values of Y and A can be obtained from known data of output
voltage and distance, the values of the parameters, a, b, and c, are calculated
respectively from the entities of matrix X.

(3.91)

(3.92)

(3.93)

3.7 Summary
In this chapter, the mathematical modeling of the ball and beam system,
including transfer function for motor model and ball and beam model as well as state
space equations, are calculated. The moment of inertia of the ball is selected
depending on the type of ball used in the system. The relation between the beam angle
and the motor angle is also considered. Moreover, PID gains and LQR gain, together
with the weighting matrices Q and R as well as the scale factor N, are calculated.
Then, equation for the angle detection unit is found out to obtain the motor angular
position. Similarly, equations for PSD sensor are calculated in order to obtain the
parameters of the sensor which are required in finding the actual ball position.
42

CHAPTER 4
EXPERIMENT AND RESULTS

4.1 Construction of Experimental Setup


Ball and beam experimental setup is composed of mechanical components and
electrical components. Mechanical components consist of five main parts: base
assembly, support post assembly, beam assembly, sensor support assembly and lever
arm assembly. To ensure the initial beam orientation is horizontal so that the ball does
not roll before the experiment is started, a spirit level device and four adjust screws
are applied to adjust the horizontal alignment of the surface. Electrical components
used in this experiment are listed in Table 4 .3.
In this experiment, a dc servo motor (RC servo) is used for PID control
experiment and a dc motor with high power gearbox (HG servo) is used for LQR
control experiment. This is because RC servo motor cannot provide the motor angular
position feedback signal which is essential in LQR control experiment. But, the
advantage of this simple RC servo motor in PID control is that it includes a built-in
motor angular position control circuit so that only the controller for ball position
control has to be designed.

Table 4.3. Electrical Components used in Experiment

No. Component Name Model Number Experiment


1 Arduino Controller Mega 2560 PID, LQR
2 Infrared distance sensor GP2Y0A21 PID, LQR
3 2.5V 100µF capacitor - PID, LQR
4 4.5 V battery source - PID, LQR
5 10KΩ variable resistor - PID, LQR
6 DC servo motor MG995 PID
7 DC motor with gearbox - LQR
8 Motor driver IC TA7291P LQR
9 0.1µF ceramic capacitor (104) - LQR
10 10KΩ variable resistor - LQR
11 10KΩ resistor - LQR
43

The complete experimental setup of the ball and beam system assembly used
in this experiment is shown in Figure 4 .18.

Lever Arm
Assembly
Beam
PSD Sensor Assembly
Sensor Support
Support Post
Assembly
Assembly
HG Servo Spirit Level
Arduino RC Servo
Controller
Base Adjust Screw
Assembly

Figure 4.18. Ball and Beam Experimental Setup

4.2 Sensor Calibration for Position Sensor


Position sensitive detector (PSD) sensor is one of the most important
components in the ball and beam control experiment. The position information from
PSD sensor is important to be exact so that the controller can determine correct
control voltage for the motor. To obtain exact position information, the parameters of
the sensor (a, b and c), derived in previous chapter, are to be found out
experimentally. This is done in such a way that the voltage of the sensor with respect
to the distance of the ball is measured, and then, the parameters are calculated by
using measured data and derived equations, as well as by checking the measured data
values with a fitting curve. After the sensor parameters are obtained, the position is
determined by using equation ( 3 .75 ) expressed in
previous chapter.
44

Figure 4.19. Sensor Calibration Process


When installing the sensor in experimental setup, the light-emitting portion
should be on the upper side and the light-receiving part on the bottom so that the
emitted light can effectively hit the ball and correctly read from the receiving part. If
the sensor is installed in opposite way, the light emitted from light-emitting side will
be reflected from the beam and then incorrectly read by the sensor as the ball position
which may result in wrong position feedback information. Light-emitting part and
light receiving part as well as pin connection for PSD sensor is shown in Figure
4 .20. Matlab programs to carry out sensor calibration process are described in
Appendix A.

Figure 4.20. PSD Sensor

4.2.1 Output Voltage Measurement of PSD Sensor


The output voltage of the sensor is measured by placing the ball at a certain
distance from the sensor at shown in Figure 4 .19. The ball is moved through
different distances and the output voltage is measured hundreds of times per each
distance. This means that the ball is placed at distances from 6cm to 36cm, with 2cm
45

increment, and voltage measurements are made 500 times for each distance. Then, the
average value of the voltage among 500 times is recorded with respect to its distance.
Finally, a graph of ‘voltage in volts’ versus ‘distance in cm’ is drawn using the
recorded data values. Here, the calibration process is repeated until the best result is
obtained and that best fit result is taken as final value for the parameters. This process
is done with the help of Matlab program in order to get précised results. The data
values of measured voltages with respect to placed distances is described in Table
4 .4. Figure 4 .21 shows the graph of recorded data values in terms of output
voltage in volts versus distance in centimeter.

Table 4.4. Recorded Data Values for Voltage Measurement and Distances

Distance (cm) Output Voltage (V) Distance (cm) Output Voltage (V)
6 3.159638 22 0.901173
8 2.722845 24 0.798065
10 2.206090 26 0.719189
12 1.803050 28 0.636940
14 1.555670 30 0.570968
16 1.329257 32 0.501965
18 1.167928 34 0.448055
20 1.024624 36 0.406872

40
Measured
35

30

25
Distance [cm]

20

15

10

0
0 0.5 1 1.5 2 2.5 3 3.5
Voltage [V]

Figure 4.21. Graph of Output Voltage versus Distance


46

4.2.2 Identification of Sensor Parameters


After the best results for recorded data values are obtained, sensor parameters
are calculated by using these data values and are checked out by a fitting curve. The
plot of data values shown in Figure 4 .21 is checked with a fitting curve drawn by
the calculated sensor parameters. The fitting curve for the identification of sensor
parameters is shown in Figure 4 .22. Since the recorded data value fits with the
calculated sensor parameters, the values of parameters a, b, and c expressed in
equation a = 36.678947 ( 4 .94) through c = - 3.660378
( 4 .96) is taken as the final parameter values.
40
Measured
35 Calculated

30

25
Distance [cm]

20

15

10

0
0 0.5 1 1.5 2 2.5 3 3.5
Voltage [V]

Figure 4.22. Fitting Curve for Recorded Data Values


The best fit values of sensor parameters obtained in this experiment are:

a = 36.678947 (4.94)
b = 0.522710 (4.95)
c = - 3.660378 (4.96)

4.2.3 Ball Position Detection Part


Since all the parameters needed to measure ball position are obtained, ball
position is calculated by equation ( 3 .75 ) derived in
47

previous chapter. A block diagram for ball position detection part is created in
Simulink as part of position control experiment and is used in both PID and LQR
control experiments. The block diagram of position detection part is shown in Figure.
Here, the signal pin of PSD sensor is connected to analog pin A2 of Arduino
controller.

Figure 4.23. Block Diagram for Position Detection Part


4.3 Identification of Motor Parameters

The parameters of high-power gearbox dc motor, namely gain K and time


constant T, which are essential in LQR control experiment, is calculated by using Step
Response Method. The angle of motor is detected by a potentiometer which is
connected to the motor gearbox through another gear put on its knob. Although the
motor can rotate full 360 degree rotation, the rotational angle of the potentiometer is
limited, and so, the potentiometer reaches its maximum angle and stops before the
motor output shaft reaches the constant rotational speed. Therefore, a feedback gain is
inserted to stabilize the controlled object in order to identity parameters by the step
response experiment. Parameter identification process with calculated results for gain
K and time constant T is explained below.
Figure 4 .24 represents the block diagram to perform motor parameter
identification process. Pulse generator setting and matlab program used for parameter
identification is described in Appendix A. Motor subsystem blocks are expressed in
Appendix B.
48

Figure 4.24. Block Diagram for Motor Parameter Identification Process

First, the feedback gain Kp_id is adjusted until the system response becomes
somewhat oscillatory. During testing to find best fit feedback gain value, the
experiment is repeated only twice. When the best value for feedback gain is found, the
experiment is repeated 8 times and the results together with their average value are
plotted. The output waveform during experiment is shown in Figure 4 .25, and the
plot of 8 repetitions together with their average value is shown in Figure 4 .26.

Figure 4.25. Output Waveform during Experiment


49

Figure 4.26. Plot of 8 Repetitions and Average Value

In this experiment, the feedback gain values from 0.085 to 0.125 are tested,
and the best result occur at gain value of 0.12. After the stabilization feedback gain is
obtained, the motor parameters are derived with the use of Matlab function
‘fminsearch’ which calculates the squared integral error and returns the value. The
equation for squared integral error (J) is:

97
(4. )
where, y [k] = actual step response data obtained in the experiment
ysim [k] = response of a model with consideration of influence of discretization
of controlled object
To find motor parameters, gain K and time constant T, a function is created to
define feedback gain and step response data obtained in above experiment. Since the
sampling period of 20 ms is not extremely short and the experimental setup is
subjected to a digital control with that sampling time, the values of gain K and time
constant T obtained may be slightly different from the actual values. Therefore, the
influence of the discretization of controlled object, which is the process of converting
continuous time signal to discrete time signal, is to be considered in this experiment.
The motor parameters are derived by a comparison plot of actual step response data
with response of a model with consideration of influence of discretization of
controlled object as expressed in equation (4.4), the equation of integral squared error.
50

Here, the time length for fitting, which is the time range where the calculated response
from the model agrees with the experimental data, is required to find by repeating
trials in order to obtain the best fit values of motor parameters. In this experiment, the
time length is taken as 1.6 seconds. The plot for comparison table and the obtained
motor parameter values are described below. Matlab program together with created
function to perform motor parameter derivation is expressed in Appendix A.

Figure 4.27. Comparison Plot of Model Response and Actual Response

The motor parameters obtained by identification process are:

K = 202.335440 (4.98)
T = 0.362771 (4.99)

4.4 Experimental Results for PID Control Experiment


PID control experiment, as mentioned above, uses simple RC servo motor.
The circuit diagram for PID control experiment is shown in Figure 4 .28. In PID
control experiment, ball position is tested for two conditions: single set point and two
set points. The results for PID control experiment are shown and explained below.
51

Figure 4.28. Circuit Diagram for PID Control Experiment

4.4.1 Results for Single Set Point PID Control Experiment


Single set point PID control experiment can be tested with different set points
values as desired except from less than 6 cm and greater than 36 cm because the
reading from sensor is not very exact at that values of ball position. It is found that the
set point at the middle of the beam which is 21 cm can support the most effective
control. This is because of the fact that in the middle of the beam, there is lesser
chance for the ball to roll to either side of the beam. However, the ball position can
still be controlled at other set points in such a way that the beam rotates up and down
continuously to make the ball stay at the desired position. The block diagram for the
single set point PID control experiment and the result for a set point of 20 cm are
shown in Figure 4 .29 and Figure 4 .30 respectively.
52

Figure 4.29. Block Diagram for Single Set Point PID Control Experiment

Figure 4.30. Single Set Point PID Control Experimental Result

As shown in Figure 4 .30, the actual ball position follows the desired set
point position in an acceptable range although there is some deviation. Therefore, it
can be said that single set point PID control experiment perform effective control
actions in order to control the ball position at a desired set point. Here, the reference
signal line is not visible because it is totally identical with the model ball position
signal. Other than desired set point position of 20 cm, other set point values can also
be tested and checked whether the control of the ball position is effective as desired.
53

4.4.2 Results for Two Set Points PID Control Experiment


After single set point ball position control experiment is tested, two set points
ball position control experiment is carried out. In this experiment, the block diagram
of the system is the same as in single set point experiment but the only difference is a
pulse generator is used instead of a constant block. The pulse generator setting and
Matlab codes for the experiment are described in Appendix C. In this experiment,
two set points for ball position, 15 cm and 25 cm, are set to test the control of ball
position. Results from scope shows first position as 0 cm and second position at 10
cm because the initial ball position r1 = 15 cm is subtracted while measuring the
position of the ball. Therefore, when the ball is at 15 cm, the scope shows 0 cm and
when it is at 25 cm, the scope shows 10 cm. In the actual experiment, the ball is
controlled to be at the desired set point values of 15 cm and 25 cm alternatively for
about 15 seconds at each position. The results for two set points PID control
experiment is shown in Figure 4 .31.

Figure 4.31. Two Set Points PID Control Experimental Result

In Figure 4 .31, ‘pulse generator’ means reference position, ‘ball and beam’
means actual ball position and discrete LPF means model ball position respectively. It
can be seen from the results that the actual ball position follows the reference position
with only a little deviation. Also, the nature of model response and actual response is
very close to each other. Therefore, the performance for two set points PID control
experiment can be considered to have acceptable results.
54

4.5 Experimental Results for LQR Control Experiment


LQR control experiment is carried out by using a dc motor with high-power
gear box, called HG servo. The circuit diagram for LQR control experiment is shown
in Figure 4 .32. In LQR control experiment, only a single set point experiment is
carried out. The block diagram for LQR control experiment is shown in Figure 4 .33.

Figure 4.32. Circuit Diagram for LQR Control Experiment

Figure 4.33. Block Diagram for LQR Control Experiment


55

Now, the experimental results for LQR control experiment will be explained.
Before carrying out the experiment, the controllability of the open loop system is
tested by a Matlab program, described in Appendix C. Then, since the result proves
that the system is controllable, LQR control experiment is carried out. Figure 4 .34
shows the experimental results for LQR control experiment. First plot represents ball
position plot for set point and actual value, second plot is the motor output response
and the third plot shows the controlled input into the motor. In this figure, it can be
seen that the actual ball position follows the set point position very well. The results
can be considered to be better than the results from PID control experimental results.
This can be because LQR control experiment controls both the ball position and the
motor position. Also, the use of scale factor N eliminates the steady-state error.

Figure 4.34. LQR Control Experimental Results


56

4.6 Root Locus and Frequency Response of Ball and Beam Control System
After the experimental results for both PID and LQR control experiments are
explained, the root locus and the frequency response of the ball and beam control
system is to be considered. The system in LQR control experiment is used to
determine the frequency response of the closed loop control system because the
transfer function of RC servo motor used in PID control experiment, which is required
to find the closed loop system transfer function, is difficult to find.
The system stability will now be discussed by means of root locus and
frequency response of the ball and beam system. First, the root locus of the closed
loop system is considered as shown in Figure 4 .35. The poles of the system are
-17.3736, -4.3697 and -1.4443 ± 1.1279 i, respectively. It can be seen that the system
is stable since all the poles of the system lies in the left half of the plane.

Root Locus of the System


40

30
Imaginary Axis (seconds -1)

20

10

-10

-20

-30

-40
-50 -40 -30 -20 -10 0 10 20 30 40
-1
Real Axis (seconds )

Figure 4.35. Root Locus of Closed Loop System


The frequency response of the ball and beam control system is determined by
Nyquist plot and Bode diagram of the closed loop system. The Nyquist plot of the ball
and beam system is shown in Figure 4 .36. In this figure, the black circle shows the
minimum stability margin and the blue curve represents the Nyquist plot of the
system. It can be seen from Figure 4 .36 that the contour exits entirely in the stability
region. Also, Nyquist criterion states that “When the number of poles in the right half
s-plane is zero, the feedback system is stable if and only if the contour in the L(s)
57

plane does not encircle (-1, 0) point”. In this ball and beam system, since there is no
poles in the right half s-plane and the contour does not encircle the point (-1, 0), the
system is said to be stable.

Nyquist Plot of the System


1

0.8

0.6

0.4
Imaginary Axis

0.2

-0.2

-0.4

-0.6

-0.8

-1
-1 -0.5 0 0.5 1 1.5
Real Axis

Figure 4.36. Nyquist Plot of Closed Loop System


Now, the Bode diagram of the system is to be considered. Figure 4 .37 shows
the Bode diagram of the ball and beam system. The system is in closed loop stable
condition with the stability margin of the system, such that, the gain margin of the
system is 14.1 dB at 3.44 rad/s and the phase margin is -180 degree at 0 rad/s.
Bode Diagram of the System
0

System: c_sys
Magnitude (dB)

-50
Gain Margin (dB): 14.1
At frequency (rad/s): 3.44
-100 Closed loop stable? Yes

-150

-200
90

0
Phase (deg)

System: c_sys
-90 Phase Margin (deg): -180
Delay Margin (sec): Inf
-180
At frequency (rad/s): 0
-270 Closed loop stable? Yes

-360
10 -3 10 -2 10 -1 10 0 10 1 10 2 10 3
Frequency (rad/s)

Figure 4.37. Bode Diagram of Closed Loop System


58

4.7 Graphical User Interface for Ball and Beam Control System
The Graphical User Interface (GUI) for ball and beam control system includes
ball and beam system model, description for ball and beam GUI control panel, a ball
position slider to set the ball position, radio button panel to select whether to carry out
PID or LQR control experiment, an axes to plot ball position output response, as well
as Set Position, Open Simulink, Plot Output and Clear Axes buttons to set ball
position, to open Simulink block diagrams, to plot system output response and to clear
output response plot, respectively. The created GUI for the ball and beam control
system is shown in Figure 4 .38. GUI for ball and beam control system is created
only by single set point experiment both for PID and LQR control experiments.

Figure 4.38. Graphical User Interface for Ball and Beam Control System

The limitations of the GUI used in this system are as described below:

 GUI only open the Simulink block diagrams and experiment can only be
started and stopped from Matlab/Simulink model
 Output response can be plotted only when simulation process is completed,
real-time plot of the system is not available
 System model is only to show the configuration of ball and beam system,
animation of the system is not created
59

4.8 Errors and Limitations in PID and LQR Control Experiment


Although the acceptable results for both PID and LQR control experiments are
obtained, there exist some errors and deviations as well as some limitations while
carrying out the experiment. These include deviation of output response from desired
set point, oscillation in the output response and an extent amount of initialization time
before the control process occurs. These problems are shown in the following figures.

Figure 4.39. Initialization Time before Control Process in PID Control


As shown in Figure 4 .39, an initialization time of about 15 seconds
exists before the control of ball position takes place. This is found in PID control
experiment in which a simple RC servo is used. This time is very short in LQR
control experiment, therefore, it can be considered as a limitation for the PID control
experiment. Therefore, it is required to wait 15 seconds before the control action on
the ball and beam system becomes effective while carrying out the PID control
experiment.
Figure 4 .40 and Figure 4 .41 represents the deviation of output response
from the desired set point values in PID and LQR control experiments. This occurs
even for the gain values which can give effective control of the ball position. These
deviations are undesired responses which exist even during the control experiment
with desired control performance for the control of the ball position. Therefore, these
60

deviations are considered as undesired errors occurred during ball and beam control
experiment.

Figure 4.40. Deviation of Output Response in PID Control Experiment

Figure 4.41. Deviation of Output Response in LQR Control Experiment

4.9 Summary
In this chapter, the experimental setup of the ball and beam control system,
which is composed of mechanical and electrical components, is first described. Before
the control experiment, the results for sensor calibration process and motor parameter
identification process are explained with associated plots from Simulink scope. Then,
PID and LQR control experimental results are explained and discussed. Limitations
and deviations occurred during the experiment are also described by associated
figures. Moreover, the root locus and the frequency response plots of the system are
61

also shown and explained based on their nature. The graphical user interface of the
ball and beam control system is presented with its functions and options.
62

CHAPTER 5
DISCUSSION AND CONCLUSION

5.1 Discussion

In this thesis, modeling and control of ball and beam system using Arduino
and Matlab/Simulink software is carried out. For modeling of the ball and beam
system, Newtonian method, which considers force and torque equations, is used. The
transfer function and the state equations of the system, which are to be used in control
system design, are calculated. To control the ball and beam system, PID and LQR
control methods are studied in order to design I-PD+FF controller and LQR controller
with scale factor N, respectively. The values of PID gains are determined by pole
assignment method and that of LQR gain is considered by Riccati equation. Then,
program codes and block diagrams of the ball and beam control system for both PID
and LQR control experiment are developed and the experiment is carried out.
In PID control experiment, single set point and two set points inputs are tested
while in LQR control, only single set point input experiment is carried out. Although
there are some limitation and deviation during the experiment, both PID and LQR
control experiments are considered to have desired control action which gives
acceptable results for controlling the ball position at a desired reference point. After
both PID and LQR control experiments yield output results with desired
characteristics, these experiments are carried out through a graphical user interface
control panel. The output responses of the ball and beam control system are also
plotted in that GUI window.
In this thesis, Arduino I/O library is used to interface Arduino controller and
Matlab/Simulink software. Arduino controller is just an interface for inputs and
outputs of the system. All the programs and Simulink models are developed and run
in Matlab/Simulink software and are not built into the Arduino controller. A USB
cable is required to connect Arduino controller and the PC in which Matlab/Simulink
63

software is installed. With Arduino I/O library, it is not available to develop real-time
application of the system.
64

If a library, called Arduino Support Package, is used, Simulink models and the
associated programs can be built into the Arduino controller. And so, the experiment
can either be carried out only using the Arduino controller or be connected to Matlab
in order to the monitor the real-time system response. In this case, the USB cable is to
import and export real-time data to and from Arduino controller and Matlab software.
This is termed as Run on Target Hardware (RoTH) process with which the real-time
application of the system can be available.

5.2 Conclusion

For the control of the ball position at a desired reference point, the results for
LQR control experiment are better compared to that of PID control experiment.
Moreover, initialization time in LQR control experiment is relatively short compared
to that occur in PID control experiment. For both PID and LQR control experiments,
it is found that the control of ball position at the middle of the beam, i.e., 21 cm,
achieve the best control action. However, the output response for controlling the ball
position at other points along the beam also gives acceptable results. But, there is a
limitation that the ball position control process cannot be carried out for the distances
from 6 cm to 36 cm because of inexactness of the sensor reading at that position.
Also, the deviation of actual ball position from desired reference position sometimes
occurs during the experiments for both PID and LQR control methods.
For graphical user interface, there exist some limitations, too. The GUI control
panel only opens the Simulink model, but, the simulation process is started and
stopped through that Simulink model in order to carry out the experiment. The output
response of the system can only be plotted after all the simulation process is
completed. Also, the ball and beam system model is just to show the configuration of
the ball and beam system. However, the GUI contains such useful options as a
colourful graphic figure for ball and beam model, a description for ball and beam
control panel, a selection for experimental method, a ball position set slider and a plot
of experimental results together with associated pushbuttons.
Therefore, it can be concluded that the output response for both PID and LQR
control experiment as well as the features of the graphical user interface in the ball
and beam control system of this thesis are considered to achieve acceptable output
results as desired.
65

5.3 Future Work


To obtain a system with very less error and more effective control of the ball
position, the deviation of output response from desired reference point which
sometimes occur during the experiment have to be removed. Also, a distinct amount
of initialization time in PID control experiment has to be reduced. Therefore, to
improve the control performance of the ball and beam system, the cause of that error
and limitation has to be found out and to be reduced. If possible, these defects are
required to be completely eliminated. Moreover, the RoTH method, which is
explained above, should be tried out so that real-time application of the system with
better results for the control process can be achieved. Then, graphical user interface
of the ball and beam system has to be improved. More options and functions can be
added to make a GUI control panel which provides more convenient application to the
users. Such functions may include components to start and stop the experiment from
GUI control panel, to plot real-time response of the system, and if it is available, to
create an animation which follows the actual experiment of the ball and beam control.
For a more advance application, GUI can also be built with its associated packages,
including .mfile and .fig file, by application compiler in Matlab software. With this
standalone application, GUI can even be run in a PC without Matlab/Simulink
software.
66

REFERENCES

[17Ahm] Ahmed A.M., A.Taifour Ali, Almahdi H.A., Osama A.Taha and
A.Naseraldeen A, Design and Implementation of Ball and Beam
System using PID Controller, Suden University of Science and
Technology, SciEP, Vol.3, No.1, 1-4 (2017)

[17San] Santosh Anand and Rajkishore Prasad, Dynamics and Control of Ball
and Beam System, IJRITCC, Vol.5, Issue 5, June, (2017)

[17Vip] Vipulkumar D.Jadhav, Kipak P.Bhapkar, Mayuri P.Rane and Dr.


Rajendra K.Kokate, Design and Implementation of Ball and Beam
Control System, IJIRSET, Vol.6, Issue 5, May, (2017)

[15Hri] Mr.Hrishikesh R.Shirke and Dr. Prof. Mrs. N.R.Kulkarni,


Mathematical Modelling, Simulation and Control of Ball and Beam
System, IJERT, Vol.4, Issue 03, March, (2015)

[15Mat] The Math Works.Inc, Creating Graphical User Interfaces, September,


(2015)

[15Zhe] Zhen Gao, Sahan Wijesinghe, Thushan Pathinathanpillai, Eric Dyer


and Ishwar Singh, Design and Implementation of a Ball Balancing
System for a Control Theory Course, IJMEC, Vol.5(17), PP. 2363-
2374, October, (2015)

[14Car] Carlos G.Bolivar Vincenty and Gerson Beauchamp Baez, Modeling the
Ball and Beam System from Newtonian Mechanics and from Lagrange
Methods, LACCEI, July, (2014)
67

[13Sit] Sitki KOCAOGLU and Hilmi KUSCU, Design and Control of PID-
controlled Ball and Beam System, International Science Conference,
November, (2013)

[12Car] Carion Pelton, Take Home Labs and the Ball and Beam System,
Oklahoma State University, (2012)

[12Moh] Mohammad Keshmiri, Ali Fellah Jahromi, Abolfazl Mohebbi,


Mohammad Hadi Amoozgar and Wen-Fang Xie, Modeling and
Control of Ball and Beam System using Model Based and Non Model
Based Control Approaches, International Journal on Smart Sensing
and Intelligent Systems, Vol.5, No.1, March, (2012)

[11Ric] Richard C.Dorf and Robert H.Bishop, Modern Control Systems, 12th
Edition, (2011)

[10Moh] Mohd Fuaad Rahmat, Herman Wahid and Norhaliza Abdul Wahab,
Application of Intelligent Controller in a Ball and Beam Control
System, International Journal on Smart Sensing and Intelligent
Systems, Vol.3, No.1, March, (2010)

[09Tom] Tom Trapp and Wil Selby, Ball and Beam Final Report, Massachusetts
Institute of Technology, (2009)

[07Par] J.S.Park, Matlab GUI (Graphical User Interface) Tutorial for


Beginners, University of Incheon, October, (2007)

[07Wei] Wei Wang, Control of a Ball and Beam System, The University of
Adelaide, Department of Mechanical Engineering, June, (2007)

[04Jef] Jeff Lieberman, A Robotic Ball Balancing Beam, February, (2004)

[96Mat] The Math Works.Inc, Building GUI with MATLAB, December, (1996)
68

Kyle Knapp, David Nahmias and Daniel Kowalyshyn, Engineering-58,


Control Theory and Design: Ball Balancing Beam Final Project

Mitsuo Hirata and Ivan Godler, Techshare.Inc, Control System Design:


Getting Started with Arduino and Matlab

Information from Mathworks Documentation and Matlab Central: File


Exchange and Matlab Answers, http://uk.mathworks.com/
69

APPENDICES
70

APPENDIX A

PROGRAM CODE FOR SENSOR CALIBRATION AND MOTOR


PARAMETER IDENTIFICATION

Output voltage measurement of PSD sensor:

clear all; close all; clc;


a = arduino('COM16');
a.analogReference('default');
N = 500;
v_hist = zeros(N,1);
for i=1:N
v_hist(i) = a.analogRead(2)*(5/1023);
fprintf('Voltage = %f\n',v_hist(i));
end
delete(a);
figure(1)
v_ave = mean(v_hist);
t = 1:N;
plot(t,v_hist,t,ones(N,1)*v_ave);
xlabel('Step'), ylabel('Voltage')
legend('Measured','Average')
axis([0 N 0 5])
fprintf('Average = %f\n',v_ave);

Identification of sensor parameters:

clear all; close all; clc;


mydata = [
6 3.159638
8 2.722845
71

10 2.206090
12 1.803050
14 1.555670
16 1.329257
18 1.167928
20 1.024624
22 0.901173
24 0.798065
26 0.719189
28 0.636940
30 0.570968
32 0.501965
34 0.448055
36 0.406872 ];
x = mydata(:,2);
y = mydata(:,1);
N = length(x);
A = [x,y,ones(N,1)];
Y = [x.*y];
X = A\Y;
a = X(1)*X(2)+X(3);
b = -X(2);
c = X(1);
fprintf('a = %f\n',a);
fprintf('b = %f\n',b);
fprintf('c = %f\n',c);
yfit = a./(x+b)+c;
figure(1)
plot(x,y,'*',x,yfit);
xlabel('Voltage [V]')
ylabel('Position [cm]')
legend('Measured','Calculated')
axis([0 3.5 0 40])
72

Step response experiment for motor parameter identification:

clear all; close all; clc;


load model_data
ts = 1/50;
s = tf('s');
omega_n = 12; zeta = 0.6; alpha = 2;
p1 = (-zeta + j*sqrt(1-zeta^2))*omega_n;
p2 = (-zeta - j*sqrt(1-zeta^2))*omega_n;
p3 = -alpha;
Kp = (p1*p2 + p2*p3 + p3*p1)*T/K;
Kd = -((p1+p2+p3)*T + 1)/K;
Ki = -p1*p2*p3*T/K;
disp('>>> PID parameters <<<')
fprintf('Kp = %f\n',Kp);
fprintf('Ki = %f\n',Ki);
fprintf('Kd = %f\n',Kd);
r = 60;
r_cyc = 4;
dist = 2;
Ncyc = 4;
tfinal = r_cyc*Ncyc;
open_system('pos_pid_mbd_sl')
open_system('pos_pid_mbd_sl/Scope')
sim('pos_pid_mbd_sl')

Function to define feedback gain and step response data:

function J = myfunc(x,y,t,ts,Kp_id);
K = x(1); T = x(2); P = tf([0 0 K],[T 1 0]); Pd = c2d(P,ts,'zoh');
Ld = Pd*Kp_id;
Gd = feedback(Ld,1);
ysim = step(Gd,t);
J = norm(y-ysim,2);
73

Parameter derivation with fitting curve:

Lt = input('Time length for fitting = ');


L = Lt/ts;
y_fit = ym(1:L);
t_fit = (0:L-1)*ts;
x0 = [500,0.5];
xmin = fminsearch(@(x) myfunc(x,y_fit,t_fit,ts,Kp_id),x0);
K_id = xmin(1);
T_id = xmin(2);
P = tf([0 0 K_id],[T_id 1 0]);
Pd = c2d(P,ts,'zoh');
Ld = Pd*Kp_id;
Gd = feedback(Ld,1);
ymodel = step(Gd,t);
figure(2)
plot(t,ym,'b-',t_fit,y_fit,'b*',t,ymodel,'r-');
xlabel('Time [s]'), ylabel('Output [deg]')
fprintf('== Results ==\n')
fprintf('K = %f\n',K_id)
fprintf('T = %f\n',T_id)

Pulse generator setting for Motor Parameter Identification:

Amplitude r
Period r_cyc/0.02
Pulse width r_cyc/0.04
74

APPENDIX B

BLOCK DIAGRAMS FOR SUB-SYSTEMS

DC motor with high-power gear box (HG servo):

Figure B.42. HG Servo Sub-system

Here, signal pin of potentiometer used to detect motor angular position is


connected to analog pin A0 of arduino. Motor red terminal and blue terminal are
connected to digital PWM pins, pin 3 and pin 5, respectively. Saturation blocks
inserted before PWM blocks are set with a minimum value of 0 to a maximum value
of 255.

RC servo motor:

Figure B.43. RC Servo Sub-system


75

In RC servo, the signal pin is connected to digital pin 10 of arduino controller.


To use RC servo in Matlab/Simulink with Arduino I/O, ‘adioes’ program code is
uploaded to arduino controller in such a way from File >> Sketchbook >> ArduinoIO
>> pde >> adioes. The saturation block inserted before RC servo motor sub-system is
set with a minimum value of 0 to a maximum value of 180.

Block diagram for ball position block:

Figure B.44. Ball Position Detection Block Diagram

Block diagram for I-PD+FF controller:

Figure B.45. Block Diagram for I-PD+FF Controller


76

APPENCIX C

PROGRAM CODES FOR PID AND LQR CONTROL EXPERIMENT

PID experiment code for single set point:

close all; clear all; clc;


ts = 1/50;
s = tf('s');
K_theta_rc = (pi/180)*(2/13.5); %[rad/deg]
K_b = (3/5*9.8)*100;
Pb = K_b/s^2;
Pbd = c2d(Pb,ts,'zoh');
[numbd,denbd] = tfdata(Pbd,'v');
omega_n = 1.5;
zeta = 0.6;
alpha = 0.5;
p1 = (-zeta + j*sqrt(1-zeta^2))*omega_n;
p2 = (-zeta - j*sqrt(1-zeta^2))*omega_n;
p3 = -alpha;
Kp = (p1*p2 + p2*p3 +p3*p1)/K_b;
Ki = -p1*p2*p3/K_b;
Kd = -(p1+p2+p3)/K_b;
Kff = Ki/alpha;
disp('>>> PID parameters for Ball and Beam <<<')
fprintf('kp = %f\n',Kp);
fprintf('kd = %f\n',Kd);
fprintf('ki = %f\n',Ki);
fprintf('kff = %f\n',Kff);
r = 20;
77

wf=2*pi*3;
78

Fc = wf^2/(s^2+2*0.7*wf*s+wf^2);
Fd = c2d(Fc,ts,'tustin');
[numlpf,denlpf] = tfdata(Fd,'v');
a = 36.678947;
b = 0.522710;
c = -3.660378;
open_system('constant_bb_pid_rc_sl');
open_system('constant_bb_pid_rc_sl/Ball position');

PID experiment code for two set points:

Codes for two set point PID experiment are the same as single set point except
the reference for ball position:
r1 = 15; r2 = 25;

Pulse generator setting for two set points:

Amplitude r2-r1
Period r_cyc/0.02
Pulse width r_cyc/0.04

Controllability test for LQR control:

K = 202; T = 0.32;
K_b = (3/5*9.8)*100;
K_theta = (pi/180)*(2.1/15);
A = [0 1 0 0 ;
0 0 K_b*K_theta 0 ;
0 0 0 1 ;
0 0 0 -1/T];
B = [0 ; 0; 0; K/T];
co = ctrb(A,B);
unco = length(A)-rank(co);
if (unco==0)
79

fprintf('The system is controllable\n')


fprintf('LQR control method can be used\n')
else
fprintf('The system is uncontrollable\n')
fprintf('LQR control method cannot be used\n')
end

The result in command window shows that the system is controllable.


“The system is controllable
LQR control method can be used”

LQR control experiment code:

clc; close all; clear all;


ts = 1/50;
s = tf('s');
K = 202;
T = 0.32;
K_b = (3/5*9.8)*100;
K_theta = (pi/180)*(2.1/15);
A = [0 1 0 0 ;
0 0 K_b*K_theta 0 ;
0 0 0 1 ;
0 0 0 -1/T];
B = [0 ; 0; 0; K/T];
C = [1 0 0 0];
D = 0;
Pbb = ss(A,B,C,D);
Q = diag([100 20 20 1]);
R = 1000;
F = lqr(Pbb,Q,R)
N = rscale(A,B,C,D,F)
r = 20;
wf = 2*pi*3;
80

Fc = wf^2/(s^2+2*0.7*wf*s+wf^2);
Fd = c2d(Fc,ts,'tustin');
[numlpf,denlpf] = tfdata(Fd,'v');
a = 36.678947;
b = 0.522710;
c = -3.660378;
open_system('bb_lqr_con_sl');
open_system('bb_lqr_con_sl/Scope')

Function to find scale factor:

function[Nbar]=rscale(a,b,c,d,k)
error(nargchk(2,5,nargin));
nargin1 = nargin;
if (nargin1==2),
[A,B,C,D] = ssdata(a);
K = b;
elseif (nargin==5),
A=a; B=b; C=c; D=d; K=k;
else error('Input must be of the form (sys,K) or (A,B,C,D,K)')
end;
s = size(A,1);
Z = [zeros([1,s]) 1];
N = inv([A,B;C,D])*Z';
Nx = N(1:s);
Nu = N(1+s);
Nbar = Nu + K*Nx;

Frequency response plot:


clc; close all; clear all;
ts = 1/50;
s = tf('s');
K = 202; T = 0.32;
K_b = (3/5*9.8)*100;
81

K_theta = (pi/180)*(2.1/15);
A = [0 1 0 0 ;
0 0 K_b*K_theta 0 ;
0 0 0 1 ;
0 0 0 -1/T];
B = [0 ; 0; 0; K/T];
C = [1 0 0 0];
D = 0;
Pbb = ss(A,B,C,D);
Q = diag([100 20 20 1]);
R = 1000;
F = lqr(Pbb,Q,R)
N = rscale(A,B,C,D,F)
Ac = A-B*F; Bc = B*N; Cc = C; Dc = D;
[num den] = ss2tf(Ac,Bc,Cc,Dc);
c_sys = tf(num,den);
figure(1)
rlocus(c_sys)
title('Root Locus of the System')
figure(2)
bode(c_sys)
grid
title('Bode Plot of the System')
figure(3)
nyquist(c_sys)
title('Nyquist Plot of the System')
82

APPENDIX D

CODES FOR GRAPHICAL USER INTERFACE

Codes for graphical user interface with associated functions:


function varargout = bb_gui_model(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @bb_gui_model_OpeningFcn, ...
'gui_OutputFcn', @bb_gui_model_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end

function bb_gui_model_OpeningFcn(hObject, eventdata, handles, varargin)


handles.output = hObject;
bb_radius = 2;
bb_length = 42;
bb_width = 1;
axes(handles.bb_model);
axis equal
axis([-1-2*bb_radius, bb_length+2*bb_radius, -10, 5 + bb_radius]);
83

% Create Ball
v = 2*pi*(0:.05:.95)';
ball_x=bb_radius*sin(v);
ball_y = bb_radius*(1+cos(v));
x = bb_length/2;
patch(ball_x+x,ball_y,'y');
% Create Beam
beam_x = bb_length*[0 1 1 0]';
beam_y = bb_width*[0 0 -1 -1]';
patch(beam_x,beam_y,'c');
% Create Fulcrum
pivot_x = [0 0 0.5 0.5]';
pivot_y = [-bb_width -8.5 -8.5 -bb_width]';
patch(pivot_x+(x-0.25),pivot_y,3);
block_x = [0 0 4 4]';
block_y = [-8.5 -10 -10 -8.5]';
patch(block_x+(x-2),block_y,3);
piv_x = 0.5*sin(v);
piv_y = 0.5*(1+cos(v));
x = bb_length/2;
patch(piv_x+x,piv_y-bb_width,'r');
%Create motor
motor_x = [0 0 4 4]';
motor_y = [-8 -10 -10 -8]';
patch(motor_x+(3/4)*bb_length,motor_y+0.5,'k');
motorb_x = [0 0 5 5]';
motorb_y = [-9.5 -10 -10 -9.5]';
patch(motorb_x+((3/4)*bb_length)-0.5,motorb_y,'c');
%Create arm
armxbar = [0 0 0.7 0.7]';
armybar = [-bb_width -8.5 -8.5 -bb_width]';
patch(armxbar+(3/4)*bb_length+3,armybar,3);
arm_x = [0 0 3 3]';
arm_y = [0 0.5 0.5 0]';
84

patch(arm_x+(3/4)*bb_length+0.5,arm_y-8.7,'r');
xcen = 0.3*sin(v);
ycen = 0.3*(1+cos(v));
patch(xcen+(3/4)*bb_length+2,ycen-8.7,'y');
guidata(hObject, handles);

function varargout = bb_gui_model_OutputFcn(hObject, eventdata, handles)


varargout{1} = handles.output;

function ballpos_slider_Callback(hObject, eventdata, handles)


global bbSetpoint;
sliderValue = get(handles.ballpos_slider,'Value');
sliderValue = sliderValue*42;
bbSetpoint = round(sliderValue);
set(handles.ballpos_text,'String',num2str(bbSetpoint));
guidata(hObject,handles);

function ballpos_slider_CreateFcn(hObject, eventdata, handles)


if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

function ballpos_setbutton_Callback(hObject, eventdata, handles)


global bbSetpoint;
switch get(get(handles.method_button,'SelectedObject'),'Tag')
case 'pid_button'
set_param('bb_pid_con_sl/Set Point','Value',num2str(bbSetpoint));
case 'lqr_button'
set_param('bb_lqr_con_sl/Set Point','Value',num2str(bbSetpoint));
otherwise
end
guidata(hObject,handles);

function open_button_Callback(hObject, eventdata, handles)


85

switch get(get(handles.method_button,'SelectedObject'),'Tag')
case 'pid_button'
open_system('bb_pid_con_sl')
case 'lqr_button'
open_system('bb_lqr_con_sl')
otherwise
end
guidata(hObject,handles);

function plot_button_Callback(hObject, eventdata, handles)


t_val = evalin('base','yout.time');
y_val = evalin('base','yout.signals.values');
axes(handles.bb_plot)
plot(t_val,y_val);
axis([0 100 0 40])
legend('Desired Set Point','Actual Ball Position')

function clear_button_Callback(hObject, eventdata, handles)


cla(handles.bb_plot,'reset')
guidata(hObject,handles);

function reset_button_Callback(hObject, eventdata, handles)


set(handles.ballpos_slider,'Value',0);
set(handles.ballpos_text,'String','0');
guidata(hObject,handles);

You might also like