feat(cpn): simulator allow manually setting transmitter battery voltage (#5763)

This commit is contained in:
Neil Horne
2025-01-13 09:19:53 +10:00
committed by GitHub
parent 8d58ae3cf8
commit a0da434dc2
10 changed files with 137 additions and 25 deletions
+1 -1
View File
@@ -67,7 +67,7 @@ static bool adcSingleRead()
bool adcRead()
{
adcSingleRead();
// TODO: this hack needs to go away...
if (isVBatBridgeEnabled()) {
disableVBatBridge();
+26 -8
View File
@@ -27,6 +27,7 @@
#include "hal_adc_inputs.inc"
#include "board.h"
#include "edgetx.h"
void enableVBatBridge(){}
void disableVBatBridge(){}
@@ -41,18 +42,35 @@ static bool simu_start_conversion()
setAnalogValue(i, simu_get_analog(i));
}
// set VBAT / RTC_BAT
if (adcGetMaxInputs(ADC_INPUT_VBAT) > 0) {
// set batteries default voltages
int i = adcGetInputOffset(ADC_INPUT_VBAT);
if (i > 0) {
// calculate default voltage on 1st call
uint32_t adc = (simu_get_analog(i) - 2048) * 1000 / 2000;
// TRACE("raw ana: %d adj ana: %d", simu_get_analog(i), vbat);
// just in case the voltage has not been initialised in sim initialisation/startup
// these formulae must mirror OpenTxSimulator::voltageToAdc
if (adc == 0) {
uint32_t volts = (uint32_t)((g_eeGeneral.vBatWarn > 0 ? g_eeGeneral.vBatWarn : BATTERY_WARN) + 5) * 10; // +0.5V and prec2
#if defined(VBAT_MOSFET_DROP)
uint32_t vbat = (2 * (BATTERY_MAX + BATTERY_MIN) * (VBAT_DIV_R2 + VBAT_DIV_R1)) / VBAT_DIV_R1;
// TRACE("volts: %d r1: %d r2: %d drop: %d vref: %d calib: %d", volts, VBAT_DIV_R1, VBAT_DIV_R2, VBAT_MOSFET_DROP, ADC_VREF_PREC2, g_eeGeneral.txVoltageCalibration);
adc = (volts - VBAT_MOSFET_DROP) * (2 * RESX * 1000) / ADC_VREF_PREC2 / (((1000 + g_eeGeneral.txVoltageCalibration) * (VBAT_DIV_R2 + VBAT_DIV_R1)) / VBAT_DIV_R1);
#elif defined(BATT_SCALE)
uint32_t vbat = (BATTERY_MAX + BATTERY_MIN) * 5; // * 10 / 2
vbat = ((vbat - VOLTAGE_DROP) * BATTERY_DIVIDER) / (BATT_SCALE * 128);
// TRACE("volts: %d div: %d drop: %d scale: %d calib: %d", volts, BATTERY_DIVIDER, VOLTAGE_DROP, BATT_SCALE, g_eeGeneral.txVoltageCalibration);
adc = (volts - VOLTAGE_DROP) * BATTERY_DIVIDER / (128 + g_eeGeneral.txVoltageCalibration) / BATT_SCALE;
#elif defined(VOLTAGE_DROP)
// TRACE("volts: %d div: %d drop: %d", volts, BATTERY_DIVIDER, VOLTAGE_DROP);
adc = (volts - VOLTAGE_DROP) * BATTERY_DIVIDER / (1000 + g_eeGeneral.txVoltageCalibration);
#else
uint32_t vbat = (BATTERY_MAX + BATTERY_MIN) * 5; // * 10 / 2
vbat = (vbat * BATTERY_DIVIDER) / 1000;
// TRACE("volts: %d div: %d calib: %d", volts, BATTERY_DIVIDER, g_eeGeneral.txVoltageCalibration);
adc = volts * BATTERY_DIVIDER / (1000 + g_eeGeneral.txVoltageCalibration);
#endif
setAnalogValue(adcGetInputOffset(ADC_INPUT_VBAT), vbat * 2);
// TRACE("calc adc: %d", adc);
adc = adc * 2; // div by 2 in firmware filtered adc calcs
}
setAnalogValue(i, adc);
}
if (adcGetMaxInputs(ADC_INPUT_RTC_BAT) > 0) {
+22 -9
View File
@@ -24,6 +24,7 @@
#include "simulcd.h"
#include "switches.h"
#include "serial.h"
#include "myeeprom.h"
#include "hal/adc_driver.h"
#include "hal/rotary_encoder.h"
@@ -61,6 +62,8 @@ extern etx_serial_port_t * serialPorts[MAX_AUX_SERIAL];
uint16_t simu_get_analog(uint8_t idx)
{
// TODO: return raw values for ADC_INPUT_VBAT and ADC_INPUT_RTC_BAT
// 6POS simu mechanism use a different scale, so needs specific offset
if (IS_POT_MULTIPOS(idx - adcGetInputOffset(ADC_INPUT_FLEX))) {
// Use radio calibration data to determine conversion factor
@@ -182,7 +185,7 @@ OpenTxSimulator::OpenTxSimulator() :
tracebackDevices.clear();
traceCallback = firmwareTraceCb;
// When we create the simulator, we change the UART driver
// When we create the simulator, we change the UART driver
for (int i = 0; i < MAX_AUX_SERIAL; i++) {
etx_serial_port_t * port = serialPorts[i];
if (port != nullptr) {
@@ -377,6 +380,7 @@ void OpenTxSimulator::setInputValue(int type, uint8_t index, int16_t value)
if (adcGetMaxInputs(ADC_INPUT_VBAT) > 0) {
auto idx = adcGetInputOffset(ADC_INPUT_VBAT);
setAnalogValue(idx, voltageToAdc(value));
emit txBatteryVoltageChanged((unsigned int)value);
}
break;
case INPUT_SRC_SWITCH :
@@ -891,17 +895,26 @@ const char * OpenTxSimulator::getError()
return main_thread_error;
}
const int OpenTxSimulator::voltageToAdc(const int volts)
const int OpenTxSimulator::voltageToAdc(const int voltage)
{
int ret = 0;
#if defined(PCBHORUS) || defined(PCBX7)
ret = (float)volts * 16.2f;
#elif defined(PCBTARANIS)
ret = (float)volts * 13.3f;
int volts = voltage * 10; // prec2
int adc = 0;
#if defined(VBAT_MOSFET_DROP)
// TRACE("volts: %d r1: %d r2: %d drop: %d vref: %d calib: %d", volts, VBAT_DIV_R1, VBAT_DIV_R2, VBAT_MOSFET_DROP, ADC_VREF_PREC2, g_eeGeneral.txVoltageCalibration);
adc = (volts - VBAT_MOSFET_DROP) * (2 * RESX * 1000) / ADC_VREF_PREC2 / (((1000 + g_eeGeneral.txVoltageCalibration) * (VBAT_DIV_R2 + VBAT_DIV_R1)) / VBAT_DIV_R1);
#elif defined(BATT_SCALE)
// TRACE("volts: %d div: %d drop: %d scale: %d calib: %d", volts, BATTERY_DIVIDER, VOLTAGE_DROP, BATT_SCALE, g_eeGeneral.txVoltageCalibration);
adc = (volts - VOLTAGE_DROP) * BATTERY_DIVIDER / (128 + g_eeGeneral.txVoltageCalibration) / BATT_SCALE;
#elif defined(VOLTAGE_DROP)
// TRACE("volts: %d div: %d drop: %d", volts, BATTERY_DIVIDER, VOLTAGE_DROP);
adc = (volts - VOLTAGE_DROP) * BATTERY_DIVIDER / (1000 + g_eeGeneral.txVoltageCalibration);
#else
ret = (float)volts * 14.15f;
// TRACE("volts: %d div: %d calib: %d", volts, BATTERY_DIVIDER, g_eeGeneral.txVoltageCalibration);
adc = volts * BATTERY_DIVIDER / (1000 + g_eeGeneral.txVoltageCalibration);
#endif
return ret;
// TRACE("calc adc: %d", adc);
return adc * 2; // div by 2 in firmware filtered adc calcs
}
+1 -1
View File
@@ -106,7 +106,7 @@ class DLLEXPORT OpenTxSimulator : public SimulatorInterface
const char * getPhaseName(unsigned int phase);
const QString getCurrentPhaseName();
const char * getError();
const int voltageToAdc(const int volts);
const int voltageToAdc(const int voltage);
QString simuSdDirectory;
QString simuSettingsDirectory;