Optica_ammeter.nas 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Optica
  2. #
  3. # Ammeter support
  4. #
  5. # This is a very simple charge meter simulation. The idea is to use the ammeter to give a
  6. # visual clue to what is happening with the battery/alternator, not to accurately simulate
  7. # charging/discharing. Since this is rather abstract, the ammeter is dimensionless.
  8. #
  9. # The ammeter is assumed to be in charge-mode not load-mode; in other words, in series between
  10. # the battery and the load rather than in series with the alternator and the load. It monitors
  11. # the load from the master bus.
  12. #
  13. # Gary Neely aka 'Buckaroo'
  14. #
  15. var CHARGE_UPDATE = 1; # This needs to be 1 second.
  16. var BATT_CHARGE_RATE = 600; # Seconds to 'recharge' battery after using starter
  17. var battery_charge = props.globals.getNode("/systems/electrical/battery-charge");
  18. var sw_battery = props.globals.getNode("/controls/switches/battery");
  19. var sw_alternator = props.globals.getNode("/controls/switches/alternator");
  20. var sw_avionics = props.globals.getNode("/controls/switches/avionics");
  21. var sw_pitot = props.globals.getNode("/controls/switches/pitotheat");
  22. var sw_ltland = props.globals.getNode("/controls/lighting/landing");
  23. var sw_lttaxi = props.globals.getNode("/controls/lighting/taxi");
  24. var starter_used = props.globals.getNode("/systems/electrical/starter-used");
  25. var charge_update_loop = func {
  26. if (!sw_battery.getValue()) { # Meter reads zero if battery circuit open
  27. battery_charge.setValue(0); # Update the ammeter
  28. }
  29. else {
  30. if (sw_alternator.getValue() and getprop("/systems/electrical/suppliers/alternator") > 23.5) {
  31. # Alternator is supplying current:
  32. var load = 2; # Assume an arbitrary trickle charge to battery
  33. var starter_deficit = starter_used.getValue();
  34. if (starter_deficit) {
  35. load += 10 * starter_deficit/BATT_CHARGE_RATE; # Accounts for charging battery after a start
  36. }
  37. battery_charge.setValue(load); # Update the ammeter
  38. if (starter_deficit > 0) { # Decrement starter deficit to
  39. starter_used.setValue(starter_deficit - 1); # simulate charging
  40. }
  41. }
  42. else {
  43. var load = -3; # An arbitrary base system load
  44. if (sw_avionics.getValue()) { load -= 1; } # Add a little if avionics are on
  45. if (sw_ltland.getValue()) { load -= 3; } # Landing light draws a fair amount
  46. if (sw_lttaxi.getValue()) { load -= 2; } # Taxi light draw
  47. if (sw_pitot.getValue()) { load -= 2; } # Pitot heat draw
  48. battery_charge.setValue(load); # Update the ammeter
  49. }
  50. }
  51. settimer(charge_update_loop, CHARGE_UPDATE);
  52. }
  53. settimer(charge_update_loop, 2); # Delay startup a bit to allow things to initialize