simple_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 Montral
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 "simple_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  W(1,1) = 0.0;
114  W(1,2) = 0.0;
115  W(2,1) = 1.0;
116  W(2,2) = 0.0;
117  W(3,1) = 0.0;
118  W(3,2) = 0.0;
119  W(4,1) = 0.0;
120  W(4,2) = 1.0;
121 }
122 
124 {
125  Q(1,1) = 0.01*0.01;
126  Q(1,2) = 0.01*0.01/10.0;
127  Q(2,1) = 0.01*0.01/10.0;
128  Q(2,2) = 0.01*0.01;
129 }
130 
132 {
133  H(1,1) = -x(3)/(x(1)*x(1)+x(3)*x(3));
134  H(1,2) = 0.0;
135  H(1,3) = x(1)/(x(1)*x(1)+x(3)*x(3));
136  H(1,4) = 0.0;
137 
138  H(2,1) = x(1)/sqrt(x(1)*x(1)+x(3)*x(3));
139  H(2,2) = 0.0;
140  H(2,3) = x(3)/sqrt(x(1)*x(1)+x(3)*x(3));
141  H(2,4) = 0.0;
142 }
143 
145 {
146  V(1,1) = 1.0;
147  V(1,2) = 0.0;
148  V(2,1) = 0.0;
149  V(2,2) = 1.0;
150 }
151 
153 {
154  R(1,1) = 0.01*0.01;
155  R(1,2) = 0.0;
156  R(2,1) = 0.0;
157  R(2,2) = 50*50;
158 }
159 
161 {
162  Vector x_(x.size());
163  x_(1) = x(1) + x(2)*Period + (Period*Period)/2*(u(1)/Mass - Bfriction/Mass*x(2)*x(2));
164  x_(2) = x(2) + (u(1)/Mass - Bfriction/Mass*x(2)*x(2))*Period;
165  x_(3) = x(3) + x(4)*Period + (Period*Period)/2*(Portance/Mass*x(2)*x(2)-Gravity);
166  x_(4) = x(4) + (Portance/Mass*x(2)*x(2)-Gravity)*Period;
167  x.swap(x_);
168 }
169 
171 {
172  z(1)=atan2(x(3), x(1));
173  z(2)=sqrt(x(1)*x(1)+x(3)*x(3));
174 }
175 
void makeA()
Virtual creator of A.
void makeR()
Virtual creator of R.
void makeW()
Virtual creator of W.
void makeQ()
Virtual creator of Q.
void swap(KVector &v)
Constant-time swap function between two vectors.
void makeH()
Virtual creator of H.
void makeV()
Virtual creator of V.
void makeProcess()
Actual process . Fills in new x by using old x.
void makeMeasure()
Actual measurement function . Fills in z.


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