1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-17 05:15:18 +03:00

Re #3371: tx battery measuring code moved to board_xxx.cpp, battery check code rewritten for ARM platforms, Horus battery calibrated.

This commit is contained in:
Damjan Adamic 2016-03-20 16:05:17 +01:00
parent 49e70dbf12
commit a3af63edad
8 changed files with 95 additions and 15 deletions

View file

@ -42,11 +42,7 @@ void menuGeneralDiagAna(uint8_t event)
}
lcd_putsLeft(MENU_HEADER_HEIGHT+1+6*FH, STR_BATT_CALIB);
static int32_t adcBatt;
adcBatt = ((adcBatt * 7) + anaIn(TX_VOLTAGE)) / 8;
uint32_t batCalV = (adcBatt + (adcBatt*g_eeGeneral.txVoltageCalibration)/128) * BATT_SCALE;
batCalV >>= 11;
batCalV += 2; // because of the diode
putsVolts(LEN_CALIB_FIELDS*FW+4*FW, MENU_HEADER_HEIGHT+1+6*FH, batCalV, s_editMode > 0 ? BLINK|INVERS : INVERS);
uint32_t batCalV = getBatteryVoltage();
putsVolts(LEN_CALIB_FIELDS*FW+4*FW, MENU_HEADER_HEIGHT+1+6*FH, getBatteryVoltage(), (s_editMode > 0 ? BLINK : 0) | INVERS | PREC2);
if (s_editMode > 0) CHECK_INCDEC_GENVAR(event, g_eeGeneral.txVoltageCalibration, -127, 127);
}

View file

@ -98,6 +98,71 @@ void checkEeprom()
}
#endif
#define BAT_AVG_SAMPLES 8
void checkBatteryAlarms()
{
// TRACE("checkBatteryAlarms()");
if (IS_TXBATT_WARNING() && g_vbat100mV>50) {
AUDIO_TX_BATTERY_LOW();
// TRACE("checkBatteryAlarms(): battery low");
}
#if defined(PCBSKY9X)
else if (g_eeGeneral.temperatureWarn && getTemperature() >= g_eeGeneral.temperatureWarn) {
AUDIO_TX_TEMP_HIGH();
}
else if (g_eeGeneral.mAhWarn && (g_eeGeneral.mAhUsed + Current_used * (488 + g_eeGeneral.txCurrentCalibration)/8192/36) / 500 >= g_eeGeneral.mAhWarn) { // TODO move calculation into board file
AUDIO_TX_MAH_HIGH();
}
#endif
}
void checkBattery()
{
static uint32_t batSum;
static uint8_t sampleCount;
// filter battery voltage by averaging it
if (g_vbat100mV == 0) {
g_vbat100mV = (getBatteryVoltage() + 5) / 10;
batSum = 0;
sampleCount = 0;
}
else {
batSum += getBatteryVoltage();
// TRACE("checkBattery(): sampled = %d", getBatteryVoltage());
if (++sampleCount >= BAT_AVG_SAMPLES) {
g_vbat100mV = (batSum + BAT_AVG_SAMPLES * 5 ) / (BAT_AVG_SAMPLES * 10);
batSum = 0;
sampleCount = 0;
// TRACE("checkBattery(): g_vbat100mV = %d", g_vbat100mV);
}
}
}
void periodicTick_1s()
{
checkBattery();
}
void periodicTick_10s()
{
checkBatteryAlarms();
}
void periodicTick()
{
static uint8_t count10s;
static uint32_t lastTime;
if ( (get_tmr10ms() - lastTime) >= 100 ) {
lastTime += 100;
periodicTick_1s();
if (++count10s >= 10) {
count10s = 0;
periodicTick_10s();
}
}
}
#if defined(GUI) && defined(COLORLCD)
void guiMain(evt_t evt)
{
@ -328,7 +393,7 @@ void perMain()
writeLogs();
handleUsbConnection();
checkTrainerSettings();
checkBattery();
periodicTick();
evt_t evt = getEvent(false);
if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on

View file

@ -2153,6 +2153,7 @@ uint8_t getSticksNavigationEvent()
}
#endif
#if !defined(CPUARM)
void checkBattery()
{
static uint8_t counter = 0;
@ -2222,6 +2223,8 @@ void checkBattery()
}
}
}
#endif // #if !defined(CPUARM)
#if !defined(SIMU) && !defined(CPUARM)

View file

@ -544,9 +544,15 @@ uint16_t anaIn(uint8_t chan)
return th9xSim->sliders[chan]->getValue();
else if (chan<NUM_STICKS+NUM_POTS)
return th9xSim->knobs[chan-NUM_STICKS]->getValue();
#if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
#if defined(PCBHORUS)
else if (chan == TX_VOLTAGE)
return 1000;
return 1737; //~10.6V
#elif (defined(PCBTARANIS) && defined(REV9E))
else if (chan == TX_VOLTAGE)
return 1420; //~10.6V
#elif defined(PCBTARANIS) || defined(PCBFLAMENCO)
else if (chan == TX_VOLTAGE)
return 1000; //~7.4V
#elif defined(PCBSKY9X)
else if (chan == TX_VOLTAGE)
return 5.1*1500/11.3;

View file

@ -266,3 +266,9 @@ void checkTrainerSettings()
}
}
}
uint16_t getBatteryVoltage()
{
int32_t instant_vbat = anaIn(TX_VOLTAGE); // using filtered ADC value on purpose
return (uint16_t)((instant_vbat * (1000 + g_eeGeneral.txVoltageCalibration) ) / 1637);
}

View file

@ -242,12 +242,7 @@ extern uint16_t adcValues[NUMBER_ANALOG];
void adcInit(void);
void adcRead(void);
uint16_t getAnalogValue(uint8_t index);
#if defined(REV3)
#define BATT_SCALE 120
#else
#define BATT_SCALE 150
#endif
uint16_t getBatteryVoltage(); // returns current battery voltage in 10mV steps
#if defined(__cplusplus) && !defined(SIMU)
extern "C" {

View file

@ -316,3 +316,11 @@ void checkTrainerSettings()
}
}
}
uint16_t getBatteryVoltage()
{
int32_t instant_vbat = anaIn(TX_VOLTAGE); // using filtered ADC value on purpose
instant_vbat = (instant_vbat * BATT_SCALE * (128 + g_eeGeneral.txVoltageCalibration) ) / 26214;
instant_vbat += 20; // add 0.2V because of the diode TODO check if this is needed, but removal will beak existing calibrations!!!
return (uint16_t)instant_vbat;
}

View file

@ -268,6 +268,7 @@ void adcInit(void);
void adcRead(void);
extern uint16_t adcValues[NUMBER_ANALOG];
uint16_t getAnalogValue(uint8_t index);
uint16_t getBatteryVoltage(); // returns current battery voltage in 10mV steps
#define BATT_SCALE 150