1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-23 16:25:16 +03:00

[Taranis] 6Pos switch support is now ok. Calibration is automatic. More

tests needed
This commit is contained in:
Bertrand Songis 2014-01-12 12:57:14 +01:00
parent 1dff05dc02
commit a292fabb02
6 changed files with 94 additions and 46 deletions

View file

@ -1250,13 +1250,13 @@ void menuCommonCalib(uint8_t event)
#if defined(PCBTARANIS)
uint8_t idx = i - POT1;
int count = reusableBuffer.calib.xpotsPositionsCount[idx];
if (count <= 6) {
if (count <= POTS_POS_COUNT) {
bool found = false;
for (int j=0; j<count; j++) {
if (vt > reusableBuffer.calib.xpotsPositions[idx][j][0]-20 && vt < reusableBuffer.calib.xpotsPositions[idx][j][0]+20) {
if (vt > reusableBuffer.calib.xpotsPositions[idx][j][0]-10 && vt < reusableBuffer.calib.xpotsPositions[idx][j][0]+10) {
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) {
if (reusableBuffer.calib.xpotsPositions[idx][j][1] - reusableBuffer.calib.xpotsPositions[idx][j][0] > 20) {
reusableBuffer.calib.xpotsPositionsCount[idx] = 255;
}
found = true;
@ -1301,6 +1301,10 @@ void menuCommonCalib(uint8_t event)
lcd_putsAtt(0*FW, 2*FH, STR_SETMIDPOINT, s_noScroll ? INVERS : 0);
lcd_putsLeft(3*FH, STR_MENUWHENDONE);
#if defined(PCBTARANIS)
g_eeGeneral.potsType = 0;
#endif
for (uint8_t i=0; i<NUM_STICKS+NUM_POTS; i++) {
reusableBuffer.calib.loVals[i] = 15000;
reusableBuffer.calib.hiVals[i] = -15000;
@ -1320,20 +1324,6 @@ void menuCommonCalib(uint8_t event)
lcd_putsLeft(3*FH, STR_MENUWHENDONE);
for (uint8_t i=0; i<NUM_STICKS+NUM_POTS; 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; j<calib->count-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];
@ -1345,6 +1335,28 @@ void menuCommonCalib(uint8_t event)
break;
case 3:
#if defined(PCBTARANIS)
for (uint8_t i=POT1; i<=POT_LAST; i++) {
int idx = i - POT1;
int count = reusableBuffer.calib.xpotsPositionsCount[idx];
if (count > 1 && count <= POTS_POS_COUNT) {
for (int j=0; j<count; j++) {
for (int k=j+1; k<count; k++) {
if (reusableBuffer.calib.xpotsPositions[idx][k][0] < reusableBuffer.calib.xpotsPositions[idx][j][0]) {
swap(reusableBuffer.calib.xpotsPositions[idx][j][0], reusableBuffer.calib.xpotsPositions[idx][k][0]);
swap(reusableBuffer.calib.xpotsPositions[idx][j][1], reusableBuffer.calib.xpotsPositions[idx][k][1]);
}
}
}
g_eeGeneral.potsType |= (1 << idx);
StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[i];
calib->count = count - 1;
for (int j=0; j<calib->count; j++) {
calib->steps[j] = (reusableBuffer.calib.xpotsPositions[idx][j+1][0] + reusableBuffer.calib.xpotsPositions[idx][j][1]) >> 5;
}
}
}
#endif
g_eeGeneral.chkSum = evalChkSum();
eeDirty(EE_GENERAL);
reusableBuffer.calib.state = 4;
@ -1359,6 +1371,19 @@ void menuCommonCalib(uint8_t event)
#if defined(PCBTARANIS)
drawPotsBars();
for (int i=POT1; i<=POT_LAST; i++) {
uint8_t steps = 0;
if (reusableBuffer.calib.state == 2) {
steps = reusableBuffer.calib.xpotsPositionsCount[i-POT1];
}
else if (IS_MULTIPOS_POT(i)) {
StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[i];
steps = calib->count + 1;
}
if (steps > 0 && steps <= POTS_POS_COUNT) {
lcd_outdezAtt(LCD_W/2-2+(i-POT1)*5, LCD_H-6, steps, TINSIZE);
}
}
#endif
}

View file

@ -1265,8 +1265,14 @@ bool isSwitchAvailable(int16_t swtch)
#if defined(PCBTARANIS)
if (swtch >= SWSRC_P11 && swtch <= SWSRC_P26) {
int index = (swtch - SWSRC_P11) / 6;
return (g_eeGeneral.potsType & (1<<index));
int index = (swtch - SWSRC_P11) / POTS_POS_COUNT;
if (g_eeGeneral.potsType & (1<<index)) {
StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[POT1+index];
return (calib->count >= ((swtch - SWSRC_P11) % POTS_POS_COUNT));
}
else {
return false;
}
}
#endif

View file

@ -120,9 +120,9 @@
void drawPotsBars()
{
// Optimization by Mike Blandford
uint8_t x, y, len ; // declare temporary variables
for (x=LCD_W/2-5, y=NUM_STICKS; y<NUM_STICKS+NUM_POTS; x+=5, y++) {
len = ((calibratedStick[y]+RESX)*BAR_HEIGHT/(RESX*2))+1l; // calculate once per loop
uint8_t x, i, len ; // declare temporary variables
for (x=LCD_W/2-5, i=NUM_STICKS; i<NUM_STICKS+NUM_POTS; x+=5, i++) {
len = ((calibratedStick[i]+RESX)*BAR_HEIGHT/(RESX*2))+1l; // calculate once per loop
V_BAR(x, LCD_H-8, len)
}
}

View file

@ -312,9 +312,10 @@ enum BacklightMode {
#endif
#if defined(PCBTARANIS)
#define POTS_POS_COUNT 6
PACK(typedef struct {
int8_t count;
int8_t steps[5];
uint8_t count;
uint8_t steps[POTS_POS_COUNT-1];
}) StepsCalibData;
#endif

View file

@ -1336,18 +1336,12 @@ void getSwitchesPosition()
CHECK_2POS(SW_SH);
switchesPos = newPos;
#if defined(PCBTARANIS)
for (int i=0; i<NUM_XPOTS; i++) {
if (g_eeGeneral.potsType & (1 << i)) {
StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[POT1+i];
if (calib->count>0 && calib->count<6) {
uint8_t pos = calib->count;
int v = (int16_t)anaIn(POT1+i);
for (int j=0; j<calib->count; j++) {
if (v < calib->steps[j]) {
pos = j;
break;
}
}
if (calib->count>0 && calib->count<POTS_POS_COUNT) {
uint8_t pos = anaIn(POT1+i) / (2*RESX/calib->count);
uint8_t previousPos = potsPos[i] >> 4;
if (pos != previousPos) {
potsLastposStart[i] = get_tmr10ms();
@ -1360,9 +1354,10 @@ void getSwitchesPosition()
}
}
}
#endif
}
#define SWITCH_POSITION(sw) (switchesPos & (1<<(sw)))
#define POT_POSITION(sw) ((potsPos[(sw)/6] & 0x0f) == ((sw) % 6))
#define POT_POSITION(sw) ((potsPos[(sw)/POTS_POS_COUNT] & 0x0f) == ((sw) % POTS_POS_COUNT))
#else
#define getSwitchesPosition()
#define SWITCH_POSITION(idx) switchState((EnumKeys)(SW_BASE+(idx)))
@ -2502,6 +2497,15 @@ void getADC()
for (uint32_t i=0; i<4; i++) {
adcRead();
for (uint32_t x=0; x<NUMBER_ANALOG; x++) {
#if defined(PCBTARANIS)
if (s_noScroll) {
if (x>=POT1 && x<=POT2 && temp[x]!=Analog_values[x]) {
i = 0;
}
temp[x] = Analog_values[x];
}
else
#endif
temp[x] += Analog_values[x];
}
}
@ -2509,14 +2513,14 @@ void getADC()
for (uint32_t x=0; x<NUMBER_ANALOG; x++) {
uint16_t v = temp[x] >> 3;
#if defined(PCBTARANIS)
if (s_noScroll) v = temp[x] >> 1;
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;
if (IS_MULTIPOS_POT(x) && calib->count>0 && calib->count<POTS_POS_COUNT) {
uint8_t vShifted = (v >> 4);
s_anaFilt[x] = 2*RESX;
for (int i=0; i<calib->count; i++) {
if (vShifted < calib->steps[i]) {
s_anaFilt[x] = i*2*RESX/calib->count;
break;
}
}
@ -2771,10 +2775,15 @@ void evalInputs(uint8_t mode)
#ifndef SIMU
if (i < NUM_STICKS+NUM_POTS) {
if (IS_MULTIPOS_POT(i)) {
v -= RESX;
}
else {
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
if (v < -RESX) v = -RESX;
@ -3730,10 +3739,10 @@ void doMixerCalculations()
// therefore forget the exact calculation and use only 1 instead; good compromise
lastTMR = tmr10ms;
getSwitchesPosition();
getADC();
getSwitchesPosition();
#if defined(PCBSKY9X) && !defined(REVA) && !defined(SIMU)
Current_analogue = (Current_analogue*31 + s_anaFilt[8] ) >> 5 ;
if (Current_analogue > Current_max)

View file

@ -412,6 +412,12 @@ enum EnumKeys {
#include "myeeprom.h"
#if defined(PCBTARANIS)
#define IS_MULTIPOS_POT(x) ((x)>=POT1 && (x)<=POT_LAST && (g_eeGeneral.potsType & (1 << ((x)-POT1))))
#else
#define IS_MULTIPOS_POT(x) (false)
#endif
#if ROTARY_ENCODERS > 0
#define IF_ROTARY_ENCODERS(x) x,
#else
@ -1098,6 +1104,7 @@ template<class t> FORCEINLINE t min(t a, t b) { return a<b?a:b; }
template<class t> FORCEINLINE t max(t a, t b) { return a>b?a:b; }
template<class t> FORCEINLINE t sgn(t a) { return a>0 ? 1 : (a < 0 ? -1 : 0); }
template<class t> FORCEINLINE t limit(t mi, t x, t ma) { return min(max(mi,x),ma); }
template<class t> void swap(t & a, t & b) { t tmp = b; b = a; a = tmp; }
#if defined(HELI) || defined(FRSKY_HUB)
uint16_t isqrt32(uint32_t n);