mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-20 14:55:21 +03:00
Merge pull request #6067 from etracer65/rc_smoohting_crsf_init
RC smoothing - add rx frame training delay to deal with CRSF initialization
This commit is contained in:
commit
0210be305a
1 changed files with 43 additions and 33 deletions
|
@ -70,7 +70,8 @@ enum {
|
||||||
|
|
||||||
#ifdef USE_RC_SMOOTHING_FILTER
|
#ifdef USE_RC_SMOOTHING_FILTER
|
||||||
#define RC_SMOOTHING_IDENTITY_FREQUENCY 80 // Used in the formula to convert a BIQUAD cutoff frequency to PT1
|
#define RC_SMOOTHING_IDENTITY_FREQUENCY 80 // Used in the formula to convert a BIQUAD cutoff frequency to PT1
|
||||||
#define RC_SMOOTHING_FILTER_TRAINING_DELAY_MS 5000 // Wait 5 seconds after power to let the PID loop stabilize before starting average frame rate calculation
|
#define RC_SMOOTHING_FILTER_STARTUP_DELAY_MS 5000 // Time to wait after power to let the PID loop stabilize before starting average frame rate calculation
|
||||||
|
#define RC_SMOOTHING_FILTER_TRAINING_DELAY_MS 1000 // Additional time to wait after receiving first valid rx frame before training starts
|
||||||
#define RC_SMOOTHING_FILTER_TRAINING_SAMPLES 50
|
#define RC_SMOOTHING_FILTER_TRAINING_SAMPLES 50
|
||||||
|
|
||||||
static FAST_RAM_ZERO_INIT uint16_t defaultInputCutoffFrequency;
|
static FAST_RAM_ZERO_INIT uint16_t defaultInputCutoffFrequency;
|
||||||
|
@ -290,6 +291,7 @@ FAST_CODE uint8_t processRcSmoothingFilter(void)
|
||||||
static FAST_RAM_ZERO_INIT int rxFrameCount;
|
static FAST_RAM_ZERO_INIT int rxFrameCount;
|
||||||
static FAST_RAM uint16_t minRxFrameInterval = UINT16_MAX;
|
static FAST_RAM uint16_t minRxFrameInterval = UINT16_MAX;
|
||||||
static FAST_RAM_ZERO_INIT uint16_t maxRxFrameInterval;
|
static FAST_RAM_ZERO_INIT uint16_t maxRxFrameInterval;
|
||||||
|
static FAST_RAM_ZERO_INIT timeMs_t validRxFrameTimeMs;
|
||||||
|
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
initialized = true;
|
initialized = true;
|
||||||
|
@ -306,48 +308,56 @@ FAST_CODE uint8_t processRcSmoothingFilter(void)
|
||||||
// If the filter cutoffs are set to auto and we have good rx data, then determine the average rx frame rate
|
// If the filter cutoffs are set to auto and we have good rx data, then determine the average rx frame rate
|
||||||
// and use that to calculate the filter cutoff frequencies
|
// and use that to calculate the filter cutoff frequencies
|
||||||
if (!filterInitialized) {
|
if (!filterInitialized) {
|
||||||
if (rxIsReceivingSignal() && (targetPidLooptime > 0) && (millis() > RC_SMOOTHING_FILTER_TRAINING_DELAY_MS)) {
|
const timeMs_t currentTimeMs = millis();
|
||||||
rxFrameTimeSum += currentRxRefreshRate;
|
if (rxIsReceivingSignal() && (targetPidLooptime > 0) && (currentTimeMs > RC_SMOOTHING_FILTER_STARTUP_DELAY_MS)) {
|
||||||
rxFrameCount++;
|
if (validRxFrameTimeMs == 0) {
|
||||||
maxRxFrameInterval = MAX(maxRxFrameInterval, currentRxRefreshRate);
|
validRxFrameTimeMs = currentTimeMs;
|
||||||
minRxFrameInterval = MIN(minRxFrameInterval, currentRxRefreshRate);
|
} else if ((currentTimeMs - validRxFrameTimeMs) > RC_SMOOTHING_FILTER_TRAINING_DELAY_MS) {
|
||||||
DEBUG_SET(DEBUG_RC_SMOOTHING, 0, rxFrameCount); // log the step count during training
|
rxFrameTimeSum += currentRxRefreshRate;
|
||||||
DEBUG_SET(DEBUG_RC_SMOOTHING, 3, currentRxRefreshRate); // log each frame interval during training
|
rxFrameCount++;
|
||||||
if (rxFrameCount >= RC_SMOOTHING_FILTER_TRAINING_SAMPLES) {
|
maxRxFrameInterval = MAX(maxRxFrameInterval, currentRxRefreshRate);
|
||||||
rxFrameTimeSum = rxFrameTimeSum - minRxFrameInterval - maxRxFrameInterval; // Throw out high and low samples
|
minRxFrameInterval = MIN(minRxFrameInterval, currentRxRefreshRate);
|
||||||
calculatedFrameTimeAverageUs = lrintf(rxFrameTimeSum / (rxFrameCount - 2));
|
DEBUG_SET(DEBUG_RC_SMOOTHING, 0, rxFrameCount); // log the step count during training
|
||||||
const float avgRxFrameTime = (rxFrameTimeSum / (rxFrameCount - 2)) * 1e-6f;
|
DEBUG_SET(DEBUG_RC_SMOOTHING, 3, currentRxRefreshRate); // log each frame interval during training
|
||||||
|
if (rxFrameCount >= RC_SMOOTHING_FILTER_TRAINING_SAMPLES) {
|
||||||
|
rxFrameTimeSum = rxFrameTimeSum - minRxFrameInterval - maxRxFrameInterval; // Throw out high and low samples
|
||||||
|
calculatedFrameTimeAverageUs = lrintf(rxFrameTimeSum / (rxFrameCount - 2));
|
||||||
|
const float avgRxFrameTime = (rxFrameTimeSum / (rxFrameCount - 2)) * 1e-6f;
|
||||||
|
|
||||||
defaultInputCutoffFrequency = calcRcSmoothingCutoff(avgRxFrameTime, (rxConfig()->rc_smoothing_input_type == RC_SMOOTHING_INPUT_PT1));
|
defaultInputCutoffFrequency = calcRcSmoothingCutoff(avgRxFrameTime, (rxConfig()->rc_smoothing_input_type == RC_SMOOTHING_INPUT_PT1));
|
||||||
filterCutoffFrequency = (filterCutoffFrequency == 0) ? defaultInputCutoffFrequency : filterCutoffFrequency;
|
filterCutoffFrequency = (filterCutoffFrequency == 0) ? defaultInputCutoffFrequency : filterCutoffFrequency;
|
||||||
|
|
||||||
if (rxConfig()->rc_smoothing_derivative_type == RC_SMOOTHING_DERIVATIVE_OFF) {
|
if (rxConfig()->rc_smoothing_derivative_type == RC_SMOOTHING_DERIVATIVE_OFF) {
|
||||||
derivativeCutoffFrequency = 0;
|
derivativeCutoffFrequency = 0;
|
||||||
} else {
|
} else {
|
||||||
defaultDerivativeCutoffFrequency = calcRcSmoothingCutoff(avgRxFrameTime, (rxConfig()->rc_smoothing_derivative_type == RC_SMOOTHING_DERIVATIVE_PT1));
|
defaultDerivativeCutoffFrequency = calcRcSmoothingCutoff(avgRxFrameTime, (rxConfig()->rc_smoothing_derivative_type == RC_SMOOTHING_DERIVATIVE_PT1));
|
||||||
derivativeCutoffFrequency = (derivativeCutoffFrequency == 0) ? defaultDerivativeCutoffFrequency : derivativeCutoffFrequency;
|
derivativeCutoffFrequency = (derivativeCutoffFrequency == 0) ? defaultDerivativeCutoffFrequency : derivativeCutoffFrequency;
|
||||||
}
|
}
|
||||||
|
|
||||||
const float dT = targetPidLooptime * 1e-6f;
|
const float dT = targetPidLooptime * 1e-6f;
|
||||||
for (int i = 0; i < PRIMARY_CHANNEL_COUNT; i++) {
|
for (int i = 0; i < PRIMARY_CHANNEL_COUNT; i++) {
|
||||||
if ((1 << i) & interpolationChannels) {
|
if ((1 << i) & interpolationChannels) {
|
||||||
switch (rxConfig()->rc_smoothing_input_type) {
|
switch (rxConfig()->rc_smoothing_input_type) {
|
||||||
case RC_SMOOTHING_INPUT_PT1:
|
case RC_SMOOTHING_INPUT_PT1:
|
||||||
pt1FilterInit(&rcCommandFilterPt1[i], pt1FilterGain(filterCutoffFrequency, dT));
|
pt1FilterInit(&rcCommandFilterPt1[i], pt1FilterGain(filterCutoffFrequency, dT));
|
||||||
break;
|
break;
|
||||||
case RC_SMOOTHING_INPUT_BIQUAD:
|
case RC_SMOOTHING_INPUT_BIQUAD:
|
||||||
default:
|
default:
|
||||||
biquadFilterInitLPF(&rcCommandFilterBiquad[i], filterCutoffFrequency, targetPidLooptime);
|
biquadFilterInitLPF(&rcCommandFilterBiquad[i], filterCutoffFrequency, targetPidLooptime);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pidInitSetpointDerivativeLpf(derivativeCutoffFrequency, rxConfig()->rc_smoothing_debug_axis, rxConfig()->rc_smoothing_derivative_type);
|
||||||
|
filterInitialized = true;
|
||||||
}
|
}
|
||||||
pidInitSetpointDerivativeLpf(derivativeCutoffFrequency, rxConfig()->rc_smoothing_debug_axis, rxConfig()->rc_smoothing_derivative_type);
|
|
||||||
filterInitialized = true;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
rxFrameTimeSum = 0;
|
rxFrameTimeSum = 0;
|
||||||
rxFrameCount = 0;
|
rxFrameCount = 0;
|
||||||
|
validRxFrameTimeMs = 0;
|
||||||
|
minRxFrameInterval = UINT16_MAX;
|
||||||
|
maxRxFrameInterval = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue