Electrical.nas 9.1 KB

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