Automatism
Automatism
PROJECT REPORT:
CONTROLLED INVERTED PENDULUM
Acknowledgements 3
List of abbreviations 4
List of tables 5
List of figures 6
Abstract 7
1 Introduction 8
1.1 Context and Significance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2 Project Motivation and Learning Objectives . . . . . . . . . . . . . . . . . 8
1.3 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 Equation of Motion 10
1
7 Results and Discussion 37
8 Conclusion 38
Bibliography 39
A Arduino Code 40
2
Acknowledgements
Before we dive into the final project report, we would first like to extend our heartfelt
thanks and utmost gratitude to our Automatism Professor, Dr. Tran Anh Tu, who have
been our lecturer for the course as well as the concept of Automatic Control Theory as a
whole. His dedication to teaching, expertise in the subject as well as his lecturing methods
have been instrumental in shaping the trajectory of our project.
With that being said, we are grateful to USTH, our university for providing us this
amazing opportunity to be able to work on such subject and providing the tools and
environment to allow us to achieve our goals.
We also greatly appreciate Linh Kien Cau Giay hardware/electronics shop and SKF
Retails for providing us with the necessary materials and components required for the
project to be conducted.
3
List of Abbreviation
4
List of Tables
5
List of Figures
3.1 Result 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2 Result 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.3 Result 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.4 LQR control Simulink diagram . . . . . . . . . . . . . . . . . . . . . . . . 17
3.5 Simulation result 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.6 Full terms system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.7 Result of each terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.8 Simulation animation 1 - Pendulum during swing-up. . . . . . . . . . . . . 20
3.9 Simulation animation 2 - Pendulum balanced. . . . . . . . . . . . . . . . . 20
5.1 Arduino R3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.2 MPU-6050 sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.3 Stepper Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.4 A4988 Stepper Motor Driver . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6
Abstract
This project studies and builds an pre-designed self-balancing inverted pendulum model
from an online tutorial which then presents its simulation and implementation. This is an
attempt to understand the inverted pendulum problem, where a pivoted pendulum per-
form a swing-up motion and keep itself in upward balance while mounted on a moving cart.
Beforehand, a PID and a LQR control algorithm was researched and simulated in MAT-
LAB and Simulink. The inverted pendulum model is constructed from available 3D
printed parts. The electronic system includes an Arduino Uno as the main board, ac-
companied by the MPU6050 sensor. Other electronic components are a HR4988 stepper
motor driver, a NEMA 17 servo and a 12V/10A power supply.
There exist troubleshooting during the sensor data fusion collection process of the gy-
roscope and accelerometer from the inertial measurement unit (IMU). Complementary
filters and the Kalman filter were utilized for this drift-correction process.
Our group’s experimental results reflect the simulation and achieved successful pendu-
lum stabilization for the model. Limitations such as drift, wiring inconveniences, and
electrical imperfections were encountered and addressed during implementation.
7
Chapter 1
Introduction
Stabilizing a pendulum in this inverted position is a perfect benchmark for various con-
trol algorithms (i.e. PID controllers, state-space representation, neural networks, LQR
controllers, etc.). The inverted pendulum has its root in rocket/missile guidance, where
the center of gravity is often located usually near the rear, behind the center of drag
which causes aerodynamic instability. This is comparable to the pivot point behind the
pendulum. Applications of this concept can be seen in other fields such as robotics in
self-balancing. Segways, hoverboard or uni-cycles are prime examples, to name but a few.
Through this project, we aim to understand the control system and dynamics behind
the inverted pendulum problem by real-time applications. We also want to get hands-on
experience in implementing different feedback controllers when performing simulation for
the machine itself. Though not being our initial goals, addressing certain problems in
data fusion - precisely accelerometer and gyroscope reading - such as drift or revising
8
parts of the tutorial itself in terms of electronic parts redundancy/inefficiency for better
performance are all notable efforts made in working towards this project.
1.3 Overview
In this report, we will firstly demonstrate our initial theoretical approach to this problem
with theoretical modeling and accompanied mathematical representations. The control
design has been programmed in MATLAB and simulated via Simulink with clear tun-
ing for the controllers and stability outputs. Afterwards, practical implementation will
present the hardware used for the project, namely parts for the physical rig of the model
and the electronics used for Arduino-based controls. This is followed by the assembling of
the model with the wiring of the entire electronic system. The latter part will demonstrate
and explain the software - the Arduino programming for the machine - and implemen-
tation of filtering algorithms to avoid drift. Finally, we will discuss the results of the
experiment, our execution of the project and any further complications that occured dur-
ing the development of the project.
9
Chapter 2
Equation of Motion
In the pendulum motion, we consider it in two motions, the change in angularity of the
pendulum and the displacement in the x axis:
10
The Potential energy V:
V = mgl cos(θ)
Then the Lagrangian of the system is:
1 1 1 1
L = Mc ẋ2 + mẋ2 + ml2 θ̇2 + mlẋθ̇ cos θ + I θ̇2 + mgl cos(θ)
2 2 2 2
Using this Lagrangian and the equation of motion in the previous part we have:
d ∂L ∂L
− = F − cẋ
dt ∂ ẋ ∂x
d ∂L ∂L
− = −bθ̇
dt ∂ θ̇ ∂θ
As the modeling of the system.
11
Chapter 3
Using the result in the Equation of Motion, we can now create a mathematics model for
the inverted pendulum:
cos θ = cos(π + ϕ) ≈ −1
12
sin θ = sin(π + ϕ) ≈ −ϕ
θ̇2 = ϕ̇2 ≈ 0
Apply this term to the Equation of Motion, we get the following system:
The resulting transfer function T (s) for the closed-loop system from an input of force U
to an output of pendulum angle ϕ is then determined to be the following.
1 Kp = 1;
2 Ki = 1;
3 Kd = 1;
4 C = pid ( Kp , Ki , Kd ) ;
13
5 T = feedback ( P_pend , C ) ;
6 t =0:0.01:10;
7 impulse (T , t )
8 title ({ ’ Response of Pendulum Position to an Impulse Disturbance ’; ’ under
PID Control : Kp = 1 , Ki = 1 , Kd = 1 ’ }) ;
And get the result, the system was not balanced, so we need to change the control prop-
erties:
14
Then we change to Kp = 100, Ki = Kd = 1:
1 Kp = 100;
2 Ki = 1;
3 Kd = 1;
4 C = pid ( Kp , Ki , Kd ) ;
5 T = feedback ( P_pend , C ) ;
6 t =0:0.01:10;
7 impulse (T , t )
8 axis ([0 , 2.5 , -0.2 , 0.2]) ;
9 title ({ ’ Response of Pendulum Position to an Impulse Disturbance ’; ’ under
PID Control : Kp = 100 , Ki = 1 , Kd = 1 ’}) ;
And get the following result. It contains a big overshoot at start, which made the final
result can not reaches the perfect balance point as expected.
15
Then continue the simulation with Kp = 100, Ki = 1, Kd = 20:
1 Kp = 100;
2 Ki = 1;
3 Kd = 20;
4 C = pid ( Kp , Ki , Kd ) ;
5 T = feedback ( P_pend , C ) ;
6 t =0:0.01:10;
7 impulse (T , t )
8 axis ([0 , 2.5 , -0.2 , 0.2]) ;
9 title ({ ’ Response of Pendulum Position to an Impulse Disturbance ’; ’ under
PID Control : Kp = 100 , Ki = 1 , Kd = 20 ’ }) ;
And then we get the result. With this control, the oscillation has been damped, and the
overshoot has reduced.
The final result reached the perfect balance point as expected, and a small overshoot.
The system has been successfully controlled.
16
b. LQR Control
System modeling
Base on the theory of the equation of motion, we generate the system matrices to perform
state space representation:
x = −Kx, x = [θ, θ̇, x, ẋ]T
And control matrices, which are based on LQR control. With p = I(M + m) + M ml2
0 1 0 0
0 − c(I+ml2 ) − m2 gl2 0
p p
A=
0 0 0 1
mgl(M +m)
0 − mlc
p p
0
0
I+ml2
B= p
0
ml
p
1 0 0 0
0 1 0 0
C=
0
0 1 0
0 0 0 1
0 0 0 0
0 0 0 0
D=
0
0 0 0
0 0 0 0
Then we have the Simulink diagram below.
17
Figure 3.5: Simulation result 1
Result discussion
The pendulum starts with small overshoot, and get to the balance point after 3 seconds.
The system has been successfully controlled.
c. No Control model
In no control model, it required that we have to build a full system with all the terms of
pendulum motion. So we have the Simulink diagram and the simulation result below:
18
Figure 3.6: Full terms system
At the displacement of the cart, it starts with a small overshoot and damped oscilla-
tion. The cart exhibited rapidly, then settled to a stop.
In angular behavior, the pendulum starts with a sharp oscillation at the beginning, the
pendulum was undergoing a large swing, then the converted angle closed to the steady
value, which is 180o , which means that the pendulum is upright and balanced.
At the velocity of the cart, it starts with a sharp oscillation, reaches a sharp peak at
about 7th seconds, then slows down to 0. Where the pendulum is balance.
For the behavior of the angular velocity, it starts to oscillate with high frequency at
the beginning, then slowing down to 0 at the steady point of the pendulum.
19
This graph shows that the simulation is successfully controlled, but still requires more
time to damp out the oscillation.
20
Chapter 4
Since the tutorial has already provided us with a list of required hardware supplies as well
as a zipped folder containing all the 3D parts, it was relatively simple in purchasing and
printing these items.
There were some difficulties when it comes to acquiring these items due to numerous
reasons: unreliable providers, unclear/wrong instructions from the tutorial and also the
sparsity of certain items. They will be further explained later.
a. Item List
First, we have a compiled lists of these items:
21
Table 4.1: General Hardware list.
It should be noted that this was the required and used amount, certain items like
bearings, pulleys, bolts and nuts were bought in excess were there to be any cases of lost,
breakage or unexpected usage.
The silicon piping and lubricant were our team’s own resolves and were not included
in the tutorial list of required hardwares.
When these were to be slotted and screwed into the 3D printed models, they did not
fit. It turns out they were too big and we have to do further research to find the right
bearings. Measuring the slots for the parts and comparing with the feedbacks of the
tutorial showed us that we needed a bearing of the dimensions 6x19x6mm, which is the
626 bearings.
Acquiring these were rather difficult as most shops in online stores are international or
have ran out of these bearings. The official retail store of SKF (who sells industrial grade
22
bearings) do sells these bearings but were at an incredibly high price at 50.000 - 60.000
VND/bearing.
It took us up to 5 days of searching through the city to find a shop that finally had
an available stock for civilian grade ones to use for these types of project. This single
item was a logistical nightmare and greatly delayed our realization of the project’s model.
While STL files contained no measurements in exports, we had initial concerns regarding
wrong sizing when printing, however after opening them and checking with AutoDesk
Fusion, the models were up to measure and the prints were perfect.
a. Item List
First, we have a compiled lists of these items:
23
MODEL AMOUNT DESCRIPTION
2 GANTRY ROLLER
• Act as wheels
1 GANTRY PLATE
• Carries pendulum
• Connected to belts
2 BELT ATTACHMENT
24
NAME AMOUNT DESCRIPTION
1 PENDULUM HOLDER
• Pivots pendulum
1 PENDULUM-BEARING
HOLDER (A)
1 PENDULUM-BEARING
HOLDER (B)
25
NAME AMOUNT DESCRIPTION
1 PENDULUM (A)
1 PENDULUM (B)
4 END CAPS
26
NAME AMOUNT DESCRIPTION
2 IDLE PULLEY BEARING
HOLDER
1 STEPPER BRACKET
27
NAME AMOUNT DESCRIPTION
1 STEPPER HOLDER PLATE
28
b. Problems & Troubleshooting
There weren’t any problems with the 3D parts save for some replacement. We had to
replace all the spacer/fitings in the bearings with silicon piping instead as due to printing
limitations, the Pulley/Bearing hole spacer proved to be difficult to print and were to
rough to even be able to work with in the first place. Despite our efforts to sand the
model, they still easily break after a short while after being screwed in and fitted.
The Silicon piping was perfect to be made into fittings for the bearings as they were
easy to work with. Small cylinders were easily cut from the pipe with similar dimensions
to the bearing hole spacer with and can be slotted in smoothly. The inner diameter
were a perfect fit for the M4 bolts also. This cheap replacement was one of the better
improvement we made upon the tutorial’s guidance.
c. Assembly
The total physical rig is made up of 3 different part: the gantry, the idle pulley section,
and the stepper pulley section.
The Gantry
29
Figure 4.2: The gantry with the pipe
The Pendulum
30
Figure 4.4: The Idle Pulley
31
Chapter 5
5.1 Adruino R3
In this project, we utilised the Arduino R3 kit as the controller for the system. Its
specifications are shown in the following table.
Category Details
Microcontroller ATmega328P
Operating Voltage 5V
Input Voltage (recommended) 7–12V
Input Voltage (limits) 6–20V
Digital I/O Pins 14 (6 provide PWM output)
PWM Digital I/O Pins 6
Analog Input Pins 6
DC Current per I/O Pin 20 mA
DC Current for 3.3V Pin 50 mA
Flash Memory 32 KB (ATmega328P)
SRAM 2 KB (ATmega328P)
EEPROM 1 KB (ATmega328P)
Clock Speed 16 MHz
32
Figure 5.2: MPU-6050 sensor
Specification Value
Sensor Type 3-axis Gyroscope + 3-axis Accelerometer
Communication Interface I2C (up to 400 kHz)
Operating Voltage 2.3V – 3.4V (typ. 3.3V)
Logic Level 3.3V (5V tolerant on breakout boards)
Gyroscope Range ±250, ±500, ±1000, ±2000 °/s
Accelerometer Range ±2g, ±4g, ±8g, ±16g
Resolution 16-bit for both gyro and accelerometer
Power Consumption ˜3.9 mA (active mode)
Default I2C Address 0x68 (or 0x69 with AD0 high)
33
Figure 5.3: Stepper Motor
34
Specification Value
Motor Type Bipolar Stepper
Step Angle 1.8◦ (200 steps/revolution)
Holding Torque 59 N·cm (83.6 oz·in)
Rated Current/Phase 2.0 A
Phase Resistance 1.4 Ω
Inductance 3.0 mH ± 20% (1 kHz)
Specification Value
Motor Supply Voltage (VMOT) 8 V to 35 V
Logic Voltage (VDD) 3 V to 5.5 V
Continuous Current per Phase Up to 1 A (without heat sink)
Maximum Current per Phase Up to 2 A (with sufficient cooling)
Microstepping Resolutions Full, 1/2, 1/4, 1/8, 1/16 steps
Control Interface STEP and DIR pins
35
Chapter 6
36
Chapter 7
37
Chapter 8
Conclusion
38
Bibliography
39
Appendix A
Arduino Code
18 # define INTERRUPT_PIN 2
19 # define LED_PIN 13 // ( Arduino is 13 , Teensy is 11 , Teensy ++ is 6)
20 bool blinkState = false ;
21
22 // For time calculations
23 unsigned long prevTime = 0;
24 float dt = 0;
25
26 // Sensor data
27 int16_t ax , ay , az ;
28 int16_t gx , gy , gz ;
29
30 void setup () {
31 # if I2 C D E V _ I M P L E M E N T A T I O N == I 2 C DE V _ AR D U IN O _ WI R E
32 Wire . begin () ;
33 Wire . setClock (400000) ; // 400 kHz I2C clock
34 # elif I 2 C D E V _ I M P L E M E N T A T I O N == I 2 C D E V _ B U I L T I N _ F A S T W I R E
35 Fastwire :: setup (400 , true ) ;
36 # endif
37
38 Serial . begin (115200) ;
39 while (! Serial ) ;
40
40
44
45 Serial . println ( F (" Testing device connections ...") ) ;
46 Serial . println ( mpu . testConnection () ? F (" MPU6050 connection
successful ") : F (" MPU6050 connection failed ") ) ;
47
48 Setpoint = 0; // Keep pendulum upright ( roll = 0)
49
50 stepper . setMaxSpeed (1000) ;
51 stepper . setAcceleration (10000) ;
52 stepper . setSpeed (1000) ;
53
54 myPID . SetMode ( AUTOMATIC ) ;
55
56 pinMode ( LED_PIN , OUTPUT ) ;
57
58 prevTime = millis () ;
59
60 // Initial Kalman angle set
61 mpu . getMotion6 (& ax , & ay , & az , & gx , & gy , & gz ) ;
62 float roll = atan2 ( ay , az ) * 180 / M_PI ;
63 kalmanX . setAngle ( roll ) ;
64 }
65
66 void loop () {
67 // Calculate dt
68 unsigned long now = millis () ;
69 dt = ( now - prevTime ) / 1000.0; // seconds
70 prevTime = now ;
71
72 // Read raw sensor data
73 mpu . getMotion6 (& ax , & ay , & az , & gx , & gy , & gz ) ;
74
75 // Calculate roll from accelerometer ( change axis if your hardware
is different )
76 float accelRoll = atan2 ( ay , az ) * 180 / M_PI ;
77
41
100 blinkState = ! blinkState ;
101 digitalWrite ( LED_PIN , blinkState ) ;
102 }
42
Appendix B
43
Appendix C
44