HumanFilter.cpp
Go to the documentation of this file.
1 #include "HumanFilter.h"
2 
4 {
5 
6  HumanFilter::HumanFilter(const core::Pose2D& pose0, const DateTime& detectionTime, bool useKalmanFilter) : useKalmanFilter(useKalmanFilter)
7  {
8  //initialize new human for detected human
9  human.pose = pose0;
10  human.linearVelocity = Eigen::Vector2f::Zero();
11  human.detectionTime = detectionTime;
12  lastDetectedHuman = human;
13 
14  if (useKalmanFilter) {
15  //initialize new kalman filter for new detected human
18  Q.block<2, 2>(0, 0) *= params.control_pos_cov * params.control_pos_cov;
19  Q.block<1, 1>(2, 2) *= params.control_rot_cov * params.control_rot_cov;
20 
23  R.block<2, 2>(0, 0) *= params.obs_pos_cov * params.obs_pos_cov;
24  R.block<1, 1>(2, 2) *= params.obs_rot_cov * params.obs_rot_cov;
25 
28  P0.block<2, 2>(0, 0) *= params.initial_state_pos_cov * params.initial_state_pos_cov;
29  P0.block<1, 1>(2, 2) *= params.initial_state_rot_cov * params.initial_state_rot_cov;
30 
33 
34  //initial position and orientation according to detected human
35  SystemModelT::StateT state0 = toUkfState(pose0);
36 
37  ukf = std::make_unique<UnscentedKalmanFilter<SystemModelT>>(Q, R, alpha, state0, P0);
38  }
39  }
40 
41  void
43  {
44  double dt = (detectionTime - human.detectionTime).toSecondsDouble();
45 
46  if (useKalmanFilter) {
47  SystemModelT::ControlT control = toUkfControl(human.linearVelocity);
48  ukf->propagation(control, dt);
49 
50  human.pose = fromUkfState(ukf->getCurrentState());
51  human.detectionTime = detectionTime;
52  } else {
53  human.pose = human.estimateAt(detectionTime);
54  human.detectionTime = detectionTime;
55  }
56  }
57 
58  void
59  HumanFilter::update(const core::Pose2D& pose, const DateTime& detectionTime)
60  {
61  double dt = (detectionTime - lastDetectedHuman.detectionTime).toSecondsDouble();
62  core::Pose2D newPose;
63 
64  if (useKalmanFilter) {
65  //update ukf with new observation
66  SystemModelT::ObsT observation = toUkfObs(pose);
67  ukf->update(observation);
68 
69  newPose = fromUkfState(ukf->getCurrentState());
70  newPose.linear() = pose.linear();
71  } else
72  {
73  newPose = pose;
74  }
75 
76  // calculate velocity
77  Eigen::Vector2f newVelocity = human.linearVelocity;
78  if (dt > 0) {
79  Eigen::Vector2f ds = newPose.translation() - lastDetectedHuman.pose.translation();
80  Eigen::Vector2f linVelocity = ds / dt;
81  // apply exponential smoothing to velocity
82  //TODO try other approaches?
83  newVelocity = params.velocityAlpha * linVelocity + (1 - params.velocityAlpha) * human.linearVelocity;
84  }
85 
86  // Temporary fix
87  newVelocity.setZero();
88 
89  //update stored information about the human
90  human.detectionTime = detectionTime;
91  human.pose = newPose;
92  human.linearVelocity = newVelocity;
93 
94  lastDetectedHuman = human;
95  }
96 
97  const Human&
99  {
100  return human;
101  }
102 
103  Duration
104  HumanFilter::detectionAge(const DateTime& currentTime) const
105  {
106  return (currentTime - lastDetectedHuman.detectionTime);
107  }
108 
109 
111  HumanFilter::toUkfState(const core::Pose2D& pose)
112  {
113  // [mm] to [m]
114  return {{pose.translation().x() / 1000,
115  pose.translation().y() / 1000,
116  0 /*Eigen::Rotation2Df(pose.linear()).angle()*/}};
117  }
118 
120  HumanFilter::fromUkfState(const SystemModelT::StateT& pose)
121  {
123  // [m] to [mm]
124  pose2d.translation().x() = pose.pose.x() * 1000;
125  pose2d.translation().y() = pose.pose.y() * 1000;
126  pose2d.linear() = Eigen::Rotation2Df(0 /*pose.pose.angle()*/).toRotationMatrix();
127 
128  return pose2d;
129  }
130 
132  HumanFilter::toUkfControl(const Eigen::Vector2f& vel)
133  {
134  // convert velocity from global frame to human frame (required by kalman filter)
135  const auto& global_R_human = human.pose.linear();
136  const auto& human_V_vel = global_R_human.inverse() * vel;
137 
138  // [mm] to [m]
139  return {{human_V_vel.x() / 1000, human_V_vel.y() / 1000, 0}};
140  }
141 
143  HumanFilter::toUkfObs(const core::Pose2D& pose)
144  {
145  return SystemModelT::observationFunction(toUkfState(pose));
146  }
147 
148 
149 } // namespace armarx::navigation::human
armarx::navigation::human::HumanFilter::HumanFilter
HumanFilter(const core::Pose2D &pose0, const DateTime &detectionTime, bool useKalmanFilter)
HumanFilter::HumanFilter creates a new filter that filters 2D poses (position and rotation in a two d...
Definition: HumanFilter.cpp:6
armarx::navigation::human::HumanFilter::Parameters::obs_rot_cov
double obs_rot_cov
Definition: HumanFilter.h:48
HumanFilter.h
UnscentedKalmanFilter
Definition: UnscentedKalmanFilter.h:37
armarx::navigation::human::Human
Definition: types.h:33
armarx::navigation::human::HumanFilter::Parameters::initial_state_rot_cov
double initial_state_rot_cov
Definition: HumanFilter.h:50
armarx::navigation::human::HumanFilter::Parameters::control_pos_cov
double control_pos_cov
Definition: HumanFilter.h:45
armarx::navigation::human::Human::pose
core::Pose2D pose
Definition: types.h:35
armarx::navigation::human::kalman_filter::SystemModelSE2::observationFunction
static ObsT observationFunction(const StateT &state)
Definition: HumanSystemModel.cpp:73
armarx::navigation::human
This file is part of ArmarX.
Definition: aron_conversions.cpp:9
armarx::navigation::human::HumanFilter::update
void update(const core::Pose2D &pose, const DateTime &detectionTime)
HumanFilter::update update the filter a new detected pose of the tracked human and the detection time...
Definition: HumanFilter.cpp:59
armarx::navigation::human::Human::estimateAt
core::Pose2D estimateAt(const DateTime &time) const
Definition: types.cpp:9
armarx::navigation::human::HumanFilter::Parameters::obs_pos_cov
double obs_pos_cov
Definition: HumanFilter.h:47
armarx::navigation::core::Pose2D
Eigen::Isometry2f Pose2D
Definition: basic_types.h:34
armarx::navigation::human::Human::detectionTime
DateTime detectionTime
Definition: types.h:37
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::navigation::human::kalman_filter::SystemModelSE2::ControlT
ControlSE2< FloatT > ControlT
Definition: HumanSystemModel.h:103
StateSE3
Definition: SystemModel.h:40
armarx::navigation::human::HumanFilter::Parameters::initial_state_pos_cov
double initial_state_pos_cov
Definition: HumanFilter.h:49
armarx::navigation::human::kalman_filter::StateSE2
Definition: HumanSystemModel.h:81
armarx::navigation::human::HumanFilter::detectionAge
Duration detectionAge(const DateTime &currentTime) const
Definition: HumanFilter.cpp:104
armarx::navigation::human::Human::linearVelocity
Eigen::Vector2f linearVelocity
Definition: types.h:36
armarx::navigation::human::HumanFilter::Parameters::alpha
double alpha
Definition: HumanFilter.h:51
armarx::navigation::human::HumanFilter::Parameters::control_rot_cov
double control_rot_cov
Definition: HumanFilter.h:46
armarx::navigation::human::kalman_filter::SystemModelSE2::ObsT
Eigen::Matrix< FloatT, dim::obs, 1 > ObsT
Definition: HumanSystemModel.h:104
armarx::navigation::human::HumanFilter::get
const Human & get() const
HumanFilter::get returns the human whose pose is filtered containing the most recent state.
Definition: HumanFilter.cpp:98
armarx::navigation::human::HumanFilter::propagation
void propagation(const DateTime &detectionTime)
HumanFilter::propagation propagate the system model to the given point in time.
Definition: HumanFilter.cpp:42
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::navigation::human::HumanFilter::Parameters::velocityAlpha
float velocityAlpha
Definition: HumanFilter.h:53
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:17
armarx::navigation::human::kalman_filter::ControlSE2
Definition: HumanSystemModel.h:87
Eigen::Matrix< FloatT, dim::control, dim::control >
control
This file is part of ArmarX.
dt
constexpr T dt
Definition: UnscentedKalmanFilterTest.cpp:42
StateSE3::pose
manif::SE3< floatT > pose
Definition: SystemModel.h:41