tracker_kalman.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Wim Meeussen */
36 
38 
39 using namespace MatrixWrapper;
40 using namespace BFL;
41 using namespace tf;
42 using namespace std;
43 using namespace ros;
44 
45 
46 const static double damping_velocity = 0.9;
47 
48 
49 namespace estimation
50 {
51 // constructor
52 TrackerKalman::TrackerKalman(const string& name, const StatePosVel& sysnoise):
53  Tracker(name),
54  filter_(NULL),
55  sys_pdf_(NULL),
56  sys_model_(NULL),
57  meas_pdf_(NULL),
58  meas_model_(NULL),
59  sys_matrix_(6, 6),
60  tracker_initialized_(false)
61 {
62  // create sys model
63  sys_matrix_ = 0;
64  for (unsigned int i = 1; i <= 3; i++)
65  {
66  sys_matrix_(i, i) = 1;
67  sys_matrix_(i + 3, i + 3) = damping_velocity;
68  }
69  ColumnVector sys_mu(6);
70  sys_mu = 0;
71  sys_sigma_ = SymmetricMatrix(6);
72  sys_sigma_ = 0;
73  for (unsigned int i = 0; i < 3; i++)
74  {
75  sys_sigma_(i + 1, i + 1) = pow(sysnoise.pos_[i], 2);
76  sys_sigma_(i + 4, i + 4) = pow(sysnoise.vel_[i], 2);
77  }
78  Gaussian sys_noise(sys_mu, sys_sigma_);
79  sys_pdf_ = new LinearAnalyticConditionalGaussian(sys_matrix_, sys_noise);
80  sys_model_ = new LinearAnalyticSystemModelGaussianUncertainty(sys_pdf_);
81 
82 
83  // create meas model
84  Matrix meas_matrix(3, 6);
85  meas_matrix = 0;
86  for (unsigned int i = 1; i <= 3; i++)
87  meas_matrix(i, i) = 1;
88 
89  ColumnVector meas_mu(3);
90  meas_mu = 0;
91  SymmetricMatrix meas_sigma(3);
92  meas_sigma = 0;
93  for (unsigned int i = 0; i < 3; i++)
94  meas_sigma(i + 1, i + 1) = 0;
95  Gaussian meas_noise(meas_mu, meas_sigma);
96  meas_pdf_ = new LinearAnalyticConditionalGaussian(meas_matrix, meas_noise);
97  meas_model_ = new LinearAnalyticMeasurementModelGaussianUncertainty(meas_pdf_);
98 };
99 
100 
101 
102 // destructor
104 {
105  if (filter_) delete filter_;
106  if (sys_pdf_) delete sys_pdf_;
107  if (sys_model_) delete sys_model_;
108  if (meas_pdf_) delete meas_pdf_;
109  if (meas_model_) delete meas_model_;
110 };
111 
112 
113 
114 // initialize prior density of filter
115 void TrackerKalman::initialize(const StatePosVel& mu, const StatePosVel& sigma, const double time)
116 {
117  ColumnVector mu_vec(6);
118  SymmetricMatrix sigma_vec(6);
119  sigma_vec = 0;
120  for (unsigned int i = 0; i < 3; i++)
121  {
122  mu_vec(i + 1) = mu.pos_[i];
123  mu_vec(i + 4) = mu.vel_[i];
124  sigma_vec(i + 1, i + 1) = pow(sigma.pos_[i], 2);
125  sigma_vec(i + 4, i + 4) = pow(sigma.vel_[i], 2);
126  }
127  prior_ = Gaussian(mu_vec, sigma_vec);
128  filter_ = new ExtendedKalmanFilter(&prior_);
129 
130  // tracker initialized
131  tracker_initialized_ = true;
132  quality_ = 1;
133  filter_time_ = time;
134  init_time_ = time;
135 }
136 
137 
138 
139 
140 // update filter prediction
141 bool TrackerKalman::updatePrediction(const double time)
142 {
143  bool res = true;
144  if (time > filter_time_)
145  {
146  // set dt in sys model
147  for (unsigned int i = 1; i <= 3; i++)
148  sys_matrix_(i, i + 3) = time - filter_time_;
149  sys_pdf_->MatrixSet(0, sys_matrix_);
150 
151  // scale system noise for dt
152  sys_pdf_->AdditiveNoiseSigmaSet(sys_sigma_ * pow(time - filter_time_, 2));
153  filter_time_ = time;
154 
155  // update filter
156  res = filter_->Update(sys_model_);
157  if (!res) quality_ = 0;
158  else quality_ = calculateQuality();
159  }
160  return res;
161 };
162 
163 
164 
165 // update filter correction
166 bool TrackerKalman::updateCorrection(const tf::Vector3& meas, const MatrixWrapper::SymmetricMatrix& cov)
167 {
168  assert(cov.columns() == 3);
169 
170  // copy measurement
171  ColumnVector meas_vec(3);
172  for (unsigned int i = 0; i < 3; i++)
173  meas_vec(i + 1) = meas[i];
174 
175  // set covariance
176  ((LinearAnalyticConditionalGaussian*)(meas_model_->MeasurementPdfGet()))->AdditiveNoiseSigmaSet(cov);
177 
178  // update filter
179  bool res = filter_->Update(meas_model_, meas_vec);
180  if (!res) quality_ = 0;
181  else quality_ = calculateQuality();
182 
183  return res;
184 };
185 
186 
188 {
189  ColumnVector tmp = filter_->PostGet()->ExpectedValueGet();
190  for (unsigned int i = 0; i < 3; i++)
191  {
192  est.pos_[i] = tmp(i + 1);
193  est.vel_[i] = tmp(i + 4);
194  }
195 };
196 
197 
198 void TrackerKalman::getEstimate(people_msgs::PositionMeasurement& est) const
199 {
200  ColumnVector tmp = filter_->PostGet()->ExpectedValueGet();
201 
202  est.pos.x = tmp(1);
203  est.pos.y = tmp(2);
204  est.pos.z = tmp(3);
205 
206  est.header.stamp.fromSec(filter_time_);
207  est.object_id = getName();
208 }
209 
210 
211 
212 
214 {
215  double sigma_max = 0;
216  SymmetricMatrix cov = filter_->PostGet()->CovarianceGet();
217  for (unsigned int i = 1; i <= 2; i++)
218  sigma_max = max(sigma_max, sqrt(cov(i, i)));
219 
220  return 1.0 - min(1.0, sigma_max / 1.5);
221 }
222 
223 
225 {
227  return filter_time_ - init_time_;
228  else
229  return 0;
230 }
231 
233 {
235  return filter_time_;
236  else
237  return 0;
238 }
239 
240 }; // namespace
241 
242 
virtual void getEstimate(BFL::StatePosVel &est) const
get filter posterior
const std::string & getName() const
return the name of the tracker
Definition: tracker.h:59
virtual double getTime() const
return the time of the tracker
virtual double getLifetime() const
return the lifetime of the tracker
BFL::ExtendedKalmanFilter * filter_
Class representing state with pos and vel.
Definition: state_pos_vel.h:46
BFL::LinearAnalyticMeasurementModelGaussianUncertainty * meas_model_
static const double damping_velocity
BFL::LinearAnalyticSystemModelGaussianUncertainty * sys_model_
virtual ~TrackerKalman()
destructor
virtual void initialize(const BFL::StatePosVel &mu, const BFL::StatePosVel &sigma, const double time)
initialize tracker
MatrixWrapper::Matrix sys_matrix_
BFL::LinearAnalyticConditionalGaussian * meas_pdf_
BFL::LinearAnalyticConditionalGaussian * sys_pdf_
tf::Vector3 vel_
Definition: state_pos_vel.h:49
int min(int a, int b)
tf::Vector3 pos_
Definition: state_pos_vel.h:49
virtual bool updateCorrection(const tf::Vector3 &meas, const MatrixWrapper::SymmetricMatrix &cov)
MatrixWrapper::SymmetricMatrix sys_sigma_
virtual bool updatePrediction(const double time)
update tracker


people_tracking_filter
Author(s): Caroline Pantofaru
autogenerated on Fri Jun 7 2019 22:07:49