PX4 Firmware
PX4 Autopilot Software http://px4.io
integrator.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015-2016 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.cpp
36  *
37  * A resettable integrator
38  *
39  * @author Lorenz Meier <lorenz@px4.io>
40  * @author Julian Oes <julian@oes.ch>
41  */
42 
43 #include "integrator.h"
44 
45 #include <drivers/drv_hrt.h>
46 
47 Integrator::Integrator(uint32_t auto_reset_interval, bool coning_compensation) :
48  _coning_comp_on(coning_compensation)
49 {
50  set_autoreset_interval(auto_reset_interval);
51 }
52 
53 bool
54 Integrator::put(const hrt_abstime &timestamp, const matrix::Vector3f &val, matrix::Vector3f &integral,
55  uint32_t &integral_dt)
56 {
57  if (_last_integration_time == 0) {
58  /* this is the first item in the integrator */
59  _last_integration_time = timestamp;
60  _last_reset_time = timestamp;
61  _last_val = val;
62 
63  return false;
64  }
65 
66  float dt = 0.0f;
67 
68  // Integrate:
69  // Leave dt at 0 if the integration time does not make sense.
70  // Without this check the integral is likely to explode.
71  if (timestamp >= _last_integration_time) {
72  dt = static_cast<float>(timestamp - _last_integration_time) * 1e-6f;
73  }
74 
75  // Use trapezoidal integration to calculate the delta integral
76  const matrix::Vector3f delta_alpha = (val + _last_val) * dt * 0.5f;
77  _last_val = val;
78 
79  // Calculate coning corrections if required
80  if (_coning_comp_on) {
81  // Coning compensation derived by Paul Riseborough and Jonathan Challinger,
82  // following:
83  // Tian et al (2010) Three-loop Integration of GPS and Strapdown INS with Coning and Sculling Compensation
84  // Sourced: http://www.sage.unsw.edu.au/snap/publications/tian_etal2010b.pdf
85  // Simulated: https://github.com/priseborough/InertialNav/blob/master/models/imu_error_modelling.m
86  _beta += ((_last_alpha + _last_delta_alpha * (1.0f / 6.0f)) % delta_alpha) * 0.5f;
87  _last_delta_alpha = delta_alpha;
89  }
90 
91  // accumulate delta integrals
92  _alpha += delta_alpha;
93 
94  _last_integration_time = timestamp;
95 
96  // Only do auto reset if auto reset interval is not 0.
97  if (_auto_reset_interval > 0 && (timestamp - _last_reset_time) >= _auto_reset_interval) {
98 
99  // apply coning corrections if required
100  if (_coning_comp_on) {
101  integral = _alpha + _beta;
102 
103  } else {
104  integral = _alpha;
105  }
106 
107  // reset the integrals and coning corrections
108  _reset(integral_dt);
109 
110  return true;
111 
112  } else {
113  return false;
114  }
115 }
116 
117 bool
119  uint32_t &integral_dt)
120 {
121  if (_last_integration_time == 0) {
122  /* this is the first item in the integrator */
125  _last_reset_time = now;
126  _last_val = val;
127 
128  return false;
129  }
130 
131  // Create the timestamp artifically.
132  const hrt_abstime timestamp = _last_integration_time + interval_us;
133 
134  return put(timestamp, val, integral, integral_dt);
135 }
136 
138 Integrator::get(bool reset, uint32_t &integral_dt)
139 {
140  matrix::Vector3f val = _alpha;
141 
142  if (reset) {
143  _reset(integral_dt);
144  }
145 
146  return val;
147 }
148 
150 Integrator::get_and_filtered(bool reset, uint32_t &integral_dt, matrix::Vector3f &filtered_val)
151 {
152  // Do the usual get with reset first but don't return yet.
153  const matrix::Vector3f ret_integral = get(reset, integral_dt);
154 
155  // Because we need both the integral and the integral_dt.
156  filtered_val = ret_integral * 1000000 / integral_dt;
157 
158  return ret_integral;
159 }
160 
161 void
162 Integrator::_reset(uint32_t &integral_dt)
163 {
164  _alpha.zero();
165  _last_alpha.zero();
166  _beta.zero();
167 
168  integral_dt = (_last_integration_time - _last_reset_time);
169 
171 }
void _reset(uint32_t &integral_dt)
Definition: integrator.cpp:162
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.
High-resolution timer with callouts and timekeeping.
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
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
void set_autoreset_interval(uint32_t auto_reset_interval)
Set auto reset interval during runtime.
Definition: integrator.h:113
void zero()
Definition: Matrix.hpp:421
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
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
matrix::Vector3f _alpha
integrated value before coning corrections are applied
Definition: integrator.h:122
matrix::Vector3f _beta
accumulated coning corrections
Definition: integrator.h:124
matrix::Vector3f get(bool reset, uint32_t &integral_dt)
Get the current integral and reset the integrator if needed.
Definition: integrator.cpp:138
A resettable integrator.
Integrator(uint32_t auto_reset_interval=4000, bool coning_compensation=false)
Definition: integrator.cpp:47
float dt
Definition: px4io.c:73
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
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).