source: src/mlx/flight.py@ 214:19fb20505b1a

Last change on this file since 214:19fb20505b1a was 184:0a000ef19c3a, checked in by István Váradi <ivaradi@…>, 12 years ago

Added support for the entrance exam

File size: 7.4 KB
Line 
1# Module related to the high-level tracking of the flight
2
3#---------------------------------------------------------------------------------------
4
5from soundsched import SoundScheduler, ChecklistScheduler
6
7import const
8import util
9
10import threading
11
12#---------------------------------------------------------------------------------------
13
14class Flight(object):
15 """The object with the global flight state.
16
17 It is also the hub for the other main objects participating in the handling of
18 the flight."""
19 def __init__(self, logger, gui):
20 """Construct the flight."""
21 self._stage = None
22 self.logger = logger
23 self._gui = gui
24
25 gui.resetFlightStatus()
26
27 self._pilotHotkeyPressed = False
28 self._checklistHotkeyPressed = False
29
30 self.flareTimeFromFS = False
31
32 self.aircraftType = None
33 self.aircraft = None
34 self.simulator = None
35
36 self.blockTimeStart = None
37 self.flightTimeStart = None
38 self.flightTimeEnd = None
39 self.blockTimeEnd = None
40
41 self._lastDistanceTime = None
42 self._previousLatitude = None
43 self._previousLongitude = None
44 self.flownDistance = 0.0
45
46 self.startFuel = None
47 self.endFuel = None
48
49 self._endCondition = threading.Condition()
50
51 self._flareStart = None
52 self._flareStartFS = None
53
54 self._tdRate = None
55
56 self._soundScheduler = SoundScheduler(self)
57 self._checklistScheduler = ChecklistScheduler(self)
58
59 @property
60 def config(self):
61 """Get the configuration."""
62 return self._gui.config
63
64 @property
65 def stage(self):
66 """Get the flight stage."""
67 return self._stage
68
69 @property
70 def entranceExam(self):
71 """Get whether an entrance exam is being performed."""
72 return self._gui.entranceExam
73
74 @property
75 def bookedFlight(self):
76 """Get the booked flight."""
77 return self._gui.bookedFlight
78
79 @property
80 def zfw(self):
81 """Get the Zero-Fuel Weight of the flight."""
82 return self._gui.zfw
83
84 @property
85 def cruiseAltitude(self):
86 """Get the cruise altitude of the flight."""
87 return self._gui.cruiseAltitude
88
89 @property
90 def v1(self):
91 """Get the V1 speed of the flight."""
92 return self._gui.v1
93
94 @property
95 def vr(self):
96 """Get the Vr speed of the flight."""
97 return self._gui.vr
98
99 @property
100 def v2(self):
101 """Get the V2 speed of the flight."""
102 return self._gui.v2
103
104 @property
105 def vref(self):
106 """Get the VRef speed of the flight."""
107 return self._gui.vref
108
109 @property
110 def tdRate(self):
111 """Get the touchdown rate if known, None otherwise."""
112 return self._tdRate
113
114 def handleState(self, oldState, currentState):
115 """Handle a new state information."""
116 self._updateFlownDistance(currentState)
117
118 self.endFuel = sum(currentState.fuel)
119 if self.startFuel is None:
120 self.startFuel = self.endFuel
121
122 self._soundScheduler.schedule(currentState,
123 self._pilotHotkeyPressed)
124 self._pilotHotkeyPressed = False
125
126 if self._checklistHotkeyPressed:
127 self._checklistScheduler.hotkeyPressed()
128 self._checklistHotkeyPressed = False
129
130 def setStage(self, timestamp, stage):
131 """Set the flight stage.
132
133 Returns if the stage has really changed."""
134 if stage!=self._stage:
135 self._stage = stage
136 self._gui.setStage(stage)
137 self.logger.stage(timestamp, stage)
138 if stage==const.STAGE_PUSHANDTAXI:
139 self.blockTimeStart = timestamp
140 elif stage==const.STAGE_TAKEOFF:
141 self.flightTimeStart = timestamp
142 elif stage==const.STAGE_TAXIAFTERLAND:
143 self.flightTimeEnd = timestamp
144 elif stage==const.STAGE_PARKING:
145 self.blockTimeEnd = timestamp
146 elif stage==const.STAGE_END:
147 with self._endCondition:
148 self._endCondition.notify()
149 return True
150 else:
151 return False
152
153 def handleFault(self, faultID, timestamp, what, score):
154 """Handle the given fault.
155
156 faultID as a unique ID for the given kind of fault. If another fault of
157 this ID has been reported earlier, it will be reported again only if
158 the score is greater than last time. This ID can be, e.g. the checker
159 the report comes from."""
160 self.logger.fault(faultID, timestamp, what, score)
161 self._gui.setRating(self.logger.getRating())
162
163 def handleNoGo(self, faultID, timestamp, what, shortReason):
164 """Handle a No-Go fault."""
165 self.logger.noGo(faultID, timestamp, what)
166 self._gui.setNoGo(shortReason)
167
168 def flareStarted(self, flareStart, flareStartFS):
169 """Called when the flare time has started."""
170 self._flareStart = flareStart
171 self._flareStartFS = flareStartFS
172
173 def flareFinished(self, flareEnd, flareEndFS, tdRate):
174 """Called when the flare time has ended.
175
176 Return a tuple of the following items:
177 - a boolean indicating if FS time is used
178 - the flare time
179 """
180 self._tdRate = tdRate
181 if self.flareTimeFromFS:
182 return (True, flareEndFS - self._flareStartFS)
183 else:
184 return (False, flareEnd - self._flareStart)
185
186 def wait(self):
187 """Wait for the flight to end."""
188 with self._endCondition:
189 while self._stage!=const.STAGE_END:
190 self._endCondition.wait(1)
191
192 def getFleet(self, callback, force = False):
193 """Get the fleet and call the given callback."""
194 self._gui.getFleetAsync(callback = callback, force = force)
195
196 def pilotHotkeyPressed(self):
197 """Called when the pilot hotkey is pressed."""
198 self._pilotHotkeyPressed = True
199
200 def checklistHotkeyPressed(self):
201 """Called when the checklist hotkey is pressed."""
202 self._checklistHotkeyPressed = True
203
204 def _updateFlownDistance(self, currentState):
205 """Update the flown distance."""
206 if not currentState.onTheGround:
207 updateData = False
208 if self._lastDistanceTime is None or \
209 self._previousLatitude is None or \
210 self._previousLongitude is None:
211 updateData = True
212 elif currentState.timestamp >= (self._lastDistanceTime + 30.0):
213 updateData = True
214 self.flownDistance += self._getDistance(currentState)
215
216 if updateData:
217 self._previousLatitude = currentState.latitude
218 self._previousLongitude = currentState.longitude
219 self._lastDistanceTime = currentState.timestamp
220 else:
221 if self._lastDistanceTime is not None and \
222 self._previousLatitude is not None and \
223 self._previousLongitude is not None:
224 self.flownDistance += self._getDistance(currentState)
225
226 self._lastDistanceTime = None
227
228 def _getDistance(self, currentState):
229 """Get the distance between the previous and the current state."""
230 return util.getDistCourse(self._previousLatitude, self._previousLongitude,
231 currentState.latitude, currentState.longitude)[0]
232
233#---------------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.