myrio - murdoch research repository...myrio has two identical expansion ports called mxp a & b....
TRANSCRIPT
MyRIO
ENG470 – ENGINEERING HONOURS THESIS
SUPERVISOR: ASSOCIATE PROFESSOR GRAEME COLE
SCHOOL OF ENGINEERING AND INFORMATION TECHNOLOGY
NUWAN KATHALUWA LIYANAGE
BACHELOR OF ENGINEERING
(INSTRUMENTATION AND CONTROL AND INDUSTRIAL COMPUTER SYSTEMS)
2016
ii
Disclaimer
I declare the following work to be my own, unless otherwise referenced, as defined by Murdoch
University’s Plagiarism and Collusion Assessment Policy.
iii
MURDOCH UNIVERSITY
ABSTRACT
MyRIO
By Nuwan Kathaluwa Liyanage
The National Instruments MyRIO is an embedded control device manufactured for students to integrate
real-world engineering problems. The device offers a broad range of functions and capabilities. MyRIO
provides a perfect platform for Industrial Computer System Engineering students to expand what they
have learned throughout their degree. National Instruments has dedicated a lot of their resources to
improve and provide support for their hardware. MyRIO makes it much easier to learn, understand and
implement projects in such a short period. A lot of help is provided in various ways, such as inbuilt,
online and learning sessions to improve the understanding of the device.
The main objectives of the thesis project involve learning and understanding the MyRIO device, creating
learning guides to future students and integrate the device into an existing embedded application.
This report describes the hardware and software capabilities of MyRIO and how to integrate the device
to control an inverted pendulum. The inverted pendulum apparatus is at Engineering and Energy
Building 220 at Murdoch University. The device itself has been constructed from an old sliding door
mechanism. There are a few sensors integrated into the apparatus to feedback information, such as an
"Absolute Encoder" and "Incremental Encoder". The first part of the thesis is dedicated to
understanding the various components and how they work. This gives a better understanding of the
broader picture rather than isolating the issues.
An Inverted pendulum dynamic model is derived. The dynamic model is used to design and simulate the
Linear Quadratic Regulator controller in Matlab. The same controller is implemented and commissioned
in MyRIO to control the inverted pendulum apparatus. There were two parts to the control of the
inverted pendulum. Only balance control is performed and commissioned due to various constraints.
The swing-up control is recommended as future work.
Overall the project is a success, and most of the project objectives completed. For reasons that are
explained, the time taken to learn, understand and implement inverted pendulum control was far
greater than expected. Problems encountered can be narrowed down to some of the hardware issues
faced and minimal support from the vendors. The main achievement is the next student does not have
to go through the tedious exercise to gather valuable information to implement the inverted pendulum
apparatus. All this information is well documented and available for future students.
iv
Table of Contents 1 Introduction .......................................................................................................................................... 1
2 MyRIO .................................................................................................................................................... 2
2.1 About the device ........................................................................................................................... 2
2.2 FPGA .............................................................................................................................................. 5
3 Embedded devices ................................................................................................................................ 6
4 Inverted Pendulum ................................................................................................................................ 8
4.1 Background.................................................................................................................................... 8
4.2 Pendulum Cart Modelling and Dynamics ...................................................................................... 9
4.2.1 Lagrangian Dynamic Analysis ................................................................................................ 9
4.2.2 Linearization and Transfer Function Generation ................................................................ 13
4.2.3 State Space Representation ................................................................................................ 16
4.3 Apparatus .................................................................................................................................... 17
4.3.1 Absolute Encoder ................................................................................................................ 17
4.3.2 Incremental Encoder ........................................................................................................... 17
4.3.3 Reed Switches ..................................................................................................................... 18
4.3.4 SSI ........................................................................................................................................ 18
4.3.5 Gray code ............................................................................................................................ 19
4.3.6 H-Bridge ............................................................................................................................... 20
5 Controller Design ................................................................................................................................. 22
5.1 Introduction ................................................................................................................................ 22
5.2 Linear Quadratic Regulator (LQR) ............................................................................................... 22
5.3 Calculation of State Space Matrices ............................................................................................ 24
5.4 LQR Controller in Matlab ............................................................................................................. 25
6 System Implementation ...................................................................................................................... 27
6.1 Background.................................................................................................................................. 27
6.2 Implementation of Absolute Encoder ......................................................................................... 29
6.3 Implementation of Incremental Encoder .................................................................................... 32
6.4 Implementation of Motor Control .............................................................................................. 33
6.5 Implementation of Discrete LQR ................................................................................................. 35
7 Conclusion ........................................................................................................................................... 38
7.1 Project review ............................................................................................................................. 38
7.2 Recommendation for future work .............................................................................................. 38
8 References ........................................................................................................................................... 39
9 Appendix ............................................................................................................................................. 41
9.1 Appendix A – Drawings ............................................................................................................... 41
9.2 Appendix B – Matlab Scripts ....................................................................................................... 43
v
List of Figures
Figure 1 - MyRIO Hardware Architecture (Instruments n.d.) ........................................................................ 2
Figure 2 - MXP A & B Pin connection (Instruments n.d.) .............................................................................. 3
Figure 3 - MXP AI Connector Topology (Instruments n.d.) ........................................................................... 3
Figure 4 - MXP AO Connector Topology (Instruments n.d.) .......................................................................... 4
Figure 5 - DIO Lines 0 - 13 Connection (Instruments n.d.) ............................................................................ 4
Figure 6 - DIO Lines 14 & 15 Connection (Instruments n.d.) ........................................................................ 4
Figure 7 - The Different Parts of a FPGA (W. v. Commons 2009) .................................................................. 5
Figure 8 - MyRIO FPGA Processor (Instruments n.d.) ................................................................................... 6
Figure 9 - Pull-down Resistor ........................................................................................................................ 7
Figure 10 - Pull-up Resistor ........................................................................................................................... 7
Figure 11 - A Segway (W. Commons, commons.wikimedia.org n.d.) ........................................................... 8
Figure 12 - SpaceX Grasshopper rocket (W. Commons, commons.wikimedia.org n.d.) .............................. 9
Figure 13 - State Diagram of Pendulum and Cart ....................................................................................... 10
Figure 14 - Pole-Zero Map and Bode Diagram for system in inverted position .......................................... 15
Figure 15 - Incremental Encoder Pulse Diagram (Controls 2003) ............................................................... 17
Figure 16 - Simple SSI Block Diagram .......................................................................................................... 18
Figure 17 - SSI Timing Diagram (W. Commons, en.wikipedia.org n.d.)....................................................... 19
Figure 18 - H-Bridge Configuration (Tantos 2011) ...................................................................................... 20
Figure 19 - H-Bridge Operation Modes (Tantos 2011) ................................................................................ 21
Figure 20 - State Feedback System ............................................................................................................. 23
Figure 21 - Step Response of Inverted Pendulum with 'dlqr' in Matlab q1=1, q3=1 and R=1 .................... 26
Figure 22 - Step Response of Inverted Pendulum with 'dlqr' in Matlab q1=1+E5, q3=1+E5 and R=0.01 ... 26
Figure 23 - Pendulum Cart System Block Diagram ...................................................................................... 27
Figure 24 - Overall Connection Diagram ..................................................................................................... 28
Figure 25 - Absolute Encoder Attached to the Pendulum .......................................................................... 29
Figure 26 - MyRIO I/O Monitoring Module ................................................................................................. 31
Figure 27 - Differential Clock and Data Signals in Digital Oscilloscope ....................................................... 31
Figure 28 - Mounting Arrangement for Incremental Encoder .................................................................... 32
Figure 29 - Encoder Express VI .................................................................................................................... 33
Figure 30 - KAG 24V DC Motor with worm gear ......................................................................................... 33
Figure 31 - 36V Dual 15A H-Bridge Motor Driver ........................................................................................ 34
Figure 32 - Software Motor Speed and Direction Control .......................................................................... 34
Figure 33 - LabVIEW "LQR VI" under State Feedback Design palette ......................................................... 35
Figure 34 - LabVIEW "CD Linear Quadratic Regulator (Discretised LQR)” .................................................. 36
Figure 35 - Comparison of Process Variable and Manipulated Variable (dt =0.1s, q1=10, q3=10 & R=1).. 36
Figure 36 - Comparison of Process Variable and Manipulated Variable (dt =0.01s, q1=100, q3=1+E5 &
R=0.1) .......................................................................................................................................................... 37
Figure 37 - Comparison of Process Variable and Manipulated Variable (dt =0.001s, q1=100, q3=1+E5 &
R=0.03) ........................................................................................................................................................ 37
Figure 38 - Comparison of Process Variable and Manipulated Variable (dt =0.001s, q1=100, q3=1+E5 &
R=0.01) ........................................................................................................................................................ 37
vi
List of Tables
Table 1 - Inverted Pendulum System Model Parameters ........................................................................... 24
Table 2 - Performance Criterion for Inverted Pendulum ............................................................................ 25
Table 3 - SICK ARS60-A4A08192 Pin Configuration ..................................................................................... 30
vii
ACKNOWLEDGMENTS
I would like to express my gratitude to my academic supervisor; Associate Professor Graeme R. Cole for
his continuous support on this thesis project and the support provided throughout my degree at
Murdoch University. I would like to thank the following individuals for their support throughout my
thesis: Mr. Iafeta Laava and Mr. Will Stirling.
I would like to thank my wife Rebecca Liyanage for her patience, love and support throughout my
degree, without her I wouldn’t be able to make this journey for the last six years. I would like to thank
my parents for their guidance, love and support.
Finally, a word of appreciation goes out to all my friends at the University of Murdoch and special thanks
to Mr. Andy Stevenson from work to encouraging me to take part in this degree.
viii
Glossary
AI – Analog Input
AO – Analog Output
ASICs – Application Specific Integrated Circuits
CLK - Clock
DIO – Digital Input & Output
DAC – Digital to Analog Converter
DTA – Data
EMF – Electromagnetic force
EMI – Electromagnetic Interference
FPGA – Field Programmable Gate Array
IGBT - Insulated gate bipolar transistors
LSB – Least Significant Bit
LQR – Linear Quadratic Regulator
MISO – Master in Slave out
MOSI – Master out Slave in
MSB – Most Significant Bit
PPR – Pulses per revolution
PWM – Pulse Width Modulation
RFI – Radio Frequency Interference
SSI - Synchronous Serial Interface
SPI - Serial Peripheral Interface
OTP – One Time Programmed
1
1 Introduction This thesis is concerned with the new LabVIEW embedded development environments. Embedded
systems are used in lot of devices we use from day to day. Embedded systems are used to control many
devices such as mobile phones, laptops, and modern calculators. Most of the microprocessors are part
of the integrated system. The National Instruments MyRIO is an embedded device purely designed for
educational purposes (Instruments n.d.). The device allows students to develop real world problems on
a real-time target. The device consists of two processors for real-time operation and a Field
Programmable Gate Array for reconfigurable I/O. The LabVIEW design software can be used within both
processors and the FPGA to develop real-time applications. The device offers a broad range of functions
and capabilities. MyRIO provides a perfect platform for Industrial Computer System Engineering
students to expand what they have learned throughout their degree. National Instruments has
dedicated a lot of their resources to improving and providing support for their hardware. More help
makes it much easier to learn, understand and implement projects in such a short period. There are a lot
of aids built to help in various ways, such as inbuilt, online and learning sessions to improve the
understanding of the device.
The main objectives of the thesis project involved learning and understanding the MyRIO device, create
learning guides for future students and integrate the device into an existing embedded application.
The following sections of this report describe the hardware and software capabilities of MyRIO. The
inverted pendulum apparatus located at Engineering and Energy Building 220 in Murdoch University has
been chosen as the application to integrate MyRIO. An inverted pendulum is an ideal apparatus to apply
control theory, learn about different sensors and communication protocols. The apparatus itself has
constructed off an old sliding door mechanism. There are sensors incorporated into the apparatus to
prude feedback information, such as an "Absolute Encoder" and "Incremental Encoder".
An inverted pendulum dynamic model is derived. The dynamic model is used to design and simulate the
Linear Quadratic Regulator controller in Matlab. The same controller is implemented and commissioned
in MyRIO to control the inverted pendulum apparatus.
The following sections describe in detail the inverted pendulum control design and system
implementation undertaken as part of this thesis.
2
2 MyRIO
2.1 About the device The National Instruments MyRIO is a portable reconfigurable I/O device made purely for educational
purposes. It is better to understand the functional capabilities of MyRIO prior to operating. There are
predefined voltages and currents which may compromise the safety of the device if misused.
MyRIO provides analog inputs (AI), analog outputs (AO), digital input and outputs (DIO), audio and
power outputs in the same device. MyRIO connects to a host computer through USB or wireless
802.11b,g,n (Liptak 2002).
A block diagram of the Hardware architecture shown in Figure 1 below. This diagram outlines all the
inputs, outputs, and functions connected to the device. There are two main processors, which are a real-
time processor and a Field Programmable Gate Array (FPGA) processor. These processors when
combined handle 40 DIO, eight single ended AI, four single ended AO, two differential AI and two
ground reference AO. It has built-in specific communication protocols such as SPI, I2C, and UART
(Instruments n.d.). The device supports PWM, interrupts and quadrature encoder inputs (Instruments
n.d.).
Figure 1 - MyRIO Hardware Architecture (Instruments n.d.)
3
MyRIO has two identical expansion ports called MXP A & B. The signals within the port are differentiated
by software, as an example connector A/DIO 1 and connector B/DIO 1. The following Figure 2 below
shows the pinout for both MXP connectors.
Figure 2 - MXP A & B Pin connection (Instruments n.d.)
There are analog input channels on MyRIO Expansion Port (MXP) connectors A and B. The analog inputs
are multiplexed to a single analog to digital converter that samples all channels. An analog input is
sampled at 500kS/s and it has a resolution of 12 bits. MXP has four single-ended analog input channels
per connector. Figure 3 below show the topology.
Figure 3 - MXP AI Connector Topology (Instruments n.d.)
There are also analog output channels on MyRIO MXP connectors A and B. Each analog output channel
has a dedicated digital to analog converter (DAC). The DACs for the analog output channels are
controlled by two serial communication buses. MXP connectors A and B share one bus. An analog
output is sampled at 345kS/s and it has a resolution of 12 bits. Each connector has two analog outputs.
Figure 4 below shows the topology.
4
Figure 4 - MXP AO Connector Topology (Instruments n.d.)
MyRIO has 3.3V general purpose DIO lines on the MXP connectors. There are 16 DIO lines per connector
and lines 0 to 13 have a 40 kΩ pull-up resistor connected to 3.3V (Refer to Figure 5). Lines 14 and 15
have 2.2 kΩ pull-up resistors connected to 3.3V (Refer to Figure 6). All lines can be programmed either
as inputs or outputs. Secondary functions include SPI, I2C, PWM and quadrature encoder inputs.
Figure 5 - DIO Lines 0 - 13 Connection (Instruments n.d.)
Figure 6 - DIO Lines 14 & 15 Connection (Instruments n.d.)
5
2.2 FPGA Field programmable gate arrays (FPGAs) are integrated circuits equipped with a matrix of reconfigurable
logic blocks. These logic blocks have programmable interconnects. FPGAs can be configured to desired
application or functionality after manufacturing. This technology was invented by Xilinx co-founders
Ross Freeman and Bernard Vonderschmitt in 1985 (PROGRAMMABLE n.d.). The main advantage that
FPGAs have over Application Specific Integrated Circuit (ASICs) is the FPGA can change its design any
time the user desires. There are one-time programmable (OTP) FPGAs available but most popular are
SRAM-based, which can be re-programmed. FPGAs are parallel in the way they are build, so the
processing operations do not have to compete for the same assets. The tasks perform by a dedicated
section within the FPGA, and this eliminates the interaction within the same logic blocks.
True parallelism is critical in applications such as controls, robotics, and other mechatronics applications.
Parallelism gives the ability to achieve faster timing as little as 25ns (40MHz). Error! Reference source
not found. Error! Reference source not found. shows the different parts of an FPGA.
Figure 7 - The Different Parts of a FPGA (W. v. Commons 2009)
The MyRIO comes with a built-in FPGA, which can be programmed through the LabVIEW FPGA Module.
The LabVIEW FPGA Module is entirely graphical and comes with a fully automated compiler. The FPGA
personality can be designed using built-in VIs. Once the personality is created, LabVIEW generates the
VHDL files required by the Xilinx compiler to perform the compilation. This creates a bitfile that needs to
be downloaded to FPGA’s flash memory. The next time the FPGA runs, the bitfile gets uploaded and
reconfigures itself to a new configuration. Figure 8 below shows the FPGA processor in MyRIO.
6
Figure 8 - MyRIO FPGA Processor (Instruments n.d.)
3 Embedded devices When interfacing embedded devices to other systems, care should be taken to protect the device itself
and the system that you are connecting to. This includes the type of measurement systems and
necessary circuitry to achieve the desired signal.
There are two types of signals supported by MyRIO, which is single ended, and differential signals.
Differential signals consist of two wires; they are signal HI and signal LO. Neither of them are referenced
to ground. Differential systems require two inputs for each channel and are preferred as they reject
common mode noise. Differential systems should be used where electromagnetic interference (EMI) or
radio frequency interference (RFI) is present.
Single ended signals consist of a single wire. Single ended is the most common method in data
acquisition devices. The single ended input has no common mode range, the reason being that all the
channels share the common ground as the return path. Single ended systems can be low in cost and
provide twice the number of inputs.
The most common technique in place is to connect either pull-up resistor or pull-down resistor. Isolators
can be used to isolate the two systems completely.
Pull-down resistors are connected between the input signal and ground. Pull-down resistor pulls the
signal to logic low level. The resistor must be larger than the input impedance of the logic circuit. If the
latter applies the voltages maybe pulled down all the time and make a logic low value. Figure 9 below
shows how the pull-down resistor is typically connected.
7
Vcc
S1Switch
R14.7k
GND
MCU
Figure 9 - Pull-down Resistor
Pull-up resistors are used to pull the signal to a high state when there is no connection. In this instance,
the input pin will read the supply voltage (Vcc). When the switch is on the input pin will read zero
voltage. Pull-up resistors are generally about 10kΩ and should be at least 1/10 of the input impedance.
The Figure 10 below shows how the pull-up resistor is typically connected.
Vcc
Button R1
GND
R2
Input Pin
MCU
GND
Figure 10 - Pull-up Resistor
8
4 Inverted Pendulum
4.1 Background A person balancing a stick on the palm of their hand is a practical example of an inverted pendulum. To
keep the stick upright, the person needs to move their hand back and forth so the stick does not fall
over. Any systems that have such characteristics can be related to an inverted pendulum.
The inverted pendulum used in this report only has one degree of freedom, unlike the stick balanced on
the palm. The pendulum is attached to an absolute encoder and encoder is mounted on a cart, which
sits on the track. The pendulums rotational movement is fed back via the absolute encoder. The position
of the pendulum is controlled via a 24VDC motor and its position is fed back through the incremental
encoder.
Since an inverted pendulum is naturally unstable, it is quite popular in control engineering. It is an
unstable system which students can apply their control theory to gain practical knowledge. There are
many variations of the system. The pendulum can be attached to a cart, motor, double inverted and sits
in a sea saw are few examples (Nise 2011). Regardless of its build, the problem stays the same; it will be
unstable, and therefore, some form of control needs to be implemented to control the pendulum
upright.
Examples of Industrial products based on inverted pendulum are the "Segway" and the rocket system
"Grasshopper" from SpaceX. Segway is a self-balancing, two-wheeled vehicle where the driver can stand
and ride. The Grasshopper is a rocket, and the rocket body acts as a pendulum arm trying to keep
upright (Refer Figure 11 & Figure 12 below).
Figure 11 - A Segway (W. Commons, commons.wikimedia.org n.d.)
9
Figure 12 - SpaceX Grasshopper rocket (W. Commons, commons.wikimedia.org n.d.)
4.2 Pendulum Cart Modelling and Dynamics For understanding the dynamics of pendulum cart model, it is necessary to analyse the system
theoretically. State space equations were derived using Lagrangian approach. Equations were linearized
only in upright position. The following sections describe the derivation of model and transfer functions.
Then the transfer functions are analysed. This section delivers a detailed explanation of the theoretical
approach to the system.
4.2.1 Lagrangian Dynamic Analysis
The following steps have been taken to derive the Lagrangian Dynamics. First, choose generalized
coordinates, derive expressions for the forces, energy functions and Lagrangian (Friedland 1986). The
final step is to derive equations of motion.
The following Figure 13 shows the state diagram for the pendulum cart system. This model and the
coordinate system will be used to analyse the system.
The Model includes:
Cart mass: M
Pendulum mass: m
Pendulum Inertia: I
Rotational and translational viscous damping coefficients: b and c
10
l
X
M
b
c
q
I, m
f(t)
i
j
Figure 13 - State Diagram of Pendulum and Cart
Since the pendulum cart system has two degrees of freedom this can be represent by two generalized
coordinates. The generalized coordinates are chosen as the horizontal displacement as x, and the
rotational displacement of the pendulum θ.
𝜉𝑗: 𝑥, 𝜃 (1)
The positive direction of the cart x is chosen to be to the right and positive direction of angular
displacement θ to be clockwise.
𝜉𝑗: 𝛿𝑥, 𝛿𝜃 (2)
Generalised Forces
An expression for the generalized forces ∈𝑗:
𝛿𝑊𝑛𝑐 = ∑𝑓𝑛𝑐
𝑁
𝑖=1
∗ 𝛿𝑅𝑖 = ∑ ∈𝑗 𝛿𝜉𝑗
𝑛
𝑗=1
(3)
Where 𝑅𝑖 is the position vector where the ith non-conservative force acts. The non-conservative forces,
in this case, result from the input force and the system damping (Friedland 1986).
𝛿𝑊𝑛𝑐 = 𝑓(𝑡)𝛿𝑥 − 𝑐𝛿𝑥 − 𝑏𝛿𝜃 (4)
Comparing equations (3) and (4)
∈𝑥= 𝑓(𝑡) − 𝑐 (5)
∈𝜃= −𝑏 (6)
11
Kinetic and Potential Energy Functions
The kinetic energy function for the cart mass (Friedland 1986):
𝑇𝑀
∗ = 1
2𝑀2
(7)
The kinetic energy function for the pendulum (Friedland 1986):
𝑇𝑚
∗ = 1
2𝑚𝑣𝑐 ∗ 𝑣𝑐 +
1
2𝐼𝜔2
(8)
I is for pendulum moment of inertia and 𝑣𝑐 is for pendulum velocity (Friedland 1986).
𝑟𝑐 = (𝑥 − 𝑙 sin 𝜃) − 𝑙 cos 𝜃 𝑗 (9)
𝑣𝑐 =
𝑑𝑟𝑐𝑑𝑡
= (𝑥 − 𝑙 cos 𝜃)𝑖 − 𝑙 sin 𝜃 𝑗 (10)
The pendulum angular velocity is 𝜔:
𝜔 = (11)
Substitute equations (10) and (11) into equation (8)
𝑇𝑚
∗ = 1
2𝑚(2 − 2𝑙 (cos 𝜃) + 𝑙2 (cos2 𝜃) 2 + 𝑙2 (sin2 𝜃) 2) +
1
2𝐼2
(12)
This simplifies to:
𝑇𝑚
∗ = 1
2𝑚(2 − 2𝑙 (cos 𝜃) + 𝑙22) +
1
2𝐼2
(13)
The total kinetic energy is:
𝑇∗ = 𝑇𝑀
∗ + 𝑇𝑚∗ =
1
2𝑀2 +
1
2𝑚(2 − 2𝑙 (cos 𝜃) + 𝑙22) +
1
2𝐼2
(14)
The cart only moves in the horizontal direction, therefor the potential energy is determined by the
following equation.
𝑉 = −𝑚𝑔𝑙 cos 𝜃 (15)
12
Lagrangian
From the kinetic and potential energy functions, the Lagrangian is given by (Friedland 1986):
𝐿 = 𝑇∗ − 𝑉 (16)
Using equations (14) and (15), the Lagrangian can be written as:
𝐿 =
1
2𝑀2 +
1
2𝑚(2 − 2𝑙 (cos 𝜃) + 𝑙22) +
1
2𝐼2 + 𝑚𝑔𝑙 (cos 𝜃)
(17)
Lagrange’s Equation
State equations can be created using Lagrange’s equation (Friedland 1986):
𝑑
𝑑𝑡 (
𝜕𝐿
𝜕𝜉𝑗) −
𝜕𝐿
𝜕𝜉𝑗= ∈𝑗
(18)
The equation for x is:
𝑑
𝑑𝑡 (
𝜕𝐿
𝜕) −
𝜕𝐿
𝜕𝑥= ∈𝑥
(19)
Using equation (17) and calculating the partial derivatives:
𝑑
𝑑𝑡(𝑀 + 𝑚 − 𝑚𝑙 (cos 𝜃) ) − 0 = 𝑓(𝑡) − 𝑐
(20)
(𝑀 + 𝑚) − 𝑚𝑙 (cos 𝜃) + 𝑚𝑙 (sin𝜃) 𝜃2 = 𝑓(𝑡) − 𝑐 (21)
The equation for θ is:
𝑑
𝑑𝑡 (
𝜕𝐿
𝜕) −
𝜕𝐿
𝜕𝜃= ∈𝜃
(22)
Using equation (17) and calculating the partial derivatives:
𝑑
𝑑𝑡(−𝑚𝑙 cos 𝜃 + 𝑚𝑙2 + 𝐼) − (𝑚𝑙 (sin 𝜃) − 𝑚𝑔𝑙 sin𝜃) = −𝑏
(23)
(𝑚𝑙2 + 𝐼) − 𝑚𝑙 cos𝜃 + 𝑚𝑙 (sin 𝜃) − 𝑚𝑙 (sin 𝜃) + 𝑚𝑔𝑙 sin𝜃 = −𝑏 (24)
The system state equations simplify to:
(𝑀 + 𝑚) + 𝑐 − 𝑚𝑙 (cos𝜃) + 𝑚𝑙 (sin𝜃) 𝜃2 = 𝑓(𝑡) (25)
(𝑚𝑙2 + 𝐼) − 𝑚𝑙 cos𝜃 + 𝑏 + 𝑚𝑔𝑙 sin𝜃 = 0 (26)
13
4.2.2 Linearization and Transfer Function Generation
System Analysis and Control design techniques may only be applied to linear systems and for this reason
equations (25) and (26) must be linearized. Since the system only controls the vertically upward
position, the equations will be linearized at 𝜃 = 𝜋. Assume that the system stays within a small margin
of this linearized region. Let 𝜃0 show the deviation of the pendulum position from the upward position.
𝜃 = 𝜃0 + 휀 (27)
= 휀 (28)
From the Taylor Series expansion (Stewart 2012)
𝑓(𝜃) ≈ 𝑓( 𝜃0) + 휀
𝑑𝑓
𝑑𝜃|
(29)
Because higher order terms are neglected,
휀2 ≈ 0 (30)
Pendulum Up (𝜃 = 𝜋)
For (𝜃 = 𝜋), equation (29) yields to first order
cos 𝜃 ≈ cos𝜋 + (𝜋 − 𝜃)(−sin𝜋) = −1 sin𝜃 ≈ sin𝜋 + (𝜋 − 𝜃)(cos 𝜋) = 𝜋 − 𝜃
(31)
New coordinate is introduced to simplify the analysis.
𝜃′ = 𝜃 − 𝜋 (32)
This will measure 𝜃 clockwise from the pendulum’s upward position and
sin𝜃 ≈ 𝜋 − (𝜃′ + 𝜋) = −𝜃′ (33)
The analysis continues from the new reference point and 𝜃′ will be written as 𝜃 from here on in. Note
also that
′ = 𝑎𝑛𝑑 𝜃′ = (34) This substitution does not have any other effects on the state equations.
By substituting these equations into state equations, we get the following:
(𝑀 + 𝑚) + 𝑐 + 𝑚𝑙 = 𝑓(𝑡)
(𝑚𝑙2 + 𝐼) + 𝑚𝑙 + 𝑏 − 𝑚𝑔𝑙𝜃 = 0
(35)
14
Taking the Laplace Transform
(𝑀 + 𝑚)𝑠2𝑋(𝑠) + 𝑐𝑠𝑋(𝑠) + 𝑚𝑙𝑠2∅(𝑠) = 𝐹(𝑠)
(𝑚𝑙2 + 𝐼)𝑠2∅(𝑠) + 𝑚𝑙𝑠2𝑋(𝑠) + 𝑏𝑠∅(𝑠) − 𝑚𝑔𝑙∅(𝑠) = 0
(36)
By substituting to eliminate either X(s) or φ(s) gives the following:
𝐺1(𝑠) =
𝑋(𝑠)
𝐹(𝑠)=
𝑎2𝑠2 + 𝑎1𝑠 + 𝑎0
𝑏4𝑠4 + 𝑏3𝑠
3 + 𝑏2𝑠2 + 𝑏1𝑠
𝐺2(𝑠) =∅(𝑠)
𝐹(𝑠)=
𝑐2𝑠
𝑏4𝑠3 + 𝑏3𝑠
2 + 𝑏2𝑠 + 𝑏1
(37)
Where
𝑎0 = −𝑚𝑔𝑙
𝑎1 = 𝑏
𝑎2 = 𝑚𝑙2 + 𝐼
𝑏1 = −𝑚𝑔𝑙𝑐
𝑏2 = −(𝑀 + 𝑚)𝑚𝑔𝑙 + 𝑏𝑐
𝑏3 = (𝑀 + 𝑚)𝑏 + (𝑚𝑙2 + 𝐼)𝑐
𝑏4 = (𝑀 + 𝑚)(𝑚𝑙2 + 𝐼) − 𝑚2𝑙2
𝑐2 = −𝑚𝑙
16
4.2.3 State Space Representation
The linear equations need to be converted to state space model (Stewart 2012) form as follow:
= 𝐴𝑥 + 𝐵𝑢 (38)
𝑦 = 𝐶𝑥 + 𝐷𝑢 (39)
Change in variable notation for state vector x is:
𝑥 = [
𝑥1
𝑥2
𝑥3
𝑥4
] = [
𝑥𝜃
]
(40)
From the variable definitions:
1 = 𝑥2 3 = 𝑥4
(41)
The result in matrix form is given by:
𝑑
𝑑𝑡 [
𝑥1
𝑥2
𝑥3
𝑥4
] =
[ 0 1 0 0
0−(𝐼 + 𝑚𝑙2)𝑏
𝐼(𝑀 + 𝑚) + 𝑀𝑚𝑙2𝑚2𝑔𝑙2
𝐼(𝑀 + 𝑚) + 𝑀𝑚𝑙20
0 0 0 1
0−𝑚𝑙𝑏
𝐼(𝑀 + 𝑚) + 𝑀𝑚𝑙2𝑚𝑔𝑙(𝑀 + 𝑚)
𝐼(𝑀 + 𝑚) + 𝑀𝑚𝑙20]
[
𝑥1
𝑥2
𝑥3
𝑥4
]
+
[
0𝐼 + 𝑚𝑙2
𝐼(𝑀 + 𝑚) + 𝑀𝑚𝑙2
0𝑚𝑙
𝐼(𝑀 + 𝑚) + 𝑀𝑚𝑙2]
𝑓(𝑡)
(42)
𝑑
𝑑𝑡[𝑥1
𝑥3] = [
1 0 0 00 0 1 0
] [𝑥1
𝑥3] + [
00] [
𝑥1
𝑥3] (43)
17
4.3 Apparatus
4.3.1 Absolute Encoder
An absolute encoder (Controls 2003) produces the position of a rotating device. The rotating device
could be a motor, axle, drive shaft or a device in this nature. The encoder generates information about
the position, angle and number of revolutions depending on the type of absolute encoder. There are
two types of absolute encoders, and they are single turn and multi-turn (Controls 2003). A single-turn
encoder measures a full 360° angle. As a multi-turn encoder measure angles beyond 360° and some
revolutions. These encoders produce a code depending on their position. The number of codes existing
for an encoder depends on its resolution. For an example, an encoder with 213-bit resolution can
measure 8192 different positions.
Absolute encoders do not lose their position data in an event of power loss. Such a device can switch
power on and off and still retain its position. Absolute encoders are classed as high precision, robust and
flexible encoders (Controls 2003).
4.3.2 Incremental Encoder
Incremental encoders (Controls 2003) provide an angle, position and number of revolutions of a rotating
device. The incremental encoders do not retain their position in the event of power loss and cannot be
used where position data are critical in a case of power loss. Incremental encoders utilize a specific
number of pulses per revolution (PPR). Figure 15 below shows the typical channels available within an
incremental encoder. Channels A and B are 90° out of phase. The phase helps to measure the speed and
direction of a rotating device. Where the direction does not matter, a single channel can be used to
measure the angle. Counting both rising and falling edges will increase its resolution.
Incremental encoders indicate their position and they use counters within the electronic hardware to do
this. Since an incremental encoder does not retain its position in an event of power loss, the device must
start from a known position to initialize the counter. Some encoders utilize a third channel called the
"marker, index or Z channel" (Controls 2003).
Figure 15 - Incremental Encoder Pulse Diagram (Controls 2003)
18
4.3.3 Reed Switches
A Reed switch (Graf 1999) utilizes a magnetic field to operate, and these can be either normally open or
normally closed. Depending on the switch it will be activated when a magnetic field is applied. When a
magnetic field is present, it will either send or stop an electrical current to the receiving device.
4.3.4 SSI
Synchronous Serial Interface (SSI) (Fraba n.d.) is an industry standard communication protocol using a
master and slave configuration. SSI utilizes point to point connection between a master and slave. The
slave updates position data to the master as soon as it receives a train of clock pulses. When the Least
significant bit (LSB) is transmitted, the slave holds the data for the pre-assigned time. When this time
has passed, the new position data is updated to master. SSI is used in industrial applications and is
mostly seen in rotary encoders. Figure 16 below shows a typical SSI block diagram.
Clock (CLK)
Data (DTA)
MyRIOMaster
Slave(Encoder)
Figure 16 - Simple SSI Block Diagram
SSI utilizes the RS-422 communication standard to send and receive data between master and slave. The
RS-422 standard also known as ANSI/TIA/EIA-422B (Fraba n.d.). The Clock (CLK) and Data (DTA) signals
are transmitted using balanced or differential signalling, for example the CLK and DTA lines utilizes
twisted pair cables. The main advantage of differential signalling is its reduced susceptibility to noise and
electromagnetic interference (EMI), mainly in industrial situations and over longer transmission lengths.
As per Figure 16, SSI has a simple design with two pairs of wire, one for CLK and one for DTA. The clock
signal is generated by the master when needed and the slave updates the position accordingly. SSI
devices support different clock frequencies ranging from 100 kHz to 2 MHz, and the number of clock
pulses depends on the number of data bits.
The data is transmitted as a single data word with the most significant bit (MSB) first. Typically single
turn rotary encoders use 13 bits of data to transmit their position, which is one revolution. The multi-
turn encoders use 25 bits of data to send their position.
19
Figure 17 below illustrates the SSI data transmission and its timing structure. There are some keywords
used within this diagram, and they are:
‘tm’ – The minimum time required by the slave to realize the transmission is complete (Fraba n.d.).
‘tp’ – The time delay between two consecutive clock pulses (Fraba n.d.).
‘tw’ – The minimum time elapsed between re-transmission of same data and this is always less than ‘tm’
(Fraba n.d.).
‘T' – The time is taken between two fallen edges or rising edges in a continuous clock cycle (Fraba n.d.).
Figure 17 - SSI Timing Diagram (W. Commons, en.wikipedia.org n.d.)
Initially, both clock and data lines are in the idle state as per Figure 17 above. Once the master sends a
train of clock pulses, it initiates the transmission mode. When the slave receives the clock pulses, with
the first rising edge, the first data bit, the MSB is transmitted. The rest of the data is sent with each
rising edge, and finally, after the LSB is transferred an extra rising clock edge will set the data output to
LOW level. This will continue for transfer timeout of 20 +µs. Then the data line is set to HIGH and start
updating position values again.
4.3.5 Gray code
The Gray code (Wakerly 2006) is a binary numbering system used widely in digital communication. The
Gray code only changes one bit at a time, and this reduces transmission errors. Once the data is
received at the other end, it needs to be changed to the ordinary binary number or decimal number to
read the correct value.
20
4.3.6 H-Bridge
As the name stands an "H-Bridge" (Tantos 2011) is a DC electric motor control circuit, consisting of four
switching elements in an ‘H' like configuration. An H-bridge may be used to control the speed and
direction of a DC motor.
As per Figure 18 below switching elements, Q1 to Q4 can be bipolar or “Field-Effect Transistor” (Graf
1999). Insulated gate bipolar transistors (IGBT) (Graf 1999) are used in high voltage applications. As per
Figure 18, four diodes are shown connected in parallel to the transistors, and they are for the protection
of the transistors. There is back-EMF from voltage spikes generated by the motor and diodes protect the
transistors from this back-EMF.
As per Figure 18 the top rail is connected to a power supply and the bottom rail is connected to ground.
M
Vcc
Q1
Q2
Q3
Q4
D1
D2
D3
D4
Figure 18 - H-Bridge Configuration (Tantos 2011)
An H-bridge can operate in four states: coasting, forward, reverse and brake as shown in Figure 19
below.
Coasting: all four diodes are turned ‘off’ and no voltage applied to the motor.
Forward: The transistors Q1 and Q4 switch ‘on’ as per Figure 19. Transistor Q1 connect to
power supply and Q4 connect to ground. This will make the motor spin in the forward direction.
Reverse: The transistor Q3 and Q2 switch ‘on‘ as per Figure 19. Transistor Q3 connect to power
supply and Q2 connect to ground. This will make the motor spin in the Reverse direction.
Brake: The transistors Q2 and Q4 switch ‘on’ as per Figure 19. Both transistors connect to
ground. This enables the motor to dissipate its energy quickly to the ground.
21
Speed control of the motor is achieved by "Pulse Width Modulation" (PWM) (Jacob 1989). The voltage
applied to the motor is controlled through PWM. A PWM signal has two sections, "On-time" and "off-
time". Changing the "on-time" through PWM will modify the duty cycle (Jacob 1989) of the signal. This
will change the applied voltage to the motor. A PWM signal has a constant frequency called its carrier
frequency.
M
Vcc
Q1
Q2
Q3
Q4
D1
D2
D3
D4
M
Vcc
Q1
Q2
Q3
Q4
D1
D2
D3
D4
M
Vcc
Q1
Q2
Q3
Q4
D1
D2
D3
D4
M
Vcc
Q1
Q2
Q3
Q4
D1
D2
D3
D4
(A) Free (coast)
(B) Forward
(C) Reverse
(D) Brake
OFF
OFF
OFF
OFF
ON
OFF
OFF
ON
OFF
ON
ON
OFF
ON
OFF
ON
OFF
Figure 19 - H-Bridge Operation Modes (Tantos 2011)
22
5 Controller Design
5.1 Introduction This section covers the controller design approach taken for the inverted pendulum. Due to time
restrictions, the pendulum swing up routine did not get implemented, only the inverted position control.
There are several controller designs available to choose from but due to its superiority a "Linear
Quadratic Regulator" (LQR) (P. Albertos 1997) chosen. The following sections present the LQR method
and how the controller is tuned.
5.2 Linear Quadratic Regulator (LQR) The pendulum is unstable in the inverted position without any control. As the transfer function equation
(37) derived earlier for cart position shows, there are two poles and zeros in the right half plane. Due to
the closeness of these poles and zeroes, it is difficult to obtain a stable response through classical
feedback control methods. For this reason, stabilization of the pendulum is carried out through a Linear
Quadratic Regulator. The LQR computes the state feedback control gain matrix (P. Albertos 1997) . The
primary goal is to achieve optimal control with stability.
The best results were derived by optimizing the response speed and control effort. The optimum speed
and control is obtained by numerically changing the Q and R matrices.
The controllable state space equation is as per equation (38).
Matrix A is a n x n matrix, and matrix B is a column vector of length n as per equation (42).
The state is a column vector of length n as per equation (40).
Q is a diagonal matrix of the form:
𝑄 = [
𝑞1 0 0 00 𝑞2 0 00 0 𝑞3 00 0 0 𝑞4
]
(44)
Feedback control gain matrix K is determined by:
𝑢(𝑡) = −𝐾𝑥(𝑡) (45)
23
Figure 20 below shows how the gain matrix ‘K’ is applied to a controller.
X = Ax + Buy = Cx + Du+
-
K
y
x
u
Figure 20 - State Feedback System
To minimise the performance index:
𝐽 = ∫(𝑥𝑄𝑥 + 𝑢𝑅𝑢)𝑑𝑡
∞
0
(46)
The matrices Q and R determine the importance of the error. The elements of the feedback control
matrix K defined in here. The state weights ‘q' is chosen per particular state responses. The control
weight R is selected per control effort u and can be optimized. For example, when q1 and q3 are small,
the weights q1 and q3 are placing a small penalty on the states x1 and x3 in the optimization problem.
Since the equation (46) is to be minimized, the optimal control action u must force the states x1 and x3
to be large. Similarly, if the control weight R is greater than any q value, then the weight R is placing a
significant penalty on the control effort u.
Finally, it is noted that larger gains may cause trouble since they cause saturations in the system. The
large gains can lead to instability. The following section explains how the Q and R weights impact on the
system:
If the q values are large, then the control u will try to minimize the corresponding x value.
If the q values are small, then the control u will try to maximize the corresponding x value.
If the R is large compared to q values, then the control u will be expensive, and the response is
sluggish.
If the R is small compared to q values, then the control u will be cheap, and the response is
faster.
24
5.3 Calculation of State Space Matrices Table 1 shows the parameters required to calculate the continuous state-space matrices. These values
were measured, estimated or assumed.
Table 1 - Inverted Pendulum System Model Parameters
Parameter Value M – Cart mass 1.887kg
m – Pole mass 0.27kg
l - Length 0.42 m
g – Gravity 9.8 ms-2
I – moment of Inertia 0.063kgm2
b – Cart friction coefficient 0.1N/m/sec
c – Pendulum damping coefficient 1Nms/rad
The A matrix is calculated by including the parameters from Error! Reference source not found. Error!
Reference source not found. into equation (42).
𝐴 = [
0 1 0 00 −0.0490 0.5582 00 0 0 10 −0.0502 10.6178 0
]
Similarly, the B matrix is calculated by including the parameters from Error! Reference source not
found. Error! Reference source not found. into equation (42).
𝐵 = [
00.491
0.5023
]
𝐶 = [1 0 0 00 0 1 0
]
𝐷 = [00]
25
5.4 LQR Controller in Matlab In this section, previously derived linearized state space equations are used to simulate the inverted
pendulum system in Matlab. The relevant Matlab scripts can be found in the Appendix B – Matlab
Scripts. There are two outputs for this problem, and they are the cart displacement (x in meters) and
pendulum angle (θ in radians). The model derived in Matlab was later compared with the real plant for
fine tuning. To measure the performance of the model and the plant, a performance criterion was set so
this could be used to tune the controllers for their optimum performance. Table 2 Error! Reference
source not found. shows the performance criterion set for a 0.2m step change.
Table 2 - Performance Criterion for Inverted Pendulum
Performance Criterion Measure Units
Settling Time for x and θ < 5 seconds
Rise time for x < 0.5 seconds
Pendulum angle deviation < 0.25 radians
Steady state error < 2%
The first step is to change the continuous state-space equations to digital form by discretising the
equations. The equations were discretised by the Matlab c2d function (Mathworks.com n.d.). The three
arguments that are required by the function are the system model, sampling time and the method.
Once this is done a discrete state space model will produce.
The next step is to calculate the control gain matrix K. The gain matrix K obtained by using built-in ‘dlqr'
function within Matlab (Mathworks.com n.d.). The result is the discretised version of "Linear Quadratic
Regulator" function. As explained in section 5.2 the performance index R and state cost matrix Q need
to be specified to use this function. Initially, R and Q values been chosen to be 1 to see the output
produced.
The q1 represents the weight of the cart position and q3 represents the weight of the pendulum angle.
Figure 21 below shows the closed loop response for a step change as specified above. This result was
deemed unacceptable since this does not meet the performance criterion. The red curve shows the
pendulum angle and the blue curve shows the cart position. Both curves need their settling time
improved and cart rise time needs to reduce.
To improve the performance criterion q1 and q3 values were increased until the desired values achieve.
Figure 22 below shows the improved response with q1 and q3 values. The cart position has an offset but
current control disregard this due to the primary focus on pendulum angle.
26
Figure 21 - Step Response of Inverted Pendulum with 'dlqr' in Matlab q1=1, q3=1 and R=1
Figure 22 - Step Response of Inverted Pendulum with 'dlqr' in Matlab q1=1+E5, q3=1+E5 and R=0.01
27
6 System Implementation
6.1 Background This section details the implementation of inverted pendulum apparatus located in Engineering and
Energy Building 220. The apparatus itself is constructed off an old sliding door mechanism. It consists of
a track where the sliding doors used to hang, a DC motor with an incremental encoder, a rubber belt
attached to the DC motor cart system and an absolute encoder connected to the pendulum. The
primary purpose of the project was to integrate MyRIO to this apparatus, so the future students can
utilize the apparatus to apply control theory they study during their time at Murdoch University.
The model derived in Section 4.2 above will be used with the control system designed in part 5 on this
apparatus. A block diagram of the system shown in Figure 23 below with a full schematic in Error!
Reference source not found.. The software used to program MyRIO is LabVIEW by National Instruments
(National Instruments n.d.). The MyRIO toolkit needs to install in the computer in addition to the
LabVIEW software.
The following sections discuss in detail how each component was commissioned and integrated to the
overall system. It explains the problems faced during commissioning activities and how they were
overcome?
PENDULUM CARTMOTORH-BRIDGECONTROLLER
ABSOLUTE ENCODER
+- Output
Figure 23 - Pendulum Cart System Block Diagram
28
NI MyRIO - 1900
MAX3095RS-485
LINE RECEIVER
SN75LBC174A
RS-485LINE
DRIVER
SICK ENCORDERARS60 – A4A08192
INCREMENTAL ENCODER
ATTACHED TO 24V MOTOR
DIO5/SPI.CLK
DIO6/SPI.MISO
1A
Y1
6 – CLOCK (+)
11 – CLOCK (-)
2 – CW/CCW
9 - SET
1 - Us
8 - GND
12 – DATA (+)
7 – DATA (-)
GND
+10 - 32V
CLOCK (+) – 1Y
CLOCK (-) – 1Z
DATA (+) – A1
DATA (-) – B1
GND
+5V
Vcc
Vcc
GND
GND
EN
G
24V H-BRIDGE FOR MOTOR DIRECTION CONTROL
AND PULSE WIDTH
MODULATION
MDIO0
PWM0 / DIO8
DIO11/ENC.A
DIO12/ENC.B
DIO5/SPI.CLK
Vcc
DIR
PWM
GND
Vcc
A
B
Vcc
GND
GND
+5V
MX
P CO
NN
ECTO
R A
MX
P CO
NN
ECTO
R B
OVERALL CONNECTION DIAGRAM
+24VMotor Supply
Vcc
Figure 24 - Overall Connection Diagram
29
6.2 Implementation of Absolute Encoder The absolute encoder used in this apparatus is a “Sick ARS60-A4A08192" 13-bit encoder (sick.com n.d.).
This absolute encoder is a single-turn encoder with 8192 steps supported by ‘SSI' interface. The device
has a resolution of 0.005°. The encoder is fitted with a 12-pin female socket and configuration as per
Table 3 below. The pin configuration conflicts with the original datasheet. However, the previous thesis
student had found the correct pin configuration by contacting the vendor (Table 3). There are eight
connections in total, and seven of them have been used apart from pin number two, which states
direction of rotation. Two connections are for power, two connections are for the clock signal, two
connections are for data and a single connection for direction & zero set. Figure 25 below shows how
the absolute encoder is attached to the inverted pendulum apparatus.
Figure 25 - Absolute Encoder Attached to the Pendulum
The device support ‘SSI' interface operates using the RS422 communication protocol. The ‘SSI' interface
itself is a master-slave configuration. The absolute encoder acts as a slave and MyRIO serve as the
master.
Initially the communication to the encoder was attempted by a circuit diagram adopted by a “POSITAL
FRABA” encoder company (Fraba n.d.). This attempt proved to be tedious and excessively complicated
as all the components were surface mounted. The components were soldered to DIP adaptors. Then the
adaptors were connected to a breadboard to achieve the final connection to MyRIO. Even when all the
30
components matched as per circuit diagram, the desired signal could not be obtained. After trying for a
couple of months, a decision was made to change the circuitry.
The Clock signal transmission from MyRIO to the encoder switch to an RS485 differential line driver
"SN75LBC174A" (Texas Instruments 2009). The Data signal transmission from the encoder to MyRIO is
achieved by an RS485 differential line receiver "MAX3095" (Maxim Integrated 2010). All these
components were connected to an MXP breakout board and connected to relevant SPI connections
(Refer to Figure 24 above).
Table 3 - SICK ARS60-A4A08192 Pin Configuration
PIN NUMBER CORE COLOUR SIGNAL
1 ORANGE Us
2 BROWN/WHITE CW/CCW
3 N.C
4 N.C
5 N.C
6 ORANGE/WHITE Clock(+)
7 GREEN/WHITE Data(-)
8 GREEN GND
9 BROWN SET
10 N.C
11 BLUE Clock(-)
12 BLUE/WHITE Data(+)
The MyRIO has several built-in communication protocols such as SPI, I2C, and UART. It was found SPI
was ideal to implement communication between master and slave (Fraba n.d.). The SPI interface
supports both MISO and MOSI signals. In this instance, MOSI was used to communicate with the device
since MyRIO act as a master and the encoder act as a slave. As per Figure 26 belowthe MyRIO’s built in
I/O monitoring module was used to troubleshoot the application. As explained in section 4.3.4 fourteen
clock pulses were generated with 200 kHz frequency. The data received were analysed in I/O monitoring
module to confirm correct implementation of Gray Code.
31
Figure 26 - MyRIO I/O Monitoring Module
As per Gray Code, one data bit should be changing with a small step of the angle. The result confirmed
by connecting the clock signal and data signal to a digital oscilloscope. As per Figure 27 the differential
clock signal shows on lines D0 and D1. The differential data signal shows on lines D2 and D3. The
Operation was confirmed by rotating the encoder shaft and reading the relative value. As explained in
section 4.3.4 SSI data transmission and timing structure analysed for correct implementation. The data
is changing on every rising clock pulse. When the LSB is transmitted, the data line stays low for ‘tm'
period as per the SSI timing diagram and then data line shift back high. Refer to Figure 27 below for
confirmation of frequency, pause time and transfer timeout.
Figure 27 - Differential Clock and Data Signals in Digital Oscilloscope
32
Since the encoder returns the position value in Gray Code, this had to be converted to a decimal value
within the program. A sub-VI was created to change data from Gray Code to decimal. Once the decimal
value is read then by dividing by resolution and multiplying by either 360° or 2π will give the desired
angle.
𝐴𝑛𝑔𝑙𝑒 = 𝑉𝑎𝑙𝑢𝑒
𝑅𝑎𝑛𝑔𝑒∗ 360 𝑜𝑟 2𝜋 (𝑑𝑒𝑝𝑒𝑛𝑑𝑠 𝑜𝑛 deg 𝑜𝑟 𝑟𝑎𝑑)
𝐴𝑛𝑔𝑙𝑒 = 𝑉𝑎𝑙𝑢𝑒
8192∗ 2𝜋 𝑟𝑎𝑑
6.3 Implementation of Incremental Encoder The optical incremental encoder is attached to the 24V DC motor. The encoder is part of the motor unit
assembly. The encoder consists of quadrature, TTL compatible and 110° phase apart two channels (A
and B). The encoder has 100 impulses per revolution with a 5V supply voltage. Initially, 5V was applied
to the device to turn the motor and read the pulses. Channels A and B were connected to an
oscilloscope to confirm their operation. As explained in section 4.3.2 the expected square wave was
achieved only in channel A. The device was taken apart to troubleshoot which component failed. Later it
was found that the built in "Sharp GP1A71R” photo-interrupter had failed on channel B. An attempt
was made to replace the failed component but it was found that the part is obsolete. Since this is a PCB
mounted component, a replacement needed to be found with correct pin configuration. After
contacting various electronic component vendors, a decision was made to buy a similar part and glue
the device to the side of the motor housing. Figure 28 below show how the new encoder currently
connected to the motor housing.
Figure 28 - Mounting Arrangement for Incremental Encoder
33
The software implementation of the encoder was much easier because MyRIO has built in Express VIs
for most of the built-in functions. Figure 29 below shows the Express VI for encoders. Once this is
included in the ‘Block Diagram' it prompts the user through a step by step process to configure the
correct encoder settings. Once this is setup the counter value can be read and through proper data
manipulation, the motor angle was found as per Figure 29 below.
Figure 29 - Encoder Express VI
6.4 Implementation of Motor Control The motor used in this apparatus is a KAG 24V DC Motor with a worm gear
(M48x60/I+SNS1,1+IGO100/2) (kag-hannover.com 2016). The motor itself is suitable for rotation
changes and intermittent operation. The gear reduction ratio for the motor is 10:1. The worm gear
arrangement is used to reduce the rotational speed and transmit the torque. The main two elements
are the motor arrangement and how the motor fixed to the apparatus. The belt is attached to the motor
via a toothed pulley wheel, which can be seen in Figure 30 below.
Figure 30 - KAG 24V DC Motor with worm gear
34
The motor speed and direction control were achieved by a 36V Dual 15A H-Bridge Motor Driver. Figure
31 below shows the PCB for the H-Bridge. The H-Bridge operation is explained in the H-Bridge section
4.3.6 above. The H-Bridge motor driver comes with built in PWM connection and motor direction
control connections.
Figure 31 - 36V Dual 15A H-Bridge Motor Driver
Motor speed and direction control in software were achieved by MyRIO Express VIs. The Figure 32
below shows how this is implemented in LabVIEW. To avoid changing direction while enabled, simple
logic has performed as per Figure 32 below. The logic detects a direction change and disables PWM
output for the particular cycle to avoid changing motor direction. As for the motor frequency, different
values were tested. When the frequency increases, the motor makes an audible noise and tend to draw
more current so 400Hz seems to be the perfect frequency for safe operation.
Figure 32 - Software Motor Speed and Direction Control
35
6.5 Implementation of Discrete LQR A controller for the inverted pendulum was chosen of type "LQR". The reason behind this is, when the
pendulum becomes inverted the system becomes unstable and looking at transfer functions suggest
there are right-hand side poles (Refer to Figure 14 above). Implementing a conventional control method
seems rather difficult. For this reason and due to LabVIEW support via a built-in LQR function, the
decision has been made to implement the LQR controller.
LQR is a state-space controller, and it analyses the control action based on the relationship between
inputs, states, and outputs of a system (Corporation 2009). The LabVIEW software has a dedicated state-
space controller section, and LQR can be found under this chapter. Figure 33 below shows the “State
Feedback Design” palette within LabVIEW. Under this section a “CD Linear Quadratic Regulator VI” can
be found. The LQR calculates the steady state gain matrix ‘K' that minimizes the linear quadratic cost
function specified by the user (Corporation 2009). This VI supports continuous and discrete state-space
models. In this instance, a continuous model is used and let the VI return a discretised version of ‘K’ for
the model used.
The discretised LQR VI can see in Figure 34 below. The main differences between the VIs are the discrete
VI requires continuous state-space model input and sampling time input as per Figure 34. The other
inputs for this VI are Q and R. These specify the weights on the states and inputs. The Weighting type
can be chosen from a drop-down menu. Three are three types of weighting, and it lets the user choose
which cost function to minimize.
State Weighting – this cost function weights the model states (default state).
Output Weighting, Dim[Q] = Ny – this weights the outputs ‘y’ when Q is in terms of ‘y’.
Output Weighting, Dim[Q] = Nx – this weights the outputs ‘y’ when Q is in terms of ‘x’.
Figure 33 - LabVIEW "LQR VI" under State Feedback Design palette
36
Figure 34 - LabVIEW "CD Linear Quadratic Regulator (Discretised LQR)”
The default weighting type was used for the inverted pendulum. Once all the inputs were connected the
Q and R values obtained from section 5.4 above were tested. The optimum gain matrix ‘K' can be
simulated with the LQR, and the results were identical. The following section shows the results obtained
to confirm these values were the optimum gain matrix values. First, different q1, q3, R, and loop times
were used to plot the process variable and manipulated variable. As this gives a clear understanding of
how these values need to be chosen to obtain maximum performance out of the system.
The Figure 35 Figure 38 below have been taken at different time intervals. These figures have different
Q and R values. As the loop time decreases the control over the process variable also increased to
compensate the error calculated. This compensation has allowed the manipulated variable to minimize
the error between process variable and setpoint. Apparently as the loop time decreases the
manipulated variable has put less control action to reduce the error.
Figure 35 - Comparison of Process Variable and Manipulated Variable (dt =0.1s, q1=10, q3=10 & R=1)
37
Figure 36 - Comparison of Process Variable and Manipulated Variable (dt =0.01s, q1=100, q3=1+E5 & R=0.1)
Figure 37 - Comparison of Process Variable and Manipulated Variable (dt =0.001s, q1=100, q3=1+E5 & R=0.03)
Figure 38 - Comparison of Process Variable and Manipulated Variable (dt =0.001s, q1=100, q3=1+E5 & R=0.01)
38
7 Conclusion
7.1 Project review There were three principal aspects of the thesis, and all of them have been completed. The first aspect
was to learn MyRIO’s functions and its capabilities. The second was to implement a "Quick Start-up
Guide for Future Students". The third point was to integrate the MyRIO with a controlled apparatus.
Since the control apparatus was an inverted pendulum its integration can classed as a success. There are
minor improvements that have been identified, and they have shifted to the future works section.
I have gained a vast knowledge through this thesis project. Most of the time was spent in commissioning
the Sick absolute encoder. Since there was minimum support and documentation in regards to
implementation of ‘SSI' protocol into a microcontroller, the task in hand was found to be rather difficult.
Finally, this task has been overcome and achieved the goal of communication with Sick encoder. The
communication to the encoder is deemed to be the proudest moment of this thesis.
The inverted pendulum apparatus has been fully commissioned, and a controller has implemented and
tested. The controller can control the pendulum angle in its inverted position and is deemed a success in
its implementation. This gives the opportunity to future students to implement different controllers into
this apparatus and use this apparatus as a learning platform.
7.2 Recommendation for future work The current controller implemented within the inverted pendulum apparatus only controls the
pendulum angle. The cart position and pendulum angle should be able to be controlled simultaneously.
This could be achieved either by LQR or another controller. The other controllers can be "Fuzzy
Controller", "MPC" and "PID". The pendulum swing up was not implemented due to various reasons, so
this should be done in the future. The LabVIEW built in "Statechart" or "State machine" is recommended
for this type of algorithm.
39
8 References Commons, W.T.Freeman via Wikimedia. 2009. commons.wikimedia.org. 06 03. Accessed March 12,
2017. https://upload.wikimedia.org/wikipedia/commons/7/7d/Fpga1a.gif.
Commons, Wikimedia. n.d. commons.wikimedia.org. Accessed 05 23, 2016.
https://upload.wikimedia.org/wikipedia/commons/7/7d/Grasshopper_Site_at_Rocket_Develop
ment_Facility_%28crop%29.jpg.
—. n.d. commons.wikimedia.org. Accessed 05 23, 2016.
https://upload.wikimedia.org/wikipedia/commons/3/35/Police_Segway_in_Prague.jpg.
—. n.d. en.wikipedia.org. Accessed 05 23, 2016. By Anjan.nachiappa - Own work, Public Domain,
https://commons.wikimedia.org/w/index.php?curid=11013523.
Controls, Danaher Industrila. 2003. “ENCODER APPLICATION HANDBOOK.” www.dancom.com. Accessed
05 05, 2016.
https://www.dynapar.com/uploadedFiles/Products/Danaher_Encoder_Handbook.pdf.
Corporation, National Instruments. 2009. “Control Design User Manual.” National Instruments, 06.
Fraba, Posital. n.d. www.posital.com. Accessed 05 24, 2016.
https://www.posital.com/media/posital_media/documents/AbsoluteEncoders_Context_Techno
logy_SSI_AppNote.pdf.
Friedland, Bernard. 1986. Control System Design. Boston: McGraw-Hill.
Graf, Rudolf F. 1999. Modern Dictionary of Electronics. Boston: Newnes.
Instruments, National. n.d. “NI myRIO-1900 USER GUIDE AND SPECIFICATIONS.”
J.C. Jensen, E.A. Lee, S.A. Seshia. 2013. An Introductory Lab in Embedded and Cyber-Physical Systems.
Jacob, J. Michael. 1989. Industrial Control Electronics Application and Design. Englewood Cliffs, New
Jersey: Prentice-Hall International, Inc.
2016. kag-hannover.com. Accessed March 11, 2017. http://www.kag-hannover.com/wp-
content/uploads/dokumente/Katalog/EN/KAG-DC_Motors-Catalogue_2015_2016.pdf.
Liptak, Bela G. 2002. Process Software and Digital Networks. London New York Washington, D.C.: CRC
PRESS.
n.d. Mathworks.com. Accessed March 11, 2017.
https://au.mathworks.com/help/control/ref/c2d.html?s_tid=srchtitle.
n.d. Mathworks.com. Accessed March 11, 2017.
https://au.mathworks.com/help/control/ref/dlqr.html?searchHighlight=dlqr&s_tid=doc_srchtitl
e.
2010. Maxim Integrated. January. Accessed March 11, 2017.
https://datasheets.maximintegrated.com/en/ds/MAX3095-MAX3096.pdf.
n.d. National Instruments. Accessed March 11, 2017. http://www.ni.com/labview/.
Nise, Norman S. 2011. Control System Engineering. California: John Wiley & Sons, Inc.
P. Albertos, R. Strietzel, N. Mort. 1997. Control Engineering Solutions a practical approach. London: The
Institution of Electrical Engineers.
40
PROGRAMMABLE, XILINX ALL. n.d. www.xilinx.com. Accessed 05 19, 2016.
http://www.xilinx.com/training/fpga/fpga-field-programmable-gate-array.htm.
n.d. sick.com. Accessed March 11, 2017. https://www.sick.com/media/pdf/0/70/370/dataSheet_ARS60-
A4A08192_1031447_en.pdf.
Stewart, James. 2012. Calculus. Canada: CENGAGE Learning.
Stimac, Andrew K. 1999. Standup and Stabilization of the Inverted Pendulum. Bsc Thesis, Massachusetts:
Massachusetts Institute of Technology.
Tantos, Andras. 2011. Modularcircuits. November. Accessed March 10, 2017.
http://www.modularcircuits.com/blog/articles/old-h-bridge-secrets/part-1/.
2009. Texas Instruments. October. Accessed March 11, 2017.
http://www.ti.com/lit/ds/symlink/sn65lbc174a.pdf.
Wakerly, John F. 2006. Digital Design: Principles and Practices. Upper Saddle River, N.J: Pearson/Prentice
Hall.
42
NI MyRIO - 1900
MAX3095RS-485
LINE RECEIVER
SN75LBC174A
RS-485LINE
DRIVER
SICK ENCORDERARS60 – A4A08192
INCREMENTAL ENCODER
ATTACHED TO 24V MOTOR
DIO5/SPI.CLK
DIO6/SPI.MISO
1A
Y1
6 – CLOCK (+)
11 – CLOCK (-)
2 – CW/CCW
9 - SET
1 - Us
8 - GND
12 – DATA (+)
7 – DATA (-)
GND
+10 - 32V
CLOCK (+) – 1Y
CLOCK (-) – 1Z
DATA (+) – A1
DATA (-) – B1
GND
+5V
Vcc
Vcc
GND
GND
EN
G
24V H-BRIDGE FOR MOTOR DIRECTION CONTROL
AND PULSE WIDTH
MODULATION
MDIO0
PWM0 / DIO8
DIO11/ENC.A
DIO12/ENC.B
DIO5/SPI.CLK
Vcc
DIR
PWM
GND
Vcc
A
B
Vcc
GND
GND
+5V
MX
P CO
NN
ECTO
R A
MX
P CO
NN
ECTO
R B
OVERALL CONNECTION DIAGRAM
+24VMotor Supply
Vcc
43
9.2 Appendix B – Matlab Scripts File: Inverted_Pendulum_LQR.m
This file contains model parameters and generate discretised ‘K’ matrix.
%%Inverted_Pendulum_LQR.m clear;
%% System Parameters M = 1.887; % Mass of cart (kg) m = 0.27; % mass of pendulum (kg) b = 0.1; % coefficient of friction for cart (N/m/sec) I = 0.063; % mass moment of inertia of the pendulum (kg.m^2) g = 9.8; % gravitational constant l = 0.42; % length to pendulum center of mass (m)
%% State Space Representation
p = I*(M+m)+M*m*l^2; %denominator for the A and B matrices
A = [0 1 0 0; 0 -(I+m*l^2)*b/p (m^2*g*l^2)/p 0; 0 0 0 1; 0 -(m*l*b)/p m*g*l*(M+m)/p 0]; B = [ 0; (I+m*l^2)/p; 0; m*l/p];
C = [1 0 0 0; 0 0 1 0]; D = [0; 0];
%% State Space parameters
states = 'x' 'x_dot' 'phi' 'phi_dot'; inputs = 'u'; outputs = 'x'; 'phi';
sys_ss =
ss(A,B,C,D,'statename',states,'inputname',inputs,'outputname',outputs);
Ts = 1/1000;
sys_d = c2d(sys_ss,Ts,'zoh')
%% Discretised LQR, Q & R values and Plot
A = sys_d.a; B = sys_d.b; C = sys_d.c; D = sys_d.d; Q = C'*C Q(1,1) = 100000; Q(3,3) = 100000; R = 0.1;
44
[K] = dlqr(A,B,Q,R)
Ac = [(A-B*K)]; Bc = [B]; Cc = [C]; Dc = [D];
states = 'x' 'x_dot' 'phi' 'phi_dot'; inputs = 'r'; outputs = 'x'; 'phi';
sys_cl =
ss(Ac,Bc,Cc,Dc,Ts,'statename',states,'inputname',inputs,'outputname',outputs)
;
t = 0:0.001:5; r =0.2*ones(size(t)); [y,t,x]=lsim(sys_cl,r,t); [AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot'); set(get(AX(1),'Ylabel'),'String','cart position (m)') set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)') title('Step Response with Digital LQR Control')
45
File: PZMAPANDBODEDIAGRAM.m
This file plots Pole zero map and Bode diagram in the same plot
%PZMAPANDBODEDIAGRAM.m
clear; %% Model Parameters s = tf('s');
M = 1.887; % Mass of cart (kg) m = 0.27; % mass of pendulum (kg) b = 0.1; % coefficient of friction for cart (N/m/sec) I = 0.063; % mass moment of inertia of the pendulum (kg.m^2) g = 9.8; % gravitational constant l = 0.42; % length to pendulum centre of mass (m) c = 1;
a0= -m*g*l; a1= b; a2= m*l^2+I; b1= -m*g*l*c; b2= -((M+m)*m*g*l) + b*c; b3= (M+m)*b + (m*l^2+I)*c; b4= (M+m)*(m*l^2+I) - m^2*l^2; c1= -m*l;
%% Transfer Functions G1 = ((a2*s^2 + a1*s + a0)/(b4*s^4 + b3*s^3 +b2*s^2 + b1*s)); %displacement
G2 = (c1*s)/(b4*s^3 + b3*s^2 +b2*s + b1); % Angle
%plots
figure(1)
subplot(2,2,1) pzmap(G1);
subplot(2,2,2) pzmap(G2);
subplot(2,2,3) bode(G1);
subplot(2,2,4) bode(G2);