SR22TFuelPublisher.nas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2018 Stuart Buchanan
  2. # Copyright 2020 Julio Santa Cruz
  3. # This file is part of FlightGear.
  4. #
  5. # FlightGear is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # FlightGear is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with FlightGear. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # Fuel Publisher for Cirrus SR22T, providing information about fuel tank contents
  19. var SR22TFuelPublisher =
  20. {
  21. new : func (period=1.0) {
  22. var obj = {
  23. parents : [
  24. SR22TFuelPublisher,
  25. PeriodicPropertyPublisher.new(notifications.PFDEventNotification.FuelData, period)
  26. ],
  27. };
  28. obj.deltaT = period;
  29. # Hack to handle most aircraft not having proper engine hours
  30. if (getprop("/engines/engine[0]/hours") == nil) setprop("/engines/engine[0]/hours", 157.0);
  31. # Assume pilot has correct fuel quantities entered at Start of Day
  32. var tanks = props.getNode("/consumables/fuel",1).getChildren("tank");
  33. foreach(var tank; tanks) {
  34. var actual = tank.getNode("level-gal_us", 1).getValue();
  35. var indicatedNode = tank.getNode("fg1000-indicated-level-gal_us", 1);
  36. # Avoid Nasal error if value it's not a number.
  37. if (! isnum(actual)) {
  38. actual=0;
  39. }
  40. if (indicatedNode.getValue() == nil) indicatedNode.setValue(actual);
  41. }
  42. return obj;
  43. },
  44. # Custom publish method as we package the values into an array of fuel tanks,
  45. # assuming that fuel is drawn evenly from both tanks.
  46. publish : func() {
  47. var tank_data = [];
  48. var tanks = props.getNode("/consumables/fuel",1).getChildren("tank");
  49. foreach(var tank; tanks) {
  50. # fg1000-indicated-level-gal_us doesn't get updated...
  51. #var indicatedNode = tank.getNode("fg1000-indicated-level-gal_us", 1);
  52. var indicatedNode = tank.getNode("level-gal_us", 1);
  53. var fuel = indicatedNode.getValue();
  54. if (fuel == nil) fuel = 0;
  55. var fuel_flow = getprop("/engines/engine[0]/fuel-flow-gph");
  56. if (fuel_flow == nil) fuel_flow = 0;
  57. fuel = fuel - fuel_flow*me.deltaT/3600.0/2;
  58. indicatedNode.setValue(fuel);
  59. append(tank_data, {"FuelUSGal": fuel});
  60. }
  61. var notification = notifications.PFDEventNotification.new(
  62. "MFD",
  63. 1,
  64. notifications.PFDEventNotification.FuelData,
  65. { Id : "FuelData", Value : tank_data} );
  66. me._transmitter.NotifyAll(notification);
  67. },
  68. };