LCOV - code coverage report
Current view: top level - SITL - SIM_GeneratorEngine.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 21 23 91.3 %
Date: 2026-09-09 11:02:08 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "SIM_GeneratorEngine.h"
       2             : 
       3             : #include <AP_Math/AP_Math.h>
       4             : 
       5             : #include <GCS_MAVLink/GCS.h>
       6             : 
       7             : using namespace SITL;
       8             : 
       9      115834 : void SIM_GeneratorEngine::update()
      10             : {
      11      115834 :     if (current_current > 1 && is_zero(desired_rpm)) {
      12           0 :         AP_HAL::panic("Generator stalled due to high current demand (%u > 1)", (unsigned)current_current);
      13      115834 :     } else if (current_current > max_current) {
      14           0 :         AP_HAL::panic("Generator stalled due to high current demand (run)");
      15             :     }
      16             : 
      17             :     // linear degradation in RPM up to maximum load
      18      115834 :     if (!is_zero(desired_rpm)) {
      19       78013 :         desired_rpm -= 1500 * (current_current/max_current);
      20             :     }
      21             : 
      22      115834 :     const uint32_t now = AP_HAL::millis();
      23             : 
      24      115834 :     const float max_slew_rpm = max_slew_rpm_per_second * ((now - last_rpm_update_ms) / 1000.0f);
      25      115834 :     last_rpm_update_ms = now;
      26      115834 :     const float rpm_delta = current_rpm - desired_rpm;
      27      115834 :     if (rpm_delta > 0) {
      28        1791 :         current_rpm -= MIN(max_slew_rpm, rpm_delta);
      29             :     } else {
      30      114043 :         current_rpm += MIN(max_slew_rpm, abs(rpm_delta));
      31             :     }
      32             : 
      33             :     // update the temperature
      34      115834 :     const uint32_t time_delta_ms = now - last_heat_update_ms;
      35      115834 :     last_heat_update_ms = now;
      36             : 
      37      115834 :     constexpr float heat_environment_loss_factor = 0.15f;
      38             : 
      39      115834 :     const float factor = 0.0035;
      40      115834 :     temperature += (current_rpm * time_delta_ms * (1/1000.0f) * factor);
      41             :     // cap the heat of the motor:
      42      115834 :     temperature = MIN(temperature, 150);
      43             :     // now lose some heat to the environment
      44      115834 :     const float heat_loss = ((temperature * heat_environment_loss_factor * (time_delta_ms * (1/1000.0f))));  // lose some % of heat per second
      45             :     // GCS_SEND_TEXT(MAV_SEVERITY_INFO, "heat=%f loss=%f", temperature, heat_loss);
      46      115834 :     temperature -= heat_loss;
      47             : 
      48      115834 : }

Generated by: LCOV version 1.14