visual-tracking-control
GatePose.cpp
Go to the documentation of this file.
1 #include <GatePose.h>
2 
3 #include <cmath>
4 
5 using namespace bfl;
6 using namespace cv;
7 using namespace Eigen;
8 
9 
10 GatePose::GatePose(std::unique_ptr<PFVisualCorrection> visual_correction,
11  const double gate_x, const double gate_y, const double gate_z,
12  const double gate_rotation,
13  const double gate_aperture) noexcept :
14  PFVisualCorrectionDecorator(std::move(visual_correction)),
15  gate_x_(gate_x), gate_y_(gate_y), gate_z_(gate_z),
16  gate_aperture_((M_PI / 180.0) * gate_aperture),
17  gate_rotation_((M_PI / 180.0) * gate_rotation)
18 {
19  ee_pose_ = VectorXd::Zero(7);
20 }
21 
22 
23 GatePose::GatePose(std::unique_ptr<PFVisualCorrection> visual_correction) noexcept :
24  GatePose(std::move(visual_correction), 0.1, 0.1, 0.1, 5, 30) { }
25 
26 
27 GatePose::~GatePose() noexcept { }
28 
29 
30 void GatePose::innovation(const Ref<const MatrixXf>& pred_states, cv::InputArray measurements, Ref<MatrixXf> innovations)
31 {
32  PFVisualCorrectionDecorator::innovation(pred_states, measurements, innovations);
33 }
34 
35 
36 double GatePose::likelihood(const Ref<const MatrixXf>& innovations)
37 {
38  return PFVisualCorrectionDecorator::likelihood(innovations);
39 }
40 
41 
42 VisualObservationModel& GatePose::getVisualObservationModel()
43 {
44  return PFVisualCorrectionDecorator::getVisualObservationModel();
45 }
46 
47 
48 void GatePose::setVisualObservationModel(std::unique_ptr<VisualObservationModel> visual_observation_model)
49 {
50  PFVisualCorrectionDecorator::setVisualObservationModel(std::move(visual_observation_model));
51 }
52 
53 
54 void GatePose::correctStep(const Ref<const MatrixXf>& pred_states, const Ref<const VectorXf>& pred_weights, cv::InputArray measurements,
55  Ref<MatrixXf> cor_states, Ref<VectorXf> cor_weights)
56 {
57  PFVisualCorrectionDecorator::correctStep(pred_states, pred_weights, measurements,
58  cor_states, cor_weights);
59 
60  ee_pose_ = readPose();
61 
62  for (int i = 0; i < cor_states.cols(); ++i)
63  {
64  if (!isInsideEllipsoid(cor_states.col(i).head<3>()) ||
65  !isInsideCone (cor_states.col(i).middleRows<3>(3)) ||
66  !isWithinRotation (cor_states(6, i)) )
67  cor_weights(i) = std::numeric_limits<float>::min();
68  }
69 }
70 
71 
72 bool GatePose::isInsideEllipsoid(const Ref<const VectorXf>& state)
73 {
74  return ( (abs(state(0) - ee_pose_(0)) <= gate_x_) &&
75  (abs(state(1) - ee_pose_(1)) <= gate_y_) &&
76  (abs(state(2) - ee_pose_(2)) <= gate_z_) );
77 }
78 
79 
80 bool GatePose::isWithinRotation(float rot_angle)
81 {
82  float ang_diff = abs(rot_angle - ee_pose_(6));
83 
84  return (ang_diff <= gate_rotation_);
85 }
86 
87 
88 bool GatePose::isInsideCone(const Ref<const VectorXf>& state)
89 {
90  /* See: http://stackoverflow.com/questions/10768142/verify-if-point-is-inside-a-cone-in-3d-space#10772759 */
91 
92  double half_aperture = gate_aperture_ / 2.0;
93 
94  VectorXd test_direction = -state.cast<double>();
95 
96  VectorXd fwdkin_direction = -ee_pose_.middleRows<3>(3);
97 
98  return ( (test_direction.dot(fwdkin_direction) / test_direction.norm() / fwdkin_direction.norm()) >= cos(half_aperture) );
99 }
void correctStep(const Eigen::Ref< const Eigen::MatrixXf > &pred_states, const Eigen::Ref< const Eigen::VectorXf > &pred_weights, cv::InputArray measurements, Eigen::Ref< Eigen::MatrixXf > cor_states, Eigen::Ref< Eigen::VectorXf > cor_weights) override
Definition: GatePose.cpp:54
void setVisualObservationModel(std::unique_ptr< bfl::VisualObservationModel > visual_observation_model) override
Definition: GatePose.cpp:48
~GatePose() noexcept
Definition: GatePose.cpp:27
bfl::VisualObservationModel & getVisualObservationModel() override
Definition: GatePose.cpp:42
GatePose(std::unique_ptr< PFVisualCorrection > visual_correction, const double gate_x, const double gate_y, const double gate_z, const double gate_rotation, const double gate_aperture) noexcept
Definition: GatePose.cpp:10
bool isInsideCone(const Eigen::Ref< const Eigen::VectorXf > &state)
Definition: GatePose.cpp:88
bool isWithinRotation(float rot_angle)
Definition: GatePose.cpp:80
bool isInsideEllipsoid(const Eigen::Ref< const Eigen::VectorXf > &state)
Definition: GatePose.cpp:72
double likelihood(const Eigen::Ref< const Eigen::MatrixXf > &innovations) override
Definition: GatePose.cpp:36
void innovation(const Eigen::Ref< const Eigen::MatrixXf > &pred_states, cv::InputArray measurements, Eigen::Ref< Eigen::MatrixXf > innovations) override
Definition: GatePose.cpp:30