Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HITL: simulate battery voltage with any sensor #9212

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/main/fc/fc_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3607,15 +3607,11 @@ bool mspFCProcessInOutCommand(uint16_t cmdMSP, sbuf_t *dst, sbuf_t *src, mspResu
sbufAdvance(src, sizeof(uint16_t) * XYZ_AXIS_COUNT);
}

#if defined(USE_FAKE_BATT_SENSOR)
if (SIMULATOR_HAS_OPTION(HITL_EXT_BATTERY_VOLTAGE)) {
fakeBattSensorSetVbat(sbufReadU8(src) * 10);
simulatorData.vbat = sbufReadU8(src);
} else {
#endif
fakeBattSensorSetVbat((uint16_t)(SIMULATOR_FULL_BATTERY * 10.0f));
#if defined(USE_FAKE_BATT_SENSOR)
simulatorData.vbat = SIMULATOR_FULL_BATTERY;
}
#endif

if (SIMULATOR_HAS_OPTION(HITL_AIRSPEED)) {
simulatorData.airSpeed = sbufReadU16(src);
Expand Down
5 changes: 3 additions & 2 deletions src/main/fc/runtime_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ flightModeForTelemetry_e getFlightModeForTelemetry(void)

#ifdef USE_SIMULATOR
simulatorData_t simulatorData = {
.flags = 0,
.debugIndex = 0
.flags = 0,
.debugIndex = 0,
.vbat = 0
};
#endif
2 changes: 1 addition & 1 deletion src/main/fc/runtime_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ flightModeForTelemetry_e getFlightModeForTelemetry(void);

#define SIMULATOR_MSP_VERSION 2 // Simulator MSP version
#define SIMULATOR_BARO_TEMP 25 // °C
#define SIMULATOR_FULL_BATTERY 12.6f // Volts
#define SIMULATOR_FULL_BATTERY 126 // Volts*10
#define SIMULATOR_HAS_OPTION(flag) ((simulatorData.flags & flag) != 0)

typedef enum {
Expand Down
10 changes: 9 additions & 1 deletion src/main/sensors/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static bool batteryUseCapacityThresholds = false;
static bool batteryFullWhenPluggedIn = false;
static bool profileAutoswitchDisable = false;

static uint16_t vbat = 0; // battery voltage in 0.1V steps (filtered)
static uint16_t vbat = 0; // battery voltage in 0.01V steps (filtered)
static uint16_t powerSupplyImpedance = 0; // calculated impedance in milliohm
static uint16_t sagCompensatedVBat = 0; // calculated no load vbat
static bool powerSupplyImpedanceIsValid = false;
Expand Down Expand Up @@ -297,6 +297,14 @@ static void updateBatteryVoltage(timeUs_t timeDelta, bool justConnected)
vbat = 0;
break;
}

#ifdef USE_SIMULATOR
if (ARMING_FLAG(SIMULATOR_MODE_HITL) && SIMULATOR_HAS_OPTION(HITL_SIMULATE_BATTERY)) {
vbat = ((uint16_t)simulatorData.vbat)*10;
return;
}
#endif

if (justConnected) {
pt1FilterReset(&vbatFilterState, vbat);
} else {
Expand Down
Loading