FuelSystem.nas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. ### Universal fuel transfer system by Tomaskom ###
  2. #Script for automated fuel transfer from fuel tanks to Engine feed tank based on fuel tank priority. Gathers fuel from fuel tanks within a priority group that have most fuel first - results in equalized fuel levels within the group. Also manages enabling all tanks when refueling.
  3. #Fuel tanks priority: fist empty droptanks (4,3), then wingtip fixed tanks (2) and fuselage tanks are last (1).
  4. var fuel_node = props.globals.getNode("/consumables/fuel");
  5. var engFeedN = 7; #number of tank which serves as engine feed
  6. var interval = 0.1; #approximate transfer cycle interval
  7. var galsPerSec = 1.5; #fuel flow to engine feed tank (gal_us/sec)
  8. #delta times for exact flow rates
  9. var lastTime = getprop("/sim/time/elapsed-sec");
  10. #property for ground refueling
  11. var refuel_galsPerSec = 0;
  12. #class representing a fuel tank
  13. var fuelTank = {
  14. #constructor
  15. new: func(tankN, prio = 0) {
  16. var m = { parents: [fuelTank] };
  17. m.tankNum = tankN;
  18. m.priority = prio;
  19. m.dump = 0;
  20. return m;
  21. },
  22. #get fuel tank emptying priority
  23. getPriority: func {
  24. return me.priority;
  25. },
  26. #get fuel level
  27. getLevel: func {
  28. return getprop("/consumables/fuel/tank["~me.tankNum~"]/level-gal_us");
  29. },
  30. #get fuel capacity
  31. getCapacity: func {
  32. return getprop("/consumables/fuel/tank["~me.tankNum~"]/capacity-gal_us");
  33. },
  34. #get requested ammount of fuel or all remaining fuel if less than requested
  35. takeFuel: func(fuel_req) {
  36. var fuel_level = getprop("/consumables/fuel/tank["~me.tankNum~"]/level-gal_us");
  37. if(fuel_level<fuel_req) {
  38. setprop("/consumables/fuel/tank["~me.tankNum~"]/level-gal_us", 0);
  39. return fuel_level;
  40. }
  41. else {
  42. setprop("/consumables/fuel/tank["~me.tankNum~"]/level-gal_us", fuel_level-fuel_req);
  43. return fuel_req;
  44. }
  45. },
  46. #pump requested ammount of fuel in this tank, returns overflow if any
  47. pushFuel: func(fuel_push) {
  48. var fuel_level = getprop("/consumables/fuel/tank["~me.tankNum~"]/level-gal_us");
  49. var fuel_capacity = getprop("/consumables/fuel/tank["~me.tankNum~"]/capacity-gal_us");
  50. if((fuel_level+fuel_push)>fuel_capacity) {
  51. setprop("/consumables/fuel/tank["~me.tankNum~"]/level-gal_us", fuel_capacity);
  52. return fuel_level+fuel_push-fuel_capacity;
  53. }
  54. else {
  55. setprop("/consumables/fuel/tank["~me.tankNum~"]/level-gal_us", fuel_level+fuel_push);
  56. return 0;
  57. }
  58. },
  59. #get enabled status
  60. getEnable: func {
  61. return getprop("/consumables/fuel/tank["~me.tankNum~"]/selected");
  62. },
  63. #set enabled status
  64. setEnable: func(enable) {
  65. setprop("/consumables/fuel/tank["~me.tankNum~"]/selected", enable);
  66. },
  67. #set amount of fuel (gal) to be dumped per sec
  68. fuelDump: func(dumpAmmount) {
  69. me.dump = dumpAmmount;
  70. },
  71. #perform dump, to be called during main loop
  72. performDump: func(dt) {
  73. if(me.dump > 0) {
  74. me.takeFuel(dt*me.dump);
  75. }
  76. },
  77. };
  78. var engineFeedTank = fuelTank.new(engFeedN, 0);
  79. #keep Engine feed tank enabled, TODO: change when engine startup procedures are implemented
  80. engFeedEnable = func {
  81. if(engineFeedTank.getEnable() == 0 and engineFeedTank.getLevel() > 0) {
  82. engineFeedTank.setEnable(1);
  83. }
  84. else {
  85. if(engineFeedTank.getEnable() == 1 and engineFeedTank.getLevel() == 0) {
  86. engineFeedTank.setEnable(0);
  87. }
  88. }
  89. }
  90. setlistener("/consumables/fuel/tank["~engFeedN~"]/selected", engFeedEnable);
  91. var tanks = [
  92. #fuselage tanks
  93. fuelTank.new(0, 1),
  94. fuelTank.new(1, 1),
  95. #wing tanks
  96. fuelTank.new(2, 2),
  97. fuelTank.new(3, 2),
  98. #wing droptanks
  99. fuelTank.new(4, 3),
  100. fuelTank.new(6, 3),
  101. #centre droptank
  102. fuelTank.new(5, 4),
  103. ];
  104. var doRefuel = func(dt) {
  105. if( getprop("/gear/gear[0]/wow")
  106. and getprop("/gear/gear[1]/wow")
  107. and getprop("/gear/gear[2]/wow")
  108. and (getprop("/velocities/groundspeed-kt") < 2) ) {
  109. #determine fuel to be allocated in this cycle
  110. var refuelAmmount = refuel_galsPerSec * dt;
  111. var maxPrio = 0;
  112. foreach(var tank; tanks) {
  113. if(tank.getPriority() > maxPrio) maxPrio = tank.getPriority();
  114. }
  115. for(var iPrioGroup=0; iPrioGroup<=maxPrio; iPrioGroup+=1) {
  116. var thisGroup = []; #print("refueling group "~iPrioGroup);
  117. forindex(var iTank; tanks) {
  118. if(tanks[iTank].getPriority() == iPrioGroup) {
  119. append(thisGroup, {index:iTank, fuelLevel:tanks[iTank].getLevel()});
  120. #print("appending tank "~iTank~" (prio="~iPrioGroup~")");
  121. }
  122. }
  123. if(size(thisGroup) == 0) continue;
  124. #get previous array sorted - descending order
  125. var sortedGroup = sort(thisGroup,
  126. func(h1, h2){return h1.fuelLevel-h2.fuelLevel;} );
  127. #try to push remaining available fuel and save overflow for next tanks
  128. foreach(var iSorted; sortedGroup) {
  129. #print("prerefuelAmmount: "~refuelAmmount);
  130. refuelAmmount = tanks[iSorted.index].pushFuel(refuelAmmount);
  131. #print("refuelAmmount: "~refuelAmmount);
  132. }
  133. }
  134. if(refuelAmmount > 0) {
  135. screen.log.write("Refueling finished, all mounted tanks are full. ", 1, 0.6, 0.1);
  136. refuel_galsPerSec = 0;
  137. }
  138. }
  139. else {
  140. refuel_galsPerSec = 0;
  141. screen.log.write("You must be stopped on the ground for refueling!", 1, 0.6, 0.1);
  142. }
  143. }
  144. #function to be used externally to engage ground refuel - parameter sets flow in US gallons per second
  145. var refuel = func(fuelGalsPerSec) {
  146. if(fuelGalsPerSec>0) {
  147. refuel_galsPerSec = fuelGalsPerSec;
  148. }
  149. else {
  150. refuel_galsPerSec = 0;
  151. }
  152. }
  153. #cycle:
  154. #check how much fuel is missing in the Engine feed tank
  155. #check which highest priority still has fuel
  156. #get remaining fuel levels of those tanks
  157. #request balanced ammount of fuel from each tank (to keep levels equal)
  158. #store the fuel to the Engine feed tank
  159. var fuelTransfer = func {
  160. #get delta time
  161. var nowTime = getprop("/sim/time/elapsed-sec");
  162. var simSpeed = getprop("/sim/speed-up");
  163. var dTime = (nowTime - lastTime) * simSpeed;
  164. if(dTime < 0) dTime = 0;
  165. #try ground refuel
  166. if(refuel_galsPerSec > 0) doRefuel(dTime);
  167. var cycleAmmount = galsPerSec * dTime;
  168. #check how much fuel is missing in the Engine feed tank
  169. var missingFuel = engineFeedTank.getCapacity() - engineFeedTank.getLevel();
  170. var fuelToGet = (cycleAmmount < missingFuel ? cycleAmmount : missingFuel);
  171. #check which highest priority still has fuel (and perform dump if tank is set to do it)
  172. var maxNonemptyPrio = 0;
  173. forindex(var i; tanks) {
  174. tanks[i].performDump(dTime);
  175. if( tanks[i].getLevel() > 0 and tanks[i].getPriority() > maxNonemptyPrio ) {
  176. maxNonemptyPrio = tanks[i].getPriority();
  177. }
  178. }
  179. #create array of hashes, each hash contains index (from tanks[]), fuel level and fuel to be requested
  180. var activeTanks = [];
  181. forindex(var i; tanks) {
  182. if( tanks[i].getPriority() == maxNonemptyPrio ) {
  183. append(activeTanks, {index:i, fuelLevel:tanks[i].getLevel(), reqFuel:0});
  184. }
  185. }
  186. #skip loop if no tanks (eg. when not yet initialized on startup)
  187. if(size(activeTanks) == 0) {
  188. settimer(fuelTransfer, interval);
  189. lastTime = nowTime;
  190. return;
  191. }
  192. #get previous array sorted - descending order
  193. var sortedTanks = sort(activeTanks,
  194. func(h1, h2){return h2.fuelLevel-h1.fuelLevel;} );
  195. #debug check for correct sorting
  196. #forindex(var i; sortedTanks) {
  197. # print("Sorted tank " ~ sortedTanks[i].index ~ ", fuel level: " ~ sortedTanks[i].fuelLevel);
  198. #}
  199. var numTanks = size(sortedTanks);
  200. var gatheredFuel = 0;
  201. var cont = 1; #continue looping?
  202. var stopIndex = -1; #index where looping stopped
  203. for(var i = 0; cont; i+=1) {
  204. if( (i+1) < numTanks and (gatheredFuel + (sortedTanks[i].fuelLevel - sortedTanks[i+1].fuelLevel) * (i+1)) < fuelToGet ) {
  205. gatheredFuel += (sortedTanks[i].fuelLevel - sortedTanks[i+1].fuelLevel) * (i+1);
  206. }
  207. else {
  208. stopIndex = i;
  209. cont = 0;
  210. }
  211. }
  212. var toGather = fuelToGet - gatheredFuel;
  213. var leastReducedLevel = sortedTanks[stopIndex].fuelLevel; #level of tank with least fuel which still gets used
  214. #set required fuel
  215. for(var i = 0; i < stopIndex; i+=1) {
  216. sortedTanks[i].reqFuel += sortedTanks[i].fuelLevel - leastReducedLevel;
  217. }
  218. for(var i = 0; i <= stopIndex; i+=1) {
  219. sortedTanks[i].reqFuel += toGather/(stopIndex+1);
  220. }
  221. #debug check for correct reqFuel
  222. #forindex(var i; sortedTanks) {
  223. # print("Tank " ~ sortedTanks[i].index ~ ", fuel level: " ~ sortedTanks[i].fuelLevel);
  224. # print("Requesting fuel: " ~ sortedTanks[i].reqFuel);
  225. #}
  226. #execute fuel transfer
  227. var transferedFuel = 0;
  228. forindex(var i; sortedTanks) {
  229. transferedFuel += tanks[sortedTanks[i].index].takeFuel(sortedTanks[i].reqFuel);
  230. }
  231. engineFeedTank.pushFuel(transferedFuel);
  232. lastTime = nowTime;
  233. settimer(fuelTransfer, interval/simSpeed);
  234. }
  235. fuelTransfer();
  236. #Air-to-Air refueling and tank enable control - all tanks temporarily enabled when receiving fuel and disabled otherwise (except for Engine feed tank)
  237. aarHandle = func {
  238. if(getprop("/systems/refuel/contact")) {
  239. foreach (var tank; tanks) {
  240. tank.setEnable(1);
  241. }
  242. }
  243. else {
  244. foreach (var tank; tanks) {
  245. tank.setEnable(0);
  246. }
  247. }
  248. }
  249. #set listener on the aar contact property
  250. setlistener("/systems/refuel/contact", aarHandle, 1, 0);
  251. #set listeners on all tanks enable property to counter manual changes
  252. foreach (var tank; tanks) {
  253. setlistener("/consumables/fuel/tank["~tank.tankNum~"]/selected", aarHandle, 1, 0);
  254. }
  255. print("Mako fuel system initialized");