diff --git a/companion/src/simulation/simulatorinterface.h b/companion/src/simulation/simulatorinterface.h index b04379b8e2b..f72f838b243 100644 --- a/companion/src/simulation/simulatorinterface.h +++ b/companion/src/simulation/simulatorinterface.h @@ -192,6 +192,7 @@ class SimulatorInterface : public QObject void auxSerialSetBaudrate(const quint8 port_num, const quint32 baudrate); void auxSerialStart(const quint8 port_num); void auxSerialStop(const quint8 port_num); + void txBatteryVoltageChanged(const int voltage); }; class SimulatorFactory { diff --git a/companion/src/simulation/simulatormainwindow.cpp b/companion/src/simulation/simulatormainwindow.cpp index e9c6efff059..9a0f0cc759a 100644 --- a/companion/src/simulation/simulatormainwindow.cpp +++ b/companion/src/simulation/simulatormainwindow.cpp @@ -60,7 +60,11 @@ SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, const QString & simula m_radioSizeConstraint(Qt::Horizontal | Qt::Vertical), m_firstShow(true), m_showRadioDocked(true), - m_showMenubar(true) + m_showMenubar(true), + m_batMin(0), + m_batMax(0), + m_batWarn(0), + m_batVoltage(0) { if (m_simulatorId.isEmpty()) { m_simulatorId = SimulatorLoader::findSimulatorByName(getCurrentFirmware()->getSimulatorId()); @@ -143,6 +147,7 @@ SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, const QString & simula connect(ui->actionReloadRadioData, &QAction::triggered, this, &SimulatorMainWindow::simulatorRestart); connect(ui->actionReloadLua, &QAction::triggered, m_simulator, &SimulatorInterface::setLuaStateReloadPermanentScripts); + connect(ui->actionSetTxBatteryVoltage, &QAction::triggered, this, &SimulatorMainWindow::openTxBatteryVoltageDialog); if (m_outputsWidget) { connect(this, &SimulatorMainWindow::simulatorStart, m_outputsWidget, &RadioOutputsWidget::start); @@ -154,6 +159,7 @@ SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, const QString & simula connect(this, &SimulatorMainWindow::simulatorRestart, m_simulatorWidget, &SimulatorWidget::restart); connect(ui->actionScreenshot, &QAction::triggered, m_simulatorWidget, &SimulatorWidget::captureScreenshot); connect(m_simulatorWidget, &SimulatorWidget::windowTitleChanged, this, &SimulatorMainWindow::setWindowTitle); + connect(m_simulatorWidget, &SimulatorWidget::settingsBatteryChanged, this, &SimulatorMainWindow::onSettingsBatteryChanged); } connect(m_simulator, &SimulatorInterface::auxSerialSendData, hostSerialConnector, &HostSerialConnector::sendSerialData); @@ -161,6 +167,7 @@ SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, const QString & simula connect(m_simulator, &SimulatorInterface::auxSerialSetBaudrate, hostSerialConnector, &HostSerialConnector::setSerialBaudRate); connect(m_simulator, &SimulatorInterface::auxSerialStart, hostSerialConnector, &HostSerialConnector::serialStart); connect(m_simulator, &SimulatorInterface::auxSerialStop, hostSerialConnector, &HostSerialConnector::serialStop); + connect(m_simulator, &SimulatorInterface::txBatteryVoltageChanged, this, &SimulatorMainWindow::onTxBatteryVoltageChanged); } SimulatorMainWindow::~SimulatorMainWindow() @@ -555,3 +562,49 @@ void SimulatorMainWindow::showAbout(bool show) msgBox.setText(aboutStr); msgBox.exec(); } + +void SimulatorMainWindow::onSettingsBatteryChanged(const int batMin, const int batMax, const unsigned int batWarn) +{ + m_batMin = batMin; + m_batMax = batMax; + m_batWarn = batWarn; + m_batVoltage = batWarn + 5; // abitary +0.5V +} + +void SimulatorMainWindow::openTxBatteryVoltageDialog() +{ + QDialog *dlg = new QDialog(this, Qt::WindowTitleHint | Qt::WindowSystemMenuHint); + + QLabel *lbl = new QLabel(tr("Set voltage")); + QDoubleSpinBox *sb = new QDoubleSpinBox(); + sb->setDecimals(1); + // vBatWarn is voltage in 100mV, vBatMin is in 100mV but with -9V offset, vBatMax has a -12V offset + sb->setMinimum((float)((m_batMin + 90) / 10.0f)); + sb->setMaximum((float)((m_batMax + 120) / 10.0f)); + sb->setSingleStep(0.1); + sb->setSuffix(tr("V")); + sb->setValue((float)m_batVoltage / 10.0f); + + auto *btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); + connect(btnBox, &QDialogButtonBox::accepted, dlg, &QDialog::accept); + connect(btnBox, &QDialogButtonBox::rejected, dlg, &QDialog::reject); + + auto * lo = new QGridLayout(dlg); + lo->addWidget(lbl, 0, 0); + lo->addWidget(sb, 0, 1); + lo->addWidget(btnBox, 1, 0, 1, 2); + + dlg->setWindowTitle(tr("Battery")); + dlg->deleteLater(); + + if(dlg->exec()) { + unsigned int volts = (unsigned int)((float)sb->value() * 10.0f); + m_batVoltage = volts; + emit txBatteryVoltageChanged(volts); + } +} + +void SimulatorMainWindow::onTxBatteryVoltageChanged(const unsigned int voltage) +{ + m_batVoltage = voltage; +} diff --git a/companion/src/simulation/simulatormainwindow.h b/companion/src/simulation/simulatormainwindow.h index cf4a426c19f..56771ac8934 100644 --- a/companion/src/simulation/simulatormainwindow.h +++ b/companion/src/simulation/simulatormainwindow.h @@ -71,6 +71,7 @@ class SimulatorMainWindow : public QMainWindow signals: void simulatorStart(); void simulatorRestart(); + void txBatteryVoltageChanged(int volts); // this changed value protected slots: virtual void closeEvent(QCloseEvent *); @@ -84,6 +85,10 @@ class SimulatorMainWindow : public QMainWindow void openSerialPortsDialog(bool); void showHelp(bool show); void showAbout(bool show); + void openTxBatteryVoltageDialog(); + // TODO: detect if changed via ui as currently event only on GeneralSettings (re)load + void onSettingsBatteryChanged(const int batMin, const int batMax, const unsigned int batWarn); + void onTxBatteryVoltageChanged(const unsigned int voltage); // something else changed voltage protected: void createDockWidgets(); @@ -114,6 +119,10 @@ class SimulatorMainWindow : public QMainWindow bool m_firstShow; bool m_showRadioDocked; bool m_showMenubar; + int m_batMin; + int m_batMax; + unsigned int m_batWarn; + unsigned int m_batVoltage; const static quint16 m_savedUiStateVersion; }; diff --git a/companion/src/simulation/simulatormainwindow.ui b/companion/src/simulation/simulatormainwindow.ui index faf956160da..5179d15be7f 100644 --- a/companion/src/simulation/simulatormainwindow.ui +++ b/companion/src/simulation/simulatormainwindow.ui @@ -69,6 +69,7 @@ Tools + @@ -234,6 +235,14 @@ About + + + TX Battery Voltage... + + + Open set transmitter battery voltage dialog + + diff --git a/companion/src/simulation/simulatorwidget.cpp b/companion/src/simulation/simulatorwidget.cpp index 3aaeebb1efd..f370c54019c 100644 --- a/companion/src/simulation/simulatorwidget.cpp +++ b/companion/src/simulation/simulatorwidget.cpp @@ -38,6 +38,7 @@ #include "joystickdialog.h" #endif #include "simulateduiwidgetGeneric.h" +#include "simulatormainwindow.h" #include #include @@ -106,6 +107,8 @@ SimulatorWidget::SimulatorWidget(QWidget * parent, SimulatorInterface * simulato connect(simulator, &SimulatorInterface::runtimeError, this, &SimulatorWidget::onSimulatorError); connect(simulator, &SimulatorInterface::phaseChanged, this, &SimulatorWidget::onPhaseChanged); + connect((SimulatorMainWindow *)parent, &SimulatorMainWindow::txBatteryVoltageChanged, this, &SimulatorWidget::onTxBatteryVoltageChanged); + m_timer.setInterval(SIMULATOR_INTERFACE_HEARTBEAT_PERIOD * 6); connect(&m_timer, &QTimer::timeout, this, &SimulatorWidget::onTimerEvent); @@ -154,6 +157,7 @@ void SimulatorWidget::setPaths(const QString & sdPath, const QString & dataPath) void SimulatorWidget::setRadioSettings(const GeneralSettings settings) { radioSettings = settings; + emit settingsBatteryChanged(settings.vBatMin, settings.vBatMax, settings.vBatWarn); } /* @@ -228,7 +232,7 @@ bool SimulatorWidget::setStartupData(const QByteArray & dataSource, bool fromFil return false; } - radioSettings = simuData.generalSettings; + setRadioSettings(simuData.generalSettings); startupFromFile = fromFile; return true; @@ -259,7 +263,7 @@ bool SimulatorWidget::setRadioData(RadioData * radioData) } if (ret) - radioSettings = radioData->generalSettings; + setRadioSettings(radioData->generalSettings); return ret; } @@ -670,8 +674,7 @@ void SimulatorWidget::restoreRadioWidgetsState() // Set throttle stick down and locked, side depends on mode emit stickModeChange(radioSettings.stickMode); - // TODO : custom voltages - qint16 volts = radioSettings.vBatWarn + 20; // 1V above min + qint16 volts = radioSettings.vBatWarn + 5; // +0.5V emit inputValueChange(SimulatorInterface::INPUT_SRC_TXVIN, 0, volts); } @@ -686,7 +689,6 @@ void SimulatorWidget::saveRadioWidgetsState(QList & state) } } - /* * Event handlers/private slots */ @@ -889,3 +891,8 @@ void SimulatorWidget::onjoystickButtonValueChanged(int button, bool state) } #endif } + +void SimulatorWidget::onTxBatteryVoltageChanged(qint16 volts) +{ + emit inputValueChange(SimulatorInterface::INPUT_SRC_TXVIN, 0, volts); +} diff --git a/companion/src/simulation/simulatorwidget.h b/companion/src/simulation/simulatorwidget.h index 0f1e9cafd5f..bcbb93f9469 100644 --- a/companion/src/simulation/simulatorwidget.h +++ b/companion/src/simulation/simulatorwidget.h @@ -99,6 +99,7 @@ class SimulatorWidget : public QWidget void simulatorStop(); void simulatorSdPathChange(const QString & sdPath, const QString & dataPath); void simulatorVolumeGainChange(const int gain); + void settingsBatteryChanged(const int batMin, const int batMax, const unsigned int batWarn); private slots: virtual void mousePressEvent(QMouseEvent *event); @@ -114,6 +115,7 @@ class SimulatorWidget : public QWidget void onRadioWidgetValueChange(const RadioWidget::RadioWidgetType type, int index, int value); void onjoystickAxisValueChanged(int axis, int value); void onjoystickButtonValueChanged(int button, bool state); + void onTxBatteryVoltageChanged(qint16 volts); void setRadioProfileId(int value); void setupRadioWidgets(); diff --git a/radio/src/hal/adc_driver.cpp b/radio/src/hal/adc_driver.cpp index 144098f7212..33f0bb0c91c 100644 --- a/radio/src/hal/adc_driver.cpp +++ b/radio/src/hal/adc_driver.cpp @@ -67,7 +67,7 @@ static bool adcSingleRead() bool adcRead() { adcSingleRead(); - + // TODO: this hack needs to go away... if (isVBatBridgeEnabled()) { disableVBatBridge(); diff --git a/radio/src/targets/simu/adc_driver.cpp b/radio/src/targets/simu/adc_driver.cpp index 3e6f6deda1e..376592554b8 100644 --- a/radio/src/targets/simu/adc_driver.cpp +++ b/radio/src/targets/simu/adc_driver.cpp @@ -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) { diff --git a/radio/src/targets/simu/opentxsimulator.cpp b/radio/src/targets/simu/opentxsimulator.cpp index 6cb84ab2f73..bf7bfb1967b 100644 --- a/radio/src/targets/simu/opentxsimulator.cpp +++ b/radio/src/targets/simu/opentxsimulator.cpp @@ -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 } diff --git a/radio/src/targets/simu/opentxsimulator.h b/radio/src/targets/simu/opentxsimulator.h index 2b61786c8d4..faac99fe3b8 100644 --- a/radio/src/targets/simu/opentxsimulator.h +++ b/radio/src/targets/simu/opentxsimulator.h @@ -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;