source: src/mlx/flight.py@ 156:fc2987b14e61

Last change on this file since 156:fc2987b14e61 was 134:9ce031d5d4a9, checked in by István Váradi <ivaradi@…>, 12 years ago

Most of the remaining messages are implemented

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