1
0
Fork 0

c172p: use local Nasal vars, typify properties

This commit is contained in:
ThorstenB 2012-07-18 21:23:50 +02:00
parent 5ca9667f03
commit fc73069ff5
2 changed files with 92 additions and 86 deletions

View file

@ -9,23 +9,22 @@
# Initialize internal values
#
battery = nil;
alternator = nil;
var battery = nil;
var alternator = nil;
last_time = 0.0;
var last_time = 0.0;
vbus_volts = 0.0;
ebus1_volts = 0.0;
ebus2_volts = 0.0;
var vbus_volts = 0.0;
var ebus1_volts = 0.0;
var ebus2_volts = 0.0;
ammeter_ave = 0.0;
var ammeter_ave = 0.0;
##
# Initialize the electrical system
#
init_electrical = func {
print("Initializing Nasal Electrical System");
battery = BatteryClass.new();
alternator = AlternatorClass.new();
@ -35,8 +34,9 @@ init_electrical = func {
setprop("/controls/switches/master-avionics", 1);
setprop("/systems/electrical/outputs/autopilot",0.0);
# Request that the update fuction be called next frame
# Request that the update function be called next frame
settimer(update_electrical, 0);
print("Electrical system initialized");
}
@ -47,12 +47,12 @@ init_electrical = func {
BatteryClass = {};
BatteryClass.new = func {
obj = { parents : [BatteryClass],
ideal_volts : 24.0,
ideal_amps : 30.0,
amp_hours : 12.75,
charge_percent : 1.0,
charge_amps : 7.0 };
var obj = { parents : [BatteryClass],
ideal_volts : 24.0,
ideal_amps : 30.0,
amp_hours : 12.75,
charge_percent : 1.0,
charge_amps : 7.0 };
return obj;
}
@ -62,8 +62,8 @@ BatteryClass.new = func {
#
BatteryClass.apply_load = func( amps, dt ) {
amphrs_used = amps * dt / 3600.0;
percent_used = amphrs_used / me.amp_hours;
var amphrs_used = amps * dt / 3600.0;
var percent_used = amphrs_used / me.amp_hours;
me.charge_percent -= percent_used;
if ( me.charge_percent < 0.0 ) {
me.charge_percent = 0.0;
@ -76,13 +76,13 @@ BatteryClass.apply_load = func( amps, dt ) {
##
# Return output volts based on percent charged. Currently based on a simple
# polynomal percent charge vs. volts function.
# polynomial percent charge vs. volts function.
#
BatteryClass.get_output_volts = func {
x = 1.0 - me.charge_percent;
tmp = -(3.0 * x - 1.0);
factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
var x = 1.0 - me.charge_percent;
var tmp = -(3.0 * x - 1.0);
var factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
return me.ideal_volts * factor;
}
@ -95,9 +95,9 @@ BatteryClass.get_output_volts = func {
#
BatteryClass.get_output_amps = func {
x = 1.0 - me.charge_percent;
tmp = -(3.0 * x - 1.0);
factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
var x = 1.0 - me.charge_percent;
var tmp = -(3.0 * x - 1.0);
var factor = (tmp*tmp*tmp*tmp*tmp + 32) / 32;
return me.ideal_amps * factor;
}
@ -109,11 +109,11 @@ BatteryClass.get_output_amps = func {
AlternatorClass = {};
AlternatorClass.new = func {
obj = { parents : [AlternatorClass],
rpm_source : "/engines/engine[0]/rpm",
rpm_threshold : 800.0,
ideal_volts : 28.0,
ideal_amps : 60.0 };
var obj = { parents : [AlternatorClass],
rpm_source : "/engines/engine[0]/rpm",
rpm_threshold : 800.0,
ideal_volts : 28.0,
ideal_amps : 60.0 };
setprop( obj.rpm_source, 0.0 );
return obj;
}
@ -126,13 +126,13 @@ AlternatorClass.apply_load = func( amps, dt ) {
# Scale alternator output for rpms < 800. For rpms >= 800
# give full output. This is just a WAG, and probably not how
# it really works but I'm keeping things "simple" to start.
rpm = getprop( me.rpm_source );
factor = rpm / me.rpm_threshold;
var rpm = getprop( me.rpm_source );
var factor = rpm / me.rpm_threshold;
if ( factor > 1.0 ) {
factor = 1.0;
}
# print( "alternator amps = ", me.ideal_amps * factor );
available_amps = me.ideal_amps * factor;
var available_amps = me.ideal_amps * factor;
return available_amps - amps;
}
@ -144,8 +144,8 @@ AlternatorClass.get_output_volts = func {
# scale alternator output for rpms < 800. For rpms >= 800
# give full output. This is just a WAG, and probably not how
# it really works but I'm keeping things "simple" to start.
rpm = getprop( me.rpm_source );
factor = rpm / me.rpm_threshold;
var rpm = getprop( me.rpm_source );
var factor = rpm / me.rpm_threshold;
if ( factor > 1.0 ) {
factor = 1.0;
}
@ -162,8 +162,8 @@ AlternatorClass.get_output_amps = func {
# scale alternator output for rpms < 800. For rpms >= 800
# give full output. This is just a WAG, and probably not how
# it really works but I'm keeping things "simple" to start.
rpm = getprop( me.rpm_source );
factor = rpm / me.rpm_threshold;
var rpm = getprop( me.rpm_source );
var factor = rpm / me.rpm_threshold;
if ( factor > 1.0 ) {
factor = 1.0;
}
@ -177,13 +177,13 @@ AlternatorClass.get_output_amps = func {
#
update_electrical = func {
time = getprop("/sim/time/elapsed-sec");
dt = time - last_time;
var time = getprop("/sim/time/elapsed-sec");
var dt = time - last_time;
last_time = time;
update_virtual_bus( dt );
# Request that the update fuction be called again next frame
# Request that the update function be called again next frame
settimer(update_electrical, 0);
}
@ -194,24 +194,23 @@ update_electrical = func {
#
update_virtual_bus = func( dt ) {
serviceable = getprop("/systems/electrical/serviceable");
var serviceable = getprop("/systems/electrical/serviceable");
var external_volts = 0.0;
var load = 0.0;
var battery_volts = 0.0;
var alternator_volts = 0.0;
if ( serviceable ) {
battery_volts = battery.get_output_volts();
alternator_volts = alternator.get_output_volts();
} else {
battery_volts = 0.0;
alternator_volts = 0.0;
battery_volts = battery.get_output_volts();
alternator_volts = alternator.get_output_volts();
}
external_volts = 0.0;
load = 0.0;
# switch state
master_bat = getprop("/controls/engines/engine[0]/master-bat");
master_alt = getprop("/controls/engines/engine[0]/master-alt");
var master_bat = getprop("/controls/engines/engine[0]/master-bat");
var master_alt = getprop("/controls/engines/engine[0]/master-alt");
# determine power source
bus_volts = 0.0;
power_source = nil;
var bus_volts = 0.0;
var power_source = nil;
if ( master_bat ) {
bus_volts = battery_volts;
power_source = "battery";
@ -235,10 +234,10 @@ update_virtual_bus = func( dt ) {
}
setprop("systems/electrical/outputs/starter[0]", starter_volts);
if (starter_volts > 1) {
setprop("controls/engines/engine[0]/starter",1);
setprop("controls/engines/engine[0]/magnetos",3);
setprop("controls/engines/engine[0]/starter",1);
setprop("controls/engines/engine[0]/magnetos",3);
} else {
setprop("controls/engines/engine[0]/starter",0);
setprop("controls/engines/engine[0]/starter",0);
}
# bus network (1. these must be called in the right order, 2. the
@ -250,7 +249,7 @@ update_virtual_bus = func( dt ) {
load += avionics_bus_2();
# system loads and ammeter gauge
ammeter = 0.0;
var ammeter = 0.0;
if ( bus_volts > 1.0 ) {
# normal load
load += 15.0;
@ -272,7 +271,7 @@ update_virtual_bus = func( dt ) {
}
# filter ammeter needle pos
ammeter_ave = 0.8 * ammeter_ave + 0.2 * ammeter;
var ammeter_ave = 0.8 * ammeter_ave + 0.2 * ammeter;
# outputs
setprop("/systems/electrical/amps", ammeter_ave);
@ -285,9 +284,9 @@ update_virtual_bus = func( dt ) {
electrical_bus_1 = func() {
# we are fed from the "virtual" bus
bus_volts = vbus_volts;
load = 0.0;
var bus_volts = vbus_volts;
var load = 0.0;
# Cabin Lights Power
if ( getprop("/controls/circuit-breakers/cabin-lights-pwr") ) {
setprop("/systems/electrical/outputs/cabin-lights", bus_volts);
@ -333,8 +332,8 @@ electrical_bus_1 = func() {
electrical_bus_2 = func() {
# we are fed from the "virtual" bus
bus_volts = vbus_volts;
load = 0.0;
var bus_volts = vbus_volts;
var load = 0.0;
# Turn Coordinator Power
setprop("/systems/electrical/outputs/turn-coordinator", bus_volts);
@ -381,13 +380,12 @@ electrical_bus_2 = func() {
cross_feed_bus = func() {
# we are fed from either of the electrical bus 1 or 2
var bus_volts = ebus2_volts;
if ( ebus1_volts > ebus2_volts ) {
bus_volts = ebus1_volts;
} else {
bus_volts = ebus2_volts;
}
load = 0.0;
var load = 0.0;
setprop("/systems/electrical/outputs/annunciators", bus_volts);
@ -397,20 +395,18 @@ cross_feed_bus = func() {
avionics_bus_1 = func() {
master_av = getprop("/controls/switches/master-avionics");
var bus_volts = 0.0;
var load = 0.0;
# we are fed from the electrical bus 1
var master_av = getprop("/controls/switches/master-avionics");
if ( master_av ) {
bus_volts = ebus1_volts;
} else {
bus_volts = 0.0;
}
load = 0.0;
# Avionics Fan Power
setprop("/systems/electrical/outputs/avionics-fan", bus_volts);
# GPS Power
setprop("/systems/electrical/outputs/gps", bus_volts);
@ -435,15 +431,13 @@ avionics_bus_1 = func() {
avionics_bus_2 = func() {
master_av = getprop("/controls/switches/master-avionics");
var master_av = getprop("/controls/switches/master-avionics");
# we are fed from the electrical bus 2
var bus_volts = 0.0;
if ( master_av ) {
bus_volts = ebus2_volts;
} else {
bus_volts = 0.0;
}
load = 0.0;
var load = 0.0;
# NavCom 2 Power
setprop("/systems/electrical/outputs/nav[1]", bus_volts);

View file

@ -25,7 +25,7 @@ Started October 23 2001 by John Check, fgpanels@rockfish.net
<flight-model archive="y">jsb</flight-model>
<aero archive="y">c172p</aero>
<allow-toggle-cockpit>true</allow-toggle-cockpit>
<allow-toggle-cockpit type="bool">true</allow-toggle-cockpit>
<model>
<path archive="y">Aircraft/c172p/Models/c172p.xml</path>
@ -51,7 +51,7 @@ Started October 23 2001 by John Check, fgpanels@rockfish.net
<fairing2 type="bool">false</fairing2>
<fairing3 type="bool">false</fairing3>
</c172p>
<hide-yoke type="bool">false</hide-yoke>
</model>
<startup>
@ -69,10 +69,10 @@ Started October 23 2001 by John Check, fgpanels@rockfish.net
<view>
<internal type="bool" archive="y">true</internal>
<config>
<x-offset-m archive="y">-0.21</x-offset-m>
<y-offset-m archive="y">0.235</y-offset-m>
<z-offset-m archive="y">0.36</z-offset-m>
<pitch-offset-deg>-12</pitch-offset-deg>
<x-offset-m archive="y" type="double">-0.21</x-offset-m>
<y-offset-m archive="y" type="double">0.235</y-offset-m>
<z-offset-m archive="y" type="double">0.36</z-offset-m>
<pitch-offset-deg type="double">-12</pitch-offset-deg>
</config>
</view>
@ -135,12 +135,14 @@ Started October 23 2001 by John Check, fgpanels@rockfish.net
<controls>
<flight>
<aileron-trim>0.027</aileron-trim>
<rudder-trim>0.0</rudder-trim>
<aileron-trim type="double">0.027</aileron-trim>
<rudder-trim type="double">0.0</rudder-trim>
</flight>
<engines>
<engine n="0">
<magnetos>3</magnetos>
<magnetos type="int">3</magnetos>
<master-bat type="bool">true</master-bat>
<master-alt type="bool">true</master-alt>
</engine>
</engines>
<lighting>
@ -150,6 +152,16 @@ Started October 23 2001 by John Check, fgpanels@rockfish.net
<beacon type="bool">false</beacon>
<nav-lights type="bool">false</nav-lights>
</lighting>
<switches>
<master-avionics type="bool">true</master-avionics>
<starter type="bool">false</starter>
</switches>
<engines>
<engine>
<master-bat type="bool">true</master-bat>
<master-alt type="bool">true</master-alt>
</engine>
</engines>
</controls>
<autopilot>
@ -225,8 +237,8 @@ Started October 23 2001 by John Check, fgpanels@rockfish.net
</script>
</kap140>
<kr87>
<file>Aircraft/c172p/Nasal/kr87.nas</file>
</kr87>
<file>Aircraft/c172p/Nasal/kr87.nas</file>
</kr87>
</nasal>
<payload>
<weight>