electrical.nas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #### Single Starter/Generator electrical system ####
  2. #### Syd Adams ####
  3. #### Based on Curtis Olson's nasal electrical code ####
  4. var last_time = 0.0;
  5. var OutPuts = props.globals.getNode("/systems/electrical/outputs",1);
  6. var Volts = props.globals.getNode("/systems/electrical/volts",1);
  7. var Amps = props.globals.getNode("/systems/electrical/amps",1);
  8. var BATT = props.globals.getNode("/controls/electric/battery-switch",1);
  9. var ALT = props.globals.getNode("/controls/electric/engine/generator",1);
  10. var DIMMER = props.globals.getNode("/controls/lighting/instruments-norm",1);
  11. var NORM = 0.0357;
  12. var Battery={};
  13. var Alternator={};
  14. #var battery = Battery.new(volts,amps,amp_hours,charge_percent,charge_amps);
  15. Battery = {
  16. new : func {
  17. m = { parents : [Battery] };
  18. m.ideal_volts = arg[0];
  19. m.ideal_amps = arg[1];
  20. m.amp_hours = arg[2];
  21. m.charge_percent = arg[3];
  22. m.charge_amps = arg[4];
  23. return m;
  24. },
  25. apply_load : func {
  26. var amphrs_used = arg[0] * arg[1] / 3600.0;
  27. var percent_used = amphrs_used / me.amp_hours;
  28. me.charge_percent -= percent_used;
  29. if ( me.charge_percent < 0.0 ) {
  30. me.charge_percent = 0.0;
  31. } elsif ( me.charge_percent > 1.0 ) {
  32. me.charge_percent = 1.0;
  33. }
  34. return me.amp_hours * me.charge_percent;
  35. },
  36. get_output_volts : func {
  37. var x = 1.0 - me.charge_percent;
  38. var tmp = -(3.0 * x - 1.0);
  39. var factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
  40. return me.ideal_volts * factor;
  41. },
  42. get_output_amps : func {
  43. var x = 1.0 - me.charge_percent;
  44. var tmp = -(3.0 * x - 1.0);
  45. var factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
  46. return me.ideal_amps * factor;
  47. }
  48. };
  49. # var alternator = Alternator.new("rpm-source",rpm_threshold,volts,amps);
  50. Alternator = {
  51. new : func {
  52. m = { parents : [Alternator] };
  53. m.rpm_source = props.globals.getNode(arg[0],1);
  54. m.rpm_threshold = arg[1];
  55. m.ideal_volts = arg[2];
  56. m.ideal_amps = arg[3];
  57. return m;
  58. },
  59. apply_load : func( amps, dt) {
  60. var factor = me.rpm_source.getValue() / me.rpm_threshold;
  61. if ( factor > 1.0 ){
  62. factor = 1.0;
  63. }
  64. var available_amps = me.ideal_amps * factor;
  65. return available_amps - amps;
  66. },
  67. get_output_volts : func {
  68. var factor = me.rpm_source.getValue() / me.rpm_threshold;
  69. if ( factor > 1.0 ) {
  70. factor = 1.0;
  71. }
  72. return me.ideal_volts * factor;
  73. },
  74. get_output_amps : func {
  75. var factor = me.rpm_source.getValue() / me.rpm_threshold;
  76. if ( factor > 1.0 ) {
  77. factor = 1.0;
  78. }
  79. return me.ideal_amps * factor;
  80. }
  81. };
  82. var battery = Battery.new(12,30,12,1.0,7.0);
  83. var alternator = Alternator.new("/engines/engine/rpm",250.0,12.0,30.0);
  84. #####################################
  85. setlistener("/sim/signals/fdm-initialized", func {
  86. foreach(var a; props.globals.getNode("/systems/electrical/outputs").getChildren()){
  87. a.setValue(0);
  88. }
  89. props.globals.getNode("/controls/anti-ice/prop-heat",1).setBoolValue(0);
  90. props.globals.getNode("/controls/anti-ice/pitot-heat",1).setBoolValue(0);
  91. props.globals.getNode("/controls/lighting/landing-lights[0]",1).setBoolValue(0);
  92. props.globals.getNode("/controls/lighting/landing-lights[1]",1).setBoolValue(0);
  93. props.globals.getNode("/controls/lighting/beacon",1).setBoolValue(0);
  94. props.globals.getNode("/controls/lighting/nav-lights",1).setBoolValue(0);
  95. props.globals.getNode("/controls/lighting/cabin-lights",1).setBoolValue(0);
  96. props.globals.getNode("/controls/lighting/wing-lights",1).setBoolValue(0);
  97. props.globals.getNode("/controls/lighting/strobe",1).setBoolValue(0);
  98. props.globals.getNode("/controls/lighting/instrument-lights",1).setBoolValue(1);
  99. props.globals.getNode("/controls/lighting/instruments-norm",1).setBoolValue(1);
  100. props.globals.getNode("/controls/lighting/taxi-light",1).setBoolValue(0);
  101. props.globals.getNode("/controls/cabin/fan",1).setBoolValue(0);
  102. props.globals.getNode("/controls/cabin/heat",1).setBoolValue(0);
  103. props.globals.getNode("/controls/electric/external-power",1).setBoolValue(0);
  104. props.globals.getNode("/controls/electric/battery-switch",1).setBoolValue(0);
  105. props.globals.getNode("/controls/electric/key",1).setValue(0);
  106. props.globals.getNode("/controls/electric/engine/generator",1).setBoolValue(0);
  107. props.globals.getNode("/controls/engines/engine/magnetos",1).setBoolValue(0);
  108. props.globals.getNode("/controls/engines/engine/throttle",1).setValue("1");
  109. props.globals.getNode("/engines/engine/amp-v",1).setDoubleValue(0);
  110. props.globals.getNode("/controls/engines/engine/clutch",1).setBoolValue(0);
  111. props.globals.getNode("/controls/engines/engine/clutchguard",1).setBoolValue(0);
  112. props.globals.getNode("/controls/engines/engine/master-alt",1).setBoolValue(0);
  113. props.globals.getNode("/controls/engines/engine/master-bat",1).setBoolValue(0);
  114. props.globals.getNode("/controls/engines/engine/fuel-pump",1).setBoolValue(1);
  115. settimer(update_electrical,1);
  116. print("Electrical System ... OK");
  117. });
  118. var bus_volts = 0.0;
  119. var update_virtual_bus = func(dt) {
  120. var PWR = props.globals.getNode("systems/electrical/serviceable",1).getBoolValue();
  121. var engine_state = props.globals.getNode("/engines/engine/running",1).getBoolValue();
  122. var AltVolts = alternator.get_output_volts();
  123. var BatVolts = battery.get_output_volts();
  124. var load = 0.0;
  125. if (ALT.getBoolValue() and (AltVolts > (bus_volts-2))){ # Si le moteur tourne, et que le switch Alt est à ON et que la tension de l'alternateur est supérieur à celle du bus
  126. props.globals.getNode("/engines/engine/amp-v",1).setValue(AltVolts); # L'alternateur fournit la tension au moteur
  127. } elsif (BATT.getBoolValue()){ # Sinon si le switch de la batterie est à ON
  128. props.globals.getNode("/engines/engine/amp-v",1).setValue(BatVolts); # C'est la batterie qui fournie la tension au moteur
  129. } else {
  130. props.globals.getNode("/engines/engine/amp-v",1).setValue(0.0);
  131. }
  132. var bus_amps = 0.0;
  133. var power_source = nil;
  134. if (BATT.getBoolValue()){
  135. if(PWR){bus_volts = BatVolts;}
  136. power_source = "battery";
  137. } else {
  138. bus_volts = 0.0;
  139. }
  140. if (ALT.getBoolValue() and (AltVolts > (bus_volts-2))){
  141. if(PWR){bus_volts = AltVolts;}
  142. power_source = "alternator";
  143. }
  144. load += electrical_bus(bus_volts);
  145. load += avionics_bus(bus_volts);
  146. if (bus_volts > 1.0){
  147. if (power_source == "battery") { # si la source est la batterie
  148. bus_amps = load; # l'intensité utilisé par le bus est la charge de tout
  149. } else { # sinon
  150. bus_amps = battery.charge_amps; # Sinon l'amperage du bus = 7A fourni par l'alternateur (mais limité à 7 par la batterie)
  151. }
  152. }
  153. if (power_source == "battery"){ # si la source est la batterie
  154. battery.apply_load(load, dt); # on decharge la batterie
  155. } elsif (bus_volts > BatVolts){ # sinon si la tension du bus est plus grande que la tension de la batterie
  156. battery.apply_load(-battery.charge_amps, dt); # on charge la batterie en lui envoyant 7A
  157. }
  158. Amps.setValue(bus_amps);
  159. Volts.setValue(bus_volts);
  160. return load;
  161. }
  162. var electrical_bus = func(){
  163. var load = 0.0;
  164. var bus_volts = arg[0];
  165. var starter_volts = 0.0;
  166. if(props.globals.getNode("/controls/lighting/landing-lights").getBoolValue()){
  167. OutPuts.getNode("landing-lights",1).setValue(bus_volts);
  168. load += 1.2;
  169. } else {
  170. OutPuts.getNode("landing-lights",1).setValue(0.0);
  171. }
  172. if(props.globals.getNode("/controls/lighting/nav-lights").getBoolValue()){
  173. OutPuts.getNode("nav-lights",1).setValue(bus_volts);
  174. load += 0.2;
  175. } else {
  176. OutPuts.getNode("nav-lights",1).setValue(0.0);
  177. }
  178. if(props.globals.getNode("/controls/lighting/taxi-light").getBoolValue()){
  179. OutPuts.getNode("taxi-light",1).setValue(bus_volts);
  180. load += 1.2;
  181. } else {
  182. OutPuts.getNode("taxi-light",1).setValue(0.0);
  183. }
  184. if(props.globals.getNode("/controls/lighting/beacon").getBoolValue()){
  185. OutPuts.getNode("beacon",1).setValue(bus_volts);
  186. load += 1.2;
  187. } else {
  188. OutPuts.getNode("beacon",1).setValue(0.0);
  189. }
  190. if(props.globals.getNode("/controls/lighting/cabin-lights").getBoolValue()){
  191. OutPuts.getNode("cabin-lights",1).setValue(bus_volts);
  192. load += 0.6;
  193. } else {
  194. OutPuts.getNode("cabin-lights",1).setValue(0.0);
  195. }
  196. if(props.globals.getNode("/controls/engines/engine[0]/fuel-pump").getBoolValue()){
  197. OutPuts.getNode("fuel-pump",1).setValue(bus_volts);
  198. load += 1.2;
  199. } else {
  200. OutPuts.getNode("fuel-pump",1).setValue(0.0);
  201. }
  202. if (props.globals.getNode("/controls/engines/engine[0]/starter").getBoolValue()){
  203. starter_volts = bus_volts;
  204. load += 8.0;
  205. # print("Start engine");
  206. }
  207. OutPuts.getNode("starter",1).setValue(starter_volts);
  208. return load;
  209. }
  210. var avionics_bus = func() {
  211. var load = 0.0;
  212. var bus_volts = arg[0];
  213. if (props.globals.getNode("/controls/lighting/instrument-lights").getBoolValue()){
  214. OutPuts.getNode("instrument-lights",1).setValue((bus_volts) * DIMMER.getValue());
  215. load += 0.25 * DIMMER.getValue();
  216. } else {
  217. OutPuts.getNode("instrument-lights",1).setValue(0.0);
  218. }
  219. # OutPuts.getNode("transponder",1).setValue(bus_volts);
  220. # OutPuts.getNode("nav[0]",1).setValue(bus_volts);
  221. # OutPuts.getNode("nav[1]",1).setValue(bus_volts);
  222. # OutPuts.getNode("comm[0]",1).setValue(bus_volts);
  223. # OutPuts.getNode("comm[1]",1).setValue(bus_volts);
  224. setprop("/systems/electrical/outputs/nav[0]", bus_volts);
  225. setprop("/systems/electrical/outputs/comm[0]", bus_volts);
  226. if(bus_volts > 0) { #if there is power then turn on the radios
  227. setprop("systems/electrical/outputs/comm[0]", 1);
  228. if(getprop("sim/model/r44/controls/lighting/nav-lights") == 1) {
  229. setprop("controls/lighting/nav-lights",1);
  230. } else {
  231. setprop("controls/lighting/nav-lights",0);
  232. }
  233. if(getprop("sim/model/r44/controls/lighting/beacon") == 1) {
  234. setprop("controls/lighting/beacon",1);
  235. } else {
  236. setprop("controls/lighting/beacon",0);
  237. }
  238. if(getprop("sim/model/r44/use-landing-light") == 1) {
  239. setprop("/sim/rendering/als-secondary-lights/use-landing-light",1);
  240. } else {
  241. setprop("/sim/rendering/als-secondary-lights/use-landing-light",0);
  242. }
  243. } else {
  244. setprop("systems/electrical/outputs/comm[0]", 0);
  245. setprop("controls/lighting/nav-lights",0);
  246. setprop("controls/lighting/beacon",0);
  247. setprop("/sim/rendering/als-secondary-lights/use-landing-light",0);
  248. }
  249. return load;
  250. }
  251. var update_electrical = func {
  252. var time = getprop("/sim/time/elapsed-sec");
  253. var dt = time - last_time;
  254. var last_time = time;
  255. update_virtual_bus(dt);
  256. settimer(update_electrical, 0);
  257. }