From e4b3b3af850ea87509926d7f0cd4eb229315c68c Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Mon, 6 Jan 2014 11:52:05 +0100 Subject: [PATCH] [Taranis] Automatic detection of the 6pos switch within the calibration process --- radio/src/gui/menu_general.cpp | 61 +++++++++++++++++++++++++----- radio/src/gui/menus.cpp | 9 ++++- radio/src/myeeprom.h | 27 +++++++++++-- radio/src/opentx.cpp | 69 +++++++++++++++++++++++----------- radio/src/opentx.h | 9 ++++- 5 files changed, 137 insertions(+), 38 deletions(-) diff --git a/radio/src/gui/menu_general.cpp b/radio/src/gui/menu_general.cpp index e6b7956ca..78d56bc2c 100644 --- a/radio/src/gui/menu_general.cpp +++ b/radio/src/gui/menu_general.cpp @@ -1241,16 +1241,37 @@ void menuGeneralHardware(uint8_t event) void menuCommonCalib(uint8_t event) { - for (uint8_t i=0; i= NUM_STICKS && i < NUM_STICKS+NUM_POTS-2) { -#else - if (i >= NUM_STICKS) { -#endif + if (i >= POT1 && i <= POT_LAST) { reusableBuffer.calib.midVals[i] = (reusableBuffer.calib.hiVals[i] + reusableBuffer.calib.loVals[i]) / 2; +#if defined(PCBTARANIS) + uint8_t idx = i - POT1; + int count = reusableBuffer.calib.xpotsPositionsCount[idx]; + if (count <= 6) { + bool found = false; + for (int j=0; j reusableBuffer.calib.xpotsPositions[idx][j][0]-20 && vt < reusableBuffer.calib.xpotsPositions[idx][j][0]+20) { + reusableBuffer.calib.xpotsPositions[idx][j][0] = min(vt, reusableBuffer.calib.xpotsPositions[idx][j][0]); + reusableBuffer.calib.xpotsPositions[idx][j][1] = max(vt, reusableBuffer.calib.xpotsPositions[idx][j][1]); + if (reusableBuffer.calib.xpotsPositions[idx][j][1] - reusableBuffer.calib.xpotsPositions[idx][j][0] > 50) { + reusableBuffer.calib.xpotsPositionsCount[idx] = 255; + } + found = true; + break; + } + } + if (!found) { + if (count < 6) { + reusableBuffer.calib.xpotsPositions[idx][count][0] = vt; + reusableBuffer.calib.xpotsPositions[idx][count][1] = vt; + } + reusableBuffer.calib.xpotsPositionsCount[idx] += 1; + } + } +#endif } } @@ -1284,6 +1305,11 @@ void menuCommonCalib(uint8_t event) reusableBuffer.calib.loVals[i] = 15000; reusableBuffer.calib.hiVals[i] = -15000; reusableBuffer.calib.midVals[i] = anaIn(i); +#if defined(PCBTARANIS) + if (i50) { - g_eeGeneral.calibMid[i] = reusableBuffer.calib.midVals[i]; +#if defined(PCBTARANIS) + if (i>=POT1 && i<=POT_LAST && reusableBuffer.calib.xpotsPositionsCount[i-POT1] > 1) { + g_eeGeneral.potsType &= ~(1 << (i-POT1)); + if (reusableBuffer.calib.xpotsPositionsCount[i-POT1] <= 6) { + StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[i]; + calib->count = reusableBuffer.calib.xpotsPositionsCount[i-POT1]-1; + for (int j=0; jcount-1; j++) { + calib->steps[i] = (reusableBuffer.calib.xpotsPositions[i-POT1][j+1][0] + reusableBuffer.calib.xpotsPositions[i-POT1][j][1]) / 2; + } + g_eeGeneral.potsType |= (1 << (i-POT1)); + } + } + else +#endif + if (abs(reusableBuffer.calib.loVals[i]-reusableBuffer.calib.hiVals[i]) > 50) { + g_eeGeneral.calib[i].mid = reusableBuffer.calib.midVals[i]; int16_t v = reusableBuffer.calib.midVals[i] - reusableBuffer.calib.loVals[i]; - g_eeGeneral.calibSpanNeg[i] = v - v/STICK_TOLERANCE; + g_eeGeneral.calib[i].spanNeg = v - v/STICK_TOLERANCE; v = reusableBuffer.calib.hiVals[i] - reusableBuffer.calib.midVals[i]; - g_eeGeneral.calibSpanPos[i] = v - v/STICK_TOLERANCE; + g_eeGeneral.calib[i].spanPos = v - v/STICK_TOLERANCE; } } break; @@ -1316,6 +1356,7 @@ void menuCommonCalib(uint8_t event) } doMainScreenGraphics(); + #if defined(PCBTARANIS) drawPotsBars(); #endif diff --git a/radio/src/gui/menus.cpp b/radio/src/gui/menus.cpp index 69af3f0fa..9feed0435 100644 --- a/radio/src/gui/menus.cpp +++ b/radio/src/gui/menus.cpp @@ -1263,7 +1263,14 @@ bool isSwitchAvailable(int16_t swtch) swtch = -swtch; } - if (swtch>=SWSRC_FIRST_CSW && swtch<=SWSRC_LAST_CSW) { +#if defined(PCBTARANIS) + if (swtch >= SWSRC_P11 && swtch <= SWSRC_P26) { + int index = (swtch - SWSRC_P11) / 6; + return (g_eeGeneral.potsType & (1<= SWSRC_FIRST_CSW && swtch <= SWSRC_LAST_CSW) { CustomSwData * cs = cswAddress(swtch-SWSRC_FIRST_CSW); return (cs->func != CS_OFF); } diff --git a/radio/src/myeeprom.h b/radio/src/myeeprom.h index 5343ef424..9a8dbb803 100644 --- a/radio/src/myeeprom.h +++ b/radio/src/myeeprom.h @@ -221,7 +221,7 @@ enum BeeperMode { }; #if defined(CPUARM) - #define EXTRA_GENERAL_FIELDS \ + #define EXTRA_GENERAL_FIELDS_ARM \ uint8_t backlightBright; \ int8_t currentCalib; \ int8_t temperatureWarn; \ @@ -240,6 +240,14 @@ enum BeeperMode { int8_t wavVolume; \ int8_t varioVolume; \ int8_t backgroundVolume; +#endif + +#if defined(PCBTARANIS) +#define EXTRA_GENERAL_FIELDS \ + EXTRA_GENERAL_FIELDS_ARM \ + uint8_t potsType; +#elif defined(CPUARM) + #define EXTRA_GENERAL_FIELDS EXTRA_GENERAL_FIELDS_ARM #elif defined(PXX) #define EXTRA_GENERAL_FIELDS uint8_t countryCode; #else @@ -303,13 +311,24 @@ enum BacklightMode { #define SPLASH_MODE uint8_t splashMode:1; uint8_t spare4:2 #endif +#if defined(PCBTARANIS) +PACK(typedef struct { + int8_t count; + int8_t steps[5]; +}) StepsCalibData; +#endif + +PACK(typedef struct { + int16_t mid; + int16_t spanNeg; + int16_t spanPos; +}) CalibData; + #define ALTERNATE_VIEW 0x10 PACK(typedef struct t_EEGeneral { uint8_t version; uint16_t variant; - int16_t calibMid[NUM_STICKS+NUM_POTS]; - int16_t calibSpanNeg[NUM_STICKS+NUM_POTS]; - int16_t calibSpanPos[NUM_STICKS+NUM_POTS]; + CalibData calib[NUM_STICKS+NUM_POTS]; uint16_t chkSum; int8_t currModel; uint8_t contrast; diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index d3c6f5e2d..0e07afb1d 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -503,7 +503,7 @@ void generalDefault() uint16_t evalChkSum() { uint16_t sum = 0; - const int16_t *calibValues = &g_eeGeneral.calibMid[0]; + const int16_t *calibValues = (const int16_t *) &g_eeGeneral.calib[0]; for (int i=0; i(0, val / (2*RESX/5), 5); - uint8_t previousPos = potsPos[i] >> 4; - if (pos != previousPos) { - potsLastposStart[i] = get_tmr10ms(); - potsPos[i] = (pos << 4) | (potsPos[i] & 0x0f); - } - else if ((tmr10ms_t)(get_tmr10ms() - potsLastposStart[i]) > DELAY_SWITCH_3POS) { - potsLastposStart[i] = 0; - potsPos[i] = (pos << 4) | (pos); + for (int i=0; icount>0 && calib->count<6) { + uint8_t pos = calib->count; + int v = (int16_t)anaIn(POT1+i); + for (int j=0; jcount; j++) { + if (v < calib->steps[j]) { + pos = j; + break; + } + } + uint8_t previousPos = potsPos[i] >> 4; + if (pos != previousPos) { + potsLastposStart[i] = get_tmr10ms(); + potsPos[i] = (pos << 4) | (potsPos[i] & 0x0f); + } + else if ((tmr10ms_t)(get_tmr10ms() - potsLastposStart[i]) > DELAY_SWITCH_3POS) { + potsLastposStart[i] = 0; + potsPos[i] = (pos << 4) | (pos); + } + } } } } @@ -2156,8 +2167,9 @@ void checkTHR() int16_t lowLim = THRCHK_DEADBAND - 1024 ; #else getADC(); // if thr is down - do not display warning at all - int16_t lowLim = g_eeGeneral.calibMid[thrchn]; - lowLim = (g_model.throttleReversed ? - lowLim - g_eeGeneral.calibSpanPos[thrchn] : lowLim - g_eeGeneral.calibSpanNeg[thrchn]); + CalibData * calib = &g_eeGeneral.calib[thrchn]; + int16_t lowLim = calib->mid; + lowLim = (g_model.throttleReversed ? - lowLim - calib->spanPos : lowLim - calib->spanNeg); lowLim += THRCHK_DEADBAND; #endif int16_t v = thrAnaIn(thrchn); @@ -2495,7 +2507,23 @@ void getADC() } for (uint32_t x=0; x> 3; + uint16_t v = temp[x] >> 3; +#if defined(PCBTARANIS) + StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[x]; + if (x >= POT1 && x <= POT_LAST && (g_eeGeneral.potsType & (1<<(x-POT1))) && calib->count>0 && calib->count<6) { + for (int i=0; i<=calib->count; i++) { + if (i == calib->count) { + s_anaFilt[x] = RESX; + } + else if (v < calib->steps[i]) { + s_anaFilt[x] = -RESX + i*2*RESX/calib->count; + break; + } + } + } + else +#endif + s_anaFilt[x] = v; } } #else @@ -2743,10 +2771,9 @@ void evalInputs(uint8_t mode) #ifndef SIMU if (i < NUM_STICKS+NUM_POTS) { - v -= g_eeGeneral.calibMid[i]; - v = v * (int32_t)RESX / (max((int16_t)100,(v>0 ? - g_eeGeneral.calibSpanPos[i] : - g_eeGeneral.calibSpanNeg[i]))); + CalibData * calib = &g_eeGeneral.calib[i]; + v -= calib->mid; + v = v * (int32_t)RESX / (max((int16_t)100, (v>0 ? calib->spanPos : calib->spanNeg))); } #endif diff --git a/radio/src/opentx.h b/radio/src/opentx.h index 501ea65a8..d12bf7edd 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -377,7 +377,7 @@ enum EnumKeys { #define NUM_SWITCHES 8 #define IS_3POS(sw) ((sw) != 5 && (sw) != 7) #define NUM_POTS 4 - #define NUM_POTSSW (2*6) + #define NUM_XPOTS 2 #define NUM_SW_SRCRAW 8 #define SWSRC_THR SWSRC_SF2 #define SWSRC_GEA SWSRC_SG2 @@ -390,12 +390,13 @@ enum EnumKeys { #define IS_3POS(sw) ((sw) == 0) #define IS_MOMENTARY(sw) (sw == SWSRC_TRN) #define NUM_POTS 3 - #define NUM_POTSSW 0 + #define NUM_XPOTS 0 #define NUM_SW_SRCRAW 1 #define SW_DSM2_BIND SW_TRN #endif #define NUM_PSWITCH (SWSRC_LAST_SWITCH-SWSRC_FIRST_SWITCH+1) +#define NUM_POTSSW (NUM_XPOTS*6) #define NUM_SWITCH (NUM_PSWITCH+NUM_CSW+NUM_POTSSW) #if defined(PCBTARANIS) @@ -1042,12 +1043,14 @@ enum Analogs { #if defined(PCBTARANIS) POT1, POT2, + POT_LAST = POT2, SLIDER1, SLIDER2, #else POT1, POT2, POT3, + POT_LAST = POT3, #endif TX_VOLTAGE, #if defined(PCBSKY9X) && !defined(REVA) @@ -1565,6 +1568,8 @@ union ReusableBuffer int16_t loVals[NUM_STICKS+NUM_POTS]; int16_t hiVals[NUM_STICKS+NUM_POTS]; uint8_t state; + uint8_t xpotsPositionsCount[NUM_XPOTS]; + int16_t xpotsPositions[NUM_XPOTS][6][2]; } calib; #if defined(SDCARD)