plane.cpp
Go to the documentation of this file.
1 // -------------- plane.cpp - Example of Extended Kalman filter ------------------------//
2 //
3 // This file is part of kfilter.
4 // kfilter is a C++ variable-dimension extended kalman filter library.
5 //
6 // Copyright (C) 2004 Vincent Zalzal, Sylvain Marleau
7 // Copyright (C) 2001, 2004 Richard Gourdeau
8 // Copyright (C) 2004 GRPR and DGE's Automation sector
9 // École Polytechnique de Montréal
10 //
11 // Code adapted from algorithms presented in :
12 // Bierman, G. J. "Factorization Methods for Discrete Sequential
13 // Estimation", Academic Press, 1977.
14 //
15 // This library is free software; you can redistribute it and/or
16 // modify it under the terms of the GNU Lesser General Public
17 // License as published by the Free Software Foundation; either
18 // version 2.1 of the License, or (at your option) any later version.
19 //
20 // This library is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 // Lesser General Public License for more details.
24 //
25 // You should have received a copy of the GNU Lesser General Public
26 // License along with this library; if not, write to the Free Software
27 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 
29 // --------------------------- Example of Extended Kalman filter ------------------------//
30 /*
31 % A plane flights in a 2D space where the x axis is the distance traveled
32 % by the plane and y axis is its altitude. This system can be represented
33 % by the fallowing equations:
34 % (This is just an example)
35 %
36 % xpp = F/m - bx/m * xp^2
37 % ypp = p/m * xp^2 - g
38 %
39 % where m is the plane's weight (1000 kg)
40 % bx is the drag coefficient (0.35 N/m²/s²)
41 % p is the lift force (3.92 N/m²/s²)
42 % g is the gravitational acceleration (9.8 m/s²)
43 % F is the motor's thrust
44 %
45 % A station on the ground (at the origin) mesures the angle between the
46 % plane and the ground (x axis) and the distance between the plane and the station.
47 % These measures are based and the fallowing equations:
48 %
49 % theta = atan2(y,x)
50 % r = sqrt(x^2+y^2)
51 %
52 % The variance error matrix of the mesures is:
53 %
54 % R = [0.01^2 0
55 % 0 50^2]
56 %
57 % V = [1 0;
58 % 0 1];
59 %
60 % The variance error matrix of the plane's model is: WQW'
61 %
62 % Q = [0.01^2 0;
63 % 0 0.01^2];
64 %
65 % W = [0 0;
66 % 1 0;
67 % 0 0;
68 % 0 1];
69 %
70 */
71 
72 #include "plane.h"
73 #include <cmath>
74 #include <iostream>
75 
76 using namespace std;
77 
79 {
80  setDim(4, 1, 2, 2, 2);
81  Period = 0.2;
82  Gravity = 9.8;
83  Bfriction = 0.35;
84  Portance = 3.92;
85  Mass = 1000;
86 }
87 
89 {
90  A(1,1) = 1.0;
91  // A(1,2) = Period - Period*Period*Bfriction/Mass*x(2);
92  A(1,3) = 0.0;
93  A(1,4) = 0.0;
94 
95  A(2,1) = 0.0;
96  // A(2,2) = 1 - 2*Period*Bfriction/Mass*x(2);
97  A(2,3) = 0.0;
98  A(2,4) = 0.0;
99 
100  A(3,1) = 0.0;
101  // A(3,2) = Period*Period*Portance/Mass*x(2);
102  A(3,3) = 1.0;
103  A(3,4) = Period;
104 
105  A(4,1) = 0.0;
106  // A(4,2) = 2*Period*Portance/Mass*x(2);
107  A(4,3) = 0.0;
108  A(4,4) = 1.0;
109 }
110 
112 {
113  // A(1,1) = 1.0;
114  A(1,2) = Period - Period*Period*Bfriction/Mass*x(2);
115  // A(1,3) = 0.0;
116  // A(1,4) = 0.0;
117 
118  // A(2,1) = 0.0;
119  A(2,2) = 1 - 2*Period*Bfriction/Mass*x(2);
120  // A(2,3) = 0.0;
121  // A(2,4) = 0.0;
122 
123  // A(3,1) = 0.0;
124  A(3,2) = Period*Period*Portance/Mass*x(2);
125  // A(3,3) = 1.0;
126  // A(3,4) = Period;
127 
128  // A(4,1) = 0.0;
129  A(4,2) = 2*Period*Portance/Mass*x(2);
130  // A(4,3) = 0.0;
131  // A(4,4) = 1.0;
132 }
133 
135 {
136  W(1,1) = 0.0;
137  W(1,2) = 0.0;
138  W(2,1) = 1.0;
139  W(2,2) = 0.0;
140  W(3,1) = 0.0;
141  W(3,2) = 0.0;
142  W(4,1) = 0.0;
143  W(4,2) = 1.0;
144 }
145 
147 {
148  Q(1,1) = 0.01*0.01;
149  Q(1,2) = 0.01*0.01/10.0;
150  Q(2,1) = 0.01*0.01/10.0;
151  Q(2,2) = 0.01*0.01;
152 }
153 
155 {
156  // H(1,1) = -x(3)/(x(1)*x(1)+x(3)*x(3));
157  H(1,2) = 0.0;
158  // H(1,3) = x(1)/(x(1)*x(1)+x(3)*x(3));
159  H(1,4) = 0.0;
160 
161  // H(2,1) = x(1)/sqrt(x(1)*x(1)+x(3)*x(3));
162  H(2,2) = 0.0;
163  // H(2,3) = x(3)/sqrt(x(1)*x(1)+x(3)*x(3));
164  H(2,4) = 0.0;
165 }
166 
168 {
169  H(1,1) = -x(3)/(x(1)*x(1)+x(3)*x(3));
170  // H(1,2) = 0.0;
171  H(1,3) = x(1)/(x(1)*x(1)+x(3)*x(3));
172  // H(1,4) = 0.0;
173 
174  H(2,1) = x(1)/sqrt(x(1)*x(1)+x(3)*x(3));
175  // H(2,2) = 0.0;
176  H(2,3) = x(3)/sqrt(x(1)*x(1)+x(3)*x(3));
177  // H(2,4) = 0.0;
178 }
179 
181 {
182  V(1,1) = 1.0;
183  V(2,2) = 1.0;
184 }
185 
187 {
188  R(1,1) = 0.01*0.01;
189  R(2,2) = 50*50;
190 }
191 
193 {
194  Vector x_(x.size());
195  x_(1) = x(1) + x(2)*Period + (Period*Period)/2*(u(1)/Mass - Bfriction/Mass*x(2)*x(2));
196  x_(2) = x(2) + (u(1)/Mass - Bfriction/Mass*x(2)*x(2))*Period;
197  x_(3) = x(3) + x(4)*Period + (Period*Period)/2*(Portance/Mass*x(2)*x(2)-Gravity);
198  x_(4) = x(4) + (Portance/Mass*x(2)*x(2)-Gravity)*Period;
199  x.swap(x_);
200 }
201 
203 {
204  z(1)=atan2(x(3), x(1));
205  z(2)=sqrt(x(1)*x(1)+x(3)*x(3));
206 }
207 
void makeBaseQ()
Virtual pre-creator of Q.
Definition: plane.cpp:146
void makeBaseW()
Virtual pre-creator of W.
Definition: plane.cpp:134
void makeBaseV()
Virtual pre-creator of V.
Definition: plane.cpp:180
cPlaneEKF()
Definition: plane.cpp:78
void makeBaseA()
Virtual pre-creator of A.
Definition: plane.cpp:88
void makeA()
Virtual creator of A.
Definition: plane.cpp:111
void makeH()
Virtual creator of H.
Definition: plane.cpp:167
void makeBaseH()
Virtual pre-creator of H.
Definition: plane.cpp:154
void swap(KVector &v)
Constant-time swap function between two vectors.
void makeProcess()
Actual process . Fills in new x by using old x.
Definition: plane.cpp:192
void makeBaseR()
Virtual pre-creator of R.
Definition: plane.cpp:186
void makeMeasure()
Actual measurement function . Fills in z.
Definition: plane.cpp:202


kfilter
Author(s): Jorge Almeida, Vincent Zalzal, Sylvain Marleau, Richard Gourdeau
autogenerated on Mon Mar 2 2015 01:31:45