From f5d725a2a8ee95b655d736eaeb70813a404adf94 Mon Sep 17 00:00:00 2001 From: Hunter McClelland Date: Tue, 23 Jun 2026 10:36:35 -0400 Subject: [PATCH] SITL: Blimp uses internal SITL::Battery --- libraries/SITL/SIM_Blimp.cpp | 55 ++++++++++++++++++++++++++++-------- libraries/SITL/SIM_Blimp.h | 4 ++- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/libraries/SITL/SIM_Blimp.cpp b/libraries/SITL/SIM_Blimp.cpp index 849c3e8db6a..03aab06e2b7 100644 --- a/libraries/SITL/SIM_Blimp.cpp +++ b/libraries/SITL/SIM_Blimp.cpp @@ -44,6 +44,13 @@ Blimp::Blimp(const char *frame_str) : drag_gyr_lin_constant = 0.035; lock_step_scheduled = true; + + constexpr float default_battery_resistance_ohm = 0.01f; + battery.setup(sitl->batt_capacity_ah, + default_battery_resistance_ohm, + sitl->batt_voltage, + ambient_outside_temperature_degC()); + ::printf("Starting Blimp model\n"); if (strstr(frame_str, "motor")) { @@ -65,16 +72,18 @@ void Blimp::calculate_forces(const struct sitl_input &input, Vector3f &body_acc, //all fin setup for (uint8_t i=0; i<4; i++) { fin[i].last_angle = fin[i].angle; - if (input.servos[i] == 0) { - fin[i].angle = 0; - fin[i].servo_angle = 0; - } else { - // filtered_servo_angle() normalises PWM against a fixed 1500 +/- 500 - // range, but blimp.parm uses SERVOn_MIN 500 / TRIM 1350 / MAX 2200; - // the +13.5 degree offset recentres that asymmetric range to a - // symmetric -76.5..+76.5 degrees with 0 degrees at servo trim - fin[i].angle = filtered_servo_angle(input, i)*radians(45.0f)+radians(13.5); - fin[i].servo_angle = filtered_servo_angle(input, i); + if (!battery_is_empty()) { + if (input.servos[i] == 0) { + fin[i].angle = 0; + fin[i].servo_angle = 0; + } else { + // filtered_servo_angle() normalises PWM against a fixed 1500 +/- 500 + // range, but blimp.parm uses SERVOn_MIN 500 / TRIM 1350 / MAX 2200; + // the +13.5 degree offset recentres that asymmetric range to a + // symmetric -76.5..+76.5 degrees with 0 degrees at servo trim + fin[i].angle = filtered_servo_angle(input, i)*radians(45.0f)+radians(13.5); + fin[i].servo_angle = filtered_servo_angle(input, i); + } } if (fin[i].angle < fin[i].last_angle) fin[i].dir = 0; //thus 0 = "angle is reducing" @@ -291,7 +300,7 @@ void Blimp::calculate_forces(const struct sitl_input &input, Vector3f &body_acc, } else { //MotorBlimp for (uint8_t i=0; i<4; i++) { - if (input.servos[i] == 0) { + if (battery_is_empty() || input.servos[i] == 0) { mot[i].throttle = 0; } else { mot[i].throttle = filtered_servo_angle(input, i); @@ -396,3 +405,27 @@ void Blimp::update(const struct sitl_input &input) update_battery(); } + +void Blimp::update_battery() +{ + battery.maybe_reset(sitl->batt_voltage, sitl->batt_capacity_ah); + battery_current = 0.0f; + + if (!battery_is_empty()) { + if (motorblimp) { + constexpr float motor_current_scaler_amps = 2.0f; + for (uint8_t i=0; i<4; i++) { + battery_current += fabsf(mot[i].throttle) * motor_current_scaler_amps; + } + } else { + constexpr float fin_current_scaler_amps = 0.01f; + for (uint8_t i=0; i<4; i++) { + battery_current += fabsf(fin[i].vel) * fin_current_scaler_amps; + } + } + } + + battery.consume_energy(battery_current, AP_HAL::micros64()); + battery_voltage = battery.get_voltage(); + battery_temperature_degC = battery.get_temperature_degC(); +} diff --git a/libraries/SITL/SIM_Blimp.h b/libraries/SITL/SIM_Blimp.h index 9bb94074c6a..a892289b25e 100644 --- a/libraries/SITL/SIM_Blimp.h +++ b/libraries/SITL/SIM_Blimp.h @@ -30,7 +30,7 @@ struct Fins float last_angle; float servo_angle; bool dir; - float vel; // velocity, in m/s + float vel; // ang velocity, in deg/s float T; //Tangential (thrust) force, in Neutons float N; //Normal force, in Newtons float Fx; //Fx,y,z = Force in bodyframe orientation at servo position, in Newtons @@ -84,6 +84,8 @@ protected: bool motorblimp = false; void calculate_forces(const struct sitl_input &input, Vector3f &rot_accel, Vector3f &body_accel); + void update_battery() override; + bool battery_is_empty() { return battery_voltage < 0.5f; }; float sq(float a) {return powf(a,2);} };