PX4 Firmware
PX4 Autopilot Software http://px4.io
PreFlightChecker.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file PreFlightChecker.hpp
36  * Class handling the EKF2 innovation pre flight checks
37  *
38  * First call the update(...) function and then get the results
39  * using the hasXxxFailed() getters
40  */
41 
42 #pragma once
43 
46 
47 #include <matrix/matrix/math.hpp>
48 
49 #include "InnovationLpf.hpp"
50 
51 using matrix::Vector2f;
52 
54 {
55 public:
56  PreFlightChecker() = default;
57  ~PreFlightChecker() = default;
58 
59  /*
60  * Reset all the internal states of the class to their default value
61  */
62  void reset();
63 
64  /*
65  * Update the internal states
66  * @param dt the sampling time
67  * @param innov the ekf2_innovation_s struct containing the current innovations
68  */
69  void update(float dt, const ekf2_innovations_s &innov);
70 
71  /*
72  * If set to true, the checker will use a less conservative heading innovation check
73  */
75 
76  void setUsingGpsAiding(bool val) { _is_using_gps_aiding = val; }
77  void setUsingFlowAiding(bool val) { _is_using_flow_aiding = val; }
78  void setUsingEvPosAiding(bool val) { _is_using_ev_pos_aiding = val; }
79 
80  bool hasHeadingFailed() const { return _has_heading_failed; }
81  bool hasHorizVelFailed() const { return _has_horiz_vel_failed; }
82  bool hasVertVelFailed() const { return _has_vert_vel_failed; }
83  bool hasHeightFailed() const { return _has_height_failed; }
84 
85  /*
86  * Overall state of the pre fligh checks
87  * @return true if any of the check failed
88  */
89  bool hasFailed() const { return hasHorizFailed() || hasVertFailed(); }
90 
91  /*
92  * Horizontal checks overall result
93  * @return true if one of the horizontal checks failed
94  */
96 
97  /*
98  * Vertical checks overall result
99  * @return true if one of the vertical checks failed
100  */
102 
103  /*
104  * Check if the innovation fails the test
105  * To pass the test, the following conditions should be true:
106  * innov <= test_limit
107  * innov_lpf <= 2 * test_limit
108  * @param innov the current unfiltered innovation
109  * @param innov_lpf the low-pass filtered innovation
110  * @param test_limit the magnitude test limit
111  * @return true if the check failed the test, false otherwise
112  */
113  static bool checkInnovFailed(float innov, float innov_lpf, float test_limit);
114 
115  /*
116  * Check if the a innovation of a 2D vector fails the test
117  * To pass the test, the following conditions should be true:
118  * innov <= test_limit
119  * innov_lpf <= 2 * test_limit
120  * @param innov the current unfiltered innovation
121  * @param innov_lpf the low-pass filtered innovation
122  * @param test_limit the magnitude test limit
123  * @return true if the check failed the test, false otherwise
124  */
125  static bool checkInnov2DFailed(const Vector2f &innov, const Vector2f &innov_lpf, float test_limit);
126 
127  static constexpr float sq(float var) { return var * var; }
128 
129 private:
130  bool preFlightCheckHeadingFailed(const ekf2_innovations_s &innov, float alpha);
131  float selectHeadingTestLimit();
132 
133  bool preFlightCheckHorizVelFailed(const ekf2_innovations_s &innov, float alpha);
134  bool preFlightCheckVertVelFailed(const ekf2_innovations_s &innov, float alpha);
135  bool preFlightCheckHeightFailed(const ekf2_innovations_s &innov, float alpha);
136 
137  void resetPreFlightChecks();
138 
143 
148 
149  // Low-pass filters for innovation pre-flight checks
150  InnovationLpf _filter_vel_n_innov; ///< Preflight low pass filter N axis velocity innovations (m/sec)
151  InnovationLpf _filter_vel_e_innov; ///< Preflight low pass filter E axis velocity innovations (m/sec)
152  InnovationLpf _filter_vel_d_innov; ///< Preflight low pass filter D axis velocity innovations (m/sec)
153  InnovationLpf _filter_hgt_innov; ///< Preflight low pass filter height innovation (m)
154  InnovationLpf _filter_heading_innov; ///< Preflight low pass filter heading innovation magntitude (rad)
155  InnovationLpf _filter_flow_x_innov; ///< Preflight low pass filter optical flow innovation (rad)
156  InnovationLpf _filter_flow_y_innov; ///< Preflight low pass filter optical flow innovation (rad)
157 
158  // Preflight low pass filter time constant inverse (1/sec)
159  static constexpr float _innov_lpf_tau_inv = 0.2f;
160  // Maximum permissible velocity innovation to pass pre-flight checks (m/sec)
161  static constexpr float _vel_innov_test_lim = 0.5f;
162  // Maximum permissible height innovation to pass pre-flight checks (m)
163  static constexpr float _hgt_innov_test_lim = 1.5f;
164  // Maximum permissible yaw innovation to pass pre-flight checks when aiding inertial nav using NE frame observations (rad)
165  static constexpr float _nav_heading_innov_test_lim = 0.25f;
166  // Maximum permissible yaw innovation to pass pre-flight checks when not aiding inertial nav using NE frame observations (rad)
167  static constexpr float _heading_innov_test_lim = 0.52f;
168  // Maximum permissible flow innovation to pass pre-flight checks
169  static constexpr float _flow_innov_test_lim = 0.1f;
170  // Preflight velocity innovation spike limit (m/sec)
171  static constexpr float _vel_innov_spike_lim = 2.0f * _vel_innov_test_lim;
172  // Preflight position innovation spike limit (m)
173  static constexpr float _hgt_innov_spike_lim = 2.0f * _hgt_innov_test_lim;
174  // Preflight flow innovation spike limit (rad)
175  static constexpr float _flow_innov_spike_lim = 2.0f * _flow_innov_test_lim;
176 };
bool hasHeightFailed() const
void setUsingFlowAiding(bool val)
static constexpr float _innov_lpf_tau_inv
static bool checkInnovFailed(float innov, float innov_lpf, float test_limit)
InnovationLpf _filter_heading_innov
Preflight low pass filter heading innovation magntitude (rad)
~PreFlightChecker()=default
InnovationLpf _filter_vel_d_innov
Preflight low pass filter D axis velocity innovations (m/sec)
float selectHeadingTestLimit()
InnovationLpf _filter_flow_x_innov
Preflight low pass filter optical flow innovation (rad)
void setUsingGpsAiding(bool val)
void update(float dt, const ekf2_innovations_s &innov)
InnovationLpf _filter_flow_y_innov
Preflight low pass filter optical flow innovation (rad)
static constexpr float _flow_innov_spike_lim
void resetPreFlightChecks()
bool hasFailed() const
static constexpr float _hgt_innov_test_lim
bool hasHorizVelFailed() const
bool preFlightCheckHorizVelFailed(const ekf2_innovations_s &innov, float alpha)
Vector2< float > Vector2f
Definition: Vector2.hpp:69
First order "alpha" IIR digital filter with input saturation.
bool hasHorizFailed() const
static constexpr float sq(float var)
bool preFlightCheckVertVelFailed(const ekf2_innovations_s &innov, float alpha)
InnovationLpf _filter_hgt_innov
Preflight low pass filter height innovation (m)
bool preFlightCheckHeightFailed(const ekf2_innovations_s &innov, float alpha)
PreFlightChecker()=default
static constexpr float _vel_innov_test_lim
static constexpr float _heading_innov_test_lim
void setVehicleCanObserveHeadingInFlight(bool val)
static constexpr float _vel_innov_spike_lim
bool hasVertFailed() const
static constexpr float _flow_innov_test_lim
float dt
Definition: px4io.c:73
static bool checkInnov2DFailed(const Vector2f &innov, const Vector2f &innov_lpf, float test_limit)
void setUsingEvPosAiding(bool val)
InnovationLpf _filter_vel_e_innov
Preflight low pass filter E axis velocity innovations (m/sec)
static constexpr float _hgt_innov_spike_lim
bool preFlightCheckHeadingFailed(const ekf2_innovations_s &innov, float alpha)
InnovationLpf _filter_vel_n_innov
Preflight low pass filter N axis velocity innovations (m/sec)
bool hasVertVelFailed() const
bool hasHeadingFailed() const
static constexpr float _nav_heading_innov_test_lim