PX4 Firmware
PX4 Autopilot Software http://px4.io
integrator.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015-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 integrator.h
36  *
37  * A resettable integrator
38  *
39  * @author Lorenz Meier <lorenz@px4.io>
40  * @author Julian Oes <julian@oes.ch>
41  */
42 
43 #pragma once
44 
45 #include <mathlib/mathlib.h>
46 #include <matrix/math.hpp>
47 
49 {
50 public:
51  Integrator(uint32_t auto_reset_interval = 4000 /* 250 Hz */, bool coning_compensation = false);
52  ~Integrator() = default;
53 
54  // no copy, assignment, move, move assignment
55  Integrator(const Integrator &) = delete;
56  Integrator &operator=(const Integrator &) = delete;
57  Integrator(Integrator &&) = delete;
58  Integrator &operator=(Integrator &&) = delete;
59 
60  /**
61  * Put an item into the integral.
62  *
63  * @param timestamp Timestamp of the current value.
64  * @param val Item to put.
65  * @param integral Current integral in case the integrator did reset, else the value will not be modified
66  * @param integral_dt Get the dt in us of the current integration (only if reset).
67  * @return true if putting the item triggered an integral reset and the integral should be
68  * published.
69  */
70  bool put(const uint64_t &timestamp, const matrix::Vector3f &val, matrix::Vector3f &integral, uint32_t &integral_dt);
71 
72  /**
73  * Put an item into the integral but provide an interval instead of a timestamp.
74  *
75  * @param interval_us Interval in us since last integration.
76  * @param val Item to put.
77  * @param integral Current integral in case the integrator did reset, else the value will not be modified
78  * @param integral_dt Get the dt in us of the current integration (only if reset). Note that this
79  * values might not be accurate vs. hrt_absolute_time because it is just the sum of the
80  * supplied intervals.
81  * @return true if putting the item triggered an integral reset and the integral should be
82  * published.
83  */
84  bool put_with_interval(unsigned interval_us, matrix::Vector3f &val, matrix::Vector3f &integral, uint32_t &integral_dt);
85 
86  /**
87  * Get the current integral and reset the integrator if needed.
88  *
89  * @param reset Reset the integral to zero.
90  * @param integral_dt Get the dt in us of the current integration (only if reset).
91  * @return the integral since the last read-reset
92  */
93  matrix::Vector3f get(bool reset, uint32_t &integral_dt);
94 
95 
96  /**
97  * Get the current integral and reset the integrator if needed. Additionally give the
98  * integral over the samples differentiated by the integration time (mean filtered values).
99  *
100  * @param reset Reset the integral to zero.
101  * @param integral_dt Get the dt in us of the current integration (only if reset).
102  * @param filtered_val The integral differentiated by the integration time.
103  * @return the integral since the last read-reset
104  */
105  matrix::Vector3f get_and_filtered(bool reset, uint32_t &integral_dt, matrix::Vector3f &filtered_val);
106 
107 
108  /**
109  * Set auto reset interval during runtime. This won't reset the integrator.
110  *
111  * @param auto_reset_interval New reset time interval for the integrator (+- 10%).
112  */
113  void set_autoreset_interval(uint32_t auto_reset_interval) { _auto_reset_interval = 0.90f * auto_reset_interval; }
114 
115 private:
116  uint32_t _auto_reset_interval{0}; /**< the interval after which the content will be published
117  and the integrator reset, 0 if no auto-reset */
118 
119  uint64_t _last_integration_time{0}; /**< timestamp of the last integration step */
120  uint64_t _last_reset_time{0}; /**< last auto-announcement of integral value */
121 
122  matrix::Vector3f _alpha{0.0f, 0.0f, 0.0f}; /**< integrated value before coning corrections are applied */
123  matrix::Vector3f _last_alpha{0.0f, 0.0f, 0.0f}; /**< previous value of _alpha */
124  matrix::Vector3f _beta{0.0f, 0.0f, 0.0f}; /**< accumulated coning corrections */
125  matrix::Vector3f _last_val{0.0f, 0.0f, 0.0f}; /**< previous input */
126  matrix::Vector3f _last_delta_alpha{0.0f, 0.0f, 0.0f}; /**< integral from previous previous sampling interval */
127 
128  bool _coning_comp_on{false}; /**< true to turn on coning corrections */
129 
130  /* Do a reset.
131  *
132  * @param integral_dt Get the dt in us of the current integration.
133  */
134  void _reset(uint32_t &integral_dt);
135 };
void _reset(uint32_t &integral_dt)
Definition: integrator.cpp:162
Integrator & operator=(const Integrator &)=delete
uint64_t _last_reset_time
last auto-announcement of integral value
Definition: integrator.h:120
matrix::Vector3f _last_val
previous input
Definition: integrator.h:125
int reset(enum LPS22HB_BUS busid)
Reset the driver.
bool _coning_comp_on
true to turn on coning corrections
Definition: integrator.h:128
matrix::Vector3f get_and_filtered(bool reset, uint32_t &integral_dt, matrix::Vector3f &filtered_val)
Get the current integral and reset the integrator if needed.
Definition: integrator.cpp:150
bool put(const uint64_t &timestamp, const matrix::Vector3f &val, matrix::Vector3f &integral, uint32_t &integral_dt)
Put an item into the integral.
Definition: integrator.cpp:54
matrix::Vector3f _last_alpha
previous value of _alpha
Definition: integrator.h:123
~Integrator()=default
void set_autoreset_interval(uint32_t auto_reset_interval)
Set auto reset interval during runtime.
Definition: integrator.h:113
bool put_with_interval(unsigned interval_us, matrix::Vector3f &val, matrix::Vector3f &integral, uint32_t &integral_dt)
Put an item into the integral but provide an interval instead of a timestamp.
Definition: integrator.cpp:118
matrix::Vector3f _alpha
integrated value before coning corrections are applied
Definition: integrator.h:122
matrix::Vector3f _beta
accumulated coning corrections
Definition: integrator.h:124
Integrator(uint32_t auto_reset_interval=4000, bool coning_compensation=false)
Definition: integrator.cpp:47
matrix::Vector3f _last_delta_alpha
integral from previous previous sampling interval
Definition: integrator.h:126
uint32_t _auto_reset_interval
the interval after which the content will be published and the integrator reset, 0 if no auto-reset ...
Definition: integrator.h:116
uint64_t _last_integration_time
timestamp of the last integration step
Definition: integrator.h:119