[4] | 1 |
|
---|
| 2 | import fs
|
---|
| 3 | import const
|
---|
| 4 | import util
|
---|
[140] | 5 | import acft
|
---|
[4] | 6 |
|
---|
[3] | 7 | import threading
|
---|
| 8 | import os
|
---|
| 9 | import time
|
---|
[4] | 10 | import calendar
|
---|
| 11 | import sys
|
---|
[26] | 12 | import codecs
|
---|
[3] | 13 |
|
---|
[162] | 14 | if os.name == "nt" and "FORCE_PYUIPC_SIM" not in os.environ:
|
---|
[3] | 15 | import pyuipc
|
---|
| 16 | else:
|
---|
[51] | 17 | import pyuipc_sim as pyuipc
|
---|
[3] | 18 |
|
---|
[4] | 19 | #------------------------------------------------------------------------------
|
---|
| 20 |
|
---|
[298] | 21 | ## @package mlx.fsuipc
|
---|
| 22 | #
|
---|
| 23 | # The module towards FSUIPC.
|
---|
| 24 | #
|
---|
| 25 | # This module implements the simulator interface to FSUIPC.
|
---|
| 26 | #
|
---|
| 27 | # The \ref Handler class is thread handling the FSUIPC requests. It can be
|
---|
| 28 | # given read, periodic read and write, requests, that are executed
|
---|
| 29 | # asynchronously, and a callback function is called with the result. This class
|
---|
| 30 | # is used internally within the module.
|
---|
| 31 | #
|
---|
| 32 | # The \ref Simulator class is the actual interface to the flight simulator, and
|
---|
| 33 | # an instance of it is returned by \ref mlx.fs.createSimulator. This object can
|
---|
| 34 | # be used to connect to the simulator and disconnect from it, to query various
|
---|
| 35 | # data and to start and stop the monitoring of the data.
|
---|
| 36 | #
|
---|
| 37 | # \ref AircraftModel is the base class of the aircraft models. A "model" is a
|
---|
| 38 | # particular implementation of an aircraft, such as the PMDG Boeing 737NG in
|
---|
| 39 | # Flight Simulator 2004. Since each type and each model has its peculiarities
|
---|
| 40 | # (e.g. the different engine and fuel tank configurations), each aircraft type
|
---|
| 41 | # has a generic model, that should work for most of the time. However, certain
|
---|
| 42 | # models may implement various controls, gauges, etc. differently, and such
|
---|
| 43 | # peculiarites can be handled in a specific subclass of \ref
|
---|
| 44 | # AircraftModel. These subclasses should be registered as special ones, and if
|
---|
| 45 | # the simulator detects that the aircraft's model became known or has changed,
|
---|
| 46 | # it will check these special models before resorting to the generic ones.
|
---|
| 47 | #
|
---|
| 48 | # The models are responsible also for querying certain parameters, such as the
|
---|
| 49 | # fuel tank configuration. While ideally it should be specific to a type only,
|
---|
| 50 | # it is possible that the model contains different tanks, in which case some
|
---|
| 51 | # tricks may be needed. See the \ref DC3Model "DC-3 (Li-2)" aircraft as an
|
---|
| 52 | # example.
|
---|
| 53 |
|
---|
| 54 | #------------------------------------------------------------------------------
|
---|
| 55 |
|
---|
| 56 | ## The mapping of tank types to FSUIPC offsets
|
---|
[140] | 57 | _tank2offset = { const.FUELTANK_CENTRE : 0x0b74,
|
---|
| 58 | const.FUELTANK_LEFT : 0x0b7c,
|
---|
| 59 | const.FUELTANK_RIGHT : 0x0b94,
|
---|
| 60 | const.FUELTANK_LEFT_AUX : 0x0b84,
|
---|
| 61 | const.FUELTANK_RIGHT_AUX : 0x0b9c,
|
---|
| 62 | const.FUELTANK_LEFT_TIP : 0x0b8c,
|
---|
| 63 | const.FUELTANK_RIGHT_TIP : 0x0ba4,
|
---|
| 64 | const.FUELTANK_EXTERNAL1 : 0x1254,
|
---|
| 65 | const.FUELTANK_EXTERNAL2 : 0x125c,
|
---|
| 66 | const.FUELTANK_CENTRE2 : 0x1244 }
|
---|
| 67 |
|
---|
| 68 | #------------------------------------------------------------------------------
|
---|
| 69 |
|
---|
[3] | 70 | class Handler(threading.Thread):
|
---|
| 71 | """The thread to handle the FSUIPC requests."""
|
---|
[4] | 72 | @staticmethod
|
---|
[9] | 73 | def fsuipc2VS(data):
|
---|
| 74 | """Convert the given vertical speed data read from FSUIPC into feet/min."""
|
---|
| 75 | return data*60.0/const.FEETTOMETRES/256.0
|
---|
| 76 |
|
---|
| 77 | @staticmethod
|
---|
| 78 | def fsuipc2radioAltitude(data):
|
---|
| 79 | """Convert the given radio altitude data read from FSUIPC into feet."""
|
---|
| 80 | return data/const.FEETTOMETRES/65536.0
|
---|
| 81 |
|
---|
| 82 | @staticmethod
|
---|
| 83 | def fsuipc2Degrees(data):
|
---|
| 84 | """Convert the given data into degrees."""
|
---|
| 85 | return data * 360.0 / 65536.0 / 65536.0
|
---|
| 86 |
|
---|
| 87 | @staticmethod
|
---|
| 88 | def fsuipc2PositiveDegrees(data):
|
---|
| 89 | """Convert the given data into positive degrees."""
|
---|
| 90 | degrees = Handler.fsuipc2Degrees(data)
|
---|
| 91 | if degrees<0.0: degrees += 360.0
|
---|
| 92 | return degrees
|
---|
| 93 |
|
---|
| 94 | @staticmethod
|
---|
| 95 | def fsuipc2IAS(data):
|
---|
| 96 | """Convert the given data into indicated airspeed."""
|
---|
| 97 | return data / 128.0
|
---|
| 98 |
|
---|
| 99 | @staticmethod
|
---|
[4] | 100 | def _callSafe(fun):
|
---|
| 101 | """Call the given function and swallow any exceptions."""
|
---|
| 102 | try:
|
---|
| 103 | return fun()
|
---|
| 104 | except Exception, e:
|
---|
| 105 | print >> sys.stderr, str(e)
|
---|
| 106 | return None
|
---|
| 107 |
|
---|
[59] | 108 | # The number of times a read is attempted
|
---|
[21] | 109 | NUM_READATTEMPTS = 3
|
---|
| 110 |
|
---|
[59] | 111 | # The number of connection attempts
|
---|
| 112 | NUM_CONNECTATTEMPTS = 3
|
---|
| 113 |
|
---|
| 114 | # The interval between successive connect attempts
|
---|
| 115 | CONNECT_INTERVAL = 0.25
|
---|
| 116 |
|
---|
[21] | 117 | @staticmethod
|
---|
| 118 | def _performRead(data, callback, extra, validator):
|
---|
| 119 | """Perform a read request.
|
---|
| 120 |
|
---|
| 121 | If there is a validator, that will be called with the return values,
|
---|
| 122 | and if the values are wrong, the request is retried at most a certain
|
---|
| 123 | number of times.
|
---|
| 124 |
|
---|
| 125 | Return True if the request has succeeded, False if validation has
|
---|
| 126 | failed during all attempts. An exception may also be thrown if there is
|
---|
| 127 | some lower-level communication problem."""
|
---|
| 128 | attemptsLeft = Handler.NUM_READATTEMPTS
|
---|
| 129 | while attemptsLeft>0:
|
---|
| 130 | values = pyuipc.read(data)
|
---|
| 131 | if validator is None or \
|
---|
| 132 | Handler._callSafe(lambda: validator(values, extra)):
|
---|
| 133 | Handler._callSafe(lambda: callback(values, extra))
|
---|
| 134 | return True
|
---|
| 135 | else:
|
---|
| 136 | attemptsLeft -= 1
|
---|
| 137 | return False
|
---|
| 138 |
|
---|
[3] | 139 | class Request(object):
|
---|
| 140 | """A simple, one-shot request."""
|
---|
[21] | 141 | def __init__(self, forWrite, data, callback, extra, validator = None):
|
---|
[3] | 142 | """Construct the request."""
|
---|
| 143 | self._forWrite = forWrite
|
---|
| 144 | self._data = data
|
---|
| 145 | self._callback = callback
|
---|
| 146 | self._extra = extra
|
---|
[21] | 147 | self._validator = validator
|
---|
[330] | 148 |
|
---|
[3] | 149 | def process(self, time):
|
---|
[21] | 150 | """Process the request.
|
---|
| 151 |
|
---|
| 152 | Return True if the request has succeeded, False if data validation
|
---|
| 153 | has failed for a reading request. An exception may also be thrown
|
---|
[330] | 154 | if there is some lower-level communication problem."""
|
---|
[3] | 155 | if self._forWrite:
|
---|
| 156 | pyuipc.write(self._data)
|
---|
[4] | 157 | Handler._callSafe(lambda: self._callback(True, self._extra))
|
---|
[21] | 158 | return True
|
---|
[3] | 159 | else:
|
---|
[21] | 160 | return Handler._performRead(self._data, self._callback,
|
---|
| 161 | self._extra, self._validator)
|
---|
[3] | 162 |
|
---|
[4] | 163 | def fail(self):
|
---|
| 164 | """Handle the failure of this request."""
|
---|
| 165 | if self._forWrite:
|
---|
| 166 | Handler._callSafe(lambda: self._callback(False, self._extra))
|
---|
| 167 | else:
|
---|
| 168 | Handler._callSafe(lambda: self._callback(None, self._extra))
|
---|
| 169 |
|
---|
[3] | 170 | class PeriodicRequest(object):
|
---|
| 171 | """A periodic request."""
|
---|
[21] | 172 | def __init__(self, id, period, data, callback, extra, validator):
|
---|
[3] | 173 | """Construct the periodic request."""
|
---|
| 174 | self._id = id
|
---|
| 175 | self._period = period
|
---|
[290] | 176 | self._nextFire = time.time()
|
---|
[3] | 177 | self._data = data
|
---|
| 178 | self._preparedData = None
|
---|
| 179 | self._callback = callback
|
---|
| 180 | self._extra = extra
|
---|
[21] | 181 | self._validator = validator
|
---|
[330] | 182 |
|
---|
[3] | 183 | @property
|
---|
| 184 | def id(self):
|
---|
| 185 | """Get the ID of this periodic request."""
|
---|
| 186 | return self._id
|
---|
| 187 |
|
---|
| 188 | @property
|
---|
| 189 | def nextFire(self):
|
---|
| 190 | """Get the next firing time."""
|
---|
| 191 | return self._nextFire
|
---|
| 192 |
|
---|
| 193 | def process(self, time):
|
---|
| 194 | """Check if this request should be executed, and if so, do so.
|
---|
| 195 |
|
---|
[21] | 196 | time is the time at which the request is being executed. If this
|
---|
| 197 | function is called too early, nothing is done, and True is
|
---|
| 198 | returned.
|
---|
[3] | 199 |
|
---|
[21] | 200 | Return True if the request has succeeded, False if data validation
|
---|
| 201 | has failed. An exception may also be thrown if there is some
|
---|
| 202 | lower-level communication problem."""
|
---|
| 203 | if time<self._nextFire:
|
---|
| 204 | return True
|
---|
[330] | 205 |
|
---|
[3] | 206 | if self._preparedData is None:
|
---|
| 207 | self._preparedData = pyuipc.prepare_data(self._data)
|
---|
| 208 | self._data = None
|
---|
| 209 |
|
---|
[21] | 210 | isOK = Handler._performRead(self._preparedData, self._callback,
|
---|
| 211 | self._extra, self._validator)
|
---|
[3] | 212 |
|
---|
[21] | 213 | if isOK:
|
---|
| 214 | while self._nextFire <= time:
|
---|
| 215 | self._nextFire += self._period
|
---|
[330] | 216 |
|
---|
[21] | 217 | return isOK
|
---|
[3] | 218 |
|
---|
[4] | 219 | def fail(self):
|
---|
| 220 | """Handle the failure of this request."""
|
---|
| 221 | pass
|
---|
| 222 |
|
---|
[3] | 223 | def __cmp__(self, other):
|
---|
| 224 | """Compare two periodic requests. They are ordered by their next
|
---|
| 225 | firing times."""
|
---|
| 226 | return cmp(self._nextFire, other._nextFire)
|
---|
| 227 |
|
---|
[59] | 228 | def __init__(self, connectionListener,
|
---|
| 229 | connectAttempts = -1, connectInterval = 0.2):
|
---|
[3] | 230 | """Construct the handler with the given connection listener."""
|
---|
| 231 | threading.Thread.__init__(self)
|
---|
| 232 |
|
---|
| 233 | self._connectionListener = connectionListener
|
---|
[59] | 234 | self._connectAttempts = connectAttempts
|
---|
| 235 | self._connectInterval = connectInterval
|
---|
[3] | 236 |
|
---|
| 237 | self._requestCondition = threading.Condition()
|
---|
| 238 | self._connectionRequested = False
|
---|
[330] | 239 | self._connected = False
|
---|
[3] | 240 |
|
---|
| 241 | self._requests = []
|
---|
| 242 | self._nextPeriodicID = 1
|
---|
| 243 | self._periodicRequests = []
|
---|
| 244 |
|
---|
| 245 | self.daemon = True
|
---|
| 246 |
|
---|
[21] | 247 | def requestRead(self, data, callback, extra = None, validator = None):
|
---|
[3] | 248 | """Request the reading of some data.
|
---|
| 249 |
|
---|
| 250 | data is a list of tuples of the following items:
|
---|
| 251 | - the offset of the data as an integer
|
---|
| 252 | - the type letter of the data as a string
|
---|
| 253 |
|
---|
| 254 | callback is a function that receives two pieces of data:
|
---|
[4] | 255 | - the values retrieved or None on error
|
---|
[3] | 256 | - the extra parameter
|
---|
| 257 |
|
---|
| 258 | It will be called in the handler's thread!
|
---|
| 259 | """
|
---|
| 260 | with self._requestCondition:
|
---|
[21] | 261 | self._requests.append(Handler.Request(False, data, callback, extra,
|
---|
| 262 | validator))
|
---|
[3] | 263 | self._requestCondition.notify()
|
---|
| 264 |
|
---|
| 265 | def requestWrite(self, data, callback, extra = None):
|
---|
| 266 | """Request the writing of some data.
|
---|
| 267 |
|
---|
| 268 | data is a list of tuples of the following items:
|
---|
| 269 | - the offset of the data as an integer
|
---|
| 270 | - the type letter of the data as a string
|
---|
| 271 | - the data to write
|
---|
| 272 |
|
---|
[4] | 273 | callback is a function that receives two pieces of data:
|
---|
| 274 | - a boolean indicating if writing was successful
|
---|
| 275 | - the extra data
|
---|
| 276 | It will be called in the handler's thread!
|
---|
[3] | 277 | """
|
---|
| 278 | with self._requestCondition:
|
---|
[152] | 279 | request = Handler.Request(True, data, callback, extra)
|
---|
| 280 | #print "fsuipc.Handler.requestWrite", request
|
---|
| 281 | self._requests.append(request)
|
---|
[3] | 282 | self._requestCondition.notify()
|
---|
| 283 |
|
---|
[4] | 284 | @staticmethod
|
---|
| 285 | def _readWriteCallback(data, extra):
|
---|
| 286 | """Callback for the read() and write() calls below."""
|
---|
| 287 | extra.append(data)
|
---|
| 288 | with extra[0] as condition:
|
---|
| 289 | condition.notify()
|
---|
| 290 |
|
---|
[21] | 291 | def requestPeriodicRead(self, period, data, callback, extra = None,
|
---|
| 292 | validator = None):
|
---|
[3] | 293 | """Request a periodic read of data.
|
---|
| 294 |
|
---|
| 295 | period is a floating point number with the period in seconds.
|
---|
| 296 |
|
---|
| 297 | This function returns an identifier which can be used to cancel the
|
---|
| 298 | request."""
|
---|
| 299 | with self._requestCondition:
|
---|
| 300 | id = self._nextPeriodicID
|
---|
| 301 | self._nextPeriodicID += 1
|
---|
[21] | 302 | request = Handler.PeriodicRequest(id, period, data, callback,
|
---|
| 303 | extra, validator)
|
---|
[3] | 304 | self._periodicRequests.append(request)
|
---|
| 305 | self._requestCondition.notify()
|
---|
| 306 | return id
|
---|
| 307 |
|
---|
| 308 | def clearPeriodic(self, id):
|
---|
| 309 | """Clear the periodic request with the given ID."""
|
---|
| 310 | with self._requestCondition:
|
---|
| 311 | for i in range(0, len(self._periodicRequests)):
|
---|
| 312 | if self._periodicRequests[i].id==id:
|
---|
| 313 | del self._periodicRequests[i]
|
---|
| 314 | return True
|
---|
| 315 | return False
|
---|
| 316 |
|
---|
| 317 | def connect(self):
|
---|
| 318 | """Initiate the connection to the flight simulator."""
|
---|
| 319 | with self._requestCondition:
|
---|
| 320 | if not self._connectionRequested:
|
---|
| 321 | self._connectionRequested = True
|
---|
| 322 | self._requestCondition.notify()
|
---|
[330] | 323 |
|
---|
[3] | 324 | def disconnect(self):
|
---|
| 325 | """Disconnect from the flight simulator."""
|
---|
| 326 | with self._requestCondition:
|
---|
[59] | 327 | self._requests = []
|
---|
[3] | 328 | if self._connectionRequested:
|
---|
[330] | 329 | self._connectionRequested = False
|
---|
[3] | 330 | self._requestCondition.notify()
|
---|
| 331 |
|
---|
[59] | 332 | def clearRequests(self):
|
---|
| 333 | """Clear the outstanding one-shot requests."""
|
---|
| 334 | with self._requestCondition:
|
---|
| 335 | self._requests = []
|
---|
| 336 |
|
---|
[3] | 337 | def run(self):
|
---|
| 338 | """Perform the operation of the thread."""
|
---|
| 339 | while True:
|
---|
| 340 | self._waitConnectionRequest()
|
---|
[330] | 341 |
|
---|
[168] | 342 | if self._connect()>0:
|
---|
[3] | 343 | self._handleConnection()
|
---|
| 344 |
|
---|
| 345 | self._disconnect()
|
---|
[330] | 346 |
|
---|
[3] | 347 | def _waitConnectionRequest(self):
|
---|
| 348 | """Wait for a connection request to arrive."""
|
---|
| 349 | with self._requestCondition:
|
---|
| 350 | while not self._connectionRequested:
|
---|
| 351 | self._requestCondition.wait()
|
---|
[330] | 352 |
|
---|
[168] | 353 | def _connect(self, autoReconnection = False, attempts = 0):
|
---|
[21] | 354 | """Try to connect to the flight simulator via FSUIPC
|
---|
| 355 |
|
---|
| 356 | Returns True if the connection has been established, False if it was
|
---|
| 357 | not due to no longer requested.
|
---|
| 358 | """
|
---|
[3] | 359 | while self._connectionRequested:
|
---|
[168] | 360 | if attempts>=self.NUM_CONNECTATTEMPTS:
|
---|
| 361 | self._connectionRequested = False
|
---|
| 362 | if autoReconnection:
|
---|
[330] | 363 | Handler._callSafe(lambda:
|
---|
[168] | 364 | self._connectionListener.disconnected())
|
---|
| 365 | else:
|
---|
[330] | 366 | Handler._callSafe(lambda:
|
---|
[168] | 367 | self._connectionListener.connectionFailed())
|
---|
| 368 | return 0
|
---|
[330] | 369 |
|
---|
[3] | 370 | try:
|
---|
[59] | 371 | attempts += 1
|
---|
[16] | 372 | pyuipc.open(pyuipc.SIM_ANY)
|
---|
[3] | 373 | description = "(FSUIPC version: 0x%04x, library version: 0x%04x, FS version: %d)" % \
|
---|
[330] | 374 | (pyuipc.fsuipc_version, pyuipc.lib_version,
|
---|
[3] | 375 | pyuipc.fs_version)
|
---|
[59] | 376 | if not autoReconnection:
|
---|
[212] | 377 | fsType = const.SIM_MSFSX \
|
---|
| 378 | if pyuipc.fs_version == pyuipc.SIM_FSX \
|
---|
| 379 | else const.SIM_MSFS9
|
---|
[330] | 380 |
|
---|
| 381 | Handler._callSafe(lambda:
|
---|
[212] | 382 | self._connectionListener.connected(fsType,
|
---|
[59] | 383 | description))
|
---|
[21] | 384 | self._connected = True
|
---|
[168] | 385 | return attempts
|
---|
[3] | 386 | except Exception, e:
|
---|
[168] | 387 | print "fsuipc.Handler._connect: connection failed: " + str(e) + \
|
---|
| 388 | " (attempts: %d)" % (attempts,)
|
---|
[59] | 389 | if attempts<self.NUM_CONNECTATTEMPTS:
|
---|
| 390 | time.sleep(self.CONNECT_INTERVAL)
|
---|
[330] | 391 |
|
---|
[3] | 392 | def _handleConnection(self):
|
---|
| 393 | """Handle a living connection."""
|
---|
| 394 | with self._requestCondition:
|
---|
| 395 | while self._connectionRequested:
|
---|
[21] | 396 | self._processRequests()
|
---|
| 397 | self._waitRequest()
|
---|
| 398 |
|
---|
| 399 | def _waitRequest(self):
|
---|
| 400 | """Wait for the time of the next request.
|
---|
| 401 |
|
---|
| 402 | Returns also, if the connection is no longer requested.
|
---|
| 403 |
|
---|
| 404 | Should be called with the request condition lock held."""
|
---|
| 405 | while self._connectionRequested:
|
---|
| 406 | timeout = None
|
---|
| 407 | if self._periodicRequests:
|
---|
| 408 | self._periodicRequests.sort()
|
---|
| 409 | timeout = self._periodicRequests[0].nextFire - time.time()
|
---|
| 410 |
|
---|
[152] | 411 | if self._requests or \
|
---|
| 412 | (timeout is not None and timeout <= 0.0):
|
---|
[21] | 413 | return
|
---|
[330] | 414 |
|
---|
[21] | 415 | self._requestCondition.wait(timeout)
|
---|
[330] | 416 |
|
---|
[3] | 417 | def _disconnect(self):
|
---|
| 418 | """Disconnect from the flight simulator."""
|
---|
[152] | 419 | print "fsuipc.Handler._disconnect"
|
---|
[21] | 420 | if self._connected:
|
---|
| 421 | pyuipc.close()
|
---|
| 422 | self._connected = False
|
---|
[330] | 423 |
|
---|
[168] | 424 | def _processRequest(self, request, time, attempts):
|
---|
[330] | 425 | """Process the given request.
|
---|
[3] | 426 |
|
---|
[21] | 427 | If an exception occurs or invalid data is read too many times, we try
|
---|
| 428 | to reconnect.
|
---|
| 429 |
|
---|
| 430 | This function returns only if the request has succeeded, or if a
|
---|
| 431 | connection is no longer requested.
|
---|
| 432 |
|
---|
| 433 | This function is called with the request lock held, but is relased
|
---|
| 434 | whole processing the request and reconnecting."""
|
---|
[3] | 435 | self._requestCondition.release()
|
---|
| 436 |
|
---|
[152] | 437 | #print "fsuipc.Handler._processRequest", request
|
---|
| 438 |
|
---|
[21] | 439 | needReconnect = False
|
---|
[3] | 440 | try:
|
---|
[21] | 441 | try:
|
---|
| 442 | if not request.process(time):
|
---|
| 443 | print "fsuipc.Handler._processRequest: FSUIPC returned invalid data too many times, reconnecting"
|
---|
| 444 | needReconnect = True
|
---|
| 445 | except Exception as e:
|
---|
| 446 | print "fsuipc.Handler._processRequest: FSUIPC connection failed (" + \
|
---|
[168] | 447 | str(e) + "), reconnecting (attempts=%d)." % (attempts,)
|
---|
[21] | 448 | needReconnect = True
|
---|
| 449 |
|
---|
| 450 | if needReconnect:
|
---|
[59] | 451 | with self._requestCondition:
|
---|
| 452 | self._requests.insert(0, request)
|
---|
[21] | 453 | self._disconnect()
|
---|
[168] | 454 | return self._connect(autoReconnection = True, attempts = attempts)
|
---|
| 455 | else:
|
---|
| 456 | return 0
|
---|
[3] | 457 | finally:
|
---|
| 458 | self._requestCondition.acquire()
|
---|
[330] | 459 |
|
---|
[3] | 460 | def _processRequests(self):
|
---|
| 461 | """Process any pending requests.
|
---|
| 462 |
|
---|
| 463 | Will be called with the request lock held."""
|
---|
[168] | 464 | attempts = 0
|
---|
[3] | 465 | while self._connectionRequested and self._periodicRequests:
|
---|
| 466 | self._periodicRequests.sort()
|
---|
| 467 | request = self._periodicRequests[0]
|
---|
[21] | 468 |
|
---|
| 469 | t = time.time()
|
---|
| 470 |
|
---|
| 471 | if request.nextFire>t:
|
---|
| 472 | break
|
---|
[330] | 473 |
|
---|
[168] | 474 | attempts = self._processRequest(request, t, attempts)
|
---|
[3] | 475 |
|
---|
| 476 | while self._connectionRequested and self._requests:
|
---|
| 477 | request = self._requests[0]
|
---|
| 478 | del self._requests[0]
|
---|
| 479 |
|
---|
[168] | 480 | attempts = self._processRequest(request, None, attempts)
|
---|
[3] | 481 |
|
---|
| 482 | return self._connectionRequested
|
---|
[4] | 483 |
|
---|
| 484 | #------------------------------------------------------------------------------
|
---|
| 485 |
|
---|
| 486 | class Simulator(object):
|
---|
| 487 | """The simulator class representing the interface to the flight simulator
|
---|
| 488 | via FSUIPC."""
|
---|
[5] | 489 | # The basic data that should be queried all the time once we are connected
|
---|
[61] | 490 | timeData = [ (0x0240, "H"), # Year
|
---|
| 491 | (0x023e, "H"), # Number of day in year
|
---|
| 492 | (0x023b, "b"), # UTC hour
|
---|
| 493 | (0x023c, "b"), # UTC minute
|
---|
| 494 | (0x023a, "b") ] # seconds
|
---|
[330] | 495 |
|
---|
[61] | 496 | normalData = timeData + \
|
---|
| 497 | [ (0x3d00, -256), # The name of the current aircraft
|
---|
[133] | 498 | (0x3c00, -256), # The path of the current AIR file
|
---|
| 499 | (0x1274, "h") ] # Text display mode
|
---|
[9] | 500 |
|
---|
| 501 | flareData1 = [ (0x023a, "b"), # Seconds of time
|
---|
| 502 | (0x31e4, "d"), # Radio altitude
|
---|
| 503 | (0x02c8, "d") ] # Vertical speed
|
---|
[330] | 504 |
|
---|
[9] | 505 | flareStartData = [ (0x0e90, "H"), # Ambient wind speed
|
---|
| 506 | (0x0e92, "H"), # Ambient wind direction
|
---|
| 507 | (0x0e8a, "H") ] # Visibility
|
---|
[330] | 508 |
|
---|
[9] | 509 | flareData2 = [ (0x023a, "b"), # Seconds of time
|
---|
| 510 | (0x0366, "H"), # On the ground
|
---|
| 511 | (0x02c8, "d"), # Vertical speed
|
---|
| 512 | (0x030c, "d"), # Touch-down rate
|
---|
| 513 | (0x02bc, "d"), # IAS
|
---|
| 514 | (0x0578, "d"), # Pitch
|
---|
| 515 | (0x057c, "d"), # Bank
|
---|
| 516 | (0x0580, "d") ] # Heading
|
---|
| 517 |
|
---|
[148] | 518 | TIME_SYNC_INTERVAL = 3.0
|
---|
| 519 |
|
---|
[61] | 520 | @staticmethod
|
---|
| 521 | def _getTimestamp(data):
|
---|
| 522 | """Convert the given data into a timestamp."""
|
---|
| 523 | timestamp = calendar.timegm(time.struct_time([data[0],
|
---|
| 524 | 1, 1, 0, 0, 0, -1, 1, 0]))
|
---|
| 525 | timestamp += data[1] * 24 * 3600
|
---|
| 526 | timestamp += data[2] * 3600
|
---|
| 527 | timestamp += data[3] * 60
|
---|
[330] | 528 | timestamp += data[4]
|
---|
[61] | 529 |
|
---|
| 530 | return timestamp
|
---|
| 531 |
|
---|
[168] | 532 | @staticmethod
|
---|
| 533 | def _appendHotkeyData(data, offset, hotkey):
|
---|
| 534 | """Append the data for the given hotkey to the given array, that is
|
---|
| 535 | intended to be passed to requestWrite call on the handler."""
|
---|
| 536 | data.append((offset + 0, "b", ord(hotkey.key)))
|
---|
| 537 |
|
---|
| 538 | modifiers = 0
|
---|
| 539 | if hotkey.ctrl: modifiers |= 0x02
|
---|
| 540 | if hotkey.shift: modifiers |= 0x01
|
---|
| 541 | data.append((offset + 1, "b", modifiers))
|
---|
| 542 |
|
---|
| 543 | data.append((offset + 2, "b", 0))
|
---|
[330] | 544 |
|
---|
| 545 | data.append((offset + 3, "b", 0))
|
---|
[168] | 546 |
|
---|
[59] | 547 | def __init__(self, connectionListener, connectAttempts = -1,
|
---|
| 548 | connectInterval = 0.2):
|
---|
[5] | 549 | """Construct the simulator.
|
---|
[330] | 550 |
|
---|
[4] | 551 | The aircraft object passed must provide the following members:
|
---|
| 552 | - type: one of the AIRCRAFT_XXX constants from const.py
|
---|
| 553 | - modelChanged(aircraftName, modelName): called when the model handling
|
---|
| 554 | the aircraft has changed.
|
---|
[9] | 555 | - handleState(aircraftState): handle the given state.
|
---|
| 556 | - flareStarted(windSpeed, windDirection, visibility, flareStart,
|
---|
| 557 | flareStartFS): called when the flare has
|
---|
| 558 | started. windSpeed is in knots, windDirection is in degrees and
|
---|
| 559 | visibility is in metres. flareStart and flareStartFS are two time
|
---|
| 560 | values expressed in seconds that can be used to calculate the flare
|
---|
[330] | 561 | time.
|
---|
[9] | 562 | - flareFinished(flareEnd, flareEndFS, tdRate, tdRateCalculatedByFS,
|
---|
| 563 | ias, pitch, bank, heading): called when the flare has
|
---|
| 564 | finished, i.e. the aircraft is on the ground. flareEnd and flareEndFS
|
---|
| 565 | are the two time values corresponding to the touchdown time. tdRate is
|
---|
| 566 | the touch-down rate, tdRateCalculatedBySim indicates if the data comes
|
---|
| 567 | from the simulator or was calculated by the adapter. The other data
|
---|
| 568 | are self-explanatory and expressed in their 'natural' units."""
|
---|
[212] | 569 | self._fsType = None
|
---|
[16] | 570 | self._aircraft = None
|
---|
[5] | 571 |
|
---|
[168] | 572 | self._handler = Handler(self,
|
---|
[59] | 573 | connectAttempts = connectAttempts,
|
---|
| 574 | connectInterval = connectInterval)
|
---|
[168] | 575 | self._connectionListener = connectionListener
|
---|
[5] | 576 | self._handler.start()
|
---|
| 577 |
|
---|
[133] | 578 | self._scroll = False
|
---|
| 579 |
|
---|
[148] | 580 | self._syncTime = False
|
---|
| 581 | self._nextSyncTime = -1
|
---|
[330] | 582 |
|
---|
[5] | 583 | self._normalRequestID = None
|
---|
| 584 |
|
---|
| 585 | self._monitoringRequested = False
|
---|
| 586 | self._monitoring = False
|
---|
[4] | 587 |
|
---|
[5] | 588 | self._aircraftName = None
|
---|
| 589 | self._aircraftModel = None
|
---|
| 590 |
|
---|
[9] | 591 | self._flareRequestID = None
|
---|
| 592 | self._flareRates = []
|
---|
| 593 | self._flareStart = None
|
---|
| 594 | self._flareStartFS = None
|
---|
[152] | 595 |
|
---|
[168] | 596 | self._hotkeyLock = threading.Lock()
|
---|
| 597 | self._hotkeys = None
|
---|
| 598 | self._hotkeySetID = 0
|
---|
| 599 | self._hotkeySetGeneration = 0
|
---|
| 600 | self._hotkeyOffets = None
|
---|
| 601 | self._hotkeyRequestID = None
|
---|
| 602 | self._hotkeyCallback = None
|
---|
| 603 |
|
---|
[26] | 604 | self._latin1decoder = codecs.getdecoder("iso-8859-1")
|
---|
[290] | 605 | self._fuelCallback = None
|
---|
[141] | 606 |
|
---|
[16] | 607 | def connect(self, aircraft):
|
---|
[5] | 608 | """Initiate a connection to the simulator."""
|
---|
[16] | 609 | self._aircraft = aircraft
|
---|
| 610 | self._aircraftName = None
|
---|
| 611 | self._aircraftModel = None
|
---|
[5] | 612 | self._handler.connect()
|
---|
[59] | 613 | if self._normalRequestID is None:
|
---|
[148] | 614 | self._nextSyncTime = -1
|
---|
[59] | 615 | self._startDefaultNormal()
|
---|
| 616 |
|
---|
| 617 | def reconnect(self):
|
---|
| 618 | """Initiate a reconnection to the simulator.
|
---|
| 619 |
|
---|
| 620 | It does not reset already set up data, just calls connect() on the
|
---|
| 621 | handler."""
|
---|
| 622 | self._handler.connect()
|
---|
| 623 |
|
---|
| 624 | def requestZFW(self, callback):
|
---|
| 625 | """Send a request for the ZFW."""
|
---|
| 626 | self._handler.requestRead([(0x3bfc, "d")], self._handleZFW, extra = callback)
|
---|
[61] | 627 |
|
---|
[117] | 628 | def requestWeights(self, callback):
|
---|
| 629 | """Request the following weights: DOW, ZFW, payload.
|
---|
| 630 |
|
---|
| 631 | These values will be passed to the callback function in this order, as
|
---|
[330] | 632 | separate arguments."""
|
---|
[117] | 633 | self._handler.requestRead([(0x13fc, "d")], self._handlePayloadCount,
|
---|
| 634 | extra = callback)
|
---|
| 635 |
|
---|
[61] | 636 | def requestTime(self, callback):
|
---|
| 637 | """Request the time from the simulator."""
|
---|
| 638 | self._handler.requestRead(Simulator.timeData, self._handleTime,
|
---|
| 639 | extra = callback)
|
---|
[330] | 640 |
|
---|
[5] | 641 | def startMonitoring(self):
|
---|
| 642 | """Start the periodic monitoring of the aircraft and pass the resulting
|
---|
| 643 | state to the aircraft object periodically."""
|
---|
[330] | 644 | assert not self._monitoringRequested
|
---|
[5] | 645 | self._monitoringRequested = True
|
---|
[4] | 646 |
|
---|
| 647 | def stopMonitoring(self):
|
---|
| 648 | """Stop the periodic monitoring of the aircraft."""
|
---|
[330] | 649 | assert self._monitoringRequested
|
---|
[5] | 650 | self._monitoringRequested = False
|
---|
[4] | 651 |
|
---|
[9] | 652 | def startFlare(self):
|
---|
| 653 | """Start monitoring the flare time.
|
---|
| 654 |
|
---|
| 655 | At present it is assumed to be called from the FSUIPC thread, hence no
|
---|
| 656 | protection."""
|
---|
| 657 | #self._aircraft.logger.debug("startFlare")
|
---|
| 658 | if self._flareRequestID is None:
|
---|
| 659 | self._flareRates = []
|
---|
| 660 | self._flareRequestID = self._handler.requestPeriodicRead(0.1,
|
---|
| 661 | Simulator.flareData1,
|
---|
| 662 | self._handleFlare1)
|
---|
| 663 |
|
---|
| 664 | def cancelFlare(self):
|
---|
| 665 | """Cancel monitoring the flare time.
|
---|
| 666 |
|
---|
| 667 | At present it is assumed to be called from the FSUIPC thread, hence no
|
---|
| 668 | protection."""
|
---|
| 669 | if self._flareRequestID is not None:
|
---|
| 670 | self._handler.clearPeriodic(self._flareRequestID)
|
---|
| 671 | self._flareRequestID = None
|
---|
| 672 |
|
---|
[152] | 673 | def sendMessage(self, message, duration = 3,
|
---|
| 674 | _disconnect = False):
|
---|
[133] | 675 | """Send a message to the pilot via the simulator.
|
---|
| 676 |
|
---|
| 677 | duration is the number of seconds to keep the message displayed."""
|
---|
[315] | 678 |
|
---|
| 679 | print "fsuipc.Simulator.sendMessage:", message
|
---|
| 680 |
|
---|
[133] | 681 | if self._scroll:
|
---|
| 682 | if duration==0: duration = -1
|
---|
| 683 | elif duration == 1: duration = -2
|
---|
| 684 | else: duration = -duration
|
---|
| 685 |
|
---|
| 686 | data = [(0x3380, -1 - len(message), message),
|
---|
| 687 | (0x32fa, 'h', duration)]
|
---|
| 688 |
|
---|
[152] | 689 | #if _disconnect:
|
---|
| 690 | # print "fsuipc.Simulator.sendMessage(disconnect)", message
|
---|
| 691 |
|
---|
| 692 | self._handler.requestWrite(data, self._handleMessageSent,
|
---|
| 693 | extra = _disconnect)
|
---|
[141] | 694 |
|
---|
[274] | 695 | def getFuel(self, callback):
|
---|
| 696 | """Get the fuel information for the current model.
|
---|
[141] | 697 |
|
---|
[274] | 698 | The callback will be called with a list of triplets with the following
|
---|
| 699 | items:
|
---|
| 700 | - the fuel tank identifier
|
---|
[141] | 701 | - the current weight of the fuel in the tank (in kgs)
|
---|
| 702 | - the current total capacity of the tank (in kgs)."""
|
---|
[274] | 703 | if self._aircraftModel is None:
|
---|
[290] | 704 | self._fuelCallback = callback
|
---|
[274] | 705 | else:
|
---|
| 706 | self._aircraftModel.getFuel(self._handler, callback)
|
---|
[141] | 707 |
|
---|
| 708 | def setFuelLevel(self, levels):
|
---|
| 709 | """Set the fuel level to the given ones.
|
---|
| 710 |
|
---|
| 711 | levels is an array of two-tuples, where each tuple consists of the
|
---|
| 712 | following:
|
---|
| 713 | - the const.FUELTANK_XXX constant denoting the tank that must be set,
|
---|
| 714 | - the requested level of the fuel as a floating-point value between 0.0
|
---|
| 715 | and 1.0."""
|
---|
[274] | 716 | if self._aircraftModel is not None:
|
---|
| 717 | self._aircraftModel.setFuelLevel(self._handler, levels)
|
---|
[148] | 718 |
|
---|
| 719 | def enableTimeSync(self):
|
---|
| 720 | """Enable the time synchronization."""
|
---|
| 721 | self._nextSyncTime = -1
|
---|
| 722 | self._syncTime = True
|
---|
[330] | 723 |
|
---|
[148] | 724 | def disableTimeSync(self):
|
---|
| 725 | """Enable the time synchronization."""
|
---|
| 726 | self._syncTime = False
|
---|
| 727 | self._nextSyncTime = -1
|
---|
[168] | 728 |
|
---|
| 729 | def listenHotkeys(self, hotkeys, callback):
|
---|
| 730 | """Start listening to the given hotkeys.
|
---|
| 731 |
|
---|
| 732 | callback is function expecting two arguments:
|
---|
| 733 | - the ID of the hotkey set as returned by this function,
|
---|
| 734 | - the list of the indexes of the hotkeys that were pressed."""
|
---|
| 735 | with self._hotkeyLock:
|
---|
| 736 | assert self._hotkeys is None
|
---|
| 737 |
|
---|
| 738 | self._hotkeys = hotkeys
|
---|
| 739 | self._hotkeySetID += 1
|
---|
| 740 | self._hotkeySetGeneration = 0
|
---|
| 741 | self._hotkeyCallback = callback
|
---|
[330] | 742 |
|
---|
[168] | 743 | self._handler.requestRead([(0x320c, "u")],
|
---|
| 744 | self._handleNumHotkeys,
|
---|
| 745 | (self._hotkeySetID,
|
---|
| 746 | self._hotkeySetGeneration))
|
---|
| 747 |
|
---|
| 748 | return self._hotkeySetID
|
---|
| 749 |
|
---|
| 750 | def clearHotkeys(self):
|
---|
| 751 | """Clear the current hotkey set.
|
---|
| 752 |
|
---|
| 753 | Note that it is possible, that the callback function set either
|
---|
| 754 | previously or after calling this function by listenHotkeys() will be
|
---|
| 755 | called with data from the previous hotkey set.
|
---|
| 756 |
|
---|
| 757 | Therefore it is recommended to store the hotkey set ID somewhere and
|
---|
| 758 | check that in the callback function. Right before calling
|
---|
| 759 | clearHotkeys(), this stored ID should be cleared so that the check
|
---|
| 760 | fails for sure."""
|
---|
| 761 | with self._hotkeyLock:
|
---|
| 762 | if self._hotkeys is not None:
|
---|
| 763 | self._hotkeys = None
|
---|
| 764 | self._hotkeySetID += 1
|
---|
| 765 | self._hotkeyCallback = None
|
---|
| 766 | self._clearHotkeyRequest()
|
---|
[330] | 767 |
|
---|
[152] | 768 | def disconnect(self, closingMessage = None, duration = 3):
|
---|
[4] | 769 | """Disconnect from the simulator."""
|
---|
[330] | 770 | assert not self._monitoringRequested
|
---|
[152] | 771 |
|
---|
| 772 | print "fsuipc.Simulator.disconnect", closingMessage, duration
|
---|
[330] | 773 |
|
---|
[5] | 774 | self._stopNormal()
|
---|
[168] | 775 | self.clearHotkeys()
|
---|
[152] | 776 | if closingMessage is None:
|
---|
| 777 | self._handler.disconnect()
|
---|
| 778 | else:
|
---|
| 779 | self.sendMessage(closingMessage, duration = duration,
|
---|
| 780 | _disconnect = True)
|
---|
[4] | 781 |
|
---|
[168] | 782 | def connected(self, fsType, descriptor):
|
---|
| 783 | """Called when a connection has been established to the flight
|
---|
| 784 | simulator of the given type."""
|
---|
[212] | 785 | self._fsType = fsType
|
---|
[168] | 786 | with self._hotkeyLock:
|
---|
| 787 | if self._hotkeys is not None:
|
---|
| 788 | self._hotkeySetGeneration += 1
|
---|
[330] | 789 |
|
---|
[168] | 790 | self._handler.requestRead([(0x320c, "u")],
|
---|
| 791 | self._handleNumHotkeys,
|
---|
| 792 | (self._hotkeySetID,
|
---|
| 793 | self._hotkeySetGeneration))
|
---|
| 794 | self._connectionListener.connected(fsType, descriptor)
|
---|
| 795 |
|
---|
| 796 | def connectionFailed(self):
|
---|
| 797 | """Called when the connection could not be established."""
|
---|
| 798 | with self._hotkeyLock:
|
---|
| 799 | self._clearHotkeyRequest()
|
---|
| 800 | self._connectionListener.connectionFailed()
|
---|
| 801 |
|
---|
| 802 | def disconnected(self):
|
---|
| 803 | """Called when a connection to the flight simulator has been broken."""
|
---|
| 804 | with self._hotkeyLock:
|
---|
| 805 | self._clearHotkeyRequest()
|
---|
| 806 | self._connectionListener.disconnected()
|
---|
| 807 |
|
---|
[5] | 808 | def _startDefaultNormal(self):
|
---|
| 809 | """Start the default normal periodic request."""
|
---|
| 810 | assert self._normalRequestID is None
|
---|
[21] | 811 | self._normalRequestID = \
|
---|
| 812 | self._handler.requestPeriodicRead(1.0,
|
---|
| 813 | Simulator.normalData,
|
---|
| 814 | self._handleNormal,
|
---|
| 815 | validator = self._validateNormal)
|
---|
[5] | 816 |
|
---|
| 817 | def _stopNormal(self):
|
---|
| 818 | """Stop the normal period request."""
|
---|
| 819 | assert self._normalRequestID is not None
|
---|
| 820 | self._handler.clearPeriodic(self._normalRequestID)
|
---|
| 821 | self._normalRequestID = None
|
---|
[16] | 822 | self._monitoring = False
|
---|
[5] | 823 |
|
---|
[21] | 824 | def _validateNormal(self, data, extra):
|
---|
| 825 | """Validate the normal data."""
|
---|
| 826 | return data[0]!=0 and data[1]!=0 and len(data[5])>0 and len(data[6])>0
|
---|
| 827 |
|
---|
[5] | 828 | def _handleNormal(self, data, extra):
|
---|
| 829 | """Handle the reply to the normal request.
|
---|
[4] | 830 |
|
---|
[5] | 831 | At the beginning the result consists the data for normalData. When
|
---|
| 832 | monitoring is started, it contains the result also for the
|
---|
| 833 | aircraft-specific values.
|
---|
[4] | 834 | """
|
---|
[61] | 835 | timestamp = Simulator._getTimestamp(data)
|
---|
[8] | 836 |
|
---|
| 837 | createdNewModel = self._setAircraftName(timestamp, data[5], data[6])
|
---|
[290] | 838 | if self._fuelCallback is not None:
|
---|
| 839 | self._aircraftModel.getFuel(self._handler, self._fuelCallback)
|
---|
| 840 | self._fuelCallback = None
|
---|
[133] | 841 |
|
---|
| 842 | self._scroll = data[7]!=0
|
---|
[148] | 843 |
|
---|
[5] | 844 | if self._monitoringRequested and not self._monitoring:
|
---|
| 845 | self._stopNormal()
|
---|
[4] | 846 | self._startMonitoring()
|
---|
[5] | 847 | elif self._monitoring and not self._monitoringRequested:
|
---|
| 848 | self._stopNormal()
|
---|
| 849 | self._startDefaultNormal()
|
---|
[8] | 850 | elif self._monitoring and self._aircraftModel is not None and \
|
---|
| 851 | not createdNewModel:
|
---|
[330] | 852 | aircraftState = self._aircraftModel.getAircraftState(self._aircraft,
|
---|
[8] | 853 | timestamp, data)
|
---|
[161] | 854 |
|
---|
| 855 | self._checkTimeSync(aircraftState)
|
---|
[330] | 856 |
|
---|
[5] | 857 | self._aircraft.handleState(aircraftState)
|
---|
[4] | 858 |
|
---|
[161] | 859 | def _checkTimeSync(self, aircraftState):
|
---|
[148] | 860 | """Check if we need to synchronize the FS time."""
|
---|
[161] | 861 | if not self._syncTime or aircraftState.paused or \
|
---|
| 862 | self._flareRequestID is not None:
|
---|
| 863 | self._nextSyncTime = -1
|
---|
| 864 | return
|
---|
[148] | 865 |
|
---|
| 866 | now = time.time()
|
---|
| 867 | seconds = time.gmtime(now).tm_sec
|
---|
| 868 |
|
---|
| 869 | if seconds>30 and seconds<59:
|
---|
| 870 | if self._nextSyncTime > (now - 0.49):
|
---|
| 871 | return
|
---|
[330] | 872 |
|
---|
[148] | 873 | self._handler.requestWrite([(0x023a, "b", int(seconds))],
|
---|
| 874 | self._handleTimeSynced)
|
---|
[330] | 875 |
|
---|
[148] | 876 | #print "Set the seconds to ", seconds
|
---|
| 877 |
|
---|
| 878 | if self._nextSyncTime<0:
|
---|
| 879 | self._nextSyncTime = now
|
---|
[330] | 880 |
|
---|
[148] | 881 | self._nextSyncTime += Simulator.TIME_SYNC_INTERVAL
|
---|
| 882 | else:
|
---|
| 883 | self._nextSyncTime = -1
|
---|
| 884 |
|
---|
| 885 | def _handleTimeSynced(self, success, extra):
|
---|
| 886 | """Callback for the time sync result."""
|
---|
| 887 | pass
|
---|
[330] | 888 |
|
---|
[8] | 889 | def _setAircraftName(self, timestamp, name, airPath):
|
---|
[4] | 890 | """Set the name of the aicraft and if it is different from the
|
---|
| 891 | previous, create a new model for it.
|
---|
[330] | 892 |
|
---|
[8] | 893 | If so, also notifty the aircraft about the change.
|
---|
| 894 |
|
---|
| 895 | Return if a new model was created."""
|
---|
| 896 | aircraftName = (name, airPath)
|
---|
| 897 | if aircraftName==self._aircraftName:
|
---|
| 898 | return False
|
---|
[4] | 899 |
|
---|
[314] | 900 | print "fsuipc.Simulator: new aircraft name and air file path: %s, %s" % \
|
---|
| 901 | (name, airPath)
|
---|
| 902 |
|
---|
[8] | 903 | self._aircraftName = aircraftName
|
---|
| 904 | needNew = self._aircraftModel is None
|
---|
| 905 | needNew = needNew or\
|
---|
| 906 | not self._aircraftModel.doesHandle(self._aircraft, aircraftName)
|
---|
| 907 | if not needNew:
|
---|
| 908 | specialModel = AircraftModel.findSpecial(self._aircraft, aircraftName)
|
---|
| 909 | needNew = specialModel is not None and \
|
---|
| 910 | specialModel is not self._aircraftModel.__class__
|
---|
| 911 |
|
---|
| 912 | if needNew:
|
---|
| 913 | self._setAircraftModel(AircraftModel.create(self._aircraft, aircraftName))
|
---|
[330] | 914 |
|
---|
| 915 |
|
---|
[97] | 916 | self._aircraft.modelChanged(timestamp, self._latin1decoder(name)[0],
|
---|
[330] | 917 | self._aircraftModel.name)
|
---|
[8] | 918 |
|
---|
| 919 | return needNew
|
---|
[4] | 920 |
|
---|
| 921 | def _setAircraftModel(self, model):
|
---|
| 922 | """Set a new aircraft model.
|
---|
| 923 |
|
---|
| 924 | It will be queried for the data to monitor and the monitoring request
|
---|
| 925 | will be replaced by a new one."""
|
---|
| 926 | self._aircraftModel = model
|
---|
[330] | 927 |
|
---|
[5] | 928 | if self._monitoring:
|
---|
[8] | 929 | self._stopNormal()
|
---|
[5] | 930 | self._startMonitoring()
|
---|
[330] | 931 |
|
---|
[5] | 932 | def _startMonitoring(self):
|
---|
| 933 | """Start monitoring with the current aircraft model."""
|
---|
| 934 | data = Simulator.normalData[:]
|
---|
[212] | 935 | self._aircraftModel.addMonitoringData(data, self._fsType)
|
---|
[330] | 936 |
|
---|
[5] | 937 | self._normalRequestID = \
|
---|
[330] | 938 | self._handler.requestPeriodicRead(1.0, data,
|
---|
[21] | 939 | self._handleNormal,
|
---|
| 940 | validator = self._validateNormal)
|
---|
[16] | 941 | self._monitoring = True
|
---|
[4] | 942 |
|
---|
[9] | 943 | def _addFlareRate(self, data):
|
---|
| 944 | """Append a flare rate to the list of last rates."""
|
---|
| 945 | if len(self._flareRates)>=3:
|
---|
| 946 | del self._flareRates[0]
|
---|
| 947 | self._flareRates.append(Handler.fsuipc2VS(data))
|
---|
| 948 |
|
---|
| 949 | def _handleFlare1(self, data, normal):
|
---|
| 950 | """Handle the first stage of flare monitoring."""
|
---|
| 951 | #self._aircraft.logger.debug("handleFlare1: " + str(data))
|
---|
| 952 | if Handler.fsuipc2radioAltitude(data[1])<=50.0:
|
---|
| 953 | self._flareStart = time.time()
|
---|
| 954 | self._flareStartFS = data[0]
|
---|
| 955 | self._handler.clearPeriodic(self._flareRequestID)
|
---|
| 956 | self._flareRequestID = \
|
---|
[330] | 957 | self._handler.requestPeriodicRead(0.1,
|
---|
[9] | 958 | Simulator.flareData2,
|
---|
| 959 | self._handleFlare2)
|
---|
| 960 | self._handler.requestRead(Simulator.flareStartData,
|
---|
| 961 | self._handleFlareStart)
|
---|
[330] | 962 |
|
---|
[9] | 963 | self._addFlareRate(data[2])
|
---|
| 964 |
|
---|
| 965 | def _handleFlareStart(self, data, extra):
|
---|
| 966 | """Handle the data need to notify the aircraft about the starting of
|
---|
| 967 | the flare."""
|
---|
| 968 | #self._aircraft.logger.debug("handleFlareStart: " + str(data))
|
---|
| 969 | if data is not None:
|
---|
| 970 | windDirection = data[1]*360.0/65536.0
|
---|
| 971 | if windDirection<0.0: windDirection += 360.0
|
---|
| 972 | self._aircraft.flareStarted(data[0], windDirection,
|
---|
| 973 | data[2]*1609.344/100.0,
|
---|
| 974 | self._flareStart, self._flareStartFS)
|
---|
| 975 |
|
---|
| 976 | def _handleFlare2(self, data, normal):
|
---|
| 977 | """Handle the first stage of flare monitoring."""
|
---|
| 978 | #self._aircraft.logger.debug("handleFlare2: " + str(data))
|
---|
| 979 | if data[1]!=0:
|
---|
| 980 | flareEnd = time.time()
|
---|
| 981 | self._handler.clearPeriodic(self._flareRequestID)
|
---|
| 982 | self._flareRequestID = None
|
---|
| 983 |
|
---|
| 984 | flareEndFS = data[0]
|
---|
| 985 | if flareEndFS<self._flareStartFS:
|
---|
| 986 | flareEndFS += 60
|
---|
| 987 |
|
---|
| 988 | tdRate = Handler.fsuipc2VS(data[3])
|
---|
| 989 | tdRateCalculatedByFS = True
|
---|
| 990 | if tdRate==0 or tdRate>1000.0 or tdRate<-1000.0:
|
---|
| 991 | tdRate = min(self._flareRates)
|
---|
| 992 | tdRateCalculatedByFS = False
|
---|
| 993 |
|
---|
| 994 | self._aircraft.flareFinished(flareEnd, flareEndFS,
|
---|
| 995 | tdRate, tdRateCalculatedByFS,
|
---|
| 996 | Handler.fsuipc2IAS(data[4]),
|
---|
| 997 | Handler.fsuipc2Degrees(data[5]),
|
---|
| 998 | Handler.fsuipc2Degrees(data[6]),
|
---|
| 999 | Handler.fsuipc2PositiveDegrees(data[7]))
|
---|
| 1000 | else:
|
---|
| 1001 | self._addFlareRate(data[2])
|
---|
[59] | 1002 |
|
---|
| 1003 | def _handleZFW(self, data, callback):
|
---|
| 1004 | """Callback for a ZFW retrieval request."""
|
---|
| 1005 | zfw = data[0] * const.LBSTOKG / 256.0
|
---|
| 1006 | callback(zfw)
|
---|
[330] | 1007 |
|
---|
[61] | 1008 | def _handleTime(self, data, callback):
|
---|
| 1009 | """Callback for a time retrieval request."""
|
---|
| 1010 | callback(Simulator._getTimestamp(data))
|
---|
[117] | 1011 |
|
---|
| 1012 | def _handlePayloadCount(self, data, callback):
|
---|
| 1013 | """Callback for the payload count retrieval request."""
|
---|
| 1014 | payloadCount = data[0]
|
---|
| 1015 | data = [(0x3bfc, "d"), (0x30c0, "f")]
|
---|
| 1016 | for i in range(0, payloadCount):
|
---|
| 1017 | data.append((0x1400 + i*48, "f"))
|
---|
[330] | 1018 |
|
---|
[117] | 1019 | self._handler.requestRead(data, self._handleWeights,
|
---|
| 1020 | extra = callback)
|
---|
| 1021 |
|
---|
| 1022 | def _handleWeights(self, data, callback):
|
---|
| 1023 | """Callback for the weights retrieval request."""
|
---|
| 1024 | zfw = data[0] * const.LBSTOKG / 256.0
|
---|
| 1025 | grossWeight = data[1] * const.LBSTOKG
|
---|
| 1026 | payload = sum(data[2:]) * const.LBSTOKG
|
---|
| 1027 | dow = zfw - payload
|
---|
| 1028 | callback(dow, payload, zfw, grossWeight)
|
---|
[133] | 1029 |
|
---|
[152] | 1030 | def _handleMessageSent(self, success, disconnect):
|
---|
[133] | 1031 | """Callback for a message sending request."""
|
---|
[152] | 1032 | #print "fsuipc.Simulator._handleMessageSent", disconnect
|
---|
| 1033 | if disconnect:
|
---|
| 1034 | self._handler.disconnect()
|
---|
[141] | 1035 |
|
---|
[168] | 1036 | def _handleNumHotkeys(self, data, (id, generation)):
|
---|
| 1037 | """Handle the result of the query of the number of hotkeys"""
|
---|
| 1038 | with self._hotkeyLock:
|
---|
| 1039 | if id==self._hotkeySetID and generation==self._hotkeySetGeneration:
|
---|
| 1040 | numHotkeys = data[0]
|
---|
| 1041 | print "fsuipc.Simulator._handleNumHotkeys: numHotkeys:", numHotkeys
|
---|
[330] | 1042 | data = [(0x3210 + i*4, "d") for i in range(0, numHotkeys)]
|
---|
[168] | 1043 | self._handler.requestRead(data, self._handleHotkeyTable,
|
---|
| 1044 | (id, generation))
|
---|
| 1045 |
|
---|
| 1046 | def _setupHotkeys(self, data):
|
---|
| 1047 | """Setup the hiven hotkeys and return the data to be written.
|
---|
| 1048 |
|
---|
| 1049 | If there were hotkeys set previously, they are reused as much as
|
---|
| 1050 | possible. Any of them not reused will be cleared."""
|
---|
| 1051 | hotkeys = self._hotkeys
|
---|
| 1052 | numHotkeys = len(hotkeys)
|
---|
| 1053 |
|
---|
| 1054 | oldHotkeyOffsets = set([] if self._hotkeyOffets is None else
|
---|
| 1055 | self._hotkeyOffets)
|
---|
| 1056 |
|
---|
| 1057 | self._hotkeyOffets = []
|
---|
| 1058 | numOffsets = 0
|
---|
| 1059 |
|
---|
| 1060 | while oldHotkeyOffsets:
|
---|
| 1061 | offset = oldHotkeyOffsets.pop()
|
---|
| 1062 | self._hotkeyOffets.append(offset)
|
---|
| 1063 | numOffsets += 1
|
---|
| 1064 |
|
---|
| 1065 | if numOffsets>=numHotkeys:
|
---|
| 1066 | break
|
---|
| 1067 |
|
---|
| 1068 | for i in range(0, len(data)):
|
---|
| 1069 | if numOffsets>=numHotkeys:
|
---|
| 1070 | break
|
---|
| 1071 |
|
---|
| 1072 | if data[i]==0:
|
---|
| 1073 | self._hotkeyOffets.append(0x3210 + i*4)
|
---|
| 1074 | numOffsets += 1
|
---|
| 1075 |
|
---|
| 1076 | writeData = []
|
---|
| 1077 | for i in range(0, numOffsets):
|
---|
| 1078 | Simulator._appendHotkeyData(writeData,
|
---|
| 1079 | self._hotkeyOffets[i],
|
---|
| 1080 | hotkeys[i])
|
---|
| 1081 |
|
---|
| 1082 | for offset in oldHotkeyOffsets:
|
---|
| 1083 | writeData.append((offset, "u", long(0)))
|
---|
| 1084 |
|
---|
| 1085 | return writeData
|
---|
| 1086 |
|
---|
| 1087 | def _handleHotkeyTable(self, data, (id, generation)):
|
---|
| 1088 | """Handle the result of the query of the hotkey table."""
|
---|
| 1089 | with self._hotkeyLock:
|
---|
| 1090 | if id==self._hotkeySetID and generation==self._hotkeySetGeneration:
|
---|
| 1091 | writeData = self._setupHotkeys(data)
|
---|
| 1092 | self._handler.requestWrite(writeData,
|
---|
| 1093 | self._handleHotkeysWritten,
|
---|
| 1094 | (id, generation))
|
---|
[330] | 1095 |
|
---|
[168] | 1096 | def _handleHotkeysWritten(self, success, (id, generation)):
|
---|
| 1097 | """Handle the result of the hotkeys having been written."""
|
---|
[330] | 1098 | with self._hotkeyLock:
|
---|
[168] | 1099 | if success and id==self._hotkeySetID and \
|
---|
| 1100 | generation==self._hotkeySetGeneration:
|
---|
| 1101 | data = [(offset + 3, "b") for offset in self._hotkeyOffets]
|
---|
[330] | 1102 |
|
---|
[168] | 1103 | self._hotkeyRequestID = \
|
---|
| 1104 | self._handler.requestPeriodicRead(0.5, data,
|
---|
| 1105 | self._handleHotkeys,
|
---|
| 1106 | (id, generation))
|
---|
| 1107 |
|
---|
| 1108 | def _handleHotkeys(self, data, (id, generation)):
|
---|
[330] | 1109 | """Handle the hotkeys."""
|
---|
[168] | 1110 | with self._hotkeyLock:
|
---|
| 1111 | if id!=self._hotkeySetID or generation!=self._hotkeySetGeneration:
|
---|
| 1112 | return
|
---|
| 1113 |
|
---|
| 1114 | callback = self._hotkeyCallback
|
---|
| 1115 | offsets = self._hotkeyOffets
|
---|
| 1116 |
|
---|
| 1117 | hotkeysPressed = []
|
---|
| 1118 | for i in range(0, len(data)):
|
---|
| 1119 | if data[i]!=0:
|
---|
| 1120 | hotkeysPressed.append(i)
|
---|
| 1121 |
|
---|
| 1122 | if hotkeysPressed:
|
---|
| 1123 | data = []
|
---|
| 1124 | for index in hotkeysPressed:
|
---|
| 1125 | data.append((offsets[index]+3, "b", int(0)))
|
---|
| 1126 | self._handler.requestWrite(data, self._handleHotkeysCleared)
|
---|
| 1127 |
|
---|
| 1128 | callback(id, hotkeysPressed)
|
---|
| 1129 |
|
---|
| 1130 | def _handleHotkeysCleared(self, sucess, extra):
|
---|
[330] | 1131 | """Callback for the hotkey-clearing write request."""
|
---|
[168] | 1132 |
|
---|
| 1133 | def _clearHotkeyRequest(self):
|
---|
| 1134 | """Clear the hotkey request in the handler if there is any."""
|
---|
| 1135 | if self._hotkeyRequestID is not None:
|
---|
| 1136 | self._handler.clearPeriodic(self._hotkeyRequestID)
|
---|
| 1137 | self._hotkeyRequestID = None
|
---|
[330] | 1138 |
|
---|
[4] | 1139 | #------------------------------------------------------------------------------
|
---|
| 1140 |
|
---|
| 1141 | class AircraftModel(object):
|
---|
| 1142 | """Base class for the aircraft models.
|
---|
| 1143 |
|
---|
| 1144 | Aircraft models handle the data arriving from FSUIPC and turn it into an
|
---|
| 1145 | object describing the aircraft's state."""
|
---|
[8] | 1146 | monitoringData = [("paused", 0x0264, "H"),
|
---|
[89] | 1147 | ("latitude", 0x0560, "l"),
|
---|
| 1148 | ("longitude", 0x0568, "l"),
|
---|
[4] | 1149 | ("frozen", 0x3364, "H"),
|
---|
[5] | 1150 | ("replay", 0x0628, "d"),
|
---|
[4] | 1151 | ("slew", 0x05dc, "H"),
|
---|
| 1152 | ("overspeed", 0x036d, "b"),
|
---|
| 1153 | ("stalled", 0x036c, "b"),
|
---|
| 1154 | ("onTheGround", 0x0366, "H"),
|
---|
[9] | 1155 | ("zfw", 0x3bfc, "d"),
|
---|
[4] | 1156 | ("grossWeight", 0x30c0, "f"),
|
---|
| 1157 | ("heading", 0x0580, "d"),
|
---|
| 1158 | ("pitch", 0x0578, "d"),
|
---|
| 1159 | ("bank", 0x057c, "d"),
|
---|
| 1160 | ("ias", 0x02bc, "d"),
|
---|
[9] | 1161 | ("mach", 0x11c6, "H"),
|
---|
[5] | 1162 | ("groundSpeed", 0x02b4, "d"),
|
---|
[4] | 1163 | ("vs", 0x02c8, "d"),
|
---|
[8] | 1164 | ("radioAltitude", 0x31e4, "d"),
|
---|
[4] | 1165 | ("altitude", 0x0570, "l"),
|
---|
[5] | 1166 | ("gLoad", 0x11ba, "H"),
|
---|
[4] | 1167 | ("flapsControl", 0x0bdc, "d"),
|
---|
| 1168 | ("flapsLeft", 0x0be0, "d"),
|
---|
| 1169 | ("flapsRight", 0x0be4, "d"),
|
---|
| 1170 | ("lights", 0x0d0c, "H"),
|
---|
| 1171 | ("pitot", 0x029c, "b"),
|
---|
[8] | 1172 | ("parking", 0x0bc8, "H"),
|
---|
[209] | 1173 | ("gearControl", 0x0be8, "d"),
|
---|
[4] | 1174 | ("noseGear", 0x0bec, "d"),
|
---|
| 1175 | ("spoilersArmed", 0x0bcc, "d"),
|
---|
[5] | 1176 | ("spoilers", 0x0bd0, "d"),
|
---|
| 1177 | ("altimeter", 0x0330, "H"),
|
---|
| 1178 | ("nav1", 0x0350, "H"),
|
---|
[320] | 1179 | ("nav1_obs", 0x0c4e, "H"),
|
---|
[8] | 1180 | ("nav2", 0x0352, "H"),
|
---|
[321] | 1181 | ("nav2_obs", 0x0c5e, "H"),
|
---|
[317] | 1182 | ("adf1_main", 0x034c, "H"),
|
---|
| 1183 | ("adf1_ext", 0x0356, "H"),
|
---|
| 1184 | ("adf2_main", 0x02d4, "H"),
|
---|
| 1185 | ("adf2_ext", 0x02d6, "H"),
|
---|
[9] | 1186 | ("squawk", 0x0354, "H"),
|
---|
| 1187 | ("windSpeed", 0x0e90, "H"),
|
---|
[134] | 1188 | ("windDirection", 0x0e92, "H"),
|
---|
[243] | 1189 | ("visibility", 0x0e8a, "H"),
|
---|
| 1190 | ("cog", 0x2ef8, "f")]
|
---|
[7] | 1191 |
|
---|
| 1192 | specialModels = []
|
---|
| 1193 |
|
---|
| 1194 | @staticmethod
|
---|
| 1195 | def registerSpecial(clazz):
|
---|
| 1196 | """Register the given class as a special model."""
|
---|
| 1197 | AircraftModel.specialModels.append(clazz)
|
---|
| 1198 |
|
---|
[4] | 1199 | @staticmethod
|
---|
[8] | 1200 | def findSpecial(aircraft, aircraftName):
|
---|
| 1201 | for specialModel in AircraftModel.specialModels:
|
---|
| 1202 | if specialModel.doesHandle(aircraft, aircraftName):
|
---|
| 1203 | return specialModel
|
---|
| 1204 | return None
|
---|
| 1205 |
|
---|
| 1206 | @staticmethod
|
---|
[4] | 1207 | def create(aircraft, aircraftName):
|
---|
| 1208 | """Create the model for the given aircraft name, and notify the
|
---|
[7] | 1209 | aircraft about it."""
|
---|
[8] | 1210 | specialModel = AircraftModel.findSpecial(aircraft, aircraftName)
|
---|
| 1211 | if specialModel is not None:
|
---|
| 1212 | return specialModel()
|
---|
| 1213 | if aircraft.type in _genericModels:
|
---|
| 1214 | return _genericModels[aircraft.type]()
|
---|
[7] | 1215 | else:
|
---|
| 1216 | return GenericModel()
|
---|
[4] | 1217 |
|
---|
[5] | 1218 | @staticmethod
|
---|
[8] | 1219 | def convertBCD(data, length):
|
---|
| 1220 | """Convert a data item encoded as BCD into a string of the given number
|
---|
| 1221 | of digits."""
|
---|
| 1222 | bcd = ""
|
---|
| 1223 | for i in range(0, length):
|
---|
| 1224 | digit = chr(ord('0') + (data&0x0f))
|
---|
| 1225 | data >>= 4
|
---|
| 1226 | bcd = digit + bcd
|
---|
| 1227 | return bcd
|
---|
| 1228 |
|
---|
| 1229 | @staticmethod
|
---|
[5] | 1230 | def convertFrequency(data):
|
---|
| 1231 | """Convert the given frequency data to a string."""
|
---|
[8] | 1232 | bcd = AircraftModel.convertBCD(data, 4)
|
---|
| 1233 | return "1" + bcd[0:2] + "." + bcd[2:4]
|
---|
[5] | 1234 |
|
---|
[317] | 1235 | @staticmethod
|
---|
| 1236 | def convertADFFrequency(main, ext):
|
---|
| 1237 | """Convert the given ADF frequency data to a string."""
|
---|
| 1238 | mainBCD = AircraftModel.convertBCD(main, 4)
|
---|
| 1239 | extBCD = AircraftModel.convertBCD(ext, 4)
|
---|
| 1240 |
|
---|
| 1241 | return (extBCD[1] if extBCD[1]!="0" else "") + \
|
---|
| 1242 | mainBCD[1:] + "." + extBCD[3]
|
---|
[330] | 1243 |
|
---|
[4] | 1244 | def __init__(self, flapsNotches):
|
---|
| 1245 | """Construct the aircraft model.
|
---|
[330] | 1246 |
|
---|
[4] | 1247 | flapsNotches is a list of degrees of flaps that are available on the aircraft."""
|
---|
| 1248 | self._flapsNotches = flapsNotches
|
---|
[330] | 1249 |
|
---|
[4] | 1250 | @property
|
---|
| 1251 | def name(self):
|
---|
| 1252 | """Get the name for this aircraft model."""
|
---|
| 1253 | return "FSUIPC/Generic"
|
---|
[330] | 1254 |
|
---|
[8] | 1255 | def doesHandle(self, aircraft, aircraftName):
|
---|
[4] | 1256 | """Determine if the model handles the given aircraft name.
|
---|
[330] | 1257 |
|
---|
[7] | 1258 | This default implementation returns False."""
|
---|
| 1259 | return False
|
---|
[4] | 1260 |
|
---|
[7] | 1261 | def _addOffsetWithIndexMember(self, dest, offset, type, attrName = None):
|
---|
| 1262 | """Add the given FSUIPC offset and type to the given array and a member
|
---|
[330] | 1263 | attribute with the given name."""
|
---|
[7] | 1264 | dest.append((offset, type))
|
---|
| 1265 | if attrName is not None:
|
---|
[8] | 1266 | setattr(self, attrName, len(dest)-1)
|
---|
[7] | 1267 |
|
---|
| 1268 | def _addDataWithIndexMembers(self, dest, prefix, data):
|
---|
[4] | 1269 | """Add FSUIPC data to the given array and also corresponding index
|
---|
| 1270 | member variables with the given prefix.
|
---|
| 1271 |
|
---|
| 1272 | data is a list of triplets of the following items:
|
---|
| 1273 | - the name of the data item. The index member variable will have a name
|
---|
| 1274 | created by prepending the given prefix to this name.
|
---|
| 1275 | - the FSUIPC offset
|
---|
| 1276 | - the FSUIPC type
|
---|
[330] | 1277 |
|
---|
[4] | 1278 | The latter two items will be appended to dest."""
|
---|
| 1279 | for (name, offset, type) in data:
|
---|
[7] | 1280 | self._addOffsetWithIndexMember(dest, offset, type, prefix + name)
|
---|
[330] | 1281 |
|
---|
[212] | 1282 | def addMonitoringData(self, data, fsType):
|
---|
[7] | 1283 | """Add the model-specific monitoring data to the given array."""
|
---|
[8] | 1284 | self._addDataWithIndexMembers(data, "_monidx_",
|
---|
| 1285 | AircraftModel.monitoringData)
|
---|
[330] | 1286 |
|
---|
[8] | 1287 | def getAircraftState(self, aircraft, timestamp, data):
|
---|
[4] | 1288 | """Get an aircraft state object for the given monitoring data."""
|
---|
| 1289 | state = fs.AircraftState()
|
---|
[330] | 1290 |
|
---|
[4] | 1291 | state.timestamp = timestamp
|
---|
[89] | 1292 |
|
---|
| 1293 | state.latitude = data[self._monidx_latitude] * \
|
---|
| 1294 | 90.0 / 10001750.0 / 65536.0 / 65536.0
|
---|
| 1295 |
|
---|
| 1296 | state.longitude = data[self._monidx_longitude] * \
|
---|
| 1297 | 360.0 / 65536.0 / 65536.0 / 65536.0 / 65536.0
|
---|
| 1298 | if state.longitude>180.0: state.longitude = 360.0 - state.longitude
|
---|
[330] | 1299 |
|
---|
[4] | 1300 | state.paused = data[self._monidx_paused]!=0 or \
|
---|
[5] | 1301 | data[self._monidx_frozen]!=0 or \
|
---|
| 1302 | data[self._monidx_replay]!=0
|
---|
[4] | 1303 | state.trickMode = data[self._monidx_slew]!=0
|
---|
| 1304 |
|
---|
| 1305 | state.overspeed = data[self._monidx_overspeed]!=0
|
---|
| 1306 | state.stalled = data[self._monidx_stalled]!=0
|
---|
| 1307 | state.onTheGround = data[self._monidx_onTheGround]!=0
|
---|
| 1308 |
|
---|
[9] | 1309 | state.zfw = data[self._monidx_zfw] * const.LBSTOKG / 256.0
|
---|
[6] | 1310 | state.grossWeight = data[self._monidx_grossWeight] * const.LBSTOKG
|
---|
[330] | 1311 |
|
---|
[9] | 1312 | state.heading = Handler.fsuipc2PositiveDegrees(data[self._monidx_heading])
|
---|
[4] | 1313 |
|
---|
[9] | 1314 | state.pitch = Handler.fsuipc2Degrees(data[self._monidx_pitch])
|
---|
| 1315 | state.bank = Handler.fsuipc2Degrees(data[self._monidx_bank])
|
---|
[4] | 1316 |
|
---|
[9] | 1317 | state.ias = Handler.fsuipc2IAS(data[self._monidx_ias])
|
---|
| 1318 | state.mach = data[self._monidx_mach] / 20480.0
|
---|
[5] | 1319 | state.groundSpeed = data[self._monidx_groundSpeed]* 3600.0/65536.0/1852.0
|
---|
[9] | 1320 | state.vs = Handler.fsuipc2VS(data[self._monidx_vs])
|
---|
[4] | 1321 |
|
---|
[9] | 1322 | state.radioAltitude = \
|
---|
| 1323 | Handler.fsuipc2radioAltitude(data[self._monidx_radioAltitude])
|
---|
[6] | 1324 | state.altitude = data[self._monidx_altitude]/const.FEETTOMETRES/65536.0/65536.0
|
---|
[5] | 1325 |
|
---|
| 1326 | state.gLoad = data[self._monidx_gLoad] / 625.0
|
---|
[330] | 1327 |
|
---|
[4] | 1328 | numNotchesM1 = len(self._flapsNotches) - 1
|
---|
| 1329 | flapsIncrement = 16383 / numNotchesM1
|
---|
| 1330 | flapsControl = data[self._monidx_flapsControl]
|
---|
| 1331 | flapsIndex = flapsControl / flapsIncrement
|
---|
| 1332 | if flapsIndex < numNotchesM1:
|
---|
| 1333 | if (flapsControl - (flapsIndex*flapsIncrement) >
|
---|
| 1334 | (flapsIndex+1)*flapsIncrement - flapsControl):
|
---|
| 1335 | flapsIndex += 1
|
---|
| 1336 | state.flapsSet = self._flapsNotches[flapsIndex]
|
---|
[330] | 1337 |
|
---|
[4] | 1338 | flapsLeft = data[self._monidx_flapsLeft]
|
---|
[16] | 1339 | state.flaps = self._flapsNotches[-1]*flapsLeft/16383.0
|
---|
[330] | 1340 |
|
---|
[4] | 1341 | lights = data[self._monidx_lights]
|
---|
[330] | 1342 |
|
---|
[5] | 1343 | state.navLightsOn = (lights&0x01) != 0
|
---|
| 1344 | state.antiCollisionLightsOn = (lights&0x02) != 0
|
---|
| 1345 | state.landingLightsOn = (lights&0x04) != 0
|
---|
| 1346 | state.strobeLightsOn = (lights&0x10) != 0
|
---|
[330] | 1347 |
|
---|
[4] | 1348 | state.pitotHeatOn = data[self._monidx_pitot]!=0
|
---|
| 1349 |
|
---|
[8] | 1350 | state.parking = data[self._monidx_parking]!=0
|
---|
| 1351 |
|
---|
[209] | 1352 | state.gearControlDown = data[self._monidx_gearControl]==16383
|
---|
[4] | 1353 | state.gearsDown = data[self._monidx_noseGear]==16383
|
---|
| 1354 |
|
---|
| 1355 | state.spoilersArmed = data[self._monidx_spoilersArmed]!=0
|
---|
[330] | 1356 |
|
---|
[4] | 1357 | spoilers = data[self._monidx_spoilers]
|
---|
| 1358 | if spoilers<=4800:
|
---|
| 1359 | state.spoilersExtension = 0.0
|
---|
| 1360 | else:
|
---|
| 1361 | state.spoilersExtension = (spoilers - 4800) * 100.0 / (16383 - 4800)
|
---|
[5] | 1362 |
|
---|
| 1363 | state.altimeter = data[self._monidx_altimeter] / 16.0
|
---|
[330] | 1364 |
|
---|
[5] | 1365 | state.nav1 = AircraftModel.convertFrequency(data[self._monidx_nav1])
|
---|
[320] | 1366 | state.nav1_obs = data[self._monidx_nav1_obs]
|
---|
[321] | 1367 | state.nav1_manual = True
|
---|
[5] | 1368 | state.nav2 = AircraftModel.convertFrequency(data[self._monidx_nav2])
|
---|
[320] | 1369 | state.nav2_obs = data[self._monidx_nav2_obs]
|
---|
[321] | 1370 | state.nav2_manual = True
|
---|
[317] | 1371 | state.adf1 = \
|
---|
| 1372 | AircraftModel.convertADFFrequency(data[self._monidx_adf1_main],
|
---|
| 1373 | data[self._monidx_adf1_ext])
|
---|
| 1374 | state.adf2 = \
|
---|
| 1375 | AircraftModel.convertADFFrequency(data[self._monidx_adf2_main],
|
---|
| 1376 | data[self._monidx_adf2_ext])
|
---|
[8] | 1377 | state.squawk = AircraftModel.convertBCD(data[self._monidx_squawk], 4)
|
---|
[9] | 1378 |
|
---|
| 1379 | state.windSpeed = data[self._monidx_windSpeed]
|
---|
| 1380 | state.windDirection = data[self._monidx_windDirection]*360.0/65536.0
|
---|
| 1381 | if state.windDirection<0.0: state.windDirection += 360.0
|
---|
[134] | 1382 |
|
---|
| 1383 | state.visibility = data[self._monidx_visibility]*1609.344/100.0
|
---|
[330] | 1384 |
|
---|
[243] | 1385 | state.cog = data[self._monidx_cog]
|
---|
[330] | 1386 |
|
---|
[4] | 1387 | return state
|
---|
[5] | 1388 |
|
---|
| 1389 | #------------------------------------------------------------------------------
|
---|
| 1390 |
|
---|
[7] | 1391 | class GenericAircraftModel(AircraftModel):
|
---|
| 1392 | """A generic aircraft model that can handle the fuel levels, the N1 or RPM
|
---|
| 1393 | values and some other common parameters in a generic way."""
|
---|
[330] | 1394 |
|
---|
[140] | 1395 | def __init__(self, flapsNotches, fuelTanks, numEngines, isN1 = True):
|
---|
[7] | 1396 | """Construct the generic aircraft model with the given data.
|
---|
| 1397 |
|
---|
| 1398 | flapsNotches is an array of how much degrees the individual flaps
|
---|
| 1399 | notches mean.
|
---|
| 1400 |
|
---|
[140] | 1401 | fuelTanks is an array of const.FUELTANK_XXX constants about the
|
---|
| 1402 | aircraft's fuel tanks. They will be converted to offsets.
|
---|
[7] | 1403 |
|
---|
| 1404 | numEngines is the number of engines the aircraft has.
|
---|
| 1405 |
|
---|
| 1406 | isN1 determines if the engines have an N1 value or an RPM value
|
---|
| 1407 | (e.g. pistons)."""
|
---|
| 1408 | super(GenericAircraftModel, self).__init__(flapsNotches = flapsNotches)
|
---|
| 1409 |
|
---|
[140] | 1410 | self._fuelTanks = fuelTanks
|
---|
[7] | 1411 | self._fuelStartIndex = None
|
---|
| 1412 | self._numEngines = numEngines
|
---|
| 1413 | self._engineStartIndex = None
|
---|
| 1414 | self._isN1 = isN1
|
---|
| 1415 |
|
---|
[8] | 1416 | def doesHandle(self, aircraft, aircraftName):
|
---|
| 1417 | """Determine if the model handles the given aircraft name.
|
---|
[330] | 1418 |
|
---|
[8] | 1419 | This implementation returns True."""
|
---|
| 1420 | return True
|
---|
| 1421 |
|
---|
[212] | 1422 | def addMonitoringData(self, data, fsType):
|
---|
[7] | 1423 | """Add the model-specific monitoring data to the given array."""
|
---|
[212] | 1424 | super(GenericAircraftModel, self).addMonitoringData(data, fsType)
|
---|
[330] | 1425 |
|
---|
[274] | 1426 | self._fuelStartIndex = self._addFuelOffsets(data, "_monidx_fuelWeight")
|
---|
[7] | 1427 |
|
---|
[263] | 1428 | self._engineStartIndex = len(data)
|
---|
| 1429 | for i in range(0, self._numEngines):
|
---|
| 1430 | self._addOffsetWithIndexMember(data, 0x088c + i * 0x98, "h") # throttle lever
|
---|
| 1431 | if self._isN1:
|
---|
[23] | 1432 | self._addOffsetWithIndexMember(data, 0x2000 + i * 0x100, "f") # N1
|
---|
[263] | 1433 | else:
|
---|
| 1434 | self._addOffsetWithIndexMember(data, 0x0898 + i * 0x98, "H") # RPM
|
---|
| 1435 | self._addOffsetWithIndexMember(data, 0x08c8 + i * 0x98, "H") # RPM scaler
|
---|
[330] | 1436 |
|
---|
[8] | 1437 | def getAircraftState(self, aircraft, timestamp, data):
|
---|
[7] | 1438 | """Get the aircraft state.
|
---|
| 1439 |
|
---|
[330] | 1440 | Get it from the parent, and then add the data about the fuel levels and
|
---|
[7] | 1441 | the engine parameters."""
|
---|
| 1442 | state = super(GenericAircraftModel, self).getAircraftState(aircraft,
|
---|
[8] | 1443 | timestamp,
|
---|
[7] | 1444 | data)
|
---|
| 1445 |
|
---|
[274] | 1446 | (state.fuel, state.totalFuel) = \
|
---|
| 1447 | self._convertFuelData(data, index = self._monidx_fuelWeight)
|
---|
[263] | 1448 |
|
---|
| 1449 | state.n1 = [] if self._isN1 else None
|
---|
| 1450 | state.rpm = None if self._isN1 else []
|
---|
| 1451 | itemsPerEngine = 2 if self._isN1 else 3
|
---|
[330] | 1452 |
|
---|
[7] | 1453 | state.reverser = []
|
---|
| 1454 | for i in range(self._engineStartIndex,
|
---|
[263] | 1455 | self._engineStartIndex +
|
---|
| 1456 | itemsPerEngine*self._numEngines,
|
---|
| 1457 | itemsPerEngine):
|
---|
| 1458 | state.reverser.append(data[i]<0)
|
---|
| 1459 | if self._isN1:
|
---|
| 1460 | state.n1.append(data[i+1])
|
---|
| 1461 | else:
|
---|
| 1462 | state.rpm.append(data[i+1] * data[i+2]/65536.0)
|
---|
[7] | 1463 |
|
---|
| 1464 | return state
|
---|
| 1465 |
|
---|
[274] | 1466 | def getFuel(self, handler, callback):
|
---|
| 1467 | """Get the fuel information for this model.
|
---|
| 1468 |
|
---|
| 1469 | See Simulator.getFuel for more information. This
|
---|
| 1470 | implementation simply queries the fuel tanks given to the
|
---|
| 1471 | constructor."""
|
---|
| 1472 | data = []
|
---|
| 1473 | self._addFuelOffsets(data)
|
---|
| 1474 |
|
---|
| 1475 | handler.requestRead(data, self._handleFuelRetrieved,
|
---|
| 1476 | extra = callback)
|
---|
| 1477 |
|
---|
| 1478 | def setFuelLevel(self, handler, levels):
|
---|
| 1479 | """Set the fuel level.
|
---|
| 1480 |
|
---|
| 1481 | See the description of Simulator.setFuelLevel. This
|
---|
| 1482 | implementation simply sets the fuel tanks as given."""
|
---|
| 1483 | data = []
|
---|
| 1484 | for (tank, level) in levels:
|
---|
| 1485 | offset = _tank2offset[tank]
|
---|
| 1486 | value = long(level * 128.0 * 65536.0)
|
---|
| 1487 | data.append( (offset, "u", value) )
|
---|
| 1488 |
|
---|
| 1489 | handler.requestWrite(data, self._handleFuelWritten)
|
---|
| 1490 |
|
---|
| 1491 | def _addFuelOffsets(self, data, weightIndexName = None):
|
---|
| 1492 | """Add the fuel offsets to the given data array.
|
---|
| 1493 |
|
---|
| 1494 | If weightIndexName is not None, it will be the name of the
|
---|
| 1495 | fuel weight index.
|
---|
| 1496 |
|
---|
| 1497 | Returns the index of the first fuel tank's data."""
|
---|
| 1498 | self._addOffsetWithIndexMember(data, 0x0af4, "H", weightIndexName)
|
---|
| 1499 |
|
---|
| 1500 | fuelStartIndex = len(data)
|
---|
| 1501 | for tank in self._fuelTanks:
|
---|
| 1502 | offset = _tank2offset[tank]
|
---|
| 1503 | self._addOffsetWithIndexMember(data, offset, "u") # tank level
|
---|
| 1504 | self._addOffsetWithIndexMember(data, offset+4, "u") # tank capacity
|
---|
[330] | 1505 |
|
---|
[274] | 1506 | return fuelStartIndex
|
---|
| 1507 |
|
---|
| 1508 | def _convertFuelData(self, data, index = 0, addCapacities = False):
|
---|
| 1509 | """Convert the given data into a fuel info list.
|
---|
| 1510 |
|
---|
| 1511 | The list consists of two or three-tuples of the following
|
---|
| 1512 | items:
|
---|
| 1513 | - the fuel tank ID,
|
---|
| 1514 | - the amount of the fuel in kg,
|
---|
| 1515 | - if addCapacities is True, the total capacity of the tank."""
|
---|
| 1516 | fuelWeight = data[index] / 256.0
|
---|
| 1517 | index += 1
|
---|
| 1518 |
|
---|
| 1519 | result = []
|
---|
| 1520 | totalFuel = 0
|
---|
| 1521 | for fuelTank in self._fuelTanks:
|
---|
| 1522 | capacity = data[index+1] * fuelWeight * const.LBSTOKG
|
---|
| 1523 | amount = data[index] * capacity / 128.0 / 65536.0
|
---|
| 1524 | index += 2
|
---|
[330] | 1525 |
|
---|
| 1526 | result.append( (fuelTank, amount, capacity) if addCapacities
|
---|
[274] | 1527 | else (fuelTank, amount))
|
---|
| 1528 | totalFuel += amount
|
---|
| 1529 |
|
---|
[330] | 1530 | return (result, totalFuel)
|
---|
[274] | 1531 |
|
---|
| 1532 | def _handleFuelRetrieved(self, data, callback):
|
---|
| 1533 | """Callback for a fuel retrieval request."""
|
---|
[330] | 1534 | (fuelData, _totalFuel) = self._convertFuelData(data,
|
---|
[274] | 1535 | addCapacities = True)
|
---|
| 1536 | callback(fuelData)
|
---|
[330] | 1537 |
|
---|
[274] | 1538 | def _handleFuelWritten(self, success, extra):
|
---|
| 1539 | """Callback for a fuel setting request."""
|
---|
| 1540 | pass
|
---|
| 1541 |
|
---|
[7] | 1542 | #------------------------------------------------------------------------------
|
---|
| 1543 |
|
---|
| 1544 | class GenericModel(GenericAircraftModel):
|
---|
| 1545 | """Generic aircraft model for an unknown type."""
|
---|
| 1546 | def __init__(self):
|
---|
| 1547 | """Construct the model."""
|
---|
[8] | 1548 | super(GenericModel, self). \
|
---|
[7] | 1549 | __init__(flapsNotches = [0, 10, 20, 30],
|
---|
[140] | 1550 | fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_RIGHT],
|
---|
[7] | 1551 | numEngines = 2)
|
---|
| 1552 |
|
---|
| 1553 | @property
|
---|
| 1554 | def name(self):
|
---|
| 1555 | """Get the name for this aircraft model."""
|
---|
[330] | 1556 | return "FSUIPC/Generic"
|
---|
[7] | 1557 |
|
---|
| 1558 | #------------------------------------------------------------------------------
|
---|
| 1559 |
|
---|
| 1560 | class B737Model(GenericAircraftModel):
|
---|
| 1561 | """Generic model for the Boeing 737 Classing and NG aircraft."""
|
---|
[330] | 1562 | fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_CENTRE, const.FUELTANK_RIGHT]
|
---|
| 1563 |
|
---|
[7] | 1564 | def __init__(self):
|
---|
| 1565 | """Construct the model."""
|
---|
[8] | 1566 | super(B737Model, self). \
|
---|
[7] | 1567 | __init__(flapsNotches = [0, 1, 2, 5, 10, 15, 25, 30, 40],
|
---|
[274] | 1568 | fuelTanks = B737Model.fuelTanks,
|
---|
[7] | 1569 | numEngines = 2)
|
---|
| 1570 |
|
---|
| 1571 | @property
|
---|
| 1572 | def name(self):
|
---|
| 1573 | """Get the name for this aircraft model."""
|
---|
| 1574 | return "FSUIPC/Generic Boeing 737"
|
---|
| 1575 |
|
---|
| 1576 | #------------------------------------------------------------------------------
|
---|
| 1577 |
|
---|
[8] | 1578 | class PMDGBoeing737NGModel(B737Model):
|
---|
| 1579 | """A model handler for the PMDG Boeing 737NG model."""
|
---|
| 1580 | @staticmethod
|
---|
| 1581 | def doesHandle(aircraft, (name, airPath)):
|
---|
| 1582 | """Determine if this model handler handles the aircraft with the given
|
---|
| 1583 | name."""
|
---|
| 1584 | return aircraft.type in [const.AIRCRAFT_B736,
|
---|
| 1585 | const.AIRCRAFT_B737,
|
---|
[191] | 1586 | const.AIRCRAFT_B738,
|
---|
| 1587 | const.AIRCRAFT_B738C] and \
|
---|
[8] | 1588 | (name.find("PMDG")!=-1 or airPath.find("PMDG")!=-1) and \
|
---|
| 1589 | (name.find("737")!=-1 or airPath.find("737")!=-1) and \
|
---|
| 1590 | (name.find("600")!=-1 or airPath.find("600")!=-1 or \
|
---|
| 1591 | name.find("700")!=-1 or airPath.find("700")!=-1 or \
|
---|
| 1592 | name.find("800")!=-1 or airPath.find("800")!=-1 or \
|
---|
| 1593 | name.find("900")!=-1 or airPath.find("900")!=-1)
|
---|
| 1594 |
|
---|
| 1595 | @property
|
---|
| 1596 | def name(self):
|
---|
| 1597 | """Get the name for this aircraft model."""
|
---|
[212] | 1598 | return "FSUIPC/PMDG Boeing 737NG(X)"
|
---|
[8] | 1599 |
|
---|
[212] | 1600 | def addMonitoringData(self, data, fsType):
|
---|
[8] | 1601 | """Add the model-specific monitoring data to the given array."""
|
---|
[212] | 1602 | self._fsType = fsType
|
---|
[330] | 1603 |
|
---|
[212] | 1604 | super(PMDGBoeing737NGModel, self).addMonitoringData(data, fsType)
|
---|
[330] | 1605 |
|
---|
[8] | 1606 | self._addOffsetWithIndexMember(data, 0x6202, "b", "_pmdgidx_switches")
|
---|
| 1607 |
|
---|
[212] | 1608 | if fsType==const.SIM_MSFSX:
|
---|
| 1609 | print "FSX detected, adding position lights switch offset"
|
---|
| 1610 | self._addOffsetWithIndexMember(data, 0x6500, "b",
|
---|
| 1611 | "_pmdgidx_lts_positionsw")
|
---|
| 1612 |
|
---|
[8] | 1613 | def getAircraftState(self, aircraft, timestamp, data):
|
---|
| 1614 | """Get the aircraft state.
|
---|
| 1615 |
|
---|
| 1616 | Get it from the parent, and then check some PMDG-specific stuff."""
|
---|
| 1617 | state = super(PMDGBoeing737NGModel, self).getAircraftState(aircraft,
|
---|
| 1618 | timestamp,
|
---|
| 1619 | data)
|
---|
| 1620 | if data[self._pmdgidx_switches]&0x01==0x01:
|
---|
| 1621 | state.altimeter = 1013.25
|
---|
| 1622 |
|
---|
[212] | 1623 | if self._fsType==const.SIM_MSFSX:
|
---|
| 1624 | state.strobeLightsOn = data[self._pmdgidx_lts_positionsw]==0x02
|
---|
| 1625 |
|
---|
[8] | 1626 | return state
|
---|
| 1627 |
|
---|
| 1628 | #------------------------------------------------------------------------------
|
---|
| 1629 |
|
---|
[7] | 1630 | class B767Model(GenericAircraftModel):
|
---|
| 1631 | """Generic model for the Boeing 767 aircraft."""
|
---|
[330] | 1632 | fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_CENTRE, const.FUELTANK_RIGHT]
|
---|
[274] | 1633 |
|
---|
[7] | 1634 | def __init__(self):
|
---|
| 1635 | """Construct the model."""
|
---|
[8] | 1636 | super(B767Model, self). \
|
---|
[7] | 1637 | __init__(flapsNotches = [0, 1, 5, 15, 20, 25, 30],
|
---|
[274] | 1638 | fuelTanks = Boeing767Model.fuelTanks,
|
---|
[7] | 1639 | numEngines = 2)
|
---|
| 1640 |
|
---|
| 1641 | @property
|
---|
| 1642 | def name(self):
|
---|
| 1643 | """Get the name for this aircraft model."""
|
---|
| 1644 | return "FSUIPC/Generic Boeing 767"
|
---|
| 1645 |
|
---|
| 1646 | #------------------------------------------------------------------------------
|
---|
| 1647 |
|
---|
| 1648 | class DH8DModel(GenericAircraftModel):
|
---|
[16] | 1649 | """Generic model for the Bombardier Dash 8-Q400 aircraft."""
|
---|
[330] | 1650 | fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_RIGHT]
|
---|
| 1651 |
|
---|
[7] | 1652 | def __init__(self):
|
---|
| 1653 | """Construct the model."""
|
---|
[8] | 1654 | super(DH8DModel, self). \
|
---|
[7] | 1655 | __init__(flapsNotches = [0, 5, 10, 15, 35],
|
---|
[274] | 1656 | fuelTanks = DH8DModel.fuelTanks,
|
---|
[7] | 1657 | numEngines = 2)
|
---|
| 1658 |
|
---|
| 1659 | @property
|
---|
| 1660 | def name(self):
|
---|
| 1661 | """Get the name for this aircraft model."""
|
---|
[16] | 1662 | return "FSUIPC/Generic Bombardier Dash 8-Q400"
|
---|
| 1663 |
|
---|
| 1664 | #------------------------------------------------------------------------------
|
---|
| 1665 |
|
---|
| 1666 | class DreamwingsDH8DModel(DH8DModel):
|
---|
| 1667 | """Model handler for the Dreamwings Dash 8-Q400."""
|
---|
| 1668 | @staticmethod
|
---|
| 1669 | def doesHandle(aircraft, (name, airPath)):
|
---|
| 1670 | """Determine if this model handler handles the aircraft with the given
|
---|
| 1671 | name."""
|
---|
| 1672 | return aircraft.type==const.AIRCRAFT_DH8D and \
|
---|
| 1673 | (name.find("Dreamwings")!=-1 or airPath.find("Dreamwings")!=-1) and \
|
---|
| 1674 | (name.find("Dash")!=-1 or airPath.find("Dash")!=-1) and \
|
---|
| 1675 | (name.find("Q400")!=-1 or airPath.find("Q400")!=-1) and \
|
---|
| 1676 | airPath.find("Dash8Q400")!=-1
|
---|
[7] | 1677 |
|
---|
[16] | 1678 | @property
|
---|
| 1679 | def name(self):
|
---|
| 1680 | """Get the name for this aircraft model."""
|
---|
| 1681 | return "FSUIPC/Dreamwings Bombardier Dash 8-Q400"
|
---|
| 1682 |
|
---|
| 1683 | def getAircraftState(self, aircraft, timestamp, data):
|
---|
| 1684 | """Get the aircraft state.
|
---|
| 1685 |
|
---|
| 1686 | Get it from the parent, and then invert the pitot heat state."""
|
---|
| 1687 | state = super(DreamwingsDH8DModel, self).getAircraftState(aircraft,
|
---|
| 1688 | timestamp,
|
---|
| 1689 | data)
|
---|
| 1690 | state.pitotHeatOn = not state.pitotHeatOn
|
---|
| 1691 |
|
---|
| 1692 | return state
|
---|
[314] | 1693 |
|
---|
[7] | 1694 | #------------------------------------------------------------------------------
|
---|
| 1695 |
|
---|
| 1696 | class CRJ2Model(GenericAircraftModel):
|
---|
| 1697 | """Generic model for the Bombardier CRJ-200 aircraft."""
|
---|
[330] | 1698 | fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_CENTRE, const.FUELTANK_RIGHT]
|
---|
[274] | 1699 |
|
---|
[7] | 1700 | def __init__(self):
|
---|
| 1701 | """Construct the model."""
|
---|
[8] | 1702 | super(CRJ2Model, self). \
|
---|
[7] | 1703 | __init__(flapsNotches = [0, 8, 20, 30, 45],
|
---|
[274] | 1704 | fuelTanks = CRJ2Model.fuelTanks,
|
---|
[7] | 1705 | numEngines = 2)
|
---|
| 1706 |
|
---|
| 1707 | @property
|
---|
| 1708 | def name(self):
|
---|
| 1709 | """Get the name for this aircraft model."""
|
---|
| 1710 | return "FSUIPC/Generic Bombardier CRJ-200"
|
---|
| 1711 |
|
---|
| 1712 | #------------------------------------------------------------------------------
|
---|
| 1713 |
|
---|
| 1714 | class F70Model(GenericAircraftModel):
|
---|
| 1715 | """Generic model for the Fokker F70 aircraft."""
|
---|
[330] | 1716 | fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_CENTRE, const.FUELTANK_RIGHT]
|
---|
[274] | 1717 |
|
---|
[7] | 1718 | def __init__(self):
|
---|
| 1719 | """Construct the model."""
|
---|
[8] | 1720 | super(F70Model, self). \
|
---|
[7] | 1721 | __init__(flapsNotches = [0, 8, 15, 25, 42],
|
---|
[274] | 1722 | fuelTanks = F70Model.fuelTanks,
|
---|
[7] | 1723 | numEngines = 2)
|
---|
| 1724 |
|
---|
| 1725 | @property
|
---|
| 1726 | def name(self):
|
---|
| 1727 | """Get the name for this aircraft model."""
|
---|
| 1728 | return "FSUIPC/Generic Fokker 70"
|
---|
| 1729 |
|
---|
| 1730 | #------------------------------------------------------------------------------
|
---|
| 1731 |
|
---|
[314] | 1732 | class DAF70Model(F70Model):
|
---|
| 1733 | """Model for the Digital Aviation F70 implementation on FS9."""
|
---|
| 1734 | @staticmethod
|
---|
| 1735 | def doesHandle(aircraft, (name, airPath)):
|
---|
| 1736 | """Determine if this model handler handles the aircraft with the given
|
---|
| 1737 | name."""
|
---|
| 1738 | return aircraft.type == const.AIRCRAFT_F70 and \
|
---|
| 1739 | (airPath.endswith("fokker70_2k4_v4.1.air") or
|
---|
| 1740 | airPath.endswith("fokker70_2k4_v4.3.air"))
|
---|
| 1741 |
|
---|
| 1742 | @property
|
---|
| 1743 | def name(self):
|
---|
| 1744 | """Get the name for this aircraft model."""
|
---|
| 1745 | return "FSUIPC/Digital Aviation Fokker 70"
|
---|
| 1746 |
|
---|
| 1747 | def getAircraftState(self, aircraft, timestamp, data):
|
---|
| 1748 | """Get the aircraft state.
|
---|
| 1749 |
|
---|
| 1750 | Get it from the parent, and then invert the pitot heat state."""
|
---|
| 1751 | state = super(DAF70Model, self).getAircraftState(aircraft,
|
---|
| 1752 | timestamp,
|
---|
| 1753 | data)
|
---|
| 1754 | state.landingLightsOn = None
|
---|
[321] | 1755 | state.nav2_manual = aircraft.flight.stage!=const.STAGE_CRUISE
|
---|
[314] | 1756 |
|
---|
| 1757 | return state
|
---|
| 1758 |
|
---|
| 1759 | #------------------------------------------------------------------------------
|
---|
| 1760 |
|
---|
[7] | 1761 | class DC3Model(GenericAircraftModel):
|
---|
| 1762 | """Generic model for the Lisunov Li-2 (DC-3) aircraft."""
|
---|
[289] | 1763 | fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_CENTRE,
|
---|
[330] | 1764 | const.FUELTANK_RIGHT]
|
---|
[274] | 1765 | # fuelTanks = [const.FUELTANK_LEFT_AUX, const.FUELTANK_LEFT,
|
---|
| 1766 | # const.FUELTANK_RIGHT, const.FUELTANK_RIGHT_AUX]
|
---|
| 1767 |
|
---|
[7] | 1768 | def __init__(self):
|
---|
| 1769 | """Construct the model."""
|
---|
[8] | 1770 | super(DC3Model, self). \
|
---|
[7] | 1771 | __init__(flapsNotches = [0, 15, 30, 45],
|
---|
[274] | 1772 | fuelTanks = DC3Model.fuelTanks,
|
---|
[263] | 1773 | numEngines = 2, isN1 = False)
|
---|
[289] | 1774 | self._leftLevel = 0.0
|
---|
| 1775 | self._rightLevel = 0.0
|
---|
[7] | 1776 |
|
---|
| 1777 | @property
|
---|
| 1778 | def name(self):
|
---|
| 1779 | """Get the name for this aircraft model."""
|
---|
[263] | 1780 | return "FSUIPC/Generic Lisunov Li-2 (DC-3)"
|
---|
[7] | 1781 |
|
---|
[274] | 1782 | def _convertFuelData(self, data, index = 0, addCapacities = False):
|
---|
| 1783 | """Convert the given data into a fuel info list.
|
---|
| 1784 |
|
---|
| 1785 | It assumes to receive the 3 fuel tanks as seen above (left,
|
---|
| 1786 | centre and right) and converts it to left aux, left, right,
|
---|
| 1787 | and right aux. The amount in the left tank goes into left aux,
|
---|
| 1788 | the amount of the right tank goes into right aux and the
|
---|
| 1789 | amount of the centre tank goes into the left and right tanks
|
---|
| 1790 | evenly distributed."""
|
---|
| 1791 | (rawFuelData, totalFuel) = \
|
---|
| 1792 | super(DC3Model, self)._convertFuelData(data, index, addCapacities)
|
---|
| 1793 |
|
---|
| 1794 | centreAmount = rawFuelData[1][1]
|
---|
| 1795 | if addCapacities:
|
---|
| 1796 | centreCapacity = rawFuelData[1][2]
|
---|
[289] | 1797 | self._leftLevel = self._rightLevel = \
|
---|
| 1798 | centreAmount / centreCapacity / 2.0
|
---|
[330] | 1799 | fuelData = [(const.FUELTANK_LEFT_AUX,
|
---|
[274] | 1800 | rawFuelData[0][1], rawFuelData[0][2]),
|
---|
| 1801 | (const.FUELTANK_LEFT,
|
---|
| 1802 | centreAmount/2.0, centreCapacity/2.0),
|
---|
| 1803 | (const.FUELTANK_RIGHT,
|
---|
| 1804 | centreAmount/2.0, centreCapacity/2.0),
|
---|
| 1805 | (const.FUELTANK_RIGHT_AUX,
|
---|
| 1806 | rawFuelData[2][1], rawFuelData[2][2])]
|
---|
| 1807 | else:
|
---|
| 1808 | fuelData = [(const.FUELTANK_LEFT_AUX, rawFuelData[0][1]),
|
---|
| 1809 | (const.FUELTANK_LEFT, centreAmount/2.0),
|
---|
| 1810 | (const.FUELTANK_RIGHT, centreAmount/2.0),
|
---|
| 1811 | (const.FUELTANK_RIGHT_AUX, rawFuelData[2][1])]
|
---|
| 1812 |
|
---|
| 1813 | return (fuelData, totalFuel)
|
---|
| 1814 |
|
---|
| 1815 | def setFuelLevel(self, handler, levels):
|
---|
| 1816 | """Set the fuel level.
|
---|
| 1817 |
|
---|
| 1818 | See the description of Simulator.setFuelLevel. This
|
---|
| 1819 | implementation assumes to get the four-tank representation,
|
---|
| 1820 | as returned by getFuel()."""
|
---|
[289] | 1821 | leftLevel = None
|
---|
| 1822 | centreLevel = None
|
---|
| 1823 | rightLevel = None
|
---|
[330] | 1824 |
|
---|
[274] | 1825 | for (tank, level) in levels:
|
---|
[289] | 1826 | if tank==const.FUELTANK_LEFT_AUX:
|
---|
| 1827 | leftLevel = level if leftLevel is None else (leftLevel + level)
|
---|
| 1828 | elif tank==const.FUELTANK_LEFT:
|
---|
| 1829 | level /= 2.0
|
---|
| 1830 | centreLevel = (self._rightLevel + level) \
|
---|
| 1831 | if centreLevel is None else (centreLevel + level)
|
---|
| 1832 | self._leftLevel = level
|
---|
| 1833 | elif tank==const.FUELTANK_RIGHT:
|
---|
| 1834 | level /= 2.0
|
---|
| 1835 | centreLevel = (self._leftLevel + level) \
|
---|
| 1836 | if centreLevel is None else (centreLevel + level)
|
---|
| 1837 | self._rightLevel = level
|
---|
| 1838 | elif tank==const.FUELTANK_RIGHT_AUX:
|
---|
| 1839 | rightLevel = level if rightLevel is None \
|
---|
| 1840 | else (rightLevel + level)
|
---|
[274] | 1841 |
|
---|
[289] | 1842 | levels = []
|
---|
| 1843 | if leftLevel is not None: levels.append((const.FUELTANK_LEFT,
|
---|
| 1844 | leftLevel))
|
---|
| 1845 | if centreLevel is not None: levels.append((const.FUELTANK_CENTRE,
|
---|
| 1846 | centreLevel))
|
---|
| 1847 | if rightLevel is not None: levels.append((const.FUELTANK_RIGHT,
|
---|
| 1848 | rightLevel))
|
---|
| 1849 |
|
---|
| 1850 | super(DC3Model, self).setFuelLevel(handler, levels)
|
---|
[274] | 1851 |
|
---|
[7] | 1852 | #------------------------------------------------------------------------------
|
---|
| 1853 |
|
---|
| 1854 | class T134Model(GenericAircraftModel):
|
---|
| 1855 | """Generic model for the Tupolev Tu-134 aircraft."""
|
---|
[274] | 1856 | fuelTanks = [const.FUELTANK_LEFT_TIP, const.FUELTANK_EXTERNAL1,
|
---|
| 1857 | const.FUELTANK_LEFT_AUX,
|
---|
| 1858 | const.FUELTANK_CENTRE,
|
---|
| 1859 | const.FUELTANK_RIGHT_AUX,
|
---|
| 1860 | const.FUELTANK_EXTERNAL2, const.FUELTANK_RIGHT_TIP]
|
---|
| 1861 |
|
---|
[7] | 1862 | def __init__(self):
|
---|
| 1863 | """Construct the model."""
|
---|
[8] | 1864 | super(T134Model, self). \
|
---|
[7] | 1865 | __init__(flapsNotches = [0, 10, 20, 30],
|
---|
[274] | 1866 | fuelTanks = T134Model.fuelTanks,
|
---|
[7] | 1867 | numEngines = 2)
|
---|
| 1868 |
|
---|
| 1869 | @property
|
---|
| 1870 | def name(self):
|
---|
| 1871 | """Get the name for this aircraft model."""
|
---|
| 1872 | return "FSUIPC/Generic Tupolev Tu-134"
|
---|
| 1873 |
|
---|
| 1874 | #------------------------------------------------------------------------------
|
---|
| 1875 |
|
---|
| 1876 | class T154Model(GenericAircraftModel):
|
---|
| 1877 | """Generic model for the Tupolev Tu-134 aircraft."""
|
---|
[274] | 1878 | fuelTanks = [const.FUELTANK_LEFT_AUX, const.FUELTANK_LEFT,
|
---|
| 1879 | const.FUELTANK_CENTRE, const.FUELTANK_CENTRE2,
|
---|
| 1880 | const.FUELTANK_RIGHT, const.FUELTANK_RIGHT_AUX]
|
---|
| 1881 |
|
---|
[7] | 1882 | def __init__(self):
|
---|
| 1883 | """Construct the model."""
|
---|
[8] | 1884 | super(T154Model, self). \
|
---|
[7] | 1885 | __init__(flapsNotches = [0, 15, 28, 45],
|
---|
[274] | 1886 | fuelTanks = T154Model.fuelTanks,
|
---|
[7] | 1887 | numEngines = 3)
|
---|
| 1888 |
|
---|
| 1889 | @property
|
---|
| 1890 | def name(self):
|
---|
| 1891 | """Get the name for this aircraft model."""
|
---|
| 1892 | return "FSUIPC/Generic Tupolev Tu-154"
|
---|
| 1893 |
|
---|
[8] | 1894 | def getAircraftState(self, aircraft, timestamp, data):
|
---|
[7] | 1895 | """Get an aircraft state object for the given monitoring data.
|
---|
| 1896 |
|
---|
| 1897 | This removes the reverser value for the middle engine."""
|
---|
[8] | 1898 | state = super(T154Model, self).getAircraftState(aircraft, timestamp, data)
|
---|
[7] | 1899 | del state.reverser[1]
|
---|
| 1900 | return state
|
---|
| 1901 |
|
---|
| 1902 | #------------------------------------------------------------------------------
|
---|
| 1903 |
|
---|
| 1904 | class YK40Model(GenericAircraftModel):
|
---|
| 1905 | """Generic model for the Yakovlev Yak-40 aircraft."""
|
---|
[274] | 1906 | fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_RIGHT]
|
---|
[330] | 1907 |
|
---|
[7] | 1908 | def __init__(self):
|
---|
| 1909 | """Construct the model."""
|
---|
[8] | 1910 | super(YK40Model, self). \
|
---|
[7] | 1911 | __init__(flapsNotches = [0, 20, 35],
|
---|
[274] | 1912 | fuelTanks = YK40Model.fuelTanks,
|
---|
[7] | 1913 | numEngines = 2)
|
---|
| 1914 |
|
---|
| 1915 | @property
|
---|
| 1916 | def name(self):
|
---|
| 1917 | """Get the name for this aircraft model."""
|
---|
| 1918 | return "FSUIPC/Generic Yakovlev Yak-40"
|
---|
| 1919 |
|
---|
| 1920 | #------------------------------------------------------------------------------
|
---|
| 1921 |
|
---|
[191] | 1922 | _genericModels = { const.AIRCRAFT_B736 : B737Model,
|
---|
| 1923 | const.AIRCRAFT_B737 : B737Model,
|
---|
| 1924 | const.AIRCRAFT_B738 : B737Model,
|
---|
| 1925 | const.AIRCRAFT_B738C : B737Model,
|
---|
| 1926 | const.AIRCRAFT_B733 : B737Model,
|
---|
| 1927 | const.AIRCRAFT_B734 : B737Model,
|
---|
| 1928 | const.AIRCRAFT_B735 : B737Model,
|
---|
| 1929 | const.AIRCRAFT_DH8D : DH8DModel,
|
---|
| 1930 | const.AIRCRAFT_B762 : B767Model,
|
---|
| 1931 | const.AIRCRAFT_B763 : B767Model,
|
---|
| 1932 | const.AIRCRAFT_CRJ2 : B767Model,
|
---|
| 1933 | const.AIRCRAFT_F70 : F70Model,
|
---|
| 1934 | const.AIRCRAFT_DC3 : DC3Model,
|
---|
| 1935 | const.AIRCRAFT_T134 : T134Model,
|
---|
| 1936 | const.AIRCRAFT_T154 : T154Model,
|
---|
| 1937 | const.AIRCRAFT_YK40 : YK40Model }
|
---|
[8] | 1938 |
|
---|
| 1939 | #------------------------------------------------------------------------------
|
---|
| 1940 |
|
---|
| 1941 | AircraftModel.registerSpecial(PMDGBoeing737NGModel)
|
---|
[16] | 1942 | AircraftModel.registerSpecial(DreamwingsDH8DModel)
|
---|
[314] | 1943 | AircraftModel.registerSpecial(DAF70Model)
|
---|
[8] | 1944 |
|
---|
| 1945 | #------------------------------------------------------------------------------
|
---|