source: src/mlx/flight.py@ 216:93182ba275cd

Last change on this file since 216:93182ba275cd was 215:bff7327b2da0, checked in by István Váradi <ivaradi@…>, 12 years ago

Added support for flying without logging in

File size: 7.6 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 loggedIn(self):
71 """Indicate if the user has logged in properly."""
72 return self._gui.loggedIn
73
74 @property
75 def entranceExam(self):
76 """Get whether an entrance exam is being performed."""
77 return self._gui.entranceExam
78
79 @property
80 def bookedFlight(self):
81 """Get the booked flight."""
82 return self._gui.bookedFlight
83
84 @property
85 def zfw(self):
86 """Get the Zero-Fuel Weight of the flight."""
87 return self._gui.zfw
88
89 @property
90 def cruiseAltitude(self):
91 """Get the cruise altitude of the flight."""
92 return self._gui.cruiseAltitude
93
94 @property
95 def v1(self):
96 """Get the V1 speed of the flight."""
97 return self._gui.v1
98
99 @property
100 def vr(self):
101 """Get the Vr speed of the flight."""
102 return self._gui.vr
103
104 @property
105 def v2(self):
106 """Get the V2 speed of the flight."""
107 return self._gui.v2
108
109 @property
110 def vref(self):
111 """Get the VRef speed of the flight."""
112 return self._gui.vref
113
114 @property
115 def tdRate(self):
116 """Get the touchdown rate if known, None otherwise."""
117 return self._tdRate
118
119 def handleState(self, oldState, currentState):
120 """Handle a new state information."""
121 self._updateFlownDistance(currentState)
122
123 self.endFuel = sum(currentState.fuel)
124 if self.startFuel is None:
125 self.startFuel = self.endFuel
126
127 self._soundScheduler.schedule(currentState,
128 self._pilotHotkeyPressed)
129 self._pilotHotkeyPressed = False
130
131 if self._checklistHotkeyPressed:
132 self._checklistScheduler.hotkeyPressed()
133 self._checklistHotkeyPressed = False
134
135 def setStage(self, timestamp, stage):
136 """Set the flight stage.
137
138 Returns if the stage has really changed."""
139 if stage!=self._stage:
140 self._stage = stage
141 self._gui.setStage(stage)
142 self.logger.stage(timestamp, stage)
143 if stage==const.STAGE_PUSHANDTAXI:
144 self.blockTimeStart = timestamp
145 elif stage==const.STAGE_TAKEOFF:
146 self.flightTimeStart = timestamp
147 elif stage==const.STAGE_TAXIAFTERLAND:
148 self.flightTimeEnd = timestamp
149 elif stage==const.STAGE_PARKING:
150 self.blockTimeEnd = timestamp
151 elif stage==const.STAGE_END:
152 with self._endCondition:
153 self._endCondition.notify()
154 return True
155 else:
156 return False
157
158 def handleFault(self, faultID, timestamp, what, score):
159 """Handle the given fault.
160
161 faultID as a unique ID for the given kind of fault. If another fault of
162 this ID has been reported earlier, it will be reported again only if
163 the score is greater than last time. This ID can be, e.g. the checker
164 the report comes from."""
165 self.logger.fault(faultID, timestamp, what, score)
166 self._gui.setRating(self.logger.getRating())
167
168 def handleNoGo(self, faultID, timestamp, what, shortReason):
169 """Handle a No-Go fault."""
170 self.logger.noGo(faultID, timestamp, what)
171 self._gui.setNoGo(shortReason)
172
173 def flareStarted(self, flareStart, flareStartFS):
174 """Called when the flare time has started."""
175 self._flareStart = flareStart
176 self._flareStartFS = flareStartFS
177
178 def flareFinished(self, flareEnd, flareEndFS, tdRate):
179 """Called when the flare time has ended.
180
181 Return a tuple of the following items:
182 - a boolean indicating if FS time is used
183 - the flare time
184 """
185 self._tdRate = tdRate
186 if self.flareTimeFromFS:
187 return (True, flareEndFS - self._flareStartFS)
188 else:
189 return (False, flareEnd - self._flareStart)
190
191 def wait(self):
192 """Wait for the flight to end."""
193 with self._endCondition:
194 while self._stage!=const.STAGE_END:
195 self._endCondition.wait(1)
196
197 def getFleet(self, callback, force = False):
198 """Get the fleet and call the given callback."""
199 self._gui.getFleetAsync(callback = callback, force = force)
200
201 def pilotHotkeyPressed(self):
202 """Called when the pilot hotkey is pressed."""
203 self._pilotHotkeyPressed = True
204
205 def checklistHotkeyPressed(self):
206 """Called when the checklist hotkey is pressed."""
207 self._checklistHotkeyPressed = True
208
209 def _updateFlownDistance(self, currentState):
210 """Update the flown distance."""
211 if not currentState.onTheGround:
212 updateData = False
213 if self._lastDistanceTime is None or \
214 self._previousLatitude is None or \
215 self._previousLongitude is None:
216 updateData = True
217 elif currentState.timestamp >= (self._lastDistanceTime + 30.0):
218 updateData = True
219 self.flownDistance += self._getDistance(currentState)
220
221 if updateData:
222 self._previousLatitude = currentState.latitude
223 self._previousLongitude = currentState.longitude
224 self._lastDistanceTime = currentState.timestamp
225 else:
226 if self._lastDistanceTime is not None and \
227 self._previousLatitude is not None and \
228 self._previousLongitude is not None:
229 self.flownDistance += self._getDistance(currentState)
230
231 self._lastDistanceTime = None
232
233 def _getDistance(self, currentState):
234 """Get the distance between the previous and the current state."""
235 return util.getDistCourse(self._previousLatitude, self._previousLongitude,
236 currentState.latitude, currentState.longitude)[0]
237
238#---------------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.