source: src/checks.py@ 8:85698811c70e

Last change on this file since 8:85698811c70e was 8:85698811c70e, checked in by István Váradi <ivaradi@…>, 12 years ago

The tracking of flight stages and some basic logging functionality works

File size: 12.7 KB
Line 
1# The various checks that may be performed during flight
2
3#---------------------------------------------------------------------------------------
4
5import const
6
7#---------------------------------------------------------------------------------------
8
9class StateChecker(object):
10 """Base class for classes the instances of which check the aircraft's state
11 from some aspect.
12
13 As a result of the check they may log something, or notify of some fault, etc."""
14 def check(self, flight, aircraft, logger, oldState, state):
15 """Perform the check and do whatever is needed.
16
17 This default implementation raises a NotImplementedError."""
18 raise NotImplementedError()
19
20#---------------------------------------------------------------------------------------
21
22class StageChecker(StateChecker):
23 """Check the flight stage transitions."""
24 def check(self, flight, aircraft, logger, oldState, state):
25 """Check the stage of the aircraft."""
26 stage = flight.stage
27 if stage==None:
28 aircraft.setStage(state, const.STAGE_BOARDING)
29 elif stage==const.STAGE_BOARDING:
30 if not state.parking or \
31 (not state.trickMode and state.groundSpeed>5.0):
32 aircraft.setStage(state, const.STAGE_PUSHANDTAXI)
33 elif stage==const.STAGE_PUSHANDTAXI or stage==const.STAGE_RTO:
34 if state.landingLightsOn or state.strobeLightsOn or \
35 state.groundSpeed>80.0:
36 aircraft.setStage(state, const.STAGE_TAKEOFF)
37 elif stage==const.STAGE_TAKEOFF:
38 if not state.gearsDown or \
39 (state.radioAltitude>3000.0 and state.vs>0):
40 aircraft.setStage(state, const.STAGE_CLIMB)
41 elif not state.landingLightsOn and \
42 not state.strobeLightsOn and \
43 state.onTheGround and \
44 state.groundSpeed<50.0:
45 aircraft.setStage(state, const.STAGE_RTO)
46 elif stage==const.STAGE_CLIMB:
47 if (state.altitude+2000) > flight.cruiseAltitude:
48 aircraft.setStage(state, const.STAGE_CRUISE)
49 elif state.radioAltitude<2000.0 and \
50 state.vs < 0.0 and state.gearsDown:
51 aircraft.setStage(state, const.STAGE_LANDING)
52 elif stage==const.STAGE_CRUISE:
53 if (state.altitude+2000) < flight.cruiseAltitude:
54 aircraft.setStage(state, const.STAGE_DESCENT)
55 elif stage==const.STAGE_DESCENT or stage==const.STAGE_GOAROUND:
56 if state.gearsDown and state.radioAltitude<2000.0:
57 aircraft.setStage(state, const.STAGE_LANDING)
58 elif (state.altitude+2000) > flight.cruiseAltitude:
59 aircraft.setStage(state, const.STAGE_CRUISE)
60 elif stage==const.STAGE_LANDING:
61 if state.onTheGround and state.groundSpeed<50.0:
62 aircraft.setStage(state, const.STAGE_TAXIAFTERLAND)
63 elif not state.gearsDown:
64 aircraft.setStage(state, const.STAGE_GOAROUND)
65 elif state.radioAltitude>200:
66 aircraft.cancelFlare()
67 elif state.radioAltitude<150 and not state.onTheGround:
68 aircraft.flare()
69 elif stage==const.STAGE_TAXIAFTERLAND:
70 if state.parking:
71 aircraft.setStage(state, const.STAGE_PARKING)
72 elif stage==const.STAGE_PARKING:
73 if aircraft.checkFlightEnd(state):
74 aircraft.setStage(state, const.STAGE_END)
75
76#---------------------------------------------------------------------------------------
77
78class StateChangeLogger(StateChecker):
79 """Base class for classes the instances of which check if a specific change has
80 occured in the aircraft's state, and log such change."""
81 def __init__(self, logInitial = True):
82 """Construct the logger.
83
84 If logInitial is True, the initial value will be logged, not just the
85 changes later.
86
87 Child classes should define the following functions:
88 - _changed(self, oldState, state): returns a boolean indicating if the
89 value has changed or not
90 - _getMessage(self, state): return a strings containing the message to log
91 with the new value
92 """
93 self._logInitial = logInitial
94
95 def check(self, flight, aircraft, logger, oldState, state):
96 """Check if the state has changed, and if so, log the new state."""
97 shouldLog = False
98 if oldState is None:
99 shouldLog = self._logInitial
100 else:
101 shouldLog = self._changed(oldState, state)
102
103 if shouldLog:
104 logger.message(state.timestamp, self._getMessage(state))
105
106#---------------------------------------------------------------------------------------
107
108class SimpleChangeMixin(object):
109 """A mixin that defines a _changed() function which simply calls a function
110 to retrieve the value two monitor for both states and compares them.
111
112 Child classes should define the following function:
113 - _getValue(state): get the value we are interested in."""
114 def _changed(self, oldState, state):
115 """Determine if the value has changed."""
116 return self._getValue(oldState)!=self._getValue(state)
117
118#---------------------------------------------------------------------------------------
119
120class SingleValueMixin(object):
121 """A mixin that provides a _getValue() function to query a value from the
122 state using the name of the attribute."""
123 def __init__(self, attrName):
124 """Construct the mixin with the given attribute name."""
125 self._attrName = attrName
126
127 def _getValue(self, state):
128 """Get the value of the attribute from the state."""
129 return getattr(state, self._attrName)
130
131#---------------------------------------------------------------------------------------
132
133class DelayedChangeMixin(object):
134 """A mixin to a StateChangeLogger that stores the old value and reports a
135 change only if the change has been there for a certain amount of time.
136
137 Child classes should define the following function:
138 - _getValue(state): get the value we are interested in."""
139 def __init__(self, delay = 10.0):
140 """Construct the mixin with the given delay in seconds."""
141 self._delay = delay
142 self._oldValue = None
143 self._firstChange = None
144
145 def _changed(self, oldState, state):
146 """Determine if the value has changed."""
147 if self._oldValue is None:
148 self._oldValue = self._getValue(oldState)
149
150 newValue = self._getValue(state)
151 if newValue!=self._oldValue:
152 if self._firstChange is None:
153 self._firstChange = state.timestamp
154 if state.timestamp >= (self._firstChange + self._delay):
155 self._oldValue = newValue
156 self._firstChange = None
157 return True
158 else:
159 self._firstChange = None
160
161 return False
162
163#---------------------------------------------------------------------------------------
164
165class TemplateMessageMixin(object):
166 """Mixin to generate a message based on a template.
167
168 Child classes should define the following function:
169 - _getValue(state): get the value we are interested in."""
170 def __init__(self, template):
171 """Construct the mixin."""
172 self._template = template
173
174 def _getMessage(self, state):
175 """Get the message."""
176 return self._template % (self._getValue(state),)
177
178#---------------------------------------------------------------------------------------
179
180class GenericStateChangeLogger(StateChangeLogger, SingleValueMixin,
181 DelayedChangeMixin, TemplateMessageMixin):
182 """Base for generic state change loggers that monitor a single value in the
183 state possibly with a delay and the logged message comes from a template"""
184 def __init__(self, attrName, template, logInitial = True, delay = 0.0):
185 """Construct the object."""
186 StateChangeLogger.__init__(self, logInitial = logInitial)
187 SingleValueMixin.__init__(self, attrName)
188 DelayedChangeMixin.__init__(self, delay = delay)
189 TemplateMessageMixin.__init__(self, template)
190
191#---------------------------------------------------------------------------------------
192
193class AltimeterLogger(StateChangeLogger, SingleValueMixin,
194 DelayedChangeMixin):
195 """Logger for the altimeter setting."""
196 def __init__(self):
197 """Construct the logger."""
198 StateChangeLogger.__init__(self, logInitial = True)
199 SingleValueMixin.__init__(self, "altimeter")
200 DelayedChangeMixin.__init__(self)
201
202 def _getMessage(self, state):
203 """Get the message to log on a change."""
204 return "Altimeter: %.0f hPa at %.0f feet" % (state.altimeter, state.altitude)
205
206#---------------------------------------------------------------------------------------
207
208class NAV1Logger(GenericStateChangeLogger):
209 """Logger for the NAV1 radio setting."""
210 def __init__(self):
211 """Construct the logger."""
212 super(NAV1Logger, self).__init__("nav1", "NAV1 frequency: %s MHz")
213
214#---------------------------------------------------------------------------------------
215
216class NAV2Logger(GenericStateChangeLogger):
217 """Logger for the NAV2 radio setting."""
218 def __init__(self):
219 """Construct the logger."""
220 super(NAV2Logger, self).__init__("nav2", "NAV2 frequency: %s MHz")
221
222#---------------------------------------------------------------------------------------
223
224class SquawkLogger(GenericStateChangeLogger):
225 """Logger for the squawk setting."""
226 def __init__(self):
227 """Construct the logger."""
228 super(SquawkLogger, self).__init__("squawk", "Squawk code: %s",
229 delay = 10.0)
230
231#---------------------------------------------------------------------------------------
232
233class LightsLogger(StateChangeLogger, SingleValueMixin, SimpleChangeMixin):
234 """Base class for the loggers of the various lights."""
235 def __init__(self, attrName, template):
236 """Construct the logger."""
237 StateChangeLogger.__init__(self)
238 SingleValueMixin.__init__(self, attrName)
239
240 self._template = template
241
242 def _getMessage(self, state):
243 """Get the message from the given state."""
244 return self._template % ("ON" if self._getValue(state) else "OFF")
245
246#---------------------------------------------------------------------------------------
247
248class AnticollisionLightsLogger(LightsLogger):
249 """Logger for the anti-collision lights."""
250 def __init__(self):
251 LightsLogger.__init__(self, "antiCollisionLightsOn",
252 "Anti-collision lights: %s")
253
254#---------------------------------------------------------------------------------------
255
256class LandingLightsLogger(LightsLogger):
257 """Logger for the landing lights."""
258 def __init__(self):
259 LightsLogger.__init__(self, "landingLightsOn",
260 "Landing lights: %s")
261
262#---------------------------------------------------------------------------------------
263
264class StrobeLightsLogger(LightsLogger):
265 """Logger for the strobe lights."""
266 def __init__(self):
267 LightsLogger.__init__(self, "strobeLightsOn",
268 "Strobe lights: %s")
269
270#---------------------------------------------------------------------------------------
271
272class NavLightsLogger(LightsLogger):
273 """Logger for the navigational lights."""
274 def __init__(self):
275 LightsLogger.__init__(self, "navLightsOn",
276 "Navigational lights: %s")
277
278#---------------------------------------------------------------------------------------
279
280class FlapsLogger(StateChangeLogger, SingleValueMixin, SimpleChangeMixin):
281 """Logger for the flaps setting."""
282 def __init__(self):
283 """Construct the logger."""
284 StateChangeLogger.__init__(self, logInitial = True)
285 SingleValueMixin.__init__(self, "flapsSet")
286
287 def _getMessage(self, state):
288 """Get the message to log on a change."""
289 speed = state.groundSpeed if state.groundSpeed<80.0 else state.ias
290 return "Flaps set to %.0f at %.0f knots" % (state.flapsSet, speed)
291
292#---------------------------------------------------------------------------------------
293
294class GearsLogger(StateChangeLogger, SingleValueMixin, SimpleChangeMixin):
295 """Logger for the gears state."""
296 def __init__(self):
297 """Construct the logger."""
298 StateChangeLogger.__init__(self, logInitial = True)
299 SingleValueMixin.__init__(self, "gearsDown")
300
301 def _getMessage(self, state):
302 """Get the message to log on a change."""
303 return "Gears %s at %.0f knots, %f feet" % \
304 ("DOWN" if state.gearsDown else "UP", state.ias, state.altitude)
305
306#---------------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.