Skip to content

Commit

Permalink
Revert changes to mixer logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
philmoz committed Dec 8, 2023
1 parent b86da9f commit 6751d3d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
6 changes: 3 additions & 3 deletions radio/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
55 changes: 34 additions & 21 deletions radio/src/mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -760,23 +762,26 @@ 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)
// disable mixer if Lua script is used as source and script was killed
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
Expand All @@ -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);
Expand Down Expand Up @@ -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<int16_t>(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;
}
Expand Down Expand Up @@ -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;
Expand Down
40 changes: 19 additions & 21 deletions radio/src/tests/mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6751d3d

Please sign in to comment.