C: Battery Discharge Rate: Difference between revisions

From FSDeveloper Wiki
Jump to navigationJump to search
(SimConnect code to fix the battery discharge rate bug)
 
No edit summary
Line 1: Line 1:
There's a long-standing bug in all versions of FS that causes the battery to discharge in under twenty minutes from the time you start to draw power. This is not related to load. Doug Dawson built an XML add-on that fixes the problem; this is the C equivalent that can be dropped into your code. SET_BATTERY_VOLTS is the enumeration name and I call the function from the CLIENT_4SEC event. By habit I prefix all SimConnect functions with 'sc_'.
Updated 30/04/2023 to use a percentage charge/drain rate per hour as I found an edge case that would cause failure if a specific voltage charge/drain was used.
 
//----------------------
 
There's a long-standing bug in all versions of FS that causes the battery to discharge in under twenty minutes from the time you start to draw power. This is not related to load. Doug Dawson built an XML add-on that fixes the problem; this is the C equivalent that can be dropped into your code. SET_BATTERY_VOLTS is the enumeration name and I call the function from the CLIENT_1SEC event. By habit I prefix all SimConnect functions with 'sc_'.


  hr = SimConnect_AddToDataDefinition(hSimConnect, SET_BATTERY_VOLTS, "ELECTRICAL BATTERY VOLTAGE", "Volts", SIMCONNECT_DATATYPE_FLOAT64, 0);
  hr = SimConnect_AddToDataDefinition(hSimConnect, SET_BATTERY_VOLTS, "ELECTRICAL BATTERY VOLTAGE", "Volts", SIMCONNECT_DATATYPE_FLOAT64, 0);


  // *******************************************************
  // *******************************************************
  // Set the battery voltage to avoid a long-standing bug
// This is called from the CLIENT_1SEC event.
// with a sub-twenty minute drain time
  // Set the battery voltage to avoid a long-standing bug with a sub-twenty minute drain time
  // Setting rate to one prevents any battery drain at all
  // Setting charge_discharge_rate to one hundred percent prevents any battery drain at all
  // Setting rate to zero will discharge at the default rate
  // Setting charge_discharge_rate to zero will discharge at the default rate
  // Any other value between zero and one will add/subtract from the current volts
  // Setting between one and 99 will charge/discharge at that percentage per hour
  // *******************************************************
  // *******************************************************
  void sc_battery_charger(HANDLE hSimConnect, double charge_discharge_rate, double current_volts)
  void sc_battery_charger(double charge_discharge_rate, double current_volts, double target_volts)
  {
  {
   double volts_out = 0;
   double volts_out = 0;
   double rate = 0;
   double rate = 0;
   
   
   // Sanity check
   // Rate setting
   if (charge_discharge_rate < 0 || charge_discharge_rate > 1.0)rate = 1.0;
   if (charge_discharge_rate >= 100.0)volts_out = target_volts; // No discharge
   else rate = charge_discharge_rate;
   else if (charge_discharge_rate)
  //Add the charge rate
  {
  volts_out = current_volts + rate;
    rate = charge_discharge_rate / 3600; // 3600 = 1 hour
  // Sanity check
    //Add the charge to the current voltage (negative for discharge)
  if (volts_out > 24.0) volts_out = 24.0;
    volts_out = current_volts + rate;
    // Sanity check
    if (volts_out > target_volts) volts_out = target_volts;
  }
   // Set the voltage on the sim
   // Set the voltage on the sim
   SimConnect_SetDataOnSimObject(hSimConnect, SET_BATTERY_VOLTS, SIMCONNECT_OBJECT_ID_USER, 0, 1, sizeof(volts_out), &volts_out);
   if (rate)SimConnect_SetDataOnSimObject(hSimConnect, SET_BATTERY_VOLTS, SIMCONNECT_OBJECT_ID_USER, 0, 1, sizeof(volts_out), &volts_out);
 
   return;
   return;
  }
  }
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]] [[Category: P3Dv4]] [[Category: P3Dv5]]
[[Category: Aircraft Design]] [[Category: Panel and Gauge Design]] [[Category: P3Dv4]] [[Category: P3Dv5]]

Revision as of 06:06, 30 April 2023

Updated 30/04/2023 to use a percentage charge/drain rate per hour as I found an edge case that would cause failure if a specific voltage charge/drain was used.

//----------------------

There's a long-standing bug in all versions of FS that causes the battery to discharge in under twenty minutes from the time you start to draw power. This is not related to load. Doug Dawson built an XML add-on that fixes the problem; this is the C equivalent that can be dropped into your code. SET_BATTERY_VOLTS is the enumeration name and I call the function from the CLIENT_1SEC event. By habit I prefix all SimConnect functions with 'sc_'.

hr = SimConnect_AddToDataDefinition(hSimConnect, SET_BATTERY_VOLTS, "ELECTRICAL BATTERY VOLTAGE", "Volts", SIMCONNECT_DATATYPE_FLOAT64, 0);
// *******************************************************
// This is called from the CLIENT_1SEC event.
// Set the battery voltage to avoid a long-standing bug with a sub-twenty minute drain time
// Setting charge_discharge_rate to one hundred percent prevents any battery drain at all
// Setting charge_discharge_rate to zero will discharge at the default rate
// Setting between one and 99 will charge/discharge at that percentage per hour
// *******************************************************
void sc_battery_charger(double charge_discharge_rate, double current_volts, double target_volts)
{
  double volts_out = 0;
  double rate = 0;

  // Rate setting
  if (charge_discharge_rate >= 100.0)volts_out = target_volts;		// No discharge
  else if (charge_discharge_rate)
  {
    rate = charge_discharge_rate / 3600; // 3600 = 1 hour
    //Add the charge to the current voltage (negative for discharge)
    volts_out = current_volts + rate;
    // Sanity check
    if (volts_out > target_volts) volts_out = target_volts;
  }
  // Set the voltage on the sim
  if (rate)SimConnect_SetDataOnSimObject(hSimConnect, SET_BATTERY_VOLTS, SIMCONNECT_OBJECT_ID_USER, 0, 1, sizeof(volts_out), &volts_out);
  return;
}