snark
constant_speed.h
1 // This file is part of snark, a generic and flexible library
2 // for robotics research.
3 //
4 // Copyright (C) 2011 The University of Sydney
5 //
6 // snark is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // snark is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 // for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with snark. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef SNARK_FILTER_SIMPLE_CONSTANT_SPEED_H
20 #define SNARK_FILTER_SIMPLE_CONSTANT_SPEED_H
21 
22 #include <Eigen/Core>
23 
24 namespace snark{
25 
27 template< unsigned int N >
29 {
30 
31 typedef Eigen::Matrix< double, ( N << 1 ), 1 > state_type;
32 typedef Eigen::Matrix< double, ( N << 1 ), ( N << 1 ) > covariance_type;
33 
35 struct state
36 {
37  state();
38  state( const state_type& s );
39  void set_innovation( const state_type& innovation );
40 
41  static const unsigned int dimension = ( N << 1 );
42  state_type state_vector;
43  covariance_type covariance;
44 };
45 
47 struct model
48 {
49  model( double noiseSigma );
50 
51  const covariance_type& jacobian( const state & s, double dt );
52 
53  void update_state( state & s, double dt ) const;
54 
55  const covariance_type& noise_covariance( double dt );
56 
57  state_type sigma;
58  covariance_type A;
59  covariance_type noise;
60 };
61 
63 struct position
64 {
65  typedef Eigen::Matrix< double, N, 1 > position_type;
66 
67  position( const position_type& p, double sigma );
68 
69  const Eigen::Matrix< double, N, ( N << 1 ) >& measurement_jacobian( const state & s ) const
70  {
71  return jacobian;
72  }
73 
74  const Eigen::Matrix< double, N, N >& measurement_covariance( const state & s ) const
75  {
76  return covariance;
77  }
78 
79  position_type innovation( const state& s ) const
80  {
81  return ( position_vector - jacobian * s.state_vector );
82  }
83 
84  static const int dimension = N;
85  position_type position_vector;
86  Eigen::Matrix< double, N, N > covariance;
87  Eigen::Matrix< double, N, ( N << 1 ) > jacobian;
88 };
89 
90 };
91 
93 template< unsigned int N >
95  state_vector( state_type::Zero() ),
96  covariance( covariance_type::Zero() )
97 {
98 
99 }
100 
102 template< unsigned int N >
103 constant_speed< N >::state::state( const state_type& s ):
104  state_vector( s ),
105  covariance( covariance_type::Zero() )
106 {
107 
108 }
109 
111 template< unsigned int N >
112 void constant_speed< N >::state::set_innovation( const Eigen::Matrix< double, ( N << 1 ), 1 >& innovation )
113 {
114  state_vector += innovation;
115 }
116 
117 
121 template< unsigned int N >
123  sigma( state_type::Ones() * noiseSigma ),
124  A( covariance_type::Identity() ),
125  noise( covariance_type::Zero() )
126 {
127 
128 }
129 
131 template< unsigned int N >
132 const typename constant_speed< N >::covariance_type& constant_speed< N >::model::jacobian( const typename constant_speed< N >::state& s, double dt )
133 {
134  A.block( 0, N, N, N ) = Eigen::Matrix< double, N, N >::Identity() * dt;
135  return A;
136 }
137 
139 template< unsigned int N >
141 {
142  s.state_vector = A * s.state_vector;
143 }
144 
146 template< unsigned int N >
147 const typename constant_speed< N >::covariance_type& constant_speed< N >::model::noise_covariance( double dt )
148 {
149  for( unsigned int i = 0; i < ( N << 1 ); i++ )
150  {
151  noise(i,i) = dt * sigma[i];
152  }
153  return noise;
154 }
155 
159 template< unsigned int N >
160 constant_speed< N >::position::position( const position_type& p, double sigma ):
161  position_vector( p ),
162  covariance( Eigen::Matrix< double, N, N >::Identity()*sigma ),
163  jacobian( Eigen::Matrix< double, N, ( N << 1 ) >::Identity() )
164 {
165 
166 }
167 
168 
169 }
170 
171 #endif // SNARK_FILTER_SIMPLE_CONSTANT_SPEED_H