7 from builtins
import range
8 from builtins
import object
12 '''fgFDM error class''' 14 Exception.__init__(self, msg)
18 '''represent a single fgFDM variable''' 25 '''represent a list of fgFDM variable''' 30 def add(self, varname, arraylength=1, units=None):
35 '''a flightgear native FDM parser/generator''' 37 '''init a fgFDM object''' 39 self.
pack_string =
'>I 4x 3d 6f 11f 3f 2f I 4I 4f 4f 4f 4f 4f 4f 4f 4f 4f I 4f I 3I 3f 3f 3f I i f 10f' 48 (
'radians',
'degrees') : math.degrees(1),
49 (
'rps',
'dps') : math.degrees(1),
50 (
'feet',
'meters') : 0.3048,
51 (
'fps',
'mps') : 0.3048,
52 (
'knots',
'mps') : 0.514444444,
53 (
'knots',
'fps') : 0.514444444/0.3048,
54 (
'fpss',
'mpss') : 0.3048,
55 (
'seconds',
'minutes') : 60,
56 (
'seconds',
'hours') : 3600,
63 self.mapping.add(
'version')
66 self.mapping.add(
'longitude', units=
'radians')
67 self.mapping.add(
'latitude', units=
'radians')
68 self.mapping.add(
'altitude', units=
'meters')
69 self.mapping.add(
'agl', units=
'meters')
72 self.mapping.add(
'phi', units=
'radians')
73 self.mapping.add(
'theta', units=
'radians')
74 self.mapping.add(
'psi', units=
'radians')
75 self.mapping.add(
'alpha', units=
'radians')
76 self.mapping.add(
'beta', units=
'radians')
79 self.mapping.add(
'phidot', units=
'rps')
80 self.mapping.add(
'thetadot', units=
'rps')
81 self.mapping.add(
'psidot', units=
'rps')
82 self.mapping.add(
'vcas', units=
'fps')
83 self.mapping.add(
'climb_rate', units=
'fps')
84 self.mapping.add(
'v_north', units=
'fps')
85 self.mapping.add(
'v_east', units=
'fps')
86 self.mapping.add(
'v_down', units=
'fps')
87 self.mapping.add(
'v_wind_body_north', units=
'fps')
88 self.mapping.add(
'v_wind_body_east', units=
'fps')
89 self.mapping.add(
'v_wind_body_down', units=
'fps')
92 self.mapping.add(
'A_X_pilot', units=
'fpss')
93 self.mapping.add(
'A_Y_pilot', units=
'fpss')
94 self.mapping.add(
'A_Z_pilot', units=
'fpss')
97 self.mapping.add(
'stall_warning')
98 self.mapping.add(
'slip_deg', units=
'degrees')
101 self.mapping.add(
'num_engines')
114 self.mapping.add(
'num_tanks')
118 self.mapping.add(
'num_wheels')
125 self.mapping.add(
'cur_time', units=
'seconds')
126 self.mapping.add(
'warp', units=
'seconds')
127 self.mapping.add(
'visibility', units=
'meters')
130 self.mapping.add(
'elevator')
131 self.mapping.add(
'elevator_trim_tab')
132 self.mapping.add(
'left_flap')
133 self.mapping.add(
'right_flap')
134 self.mapping.add(
'left_aileron')
135 self.mapping.add(
'right_aileron')
136 self.mapping.add(
'rudder')
137 self.mapping.add(
'nose_wheel')
138 self.mapping.add(
'speedbrake')
139 self.mapping.add(
'spoilers')
145 if len(self.
values) != self.mapping._nextidx:
146 raise fgFDMError(
'Invalid variable list in initialisation')
149 '''return expected size of FG FDM packets''' 153 '''convert a value from one set of units to another''' 154 if fromunits == tounits:
156 if (fromunits,tounits)
in self.
unitmap:
157 return value * self.
unitmap[(fromunits,tounits)]
158 if (tounits,fromunits)
in self.
unitmap:
159 return value / self.
unitmap[(tounits,fromunits)]
160 raise fgFDMError(
"unknown unit mapping (%s,%s)" % (fromunits, tounits))
164 '''return the default units of a variable''' 165 if not varname
in self.mapping.vars:
166 raise fgFDMError(
'Unknown variable %s' % varname)
167 return self.mapping.vars[varname].units
171 '''return a list of available variables''' 172 return sorted(list(self.mapping.vars.keys()),
173 key =
lambda v : self.mapping.vars[v].index)
176 def get(self, varname, idx=0, units=None):
177 '''get a variable value''' 178 if not varname
in self.mapping.vars:
179 raise fgFDMError(
'Unknown variable %s' % varname)
180 if idx >= self.mapping.vars[varname].arraylength:
181 raise fgFDMError(
'index of %s beyond end of array idx=%u arraylength=%u' % (
182 varname, idx, self.mapping.vars[varname].arraylength))
183 value = self.
values[self.mapping.vars[varname].index + idx]
185 value = self.
convert(value, self.mapping.vars[varname].units, units)
188 def set(self, varname, value, idx=0, units=None):
189 '''set a variable value''' 190 if not varname
in self.mapping.vars:
191 raise fgFDMError(
'Unknown variable %s' % varname)
192 if idx >= self.mapping.vars[varname].arraylength:
193 raise fgFDMError(
'index of %s beyond end of array idx=%u arraylength=%u' % (
194 varname, idx, self.mapping.vars[varname].arraylength))
196 value = self.
convert(value, units, self.mapping.vars[varname].units)
198 if math.isinf(value)
or math.isnan(value)
or math.fabs(value) > 3.4e38:
200 self.
values[self.mapping.vars[varname].index + idx] = value
203 '''parse a FD FDM buffer''' 206 except struct.error
as msg:
207 raise fgFDMError(
'unable to parse - %s' % msg)
211 '''pack a FD FDM buffer from current values''' 212 for i
in range(len(self.
values)):
213 if math.isnan(self.
values[i]):
def get(self, varname, idx=0, units=None)
def convert(self, value, fromunits, tounits)
def set(self, varname, value, idx=0, units=None)
def add(self, varname, arraylength=1, units=None)
def __init__(self, index, arraylength, units)