source: src/mlx/flight.py@ 183:97e7e9479e0a

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

Added the Using FS2Crew option

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