PX4 Firmware
PX4 Autopilot Software http://px4.io
WindEstimator.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 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 WindEstimator.hpp
36  * A wind and airspeed scale estimator.
37  */
38 
39 #pragma once
40 
41 #include <ecl.h>
42 #include <mathlib/mathlib.h>
43 #include <matrix/math.hpp>
44 
46 {
47 public:
48  WindEstimator() = default;
49  ~WindEstimator() = default;
50 
51  // no copy, assignment, move, move assignment
52  WindEstimator(const WindEstimator &) = delete;
53  WindEstimator &operator=(const WindEstimator &) = delete;
54  WindEstimator(WindEstimator &&) = delete;
56 
57  void update(uint64_t time_now);
58 
59  void fuse_airspeed(uint64_t time_now, float true_airspeed, const matrix::Vector3f &velI,
60  const matrix::Vector2f &velIvar);
61  void fuse_beta(uint64_t time_now, const matrix::Vector3f &velI, const matrix::Quatf &q_att);
62 
63  void get_wind(float wind[2])
64  {
65  wind[0] = _state(w_n);
66  wind[1] = _state(w_e);
67  }
68 
69  bool is_estimate_valid() { return _initialised; }
70 
71  bool check_if_meas_is_rejected(uint64_t time_now, float innov, float innov_var, uint8_t gate_size,
72  uint64_t &time_meas_rejected, bool &reinit_filter);
73 
74  float get_tas_scale() { return _state(tas); }
75  float get_tas_innov() { return _tas_innov; }
76  float get_tas_innov_var() { return _tas_innov_var; }
77  float get_beta_innov() { return _beta_innov; }
78  float get_beta_innov_var() { return _beta_innov_var; }
79  void get_wind_var(float wind_var[2])
80  {
81  wind_var[0] = _P(0, 0);
82  wind_var[1] = _P(1, 1);
83  }
85 
86  void set_wind_p_noise(float wind_sigma) { _wind_p_var = wind_sigma * wind_sigma; }
87  void set_tas_scale_p_noise(float tas_scale_sigma) { _tas_scale_p_var = tas_scale_sigma * tas_scale_sigma; }
88  void set_tas_noise(float tas_sigma) { _tas_var = tas_sigma * tas_sigma; }
89  void set_beta_noise(float beta_var) { _beta_var = beta_var * beta_var; }
90  void set_tas_gate(uint8_t gate_size) {_tas_gate = gate_size; }
91  void set_beta_gate(uint8_t gate_size) {_beta_gate = gate_size; }
92 
93  // Inhibit learning of the airspeed scale factor and force the estimator to use _enforced_airspeed_scale as scale factor.
94  // Negative input values enable learning of the airspeed scale factor.
95  void enforce_airspeed_scale(float scale) {_enforced_airspeed_scale = scale; }
96 
97 private:
98  enum {
99  w_n = 0,
102  }; ///< enum which can be used to access state.
103 
104  matrix::Vector3f _state; ///< state vector
105  matrix::Matrix3f _P; ///< state covariance matrix
106 
107  float _tas_innov{0.0f}; ///< true airspeed innovation
108  float _tas_innov_var{0.0f}; ///< true airspeed innovation variance
109 
110  float _beta_innov{0.0f}; ///< sideslip innovation
111  float _beta_innov_var{0.0f}; ///< sideslip innovation variance
112 
113  bool _initialised{false}; ///< True: filter has been initialised
114 
115  float _wind_p_var{0.1f}; ///< wind process noise variance
116  float _tas_scale_p_var{0.0001f}; ///< true airspeed scale process noise variance
117  float _tas_var{1.4f}; ///< true airspeed measurement noise variance
118  float _beta_var{0.5f}; ///< sideslip measurement noise variance
119  uint8_t _tas_gate{3}; ///< airspeed fusion gate size
120  uint8_t _beta_gate{1}; ///< sideslip fusion gate size
121 
122  uint64_t _time_last_airspeed_fuse = 0; ///< timestamp of last airspeed fusion
123  uint64_t _time_last_beta_fuse = 0; ///< timestamp of last sideslip fusion
124  uint64_t _time_last_update = 0; ///< timestamp of last covariance prediction
125  uint64_t _time_rejected_beta = 0; ///< timestamp of when sideslip measurements have consistently started to be rejected
127  0; ///<timestamp of when true airspeed measurements have consistently started to be rejected
128 
129  bool _wind_estimator_reset = false; ///< wind estimator was reset in this cycle
130  float _enforced_airspeed_scale{-1.0f}; ///< by default we want to estimate the true airspeed scale factor (see enforce_airspeed_scale(...) )
131 
132  // initialise state and state covariance matrix
133  bool initialise(const matrix::Vector3f &velI, const matrix::Vector2f &velIvar, const float tas_meas);
134 
135  void run_sanity_checks();
136 };
float _tas_innov
true airspeed innovation
Adapter / shim layer for system calls needed by ECL.
uint64_t _time_last_update
timestamp of last covariance prediction
void update(uint64_t time_now)
bool check_if_meas_is_rejected(uint64_t time_now, float innov, float innov_var, uint8_t gate_size, uint64_t &time_meas_rejected, bool &reinit_filter)
void get_wind(float wind[2])
~WindEstimator()=default
uint64_t _time_last_airspeed_fuse
timestamp of last airspeed fusion
void get_wind_var(float wind_var[2])
Quaternion class.
Definition: Dcm.hpp:24
float get_beta_innov_var()
float get_beta_innov()
void fuse_beta(uint64_t time_now, const matrix::Vector3f &velI, const matrix::Quatf &q_att)
uint8_t _beta_gate
sideslip fusion gate size
float _enforced_airspeed_scale
by default we want to estimate the true airspeed scale factor (see enforce_airspeed_scale(...) )
float _tas_innov_var
true airspeed innovation variance
void set_tas_scale_p_noise(float tas_scale_sigma)
void set_beta_noise(float beta_var)
bool is_estimate_valid()
void set_beta_gate(uint8_t gate_size)
void set_tas_noise(float tas_sigma)
float get_tas_scale()
bool _initialised
True: filter has been initialised.
float _tas_scale_p_var
true airspeed scale process noise variance
uint64_t _time_rejected_tas
timestamp of when true airspeed measurements have consistently started to be rejected ...
float _beta_innov
sideslip innovation
WindEstimator()=default
float _tas_var
true airspeed measurement noise variance
float _wind_p_var
wind process noise variance
WindEstimator & operator=(const WindEstimator &)=delete
void fuse_airspeed(uint64_t time_now, float true_airspeed, const matrix::Vector3f &velI, const matrix::Vector2f &velIvar)
float _beta_var
sideslip measurement noise variance
float get_tas_innov()
void enforce_airspeed_scale(float scale)
uint64_t _time_rejected_beta
timestamp of when sideslip measurements have consistently started to be rejected
uint8_t _tas_gate
airspeed fusion gate size
bool initialise(const matrix::Vector3f &velI, const matrix::Vector2f &velIvar, const float tas_meas)
void set_tas_gate(uint8_t gate_size)
float get_tas_innov_var()
void set_wind_p_noise(float wind_sigma)
bool get_wind_estimator_reset()
float _beta_innov_var
sideslip innovation variance
matrix::Matrix3f _P
state covariance matrix
matrix::Vector3f _state
state vector
void run_sanity_checks()
uint64_t _time_last_beta_fuse
timestamp of last sideslip fusion
bool _wind_estimator_reset
wind estimator was reset in this cycle