source: src/mlx/flight.py@ 170:7cda0cc74e19

Last change on this file since 170:7cda0cc74e19 was 170:7cda0cc74e19, checked in by István Váradi <ivaradi@…>, 11 years ago

The background sounds play back properly

File size: 7.4 KB
Line 
1# Module related to the high-level tracking of the flight
2
3#---------------------------------------------------------------------------------------
4
5from soundsched import SoundScheduler
6
7import const
8import util
9
10import threading
11
12#---------------------------------------------------------------------------------------
13
14class Options(object):
15 """Various configuration options."""
16 def __init__(self):
17 """Construct the object with default values."""
18 self.fs2Crew = False
19 self.compensation = None
20
21#---------------------------------------------------------------------------------------
22
23class Flight(object):
24 """The object with the global flight state.
25
26 It is also the hub for the other main objects participating in the handling of
27 the flight."""
28 def __init__(self, logger, gui):
29 """Construct the flight."""
30 self._stage = None
31 self.logger = logger
32 self._gui = gui
33
34 gui.resetFlightStatus()
35
36 self._soundScheduler = SoundScheduler(self)
37 self._pilotHotkeyPressed = False
38 self._checklistHotkeyPressed = False
39
40 self.flareTimeFromFS = False
41 self.entranceExam = False
42
43 self.options = Options()
44
45 self.aircraftType = None
46 self.aircraft = None
47 self.simulator = None
48
49 self.blockTimeStart = None
50 self.flightTimeStart = None
51 self.flightTimeEnd = None
52 self.blockTimeEnd = None
53
54 self._lastDistanceTime = None
55 self._previousLatitude = None
56 self._previousLongitude = None
57 self.flownDistance = 0.0
58
59 self.startFuel = None
60 self.endFuel = None
61
62 self._endCondition = threading.Condition()
63
64 self._flareStart = None
65 self._flareStartFS = None
66
67 self._tdRate = None
68
69 @property
70 def config(self):
71 """Get the configuration."""
72 return self._gui.config
73
74 @property
75 def stage(self):
76 """Get the flight stage."""
77 return self._stage
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 def setStage(self, timestamp, stage):
132 """Set the flight stage.
133
134 Returns if the stage has really changed."""
135 if stage!=self._stage:
136 self._stage = stage
137 self._gui.setStage(stage)
138 self.logger.stage(timestamp, stage)
139 if stage==const.STAGE_PUSHANDTAXI:
140 self.blockTimeStart = timestamp
141 elif stage==const.STAGE_TAKEOFF:
142 self.flightTimeStart = timestamp
143 elif stage==const.STAGE_TAXIAFTERLAND:
144 self.flightTimeEnd = timestamp
145 elif stage==const.STAGE_PARKING:
146 self.blockTimeEnd = timestamp
147 elif stage==const.STAGE_END:
148 with self._endCondition:
149 self._endCondition.notify()
150 return True
151 else:
152 return False
153
154 def handleFault(self, faultID, timestamp, what, score):
155 """Handle the given fault.
156
157 faultID as a unique ID for the given kind of fault. If another fault of
158 this ID has been reported earlier, it will be reported again only if
159 the score is greater than last time. This ID can be, e.g. the checker
160 the report comes from."""
161 self.logger.fault(faultID, timestamp, what, score)
162 self._gui.setRating(self.logger.getRating())
163
164 def handleNoGo(self, faultID, timestamp, what, shortReason):
165 """Handle a No-Go fault."""
166 self.logger.noGo(faultID, timestamp, what)
167 self._gui.setNoGo(shortReason)
168
169 def flareStarted(self, flareStart, flareStartFS):
170 """Called when the flare time has started."""
171 self._flareStart = flareStart
172 self._flareStartFS = flareStartFS
173
174 def flareFinished(self, flareEnd, flareEndFS, tdRate):
175 """Called when the flare time has ended.
176
177 Return a tuple of the following items:
178 - a boolean indicating if FS time is used
179 - the flare time
180 """
181 self._tdRate = tdRate
182 if self.flareTimeFromFS:
183 return (True, flareEndFS - self._flareStartFS)
184 else:
185 return (False, flareEnd - self._flareStart)
186
187 def wait(self):
188 """Wait for the flight to end."""
189 with self._endCondition:
190 while self._stage!=const.STAGE_END:
191 self._endCondition.wait(1)
192
193 def getFleet(self, callback, force = False):
194 """Get the fleet and call the given callback."""
195 self._gui.getFleetAsync(callback = callback, force = force)
196
197 def pilotHotkeyPressed(self):
198 """Called when the pilot hotkey is pressed."""
199 self._pilotHotkeyPressed = True
200
201 def checklistHotkeyPressed(self):
202 """Called when the checklist hotkey is pressed."""
203 self._checklistHotkeyPressed = True
204
205 def _updateFlownDistance(self, currentState):
206 """Update the flown distance."""
207 if not currentState.onTheGround:
208 updateData = False
209 if self._lastDistanceTime is None or \
210 self._previousLatitude is None or \
211 self._previousLongitude is None:
212 updateData = True
213 elif currentState.timestamp >= (self._lastDistanceTime + 30.0):
214 updateData = True
215 self.flownDistance += self._getDistance(currentState)
216
217 if updateData:
218 self._previousLatitude = currentState.latitude
219 self._previousLongitude = currentState.longitude
220 self._lastDistanceTime = currentState.timestamp
221 else:
222 if self._lastDistanceTime is not None and \
223 self._previousLatitude is not None and \
224 self._previousLongitude is not None:
225 self.flownDistance += self._getDistance(currentState)
226
227 self._lastDistanceTime = None
228
229 def _getDistance(self, currentState):
230 """Get the distance between the previous and the current state."""
231 return util.getDistCourse(self._previousLatitude, self._previousLongitude,
232 currentState.latitude, currentState.longitude)[0]
233
234#---------------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.