Set blackbox file handler to NULL after closing file
[inav.git] / src / main / fc / rc_smoothing.c
blob91e5a0792b4045f6def199aa655fb0f63758fdba
1 /*
2 * This file is part of INAV.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * Alternatively, the contents of this file may be used under the terms
9 * of the GNU General Public License Version 3, as described below:
11 * This file is free software: you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation, either version 3 of the License, or (at your
14 * option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include <stdint.h>
29 #include "platform.h"
31 #include "blackbox/blackbox.h"
33 #include "build/debug.h"
35 #include "common/maths.h"
36 #include "common/filter.h"
38 #include "drivers/time.h"
40 #include "rx/rx.h"
42 #include "fc/config.h"
43 #include "fc/fc_core.h"
44 #include "fc/rc_controls.h"
45 #include "fc/rc_smoothing.h"
46 #include "fc/runtime_config.h"
48 #include "flight/mixer.h"
50 // RC Interpolation is not allowed to go below this value.
51 #define RC_INTERPOLATION_MIN_FREQUENCY 15
53 static pt3Filter_t rcSmoothFilter[4];
54 static float rcStickUnfiltered[4];
55 static uint16_t rcUpdateFrequency;
57 uint16_t getRcUpdateFrequency(void) {
58 return rcUpdateFrequency;
61 static int32_t applyRcUpdateFrequencyMedianFilter(int32_t newReading)
63 #define RC_FILTER_SAMPLES_MEDIAN 9
64 static int32_t filterSamples[RC_FILTER_SAMPLES_MEDIAN];
65 static int filterSampleIndex = 0;
66 static bool medianFilterReady = false;
68 filterSamples[filterSampleIndex] = newReading;
69 ++filterSampleIndex;
70 if (filterSampleIndex == RC_FILTER_SAMPLES_MEDIAN) {
71 filterSampleIndex = 0;
72 medianFilterReady = true;
75 return medianFilterReady ? quickMedianFilter9(filterSamples) : newReading;
78 void rcInterpolationApply(bool isRXDataNew, timeUs_t currentTimeUs)
80 // Compute the RC update frequency
81 static timeUs_t previousRcData;
82 static int filterFrequency;
83 static bool initDone = false;
85 const float dT = US2S(getLooptime());
87 if (isRXDataNew) {
88 if (!initDone) {
90 filterFrequency = rxConfig()->rcFilterFrequency;
92 // Initialize the RC smooth filter
93 for (int stick = 0; stick < 4; stick++) {
94 pt3FilterInit(&rcSmoothFilter[stick], pt3FilterGain(filterFrequency, dT));
97 initDone = true;
100 for (int stick = 0; stick < 4; stick++) {
101 rcStickUnfiltered[stick] = rcCommand[stick];
105 // Don't filter if not initialized
106 if (!initDone) {
107 return;
110 if (isRXDataNew) {
111 const timeDelta_t delta = cmpTimeUs(currentTimeUs, previousRcData);
112 rcUpdateFrequency = applyRcUpdateFrequencyMedianFilter(1.0f / (delta * 0.000001f));
113 previousRcData = currentTimeUs;
116 * If auto smoothing is enabled, update the filters
118 if (rxConfig()->autoSmooth) {
119 const int nyquist = rcUpdateFrequency / 2;
121 int newFilterFrequency = scaleRange(
122 rxConfig()->autoSmoothFactor,
124 100,
125 nyquist,
126 rcUpdateFrequency / 10
129 // Do not allow filter frequency to go below RC_INTERPOLATION_MIN_FREQUENCY or above nuyquist frequency.
130 newFilterFrequency = constrain(newFilterFrequency, RC_INTERPOLATION_MIN_FREQUENCY, nyquist);
132 if (newFilterFrequency != filterFrequency) {
134 for (int stick = 0; stick < 4; stick++) {
135 pt3FilterUpdateCutoff(&rcSmoothFilter[stick], pt3FilterGain(newFilterFrequency, dT));
137 filterFrequency = newFilterFrequency;
143 for (int stick = 0; stick < 4; stick++) {
144 rcCommand[stick] = pt3FilterApply(&rcSmoothFilter[stick], rcStickUnfiltered[stick]);