electrical.nas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #### piston engine electrical system ####
  2. #### Syd Adams ####
  3. var ammeter_ave = 0.0;
  4. var outPut = "systems/electrical/outputs/";
  5. var BattVolts = props.globals.getNode("systems/electrical/batt-volts",1);
  6. var Volts = props.globals.getNode("/systems/electrical/volts",1);
  7. var Amps = props.globals.getNode("/systems/electrical/amps",1);
  8. var EXT = props.globals.getNode("/controls/electric/external-power",0);
  9. var switch_list=[];
  10. var output_list=[];
  11. var watt_list=[];
  12. var breaker_list=[];
  13. strobe_switch = props.globals.getNode("controls/lighting/strobe", 1);
  14. aircraft.light.new("controls/lighting/strobe-state", [0.05, 1.0], strobe_switch);
  15. beacon_switch = props.globals.getNode("controls/lighting/beacon", 1);
  16. aircraft.light.new("controls/lighting/beacon-state", [1.0, 1.0], beacon_switch);
  17. props.globals.getNode("controls/lighting/beacon", 1).setBoolValue(0);
  18. props.globals.getNode("controls/circuit-breakers/master", 1).setBoolValue(1);
  19. props.globals.getNode("controls/circuit-breakers/instruments", 1).setBoolValue(1);
  20. props.globals.getNode("controls/circuit-breakers/avionic-bus", 1).setBoolValue(1);
  21. props.globals.getNode("controls/circuit-breakers/fuel-pump", 1).setBoolValue(1);
  22. props.globals.getNode("controls/circuit-breakers/starter", 1).setBoolValue(1);
  23. props.globals.getNode("controls/electric/battery-switch", 1).setBoolValue(0);
  24. props.globals.getNode("controls/electric/engine/generator", 1).setBoolValue(0);
  25. props.globals.getNode("controls/switches/landing-light", 1).setBoolValue(0);
  26. props.globals.getNode("controls/gear/brake-parking", 1).setBoolValue(1);
  27. #props.globals.getNode("controls/electric/avionics-switch", 1).setBoolValue(0);
  28. #props.globals.getNode("controls/switches/fuel-pump", 1).setBoolValue(0);
  29. #props.globals.getNode("controls/lighting/instrument-lights", 1).setBoolValue(0);
  30. #props.globals.getNode("controls/gear/gear-down", 1).setBoolValue(0);
  31. #var battery = Battery.new(switch-prop,volts,amps,amp_hours,charge_percent,charge_amps);
  32. Battery = {
  33. new : func(swtch,vlt,amp,hr,chp,cha){
  34. m = { parents : [Battery] };
  35. m.switch = props.globals.getNode(swtch,1);
  36. if(m.switch.getValue()==nil)m.switch.setBoolValue(0);
  37. m.ideal_volts = vlt;
  38. m.ideal_amps = amp;
  39. m.amp_hours = hr;
  40. m.charge_percent = chp;
  41. m.charge_amps = cha;
  42. return m;
  43. },
  44. apply_load : func(load,dt) {
  45. if(me.switch.getValue()){
  46. var amphrs_used = load * dt / 3600.0;
  47. var percent_used = amphrs_used / me.amp_hours;
  48. me.charge_percent -= percent_used;
  49. if ( me.charge_percent < 0.0 ) {
  50. me.charge_percent = 0.0;
  51. } elsif ( me.charge_percent > 1.0 ) {
  52. me.charge_percent = 1.0;
  53. }
  54. var output =me.amp_hours * me.charge_percent;
  55. return output;
  56. }else return 0;
  57. },
  58. get_output_volts : func {
  59. if(me.switch.getValue()){
  60. var x = 1.0 - me.charge_percent;
  61. var tmp = -(3.0 * x - 1.0);
  62. var factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
  63. var output =me.ideal_volts * factor;
  64. return output;
  65. }else return 0;
  66. },
  67. get_output_amps : func {
  68. if(me.switch.getValue()){
  69. var x = 1.0 - me.charge_percent;
  70. var tmp = -(3.0 * x - 1.0);
  71. var factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
  72. var output =me.ideal_amps * factor;
  73. return output;
  74. }else return 0;
  75. }
  76. };
  77. #var alternator = Alternator.new(num,switch,n2_source,n2_threshold,volts,amps);
  78. Alternator = {
  79. new : func (num,switch,src,thr,vlt,amp){
  80. m = { parents : [Alternator] };
  81. m.switch = props.globals.getNode(switch,1);
  82. if(m.switch.getValue()==nil)m.switch.setBoolValue(0);
  83. m.meter = props.globals.getNode("systems/electrical/gen-load["~num~"]",1);
  84. m.meter.setDoubleValue(0);
  85. m.gen_output = props.globals.getNode("engines/engine["~num~"]/amp-v",1);
  86. m.gen_output.setDoubleValue(0);
  87. m.meter.setDoubleValue(0);
  88. m.n2_source = props.globals.getNode(src,1);
  89. m.n2_threshold = thr;
  90. m.ideal_volts = vlt;
  91. m.ideal_amps = amp;
  92. return m;
  93. },
  94. apply_load : func(load) {
  95. var cur_volt=me.gen_output.getValue();
  96. var cur_amp=me.meter.getValue();
  97. if(cur_volt >1){
  98. var factor=1/cur_volt;
  99. gout = (load * factor);
  100. if(gout>1)gout=1;
  101. }else{
  102. gout=0;
  103. }
  104. if(cur_amp > gout)me.meter.setValue(cur_amp - 0.01);
  105. if(cur_amp < gout)me.meter.setValue(cur_amp + 0.01);
  106. },
  107. get_output_volts : func {
  108. var out = 0;
  109. if(me.switch.getBoolValue()){
  110. var factor = me.n2_source.getValue() / me.n2_threshold;
  111. if ( factor > 1.0 )factor = 1.0;
  112. var out = (me.ideal_volts * factor);
  113. }
  114. me.gen_output.setValue(out);
  115. return out;
  116. },
  117. get_output_amps : func {
  118. var ampout =0;
  119. if(me.switch.getBoolValue()){
  120. var factor = me.n2_source.getValue() / me.n2_threshold;
  121. if ( factor > 1.0 ) {
  122. factor = 1.0;
  123. }
  124. ampout = me.ideal_amps * factor;
  125. }
  126. return ampout;
  127. }
  128. };
  129. var battery = Battery.new("/controls/electric/battery-switch",24,30,34,1.0,7.0);
  130. var alternator1 = Alternator.new(0,"controls/electric/engine[0]/generator","/engines/engine[0]/n2",20.0,27.0,70.0);
  131. var alternator2 = Alternator.new(0,"controls/electric/engine[1]/generator","/engines/engine[1]/n2",20.0,27.0,70.0);
  132. #####################################
  133. setlistener("/sim/signals/fdm-initialized", func {
  134. BattVolts.setDoubleValue(0);
  135. init_switches();
  136. settimer(update_electrical,5);
  137. print("Electrical System ... ok");
  138. });
  139. init_switches = func() {
  140. var tprop=props.globals.getNode("controls/electric/ammeter-switch",1);
  141. if(tprop.getValue()==nil)tprop.setBoolValue(0);
  142. setprop("controls/lighting/instrument-lights-norm",0.8);
  143. append(switch_list,"controls/engines/engine/starter");
  144. append(output_list,"starter");
  145. append(watt_list,10.0);
  146. append(breaker_list,"controls/circuit-breakers/starter");
  147. append(switch_list,"controls/anti-ice/pitot-heat");
  148. append(output_list,"pitot-heat");
  149. append(watt_list,0.5);
  150. append(breaker_list,"controls/circuit-breakers/master");
  151. append(switch_list,"controls/lighting/landing-lights");
  152. append(output_list,"landing-lights");
  153. append(watt_list,1.0);
  154. append(breaker_list,"controls/circuit-breakers/master");
  155. append(switch_list,"controls/lighting/instrument-lights");
  156. append(output_list,"instrument-lights");
  157. append(watt_list,0.2);
  158. append(breaker_list,"controls/circuit-breakers/master");
  159. append(switch_list,"controls/lighting/panel-norm");
  160. append(output_list,"panel-norm");
  161. append(watt_list,0.2);
  162. append(breaker_list,"controls/circuit-breakers/master");
  163. append(switch_list,"controls/lighting/instruments-norm");
  164. append(output_list,"instruments-norm");
  165. append(watt_list,0.2);
  166. append(breaker_list,"controls/circuit-breakers/master");
  167. append(switch_list,"controls/lighting/beacon-state/state");
  168. append(output_list,"beacon");
  169. append(watt_list,0.5);
  170. append(breaker_list,"controls/circuit-breakers/master");
  171. append(switch_list,"controls/lighting/nav-lights");
  172. append(output_list,"nav-lights");
  173. append(watt_list,0.5);
  174. append(breaker_list,"controls/circuit-breakers/master");
  175. append(switch_list,"controls/lighting/strobe-state/state");
  176. append(output_list,"strobe");
  177. append(watt_list,0.5);
  178. append(breaker_list,"controls/circuit-breakers/master");
  179. append(switch_list,"controls/engines/engine/fuel-pump");
  180. append(output_list,"fuel-pump");
  181. append(watt_list,0.5);
  182. append(breaker_list,"controls/circuit-breakers/fuel-pump");
  183. append(switch_list,"controls/electric/avionics-switch");
  184. append(output_list,"adf");
  185. append(watt_list,0.2);
  186. append(breaker_list,"controls/circuit-breakers/instruments");
  187. append(switch_list,"controls/electric/avionics-switch");
  188. append(output_list,"dme");
  189. append(watt_list,0.2);
  190. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  191. append(switch_list,"controls/electric/avionics-switch");
  192. append(output_list,"KNS80");
  193. append(watt_list,0.2);
  194. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  195. append(switch_list,"controls/electric/avionics-switch");
  196. append(output_list,"autopilot");
  197. append(watt_list,0.7);
  198. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  199. append(switch_list,"controls/electric/avionics-switch");
  200. append(output_list,"adf");
  201. append(watt_list,0.2);
  202. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  203. append(switch_list,"controls/electric/avionics-switch");
  204. append(output_list,"gps");
  205. append(watt_list,0.5);
  206. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  207. append(switch_list,"controls/electric/avionics-switch");
  208. append(output_list,"adf");
  209. append(watt_list,0.2);
  210. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  211. append(switch_list,"controls/electric/avionics-switch");
  212. append(output_list,"DG");
  213. append(watt_list,0.2);
  214. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  215. append(switch_list,"controls/electric/avionics-switch");
  216. append(output_list,"transponder");
  217. append(watt_list,0.2);
  218. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  219. # append(switch_list,"controls/electric/avionics-switch");
  220. # append(output_list,"ki525a");
  221. # append(watt_list,0.2);
  222. # append(switch_list,"controls/electric/avionics-switch");
  223. # append(output_list,"tacan");
  224. # append(watt_list,0.2);
  225. append(switch_list,"controls/electric/avionics-switch");
  226. append(output_list,"turn-coordinator");
  227. append(watt_list,0.2);
  228. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  229. append(switch_list,"controls/electric/avionics-switch");
  230. append(output_list,"comm");
  231. append(watt_list,0.2);
  232. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  233. append(switch_list,"controls/electric/avionics-switch");
  234. append(output_list,"comm[1]");
  235. append(watt_list,0.2);
  236. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  237. append(switch_list,"controls/electric/avionics-switch");
  238. append(output_list,"nav");
  239. append(watt_list,0.2);
  240. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  241. append(switch_list,"controls/electric/avionics-switch");
  242. append(output_list,"nav[1]");
  243. append(watt_list,0.2);
  244. append(breaker_list,"controls/circuit-breakers/avionic-bus");
  245. for(var i=0; i<size(switch_list); i+=1) {
  246. var tmp = props.globals.getNode(switch_list[i],1);
  247. tmp.setBoolValue(0);
  248. }
  249. setprop("controls/electric/avionics-switch",1);
  250. }
  251. update_virtual_bus = func( dt ) {
  252. var PWR = getprop("systems/electrical/serviceable");
  253. var battery_volts = battery.get_output_volts();
  254. battery_volts *= getprop("controls/circuit-breakers/master") or 0;
  255. BattVolts.setValue(battery_volts);
  256. var alternator1_volts = alternator1.get_output_volts();
  257. var alternator2_volts = alternator2.get_output_volts();
  258. var external_volts = 24.0;
  259. load = 0.0;
  260. bus_volts = 0.0;
  261. power_source = nil;
  262. bus_volts = battery_volts;
  263. power_source = "battery";
  264. if (alternator1_volts > bus_volts) {
  265. bus_volts = alternator1_volts;
  266. power_source = "alternator1";
  267. }
  268. if (alternator2_volts > bus_volts) {
  269. bus_volts = alternator2_volts;
  270. power_source = "alternator2";
  271. }
  272. if ( EXT.getBoolValue() and ( external_volts > bus_volts) ) {
  273. bus_volts = external_volts;
  274. }
  275. bus_volts *=PWR;
  276. load += electrical_bus(bus_volts);
  277. ammeter = 0.0;
  278. if ( power_source == "battery" ) {
  279. ammeter = -load;
  280. } else {
  281. ammeter = battery.charge_amps;
  282. }
  283. if ( power_source == "battery" ) {
  284. battery.apply_load( load, dt );
  285. } elsif ( bus_volts > battery_volts ) {
  286. battery.apply_load( -battery.charge_amps, dt );
  287. }
  288. ammeter_ave = 0.8 * ammeter_ave + 0.2 * ammeter;
  289. Amps.setValue(ammeter_ave);
  290. Volts.setValue(bus_volts);
  291. alternator1.apply_load(load);
  292. return load;
  293. }
  294. electrical_bus = func(bv) {
  295. var bus_volts = bv;
  296. var load = 0.0;
  297. var srvc = 0.0;
  298. for(var i=0; i<size(switch_list); i+=1) {
  299. var srvc = getprop(switch_list[i] or 0);
  300. var brkr = getprop(breaker_list[i]) or 0;
  301. load = load + srvc * watt_list[i];
  302. setprop(outPut~output_list[i],bus_volts * srvc * brkr);
  303. }
  304. var DIMMER = bus_volts * getprop("controls/lighting/instrument-lights-norm");
  305. var INSTR_SWTCH = getprop("controls/lighting/instrument-lights");
  306. DIMMER=DIMMER*INSTR_SWTCH;
  307. setprop(outPut~"instrument-lights",DIMMER);
  308. setprop(outPut~"instrument-lights-norm",DIMMER * 0.0357);
  309. return load;
  310. }
  311. update_electrical = func {
  312. var scnd = getprop("sim/time/delta-sec");
  313. update_virtual_bus( scnd );
  314. settimer(update_electrical, 0);
  315. }