From 6751d3d5c8ba6f975173d5c04aac673d58da64bb Mon Sep 17 00:00:00 2001 From: philmoz Date: Fri, 8 Dec 2023 17:08:26 +1100 Subject: [PATCH] Revert changes to mixer logic. --- radio/src/globals.h | 6 ++--- radio/src/mixer.cpp | 55 ++++++++++++++++++++++++--------------- radio/src/tests/mixer.cpp | 40 ++++++++++++++-------------- 3 files changed, 56 insertions(+), 45 deletions(-) diff --git a/radio/src/globals.h b/radio/src/globals.h index 407fc6758e4..1cfdca8963f 100644 --- a/radio/src/globals.h +++ b/radio/src/globals.h @@ -63,11 +63,11 @@ enum MainRequest { extern uint8_t mainRequestFlags; PACK(struct MixState { - int32_t lastValue; - uint16_t delay:13; // max = 2550 + uint16_t delay:14; // max = 2550 uint8_t activeMix:1; uint8_t activeExpo:1; - bool lastSwitchState:1; + int16_t now; // timer trigger source -> off, abs, stk, stk%, sw/!sw, !m_sw/!m_sw + int16_t prev; }); extern MixState mixState[MAX_MIXERS]; diff --git a/radio/src/mixer.cpp b/radio/src/mixer.cpp index 64ecc5575c4..90a22fb7659 100644 --- a/radio/src/mixer.cpp +++ b/radio/src/mixer.cpp @@ -30,6 +30,8 @@ #include "hal/trainer_driver.h" #include "hal/switch_driver.h" +#define DELAY_POS_MARGIN 3 + uint8_t s_mixer_first_run_done = false; int8_t virtualInputsTrims[MAX_INPUTS]; @@ -760,15 +762,17 @@ void evalFlightModeMixes(uint8_t mode, uint8_t tick10ms) chans[md->destCh] = 0; //========== FLIGHT MODE && SWITCH ===== + bool mixCondition = (md->flightModes != 0 || md->swtch); bool fmEnabled = (md->flightModes & (1 << mixerCurrentFlightMode)) == 0; - bool mixLineSwitchActive = getSwitch(md->swtch); - bool mixLineActive = fmEnabled && mixLineSwitchActive; + bool mixLineActive = fmEnabled && getSwitch(md->swtch); + delayval_t mixEnabled = (mixLineActive) ? DELAY_POS_MARGIN+1 : 0; if (mixLineActive) { // disable mixer using trainer channels if not connected if (md->srcRaw >= MIXSRC_FIRST_TRAINER && md->srcRaw <= MIXSRC_LAST_TRAINER && !is_trainer_connected()) { - mixLineActive = false; + mixCondition = true; + mixEnabled = 0; } #if defined(LUA_MODEL_SCRIPTS) @@ -776,7 +780,8 @@ void evalFlightModeMixes(uint8_t mode, uint8_t tick10ms) if (md->srcRaw >= MIXSRC_FIRST_LUA && md->srcRaw <= MIXSRC_LAST_LUA) { div_t qr = div(md->srcRaw - MIXSRC_FIRST_LUA, MAX_SCRIPT_OUTPUTS); if (scriptInternalData[qr.quot].state != SCRIPT_OK) { - mixLineActive = false; + mixCondition = true; + mixEnabled = 0; } } #endif @@ -786,8 +791,10 @@ void evalFlightModeMixes(uint8_t mode, uint8_t tick10ms) getvalue_t v = 0; if (mode > e_perout_mode_inactive_flight_mode) { - if (!mixLineActive) continue; - v = getValue(md->srcRaw); + if (mixEnabled) + v = getValue(md->srcRaw); + else + continue; } else { mixsrc_t srcRaw = md->srcRaw; v = getValue(srcRaw); @@ -815,41 +822,49 @@ void evalFlightModeMixes(uint8_t mode, uint8_t tick10ms) } } } + if (!mixCondition) + mixEnabled = v; } bool applyOffsetAndCurve = true; //========== DELAYS =============== - bool lastSwitchState = mixState[i].lastSwitchState; + delayval_t _swOn = mixState[i].now; + delayval_t _swPrev = mixState[i].prev; + bool swTog = (mixEnabled > _swOn+DELAY_POS_MARGIN || mixEnabled < _swOn-DELAY_POS_MARGIN); - // Has switch state changed - bool swTog = fmEnabled && (mixLineSwitchActive != lastSwitchState); if (mode == e_perout_mode_normal && swTog) { - mixState[i].lastSwitchState = mixLineSwitchActive; - mixState[i].delay = (lastSwitchState ? md->delayDown : md->delayUp) * 10; + if (!mixState[i].delay) + _swPrev = _swOn; + mixState[i].delay = (mixEnabled > _swOn ? md->delayUp : md->delayDown) * 10; + mixState[i].now = mixEnabled; + mixState[i].prev = _swPrev; } if (mode == e_perout_mode_normal && mixState[i].delay > 0) { mixState[i].delay = max(0, (int16_t)mixState[i].delay - tick10ms); // Freeze value until delay expires - v = mixState[i].lastValue; - if (mixLineActive) + if (!mixCondition) + v = _swPrev; + else if (mixEnabled) continue; } else { if (mode == e_perout_mode_normal) { - mixState[i].lastSwitchState = mixLineSwitchActive; + mixState[i].now = mixState[i].prev = mixEnabled; } - if (!mixLineActive) { + if (!mixEnabled) { if ((md->speedDown || md->speedUp) && md->mltpx != MLTPX_REPL) { - v = (md->mltpx == MLTPX_ADD ? 0 : RESX); - applyOffsetAndCurve = false; - } else { + if (mixCondition) { + v = (md->mltpx == MLTPX_ADD ? 0 : RESX); + applyOffsetAndCurve = false; + } + } else if (mixCondition) { continue; } } } - if (mode == e_perout_mode_normal && (mixLineActive || mixState[i].delay)) { + if (mode == e_perout_mode_normal && (!mixCondition || mixEnabled || mixState[i].delay)) { if (md->mixWarn) lv_mixWarning |= 1 << (md->mixWarn - 1); activeMixes[i] = true; } @@ -991,8 +1006,6 @@ void evalFlightModeMixes(uint8_t mode, uint8_t tick10ms) // *ptr=limit( int32_t(int32_t(-1)<<23), *ptr, int32_t(int32_t(1)<<23)); // limit code cost 72 bytes // *ptr=limit( int32_t((-32767+RESXl)<<8), *ptr, int32_t((32767-RESXl)<<8)); // limit code cost 80 bytes #endif - - mixState[i].lastValue = v; } //endfor mixers tick10ms = 0; diff --git a/radio/src/tests/mixer.cpp b/radio/src/tests/mixer.cpp index 5304f6173cb..10121b92971 100644 --- a/radio/src/tests/mixer.cpp +++ b/radio/src/tests/mixer.cpp @@ -686,35 +686,33 @@ TEST_F(MixerTest, DelayOnSwitch) TEST_F(MixerTest, DelayOnSwitch2) { - // SWSRC_ON is not a valid Switch option for Mixes ???? - - // g_eeGeneral.switchConfig = SWITCH_3POS; + g_eeGeneral.switchConfig = SWITCH_3POS; - // g_model.mixData[0].destCh = 0; - // g_model.mixData[0].mltpx = MLTPX_ADD; - // g_model.mixData[0].srcRaw = MIXSRC_FIRST_SWITCH; - // g_model.mixData[0].weight = 100; + g_model.mixData[0].destCh = 0; + g_model.mixData[0].mltpx = MLTPX_ADD; + g_model.mixData[0].srcRaw = MIXSRC_FIRST_SWITCH; + g_model.mixData[0].weight = 100; // g_model.mixData[0].swtch = SWSRC_ON; - // g_model.mixData[0].delayUp = 50; - // g_model.mixData[0].delayDown = 50; + g_model.mixData[0].delayUp = 50; + g_model.mixData[0].delayDown = 50; - // int switch_index = 0; - // simuSetSwitch(switch_index, -1); + int switch_index = 0; + simuSetSwitch(switch_index, -1); - // evalFlightModeMixes(e_perout_mode_normal, 0); - // EXPECT_EQ(chans[0], 0); + evalFlightModeMixes(e_perout_mode_normal, 0); + EXPECT_EQ(chans[0], 0); - // simuSetSwitch(switch_index, 1); - // CHECK_DELAY(0, 500); + simuSetSwitch(switch_index, 1); + CHECK_DELAY(0, 500); - // evalFlightModeMixes(e_perout_mode_normal, 1); - // EXPECT_EQ(chans[0], CHANNEL_MAX); + evalFlightModeMixes(e_perout_mode_normal, 1); + EXPECT_EQ(chans[0], CHANNEL_MAX); - // simuSetSwitch(switch_index, 0); - // CHECK_DELAY(0, 500); + simuSetSwitch(switch_index, 0); + CHECK_DELAY(0, 500); - // evalFlightModeMixes(e_perout_mode_normal, 1); - // EXPECT_EQ(chans[0], 0); + evalFlightModeMixes(e_perout_mode_normal, 1); + EXPECT_EQ(chans[0], 0); } TEST_F(MixerTest, SlowOnMultiply)