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 #include <algorithm>
39 #include <string>
40 
41 static const double damping_velocity = 0.9;
42 
43 namespace estimation
44 {
45 
46 using MatrixWrapper::Matrix;
47 using MatrixWrapper::ColumnVector;
48 using MatrixWrapper::SymmetricMatrix;
49 
50 // constructor
51 TrackerKalman::TrackerKalman(const std::string& name, const BFL::StatePosVel& sysnoise):
52  Tracker(name),
53  filter_(NULL),
54  sys_pdf_(NULL),
55  sys_model_(NULL),
56  meas_pdf_(NULL),
57  meas_model_(NULL),
58  sys_matrix_(6, 6),
59  tracker_initialized_(false)
60 {
61  // create sys model
62  sys_matrix_ = 0;
63  for (unsigned int i = 1; i <= 3; i++)
64  {
65  sys_matrix_(i, i) = 1;
66  sys_matrix_(i + 3, i + 3) = damping_velocity;
67  }
68  ColumnVector sys_mu(6);
69  sys_mu = 0;
70  sys_sigma_ = SymmetricMatrix(6);
71  sys_sigma_ = 0;
72  for (unsigned int i = 0; i < 3; i++)
73  {
74  sys_sigma_(i + 1, i + 1) = pow(sysnoise.pos_[i], 2);
75  sys_sigma_(i + 4, i + 4) = pow(sysnoise.vel_[i], 2);
76  }
77  BFL::Gaussian sys_noise(sys_mu, sys_sigma_);
80 
81  // create meas model
82  Matrix meas_matrix(3, 6);
83  meas_matrix = 0;
84  for (unsigned int i = 1; i <= 3; i++)
85  meas_matrix(i, i) = 1;
86 
87  ColumnVector meas_mu(3);
88  meas_mu = 0;
89  SymmetricMatrix meas_sigma(3);
90  meas_sigma = 0;
91  for (unsigned int i = 0; i < 3; i++)
92  meas_sigma(i + 1, i + 1) = 0;
93  BFL::Gaussian meas_noise(meas_mu, meas_sigma);
94  meas_pdf_ = new BFL::LinearAnalyticConditionalGaussian(meas_matrix, meas_noise);
96 };
97 
98 // destructor
100 {
101  if (filter_) delete filter_;
102  if (sys_pdf_) delete sys_pdf_;
103  if (sys_model_) delete sys_model_;
104  if (meas_pdf_) delete meas_pdf_;
105  if (meas_model_) delete meas_model_;
106 };
107 
108 // initialize prior density of filter
109 void TrackerKalman::initialize(const BFL::StatePosVel& mu, const BFL::StatePosVel& sigma, const double time)
110 {
111  ColumnVector mu_vec(6);
112  SymmetricMatrix sigma_vec(6);
113  sigma_vec = 0;
114  for (unsigned int i = 0; i < 3; i++)
115  {
116  mu_vec(i + 1) = mu.pos_[i];
117  mu_vec(i + 4) = mu.vel_[i];
118  sigma_vec(i + 1, i + 1) = pow(sigma.pos_[i], 2);
119  sigma_vec(i + 4, i + 4) = pow(sigma.vel_[i], 2);
120  }
121  prior_ = BFL::Gaussian(mu_vec, sigma_vec);
123 
124  // tracker initialized
125  tracker_initialized_ = true;
126  quality_ = 1;
127  filter_time_ = time;
128  init_time_ = time;
129 }
130 
131 // update filter prediction
132 bool TrackerKalman::updatePrediction(const double time)
133 {
134  bool res = true;
135  if (time > filter_time_)
136  {
137  // set dt in sys model
138  for (unsigned int i = 1; i <= 3; i++)
139  sys_matrix_(i, i + 3) = time - filter_time_;
141 
142  // scale system noise for dt
143  sys_pdf_->AdditiveNoiseSigmaSet(sys_sigma_ * pow(time - filter_time_, 2));
144  filter_time_ = time;
145 
146  // update filter
147  res = filter_->Update(sys_model_);
148  if (!res) quality_ = 0;
149  else quality_ = calculateQuality();
150  }
151  return res;
152 };
153 
154 // update filter correction
155 bool TrackerKalman::updateCorrection(const tf::Vector3& meas, const MatrixWrapper::SymmetricMatrix& cov)
156 {
157  assert(cov.columns() == 3);
158 
159  // copy measurement
160  ColumnVector meas_vec(3);
161  for (unsigned int i = 0; i < 3; i++)
162  meas_vec(i + 1) = meas[i];
163 
164  // set covariance
165  static_cast<BFL::LinearAnalyticConditionalGaussian*>(meas_model_->MeasurementPdfGet())->AdditiveNoiseSigmaSet(cov);
166 
167  // update filter
168  bool res = filter_->Update(meas_model_, meas_vec);
169  if (!res) quality_ = 0;
170  else quality_ = calculateQuality();
171 
172  return res;
173 };
174 
176 {
177  ColumnVector tmp = filter_->PostGet()->ExpectedValueGet();
178  for (unsigned int i = 0; i < 3; i++)
179  {
180  est.pos_[i] = tmp(i + 1);
181  est.vel_[i] = tmp(i + 4);
182  }
183 };
184 
185 void TrackerKalman::getEstimate(people_msgs::PositionMeasurement& est) const
186 {
187  ColumnVector tmp = filter_->PostGet()->ExpectedValueGet();
188 
189  est.pos.x = tmp(1);
190  est.pos.y = tmp(2);
191  est.pos.z = tmp(3);
192 
193  est.header.stamp.fromSec(filter_time_);
194  est.object_id = getName();
195 }
196 
198 {
199  double sigma_max = 0;
200  SymmetricMatrix cov = filter_->PostGet()->CovarianceGet();
201  for (unsigned int i = 1; i <= 2; i++)
202  sigma_max = std::max(sigma_max, sqrt(cov(i, i)));
203 
204  return 1.0 - std::min(1.0, sigma_max / 1.5);
205 }
206 
208 {
210  return filter_time_ - init_time_;
211  else
212  return 0;
213 }
214 
216 {
218  return filter_time_;
219  else
220  return 0;
221 }
222 } // namespace estimation
virtual void getEstimate(BFL::StatePosVel &est) const
get filter posterior
virtual MatrixWrapper::SymmetricMatrix CovarianceGet() const
const std::string & getName() const
return the name of the tracker
Definition: tracker.h:59
virtual MatrixWrapper::ColumnVector ExpectedValueGet() const
virtual double getTime() const
return the time of the tracker
void MatrixSet(unsigned int i, const MatrixWrapper::Matrix &m)
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 Gaussian * PostGet()
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_
ConditionalPdf< MatrixWrapper::ColumnVector, MatrixWrapper::ColumnVector > * MeasurementPdfGet()
tf::Vector3 vel_
Definition: state_pos_vel.h:49
tf::Vector3 pos_
Definition: state_pos_vel.h:49
virtual bool updateCorrection(const tf::Vector3 &meas, const MatrixWrapper::SymmetricMatrix &cov)
virtual bool Update(SystemModel< MatrixWrapper::ColumnVector > *const sysmodel, const MatrixWrapper::ColumnVector &u, MeasurementModel< MatrixWrapper::ColumnVector, MatrixWrapper::ColumnVector > *const measmodel, const MatrixWrapper::ColumnVector &z, const MatrixWrapper::ColumnVector &s)
void AdditiveNoiseSigmaSet(const MatrixWrapper::SymmetricMatrix &sigma)
MatrixWrapper::SymmetricMatrix sys_sigma_
virtual bool updatePrediction(const double time)
update tracker
TrackerKalman(const std::string &name, const BFL::StatePosVel &sysnoise)
constructor


people_tracking_filter
Author(s): Caroline Pantofaru
autogenerated on Sun Feb 21 2021 03:56:47