Electrical.nas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #### electrical system taken from the S76C, modified for EC130 after POH ####
  2. #### Syd Adams ####
  3. var epu = func{
  4. var EP = props.globals.getValue("/controls/electric/external-power") or 0;
  5. if (EP > 0){
  6. setprop("/systems/electrical/external_volts", 29);
  7. }else{
  8. setprop("/systems/electrical/external_volts", 0);
  9. }
  10. settimer(epu, 0.1);
  11. }
  12. epu();
  13. var ammeter_ave = 0.0;
  14. var outPut = "/systems/electrical/outputs/";
  15. var BattVolts = props.globals.getNode("/systems/electrical/batt-volts",1);
  16. var Volts = props.globals.getNode("/systems/electrical/volts",1);
  17. var Amps = props.globals.getNode("/systems/electrical/amps",1);
  18. var EXT = props.globals.getNode("/controls/electric/external-power",1);
  19. var switch_list=[];
  20. var output_list=[];
  21. var watt_list=[];
  22. strobe_switch = props.globals.getNode("/controls/lighting/strobe", 1);
  23. aircraft.light.new("/controls/lighting/strobe-state", [0.05, 0.2, 0.05, 1.30], strobe_switch);
  24. beacon_switch = props.globals.getNode("/controls/lighting/beacon", 1);
  25. aircraft.light.new("/controls/lighting/beacon-state", [0.5, 1.0], beacon_switch);
  26. #var battery = Battery.new(switch-prop,volts,amps,amp_hours,charge_percent,charge_amps);
  27. Battery = {
  28. new : func(swtch,vlt,amp,hr,chp,cha){
  29. m = { parents : [Battery] };
  30. m.switch = props.globals.getNode(swtch,1);
  31. m.switch.setBoolValue(0);
  32. m.ideal_volts = vlt;
  33. m.ideal_amps = amp;
  34. m.amp_hours = hr;
  35. m.charge_percent = chp;
  36. m.charge_amps = cha;
  37. return m;
  38. },
  39. apply_load : func(load,dt) {
  40. if(me.switch.getValue()){
  41. var amphrs_used = load * dt / 3600.0;
  42. var percent_used = amphrs_used / me.amp_hours;
  43. me.charge_percent -= percent_used;
  44. if ( me.charge_percent < 0.0 ) {
  45. me.charge_percent = 0.0;
  46. } elsif ( me.charge_percent > 1.0 ) {
  47. me.charge_percent = 1.0;
  48. }
  49. var output =me.amp_hours * me.charge_percent;
  50. return output;
  51. } else return 0;
  52. },
  53. get_output_volts : func {
  54. if(me.switch.getValue()){
  55. var x = 1.0 - me.charge_percent;
  56. var tmp = -(3.0 * x - 1.0);
  57. var factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
  58. var output =me.ideal_volts * factor;
  59. return output;
  60. } else return 0;
  61. },
  62. get_output_amps : func {
  63. if(me.switch.getValue()){
  64. var x = 1.0 - me.charge_percent;
  65. var tmp = -(3.0 * x - 1.0);
  66. var factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
  67. var output =me.ideal_amps * factor;
  68. return output;
  69. } else return 0;
  70. }
  71. };
  72. # var alternator = Alternator.new(num,switch,rpm_source,rpm_threshold,volts,amps);
  73. Alternator = {
  74. new : func (num,switch,src,thr,vlt,amp){
  75. m = { parents : [Alternator] };
  76. m.switch = props.globals.getNode(switch,1);
  77. m.switch.setBoolValue(0);
  78. m.meter = props.globals.getNode("/systems/electrical/gen-load["~num~"]",1);
  79. m.meter.setDoubleValue(0);
  80. m.gen_output = props.globals.getNode("/engines/engine["~num~"]/amp-v",1);
  81. m.gen_output.setDoubleValue(0);
  82. m.meter.setDoubleValue(0);
  83. m.rpm_source = props.globals.getNode(src,1);
  84. m.rpm_threshold = thr;
  85. m.ideal_volts = vlt;
  86. m.ideal_amps = amp;
  87. return m;
  88. },
  89. apply_load : func(load) {
  90. var cur_volt=me.gen_output.getValue();
  91. var cur_amp=me.meter.getValue();
  92. if(cur_volt >1){
  93. var factor=1/cur_volt;
  94. gout = (load * factor);
  95. if(gout>1)gout=1;
  96. }else{
  97. gout=0;
  98. }
  99. if(cur_amp > gout)me.meter.setValue(cur_amp - 0.01);
  100. if(cur_amp < gout)me.meter.setValue(cur_amp + 0.01);
  101. },
  102. get_output_volts : func {
  103. var out = 0;
  104. if(me.switch.getBoolValue()){
  105. var factor = me.rpm_source.getValue() / me.rpm_threshold;
  106. if ( factor > 1.0 )factor = 1.0;
  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",28,30,15,1.0,7.0);
  125. var alternator1 = Alternator.new(0,"controls/electric/engine[0]/generator","/engines/engine[0]/rpm",160.0,28.0,60.0);
  126. #####################################
  127. setlistener("/sim/signals/fdm-initialized", func {
  128. BattVolts.setDoubleValue(0);
  129. init_switches();
  130. settimer(update_electrical,5);
  131. print("Electrical System ... ok");
  132. });
  133. init_switches = func() {
  134. var tprop=props.globals.getNode("/controls/electric/ammeter-switch",1);
  135. tprop.setBoolValue(1);
  136. tprop=props.globals.getNode("/controls/lighting/instrument-lights",1);
  137. tprop.setBoolValue(0);
  138. # mhab added
  139. tprop=props.globals.getNode("/instrumentation/VEMD/buttons/OFF1",1);
  140. tprop.setBoolValue(0);
  141. tprop=props.globals.getNode("/instrumentation/VEMD/buttons/OFF2",1);
  142. tprop.setBoolValue(0);
  143. tprop=props.globals.getNode("/instrumentation/VEMD/buttons/SCROLL",1);
  144. tprop.setBoolValue(0);
  145. tprop=props.globals.getNode("/instrumentation/VEMD/buttons/RESET",1);
  146. tprop.setBoolValue(0);
  147. tprop=props.globals.getNode("/instrumentation/VEMD/buttons/SELECT",1);
  148. tprop.setBoolValue(0);
  149. tprop=props.globals.getNode("/instrumentation/VEMD/buttons/PLUS",1);
  150. tprop.setBoolValue(0);
  151. tprop=props.globals.getNode("/instrumentation/VEMD/buttons/MINUS",1);
  152. tprop.setBoolValue(0);
  153. tprop=props.globals.getNode("/instrumentation/VEMD/buttons/ENTER",1);
  154. tprop.setBoolValue(0);
  155. tprop=props.globals.getNode("/instrumentation/VEMD/buttons/BRTP",1);
  156. tprop.setBoolValue(0);
  157. tprop=props.globals.getNode("/instrumentation/VEMD/buttons/BRTM",1);
  158. tprop.setBoolValue(0);
  159. # mhab added
  160. setprop("/controls/lighting/instrument-lights-selection",0);
  161. setprop("/controls/lighting/instrument-lights-norm",0.5);
  162. # mhab added
  163. setprop("/controls/lighting/instrument-lights-vemd-norm",0.5);
  164. # mhab added
  165. setprop("/controls/electric/horn",0);
  166. setprop("/controls/electric/directbat-switch",0);
  167. setprop("/controls/lighting/instrument-lights2",0);
  168. setprop("/controls/lighting/instrument-lights2-norm",0.5);
  169. setprop("/controls/lighting/tach-light-norm",0.5);
  170. setprop("/systems/electrical/outputs/tach-light-norm",0.5);
  171. setprop("/controls/engines/engine/governor",1);
  172. setprop("/controls/engines/engine/ebcauguard",1);
  173. setprop("/controls/engines/engine/ebcautest",0);
  174. append(switch_list,"/controls/engines/engine/starter");
  175. append(output_list,"starter");
  176. append(watt_list,10.0);
  177. append(switch_list,"/controls/anti-ice/pitot-heat");
  178. append(output_list,"pitot-heat");
  179. append(watt_list,0.5);
  180. # mhab added
  181. append(switch_list,"/controls/lighting/taxi-light");
  182. append(output_list,"taxi-light");
  183. append(watt_list,5.0);
  184. append(switch_list,"/controls/lighting/landing-lights");
  185. append(output_list,"landing-light");
  186. append(watt_list,10.0);
  187. append(switch_list,"/controls/lighting/instrument-lights");
  188. append(output_list,"instrument-lights");
  189. append(watt_list,10);
  190. append(switch_list,"/controls/lighting/dome-light");
  191. append(output_list,"dome-light");
  192. append(watt_list,10);
  193. append(switch_list,"/controls/lighting/beacon-state/state");
  194. append(output_list,"beacon");
  195. append(watt_list,0.5);
  196. # mhab 20140501 strobe added
  197. append(switch_list,"/controls/lighting/strobe-state/state");
  198. append(output_list,"strobe");
  199. append(watt_list,0.5);
  200. append(switch_list,"/controls/lighting/nav-lights");
  201. append(output_list,"nav-lights");
  202. append(watt_list,0.5);
  203. append(switch_list,"/controls/fuel/tank/boost-pump");
  204. append(output_list,"boost-pump");
  205. append(watt_list,16);
  206. append(switch_list,"/controls/electric/warningtest");
  207. append(output_list,"wlttest");
  208. append(watt_list,0.5);
  209. append(switch_list,"/controls/electric/firetest");
  210. append(output_list,"firetest");
  211. append(watt_list,0.5);
  212. append(switch_list,"/controls/electric/CWS");
  213. append(output_list,"CWS");
  214. append(watt_list,0.5);
  215. append(switch_list,"/controls/electric/servotest");
  216. append(output_list,"servo");
  217. append(watt_list,0.5);
  218. append(switch_list,"/controls/electric/horn");
  219. append(output_list,"horn");
  220. append(watt_list,0.5);
  221. append(switch_list,"/controls/electric/attitude");
  222. append(output_list,"attitude");
  223. append(watt_list,0.5);
  224. append(switch_list,"/controls/electric/avionics-switch");
  225. append(output_list,"avionics-switch");
  226. append(watt_list,10);
  227. append(switch_list,"/controls/electric/avionics-switch");
  228. append(output_list,"adf");
  229. append(watt_list,0.2);
  230. append(switch_list,"/controls/electric/gyrocompass");
  231. append(output_list,"gyrocompass");
  232. append(watt_list,0.5);
  233. append(switch_list,"/controls/electric/turn-coordinator");
  234. append(output_list,"turn-coordinator");
  235. append(watt_list,0.5);
  236. append(switch_list,"/controls/electric/avionics-switch");
  237. append(output_list,"dme");
  238. append(watt_list,0.2);
  239. # mhab commented out because redundant ?!
  240. #append(switch_list,"/controls/electric/avionics-switch");
  241. #append(output_list,"adf");
  242. #append(watt_list,0.2);
  243. # mhab commented out because redundant ?!
  244. #append(switch_list,"/controls/electric/avionics-switch");
  245. #append(output_list,"adf");
  246. #append(watt_list,0.2);
  247. append(switch_list,"/controls/electric/avionics-switch");
  248. append(output_list,"DG");
  249. append(watt_list,0.2);
  250. append(switch_list,"/controls/electric/avionics-switch");
  251. append(output_list,"transponder");
  252. append(watt_list,0.2);
  253. append(switch_list,"/controls/electric/avionics-switch");
  254. append(output_list,"turn-coordinator");
  255. append(watt_list,0.2);
  256. append(switch_list,"/controls/electric/avionics-switch");
  257. append(output_list,"comm");
  258. append(watt_list,0.2);
  259. append(switch_list,"/controls/electric/avionics-switch");
  260. append(output_list,"comm[1]");
  261. append(watt_list,10);
  262. append(switch_list,"/controls/electric/avionics-switch");
  263. append(output_list,"nav");
  264. append(watt_list,10);
  265. append(switch_list,"/controls/electric/avionics-switch");
  266. append(output_list,"nav[1]");
  267. append(watt_list,10);
  268. append(switch_list,"/controls/electric/directbat-switch");
  269. append(output_list,"transponder");
  270. append(watt_list,10);
  271. append(switch_list,"/controls/electric/directbat-switch");
  272. append(output_list,"NRgauge");
  273. append(watt_list,10);
  274. append(switch_list,"/controls/electric/directbat-switch");
  275. append(output_list,"instrument-lights2");
  276. append(watt_list,10);
  277. #append(switch_list,"/controls/electric/directbat-switch");
  278. #append(output_list,"dome-light");
  279. #append(watt_list,10);
  280. append(switch_list,"/controls/electric/directbat-switch");
  281. append(output_list,"emerg-float");
  282. append(watt_list,16);
  283. for(var i=0; i<size(switch_list); i+=1) {
  284. var tmp = props.globals.getNode(switch_list[i],1);
  285. tmp.setBoolValue(0);
  286. }
  287. setprop("/controls/electric/emergency-switch",0);
  288. setprop("/controls/electric/emergency-switch-locked",1);
  289. }
  290. ##################################
  291. update_virtual_bus = func( dt ) {
  292. var PWR = getprop("/systems/electrical/serviceable");
  293. var battery_volts = battery.get_output_volts();
  294. BattVolts.setValue(battery_volts);
  295. var alternator1_volts = alternator1.get_output_volts();
  296. var external_volts = props.globals.getValue("/systems/electrical/external_volts") or 0;
  297. var batepu_sw = props.globals.getValue("/controls/electric/battery-switch") or 0;
  298. var EP = props.globals.getValue("/controls/electric/external-power") or 0;
  299. load = 0.0;
  300. bus_volts = 0.0;
  301. power_source = nil;
  302. bus_volts = battery_volts;
  303. power_source = "battery";
  304. if (alternator1_volts > bus_volts + 0.5) {
  305. bus_volts = alternator1_volts;
  306. power_source = "alternator1";
  307. }
  308. if (( external_volts > bus_volts) and (batepu_sw >0)){
  309. bus_volts = external_volts;
  310. }
  311. if (EP >0) {
  312. BattVolts.setValue(0);
  313. }
  314. bus_volts *=PWR;
  315. load += electrical_bus(bus_volts);
  316. ammeter = 0.0;
  317. if ( power_source == "battery" ) {
  318. ammeter = -load;
  319. } else {
  320. ammeter = battery.charge_amps;
  321. }
  322. if ( power_source == "battery" ) {
  323. battery.apply_load( load, dt );
  324. } elsif ( bus_volts > battery_volts ) {
  325. battery.apply_load( -battery.charge_amps, dt );
  326. }
  327. ammeter_ave = 0.8 * ammeter_ave + 0.2 * ammeter;
  328. Amps.setValue(ammeter_ave);
  329. Volts.setValue(bus_volts);
  330. alternator1.apply_load(load);
  331. return load;
  332. }
  333. ################################
  334. electrical_bus = func(bv) {
  335. var bus_volts = bv;
  336. var load = 0.0;
  337. var srvc = 0.0;
  338. for(var i=0; i<size(switch_list); i+=1) {
  339. var srvc = getprop(switch_list[i]);
  340. load = load + srvc * watt_list[i];
  341. setprop(outPut~output_list[i],bus_volts * srvc);
  342. }
  343. # mhab added SELECT, DIMMER2, DIMM_VD, SYST_SWITCH
  344. var SELECT = getprop("/controls/lighting/instrument-lights-selection");
  345. var DIMMER = bus_volts * getprop("/controls/lighting/instrument-lights-norm");
  346. var DIMMER2= bus_volts * getprop("/controls/lighting/instrument-lights2-norm");
  347. var DIMM_VD= bus_volts * getprop("/controls/lighting/instrument-lights-vemd-norm");
  348. var INSTR_SWTCH = getprop("/controls/lighting/instrument-lights");
  349. var SYST_SWTCH = getprop("/controls/lighting/instrument-lights2");
  350. # day,night1/2: 0 .. no dim, 1 .. 0.5 dim, 2 .. 0.25 dim
  351. corr_factor=1;
  352. # selector night 1
  353. if ( SELECT == 1 ) corr_factor=0.5;
  354. # selector night 2
  355. if ( SELECT == 2 ) corr_factor=0.5;
  356. DIMMER=DIMMER*INSTR_SWTCH*corr_factor;
  357. DIMMER2=DIMMER2*SYST_SWTCH*corr_factor;
  358. DIMM_VD=(DIMMER+DIMM_VD)*0.5;
  359. setprop(outPut~"instrument-lights",DIMMER);
  360. setprop(outPut~"instrument-lights-norm",DIMMER * 0.0357);
  361. setprop(outPut~"instrument-lights2",DIMMER2);
  362. setprop(outPut~"instrument-lights2-norm",DIMMER2 * 0.0357);
  363. setprop(outPut~"instrument-lights-vemd",DIMM_VD);
  364. setprop(outPut~"instrument-lights-vemd-norm",DIMM_VD * 0.0357);
  365. # mhab added
  366. # rpm dimming only if selector is 1 or 2
  367. var DIMMER_RPM = bus_volts * getprop("/controls/lighting/tach-light-norm");
  368. if ( SELECT == 0 ) { DIMMER_RPM = bus_volts * 1; }
  369. setprop(outPut~"tach-light-norm",DIMMER_RPM * 0.0357);
  370. return load;
  371. }
  372. update_electrical = func {
  373. var scnd = getprop("/sim/time/delta-sec");
  374. update_virtual_bus( scnd );
  375. settimer(update_electrical, 0);
  376. }