Merge pull request #3373 from opentx/projectkk2glider/issue_3371_horus_tx_battery

Projectkk2glider/issue 3371 horus tx battery
This commit is contained in:
Bertrand Songis
2016-03-20 18:06:58 +01:00
15 changed files with 122 additions and 33 deletions
+3
View File
@@ -45,6 +45,9 @@ class DMAFifo
bool isEmpty()
{
#if defined(SIMU)
return true;
#endif
return (ridx == N - stream->NDTR);
}
@@ -46,6 +46,7 @@ enum menuGeneralHwItems {
ITEM_SETUP_HW_SH,
ITEM_SETUP_HW_BLUETOOTH,
// ITEM_SETUP_HW_UART3_MODE,
ITEM_SETUP_HW_BAT_CAL,
ITEM_SETUP_HW_MAX
};
@@ -175,6 +176,12 @@ bool menuGeneralHardware(evt_t event)
}
break;
#endif
case ITEM_SETUP_HW_BAT_CAL:
lcdDrawText(MENUS_MARGIN_LEFT, y, "Battery cal.");
lcdDrawNumber(HW_SETTINGS_COLUMN, y, getBatteryVoltage(), attr|LEFT|PREC2, 0, NULL, "V");
if (attr) CHECK_INCDEC_GENVAR(event, g_eeGeneral.txVoltageCalibration, -127, 127);
break;
}
}
+1 -1
View File
@@ -329,7 +329,7 @@ bool menuGeneralSetup(evt_t event)
case ITEM_SETUP_BATTERY_WARNING:
lcdDrawText(MENUS_MARGIN_LEFT, y, STR_BATTERYWARNING);
putsValueWithUnit(RADIO_SETUP_2ND_COLUMN, y, g_eeGeneral.vBatWarn, UNIT_VOLTS, attr|PREC1|LEFT);
if (attr) CHECK_INCDEC_GENVAR(event, g_eeGeneral.vBatWarn, 27, 42); // 2.7-4.2V
if (attr) CHECK_INCDEC_GENVAR(event, g_eeGeneral.vBatWarn, 40, 120); //4-12V
break;
#if 0
@@ -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);
}
+66 -1
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
+9 -16
View File
@@ -2153,6 +2153,7 @@ uint8_t getSticksNavigationEvent()
}
#endif
#if !defined(CPUARM)
void checkBattery()
{
static uint8_t counter = 0;
@@ -2166,14 +2167,7 @@ void checkBattery()
if (counter-- == 0) {
counter = 10;
int32_t instant_vbat = anaIn(TX_VOLTAGE);
#if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
instant_vbat = (instant_vbat + instant_vbat*(g_eeGeneral.txVoltageCalibration)/128) * BATT_SCALE;
instant_vbat >>= 11;
instant_vbat += 2; // because of the diode
#elif defined(PCBSKY9X)
instant_vbat = (instant_vbat + instant_vbat*(g_eeGeneral.txVoltageCalibration)/128) * 4191;
instant_vbat /= 55296;
#elif defined(CPUM2560)
#if defined(CPUM2560)
instant_vbat = (instant_vbat*1112 + instant_vbat*g_eeGeneral.txVoltageCalibration + (BandGap<<2)) / (BandGap<<3);
#else
instant_vbat = (instant_vbat*16 + instant_vbat*g_eeGeneral.txVoltageCalibration/8) / BandGap;
@@ -2211,17 +2205,11 @@ void checkBattery()
if (IS_TXBATT_WARNING() && g_vbat100mV>50) {
AUDIO_TX_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) {
AUDIO_TX_MAH_HIGH();
}
#endif
}
}
}
#endif // #if !defined(CPUARM)
#if !defined(SIMU) && !defined(CPUARM)
@@ -2746,6 +2734,11 @@ int main()
if ((shutdown_state=pwrCheck()) > e_power_trainer)
break;
#endif
#if defined(SIMU)
sleep(5/*ms*/);
if (main_thread_running == 0)
return 0;
#endif
perMain();
+2
View File
@@ -73,6 +73,7 @@ void processSbusFrame(uint8_t * sbus, int16_t * pulses, uint32_t size)
void processSbusInput()
{
#if !defined(SIMU)
uint8_t rxchar;
uint32_t active = 0;
while (sbusGetByte(&rxchar)) {
@@ -94,4 +95,5 @@ void processSbusInput()
}
}
}
#endif
}
-1
View File
@@ -26,6 +26,5 @@
#define SBUS_MAX_FRAME_SIZE 28
void processSbusInput();
void processSbusFrame(uint8_t * sbus, int16_t * pulses, uint32_t size);
#endif // _SBUS_H_
+8 -2
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;
+6
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);
}
+1 -6
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" {
+7
View File
@@ -590,3 +590,10 @@ void checkTrainerSettings()
}
}
uint16_t getBatteryVoltage()
{
int32_t instant_vbat = anaIn(TX_VOLTAGE); // using filtered ADC value on purpose
instant_vbat = (instant_vbat + instant_vbat*(g_eeGeneral.txVoltageCalibration)/128) * 4191;
instant_vbat /= 5530;
return (uint16_t)instant_vbat;
}
+1
View File
@@ -228,6 +228,7 @@ enum Analogs {
void adcInit();
void adcRead(void);
uint16_t getAnalogValue(uint8_t index);
uint16_t getBatteryVoltage(); // returns current battery voltage in 10mV steps
void setSticksGain(uint8_t gains);
// Buzzer driver
@@ -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;
}
@@ -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