Documente Academic
Documente Profesional
Documente Cultură
Date: 7-6-2010
By:
Erik Torstensson, F06
Babak Rajabian, E06
Johan Andersson, E06
Kujtim Iljazi, E00
1
Abstract
This project was to investigate and implement different possible controllers for the Segway
(ElektorWheelie) to stabilize the process. In order to find the most efficient controller we
started with investigate LQR controller. The main findings were that the most effective
controller was an LQR-controller with Kalman-filtering.
2
Contents
Acknowledgments .................................................................................................................................... 1
Abstract .................................................................................................................................................... 2
1. Introduction .......................................................................................................................................... 4
1.1 The hardware ................................................................................................................................. 4
1.1.1 Motors .................................................................................................................................... 4
1.1.2 Energy source ......................................................................................................................... 4
1.1.3 Sensors and CPU ..................................................................................................................... 4
1.1.4 Software .................................................................................................................................. 4
1.2 How does it work?.......................................................................................................................... 5
2. Objectives ............................................................................................................................................. 6
2.1 Analyze ........................................................................................................................................... 6
2.2 Research ......................................................................................................................................... 6
2.3 Computing ...................................................................................................................................... 6
2.4 Programming .................................................................................................................................. 6
3. Theory ................................................................................................................................................... 7
3.1 inverted pendulum ......................................................................................................................... 7
4. The model ............................................................................................................................................. 9
4.1 mat mod ......................................................................................................................................... 9
4.2 Matlab/Simulink model ................................................................................................................ 14
4.3 Motor identification ..................................................................................................................... 14
5. Methodology ...................................................................................................................................... 17
5.1 Safety during experiments ........................................................................................................... 17
5.2 State Estimation ........................................................................................................................... 17
5.3 LQR ............................................................................................................................................... 18
5.4 Filters ............................................................................................................................................ 19
6. Time plan ............................................................................................................................................ 20
7. Findings............................................................................................................................................... 21
7 .1 State estimation .......................................................................................................................... 21
7.2 LQR results ................................................................................................................................... 21
7.3 Filters ............................................................................................................................................ 22
8. Discussion ........................................................................................................................................... 23
8.1 Future improvements .................................................................................................................. 23
8.2 Sidetracks ..................................................................................................................................... 23
Reference ............................................................................................................................................... 24
3
1. Introduction
The ElektorWheelie is a Segway developed and produced by the German company “Elektor”.
ElektorWheelie is an open source product and makes it possible to reprogram and fill in with
new features. (ElektorWheelie - Elektor’s DIY self-balancing vehicle)
1.1.1 Motors
Inside the ElektorWheelie we find two 24V DC electric motors with an effect of 1 kW that
gives a maximum speed of 12 km/h (7.5 mph). The manufacturer of the motors is “Zhejiang
Unite Motor Co., Ltd” 1. (ElektorWheelie - Elektor’s DIY self-balancing vehicle)
1.1.4 Software
The microcontroller, Atmel AVR-mega32, is programmed in BASCOM-AVR.
1
http://unitemotor.ec51.com/
2
http://en.wikipedia.org/wiki/VRLA_battery
4
1.2 How does it work?
The process is very simple and this section will describe the model the very simple way it is.
When the user stands on the ElektorWheelie the process, the sensors gyro and
accelerometer, starts to give output data into the controller. Although the output depends
on the users balance and lean-angel, make left, right, forward or backwards. When the
controller have received the output data from the process as an input. It starts to calculate
the changes that have occurred, the different states3, and gives an output to the process,
which will be the input, as seen Figure 1. The input signal will be the changed variables,
which furthermore will make it possible to balance and perform the usage of the
ElektorWheelie.
3
For more information see section 4.1
5
2. Objectives
The main objective for this project was to find a feasible controller that stabilizes the
ElektorWheelie in the best possible way. To be able to find the best feasible controller the
project was divided into several main objectives.
2.1 Analyze
To be able to gathering information about what to determine and perform is to analyze the
Segway. Without analysis there is no information to build up any conclusions. Without
conclusions it’s hard to be able to make any decisions.
2.2 Research
The objective for the research area was to understand the different parts in the Segway. The
main objective for the research part was to understand the technical information that was
given and try to find out some more. What includes for the technical information is for
instance how the sensors works, how the motors works, programming, mathematical
models for the Segway and analyze valuable data. Furthermore the given information given
during the research helped us to compute new information by our own knowledge.
2.3 Computing
The computing is the most important objective. The purpose is to compute the values that
matters and affect the Segway. This part shows how the Segway acts depending on how the
values differ. Furthermore a precision on the computing is very important to make the
process as accurate as possible.
2.4 Programming
To be able to use the ElektorWheelie it’s necessary to express all the computed and
determined parts into, in our case, BASIC with the BASCOM AVR language. So basically if
there is none computed parts there will not be any code to program since the programming
is based on the computed parts.
6
3. Theory
To understand how the Segway works in detail it’s important to introduce the inverted
pendulum.
Compared to a normal pendulum whereas it’s stable when hanging the inverted pendulum is
normally unstable, therefore it must be actively balanced in order to remain the mass above
its pivot point. To be able to balance it you can either apply a torque at the pivot point or
move the pivot point horizontally, for example on a chart as part of a feedback system.
4
http://www.control.lth.se/documents/2006/ake+06cca.pdf
7
The inverted pendulum is a classic problem in control theory. The problem maintains the
same interest, i.e. used as a benchmark for testing control algorithms and therefore as
described above there is many variations. Furthermore the experimentations are wide
because of the widely usage of this problem, but except the Segway there is other areas of
the daily usage. For example the lifting cranes on shipyards during the transportation of
containers. The cranes move the box in such a precision that the containers never swings,
they are even perfectly positioned under the operator when moving or stopping the
containers quickly5 (Websource2).
5
http://en.wikipedia.org/wiki/Inverted_pendulum
8
4. The model
x
x
A B
y Cx D
v2 (rw) 2 w2 w2
Er 2
dm 2
dm
2
r 2dm 2
I
With I r 2 dm ,which is the body’s moment of inertia
9
The Lagrangian function will be L = Kinetic Energy – Potential Energy, following:
L Ek E p
1
mv2
L IM 2 m l gcos( )
2 2
With : IM M r 2
We have that:
Therefore we get:
2 2 2 2
v l r 2 r l cos( )
2
We now get:
1
m
L Mr 2 2 l 2 2 r 2 2 2rl cos( ) m g lcos( )
2 2
1 ml2 2
L r 2 2 (M m) m r l cos( ) m g lcos( )
2 2
We now use Euler-Lagrange:
d L L
dt
d L L
dt
10
We simplify the equations:
L
m l m r l cos( )
2
L d L
d L
dt
d L L
dt m l 2
m r l( cos( ) sin( )) m r l sin( ) m l gsin( )
d L L r 2 (M m) m r l( cos( ) 2 sin( )) 0
dt
d L L
dt m l 2
m r l cos( ) m l gsin( )
d L L r 2 (M m) m r l cos( ) m r l 2 sin( )
dt
We now have:
d L L
dt m l m r l cos( ) m l gsin( )
2
d L L r 2 (M m) m r l cos( ) m r l 2 sin( )
dt
sin( )
and
cos( ) 1
11
Therefore we can rewrite the equations as:
d L L
dt m l 2
m r l m l g
d L L r 2 (M m) m r l m r l 2
dt
Furthermore we have:
2
m l m r l m l g
r (M m) m r l m r l
2 2
2
m l m r l m l g (1)
r (M m) m r l
2
(2)
If we now solve and put into (1) we get:
m r l
r (M m)
2
r (M m)
2
m r l
m l 2 m l g m r l 2 2
r (M m) r (M m)
m 2 r 2 l 2 m r l
m l 2 m l g 2 1
r (M m)
2
r (M m)
m l 2 r 2 (M m) m 2 r 2 l 2 r 2 (M m) m r l
m l g
r 2 (M m) r (M m)
2
(M m)g r(M m) m l
2
Ml r l M m
12
Now, insertion of into (2) will give us:
M m r l 2 m r 2 l(M m) m 2 r l 2 m g
r 2 (M m)M m r l 2 M r
M l 2 M rl m r l m l 2 m g
r 2 (M m)M l 2 M r
(l 2 r l)(M m) m g
2 2
r (M m)M l M r
(l 2 r l) m g
2 2
r M l M r
l r m g
2
r M l M r
lr mg
2
r lM Mr
13
So, now we have:
2
r l M Mr
If we introduce:
x1 x
1 x 2
x 2 x 2
x 3
x x
3 4
x 4
x 4
We can rewrite:
2
r l M Mr
x1 0 1 0 0 0
(M m)g x1 (M r m r m l)
0 0 0
x 2 M l x2 M m l 2r
0
0 0 1x 3 0
x 3 m g rl
Mr
0 0 0 x 4
M l r2
x 4
Which is x A x B
4.2 Matlab/Simulink model
The statespace model and the motor model of the Elektor Wheelie where combined and
added to a Matlab script. This made it easy to develop and test the LQED estimater, LQRD
controller and the moving average filters on real data from the sensors and motors PWM
from the Wheelie. Being able to get nice plots.
𝜏 = −𝛽1 𝜑 − 𝜃 + 𝛽2 𝑢
14
To estimate 2 a voltage to the motor was applied and then measuring the torque required
stopping the wheel from spinning. This was done for different voltages. The resulting points
are interpolated with a first degree linear polynomial. The resulting figure for the Elektor
Wheelie motor is in Figure 5 for 2. With a 2 = 3.6136 Nm/Volts.
80
70
60
50
Torque [Nm]
40
30
20
10
-10
0 5 10 15 20 25
Voltage [Volt]
Figure 5 - Plot of the resulting torque versus applied voltage of the Elektor Wheelie DC motor
The parameter 1 in the DC motor model is estimated by measuring the revolutions of the
wheel in radianse after applying a voltage to the DC motor. Having a estimate of 2 and
letting the wheel spinn without applying a stopping torque the parameter 1 can be solved
for. The results after experiments are in Figure 6.
15
Scatterplot of Beta1: Mean value = 0.3626
1.6
1.4
1.2
0.8
Beta1
0.6
0.4
0.2
-0.2
0 5 10 15 20 25
Measurement
Figure 6 - ters
16
5. Methodology
The methods were in general to derive controller offline using Matlab, and later test the
controller on the real process, with some safety measures.
𝑦𝑔𝑦𝑟𝑜 = 𝛩 + 𝑑 + 𝑤1 (1)
𝑦𝑎𝑐𝑐 = 𝛩 + 𝑣1 (2)
𝛩 = 0 −1 𝛩 1 𝑤1
+ 𝑦𝑔𝑦𝑟𝑜 + 𝐺 𝑤
d 0 0 𝑑 0 2
𝛩
𝑦𝑎𝑐𝑐 = 1 0 + 𝑣1
𝑑
This is a system with the gyroscope signal as input and accelerometer signal as
output and hence a kalman filter can be derived to estimate the states. Equation (1)
describes the signal from the gyroscope really good, the angle velocity but with a
drift is exactly what this signal is. Equation (2) is not that good as the accelerometer
is really measuring both the angle and the acceleration when the segway is going
backwards and forwards. The acceleration is because of this considered as noise.
17
The next thing to be done is to make a kalman filter for this system. As this system is
written in normal state space form this is not very hard to achieve. Below are the
system equations for a kalman filter.
𝑥 = 𝐴𝑥 + 𝐵𝑢 + 𝐾(𝑦 − 𝐶𝑥 )
𝑦 = 𝐶𝑥
𝑥 = (𝐴 − 𝐾𝐶)𝑥 + 𝐵𝑢 + 𝐾𝑦
𝑦 = 𝐶𝑥
The estimation of the states angle and drift from the kalman filter is then actually the
output from the following system.
𝛩 = 𝐴𝑒 𝛩 + 𝐵𝑒 𝑦𝑔𝑦𝑟𝑜
𝑦𝑎𝑐𝑐 𝐴𝑒 = 𝐴 − 𝐾𝐶 𝐵𝑒 = 𝐵 𝐾
𝑑 𝑑 With 1 0
𝐶𝑒 =
𝑦 = 𝐶𝑒 𝛩 0 1
𝑑
The only thing that is needed know to get this system is the K which completes this
kalman filter. The Matlab command lqe was used to get a good K. After this the
continuous system has to be discretizised which was done with the Matlab command
c2d. Actually these two commands were used over and over again and the kalman
filter was then tested in Matlab with some data which was collected from the actual
process. This way the inputs to the lqe command was tuned to get a good kalman
filter.
5.3 LQR
To control the process, state feedback was used where the gain on different states was
calculated with a discrete linear quadratic regulator in Matlab, based on the mathematical
model of the process and the motor. The feedback gains on different states are calculated
offline in Matlab with the command lqrd. This control gives feedback on all the three states
(body angle, body angle rate, and wheel angle rate).The lqrd needs some design parameters
on the weighting matrices for both the importance of states, and the cost of the control
signal. The feedback vector L is then used to close the loop with the feedback law
(1) 𝑢 = −𝐿𝑥
18
5.4 Filters
Sensors often have some kind of noise and sometimes it is needed to filter the
signals. On the process concerned in this project there are three sensors. The
gyroscope, the accelerometer and the rocker. All three of them needed some kind of
low pass filter because of some high frequency noise. Especially the accelerometer
had to be filtered as the signal also consists of the acceleration which is considered
as noise. The acceleration was seen as big spikes in the signal and these spikes
occurred because of some backlash in the motors making the segway to shake while
standing still. There is almost an infinite amount of low pass filters to choose from
but some sophisticated filter is not needed in this case. The reason for this is that a
perfect signal is not needed and it will be implemented in a microcontroller. The kind
of filter chosen was an easy moving average filter.
19
6. Time plan
The project was lagging behind the original time plan quite early due to mathematical model
taking a bit longer then we thought. This made us abandon our plans on testing different
kind of controllers and we went straight for LQR. A lot of time was spent trying to derive and
implement a controller in Simulink. After a couple of weeks we were more and more lacking
behind our time plans, we realized that we were travelling down a sidetrack. In study week 7
we made a big breakthrough and the segway could stabilize. We had thus used up both of
our reserve weeks and still got a report to finish. Testing different controllers, as well as
improving steering and start up did not get the time that we had wished.
20
7. Findings
7 .1 State estimation
We tuned the kalman filter quite a bit to get a good estimation. To tune the filter
there is three weights that are changed to get a good explanation of how much noise
there is on each signal. We knew that we should trust the gyroscope more than the
accelerometer because the accelerometer has dynamics not described in the model.
The weight on the drift was known to be quite small as the drift is a slow change in
the gyroscope. After some tuning we got the following.
𝛩 = 0 −1 𝛩 1 𝑤1
+ 𝑦𝑔𝑦𝑟𝑜 + 𝐺 𝑤
𝑑 0 0 𝑑 0 2
𝛩
𝑦𝑎𝑐𝑐 = 1 0 + 𝑣1
𝑑
1 0
𝐸(𝑤𝑤 𝑇 ) = 𝑄 = 𝐸(𝑣𝑣 𝑇 ) = 𝑅 = 10
0 4
Here the first element in Q is saying how big the noise is on the gyroscope; the last
element in Q is how big the noise is on the drift, i.e. how fast the drift is changing. R
is saying how big the noise is on the accelerometer. As can be seen the parameters
we finally settled for is kind of expected with a large noise on the accelerometer, this
is basically saying that we trust more on the gyroscope. With these inputs to the lqe
command we get 𝐾 = (1.1683 − 0.6325)𝑇 which get the following kalman filter.
21
do so. Estimation of the wheel velocity was made based on the mathematical model and the
estimation of 𝛩 and 𝛩 that was made in the LQE kalman filter, these estimation seemed
however to be a bit unreliable and was disregarded in the feedback loop after some testing.
Another reason to not use the third state in the feedback is that the controller would then
like to force the segway to stand still, which apparently is not optimal for a transportation
vehicle.
The weightings were made so that the body angle 𝛩 was most important to control to the
reference value (upright position), and the weighting on the third state was put to zero.
700 0 0
𝑄= 0 0.01 0
0 0 0
7.3 Filters
As said before we chose a moving average filter for our sensors. A moving average
filter can be implemented in a number of different ways but as this is done online we
can only use old data for filtering. There is also different ways to implement an online
moving average filter with for example different weights for older data and so on. We
chose the easiest one with the weight one for every old data. To save memory in the
microcontroller we did not save all the old data but instead we had a sum which we
updated in the following way.
𝑛−1
𝑆 𝑘 = 𝑆 𝑘−1 +𝑦 𝑘
𝑛
𝑆(𝑘)
𝑦𝑎𝑣𝑔 (𝑘) =
𝑛
We averaged the gyroscope and the accelerometer over twenty data points and the
rocker over ten data points. As a moving average filter like this actually delays the
real signal, we had to find a number where we had a good low pass filtering and at
the same time not a too long delay. We found out that as we had a delay on the
signals with this filter we really had to have the same length both on the gyroscope
and the accelerometer. If we did not have this the signals was not working together
and we did not get a good estimation.
22
8. Discussion
The segway ended up with a state feedback controller where two out of three states was
used. The final code did succeed with both the balancing problem, and even removed a big
part of the “backlashing”-like effect that occur. The segway now demands more tilt to get to
same speeds as the original code did. We spent a lot of time on the mathematical model
aswell as the simulink models. However our simulink models were build with two Kalman
filters, a method we later threw away. After that sidetrack was corrected, it went quite fast
and only fine tuning remained.
The things that have made the biggest improvements in comparison to prior code are the
filtering that we applied to the steering, and the ramping of the control signal during the
start up.
8.2 Sidetracks
For quite some time in the middle of the project we tried to use two different Kalman filters,
one that was based on the Kalman filter in YAIP by Johan Åkesson, and one that calculated
all three states based on the mathematical model we had derived. The original reason for
these attempts where that we wanted to estimate the wheel angular velocity as well as the
two other states. Our simulink models used this method. When we finally removed this filter
and only used the feedback that was calculated from the mathematical model we
implemented it on the real process due to time limit.
Another sidetrack we explored was how the different states were declared. More specifically
we initially used 𝛳 ,𝛳 , x and 𝑥 where x and 𝑥 is the forward position and velocity.
It is of course closely related to wheel angle, and wheel angle rate, and the reason we chose
wheel angle was mainly to make the comparison of our mathematical model with known
models easier.
23
Reference
24