Ignore:
Timestamp:
07/05/12 17:51:58 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Reworked fuel handling so that the model can tell what tanks there are

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/fsuipc.py

    r263 r274  
    658658                                   extra = _disconnect)
    659659
    660     def getFuel(self, tanks, callback):
    661         """Get the fuel from the given tanks.
    662 
    663         The callback will be called with a list of two-tuples, where the tuples
    664         have the following items:
     660    def getFuel(self, callback):
     661        """Get the fuel information for the current model.
     662
     663        The callback will be called with a list of triplets with the following
     664        items:
     665        - the fuel tank identifier
    665666        - the current weight of the fuel in the tank (in kgs)
    666667        - the current total capacity of the tank (in kgs)."""
    667         data = [(0x0af4, "H")]     # Fuel weight
    668         for tank in tanks:
    669             offset = _tank2offset[tank]
    670             data.append( (offset, "u") )     # tank level
    671             data.append( (offset+4, "u") )   # tank capacity
    672 
    673         self._handler.requestRead(data, self._handleFuelRetrieved,
    674                                   extra = callback)
     668        if self._aircraftModel is None:
     669            callback([])
     670        else:
     671            self._aircraftModel.getFuel(self._handler, callback)
    675672
    676673    def setFuelLevel(self, levels):
     
    682679        - the requested level of the fuel as a floating-point value between 0.0
    683680        and 1.0."""
    684         data = []
    685         for (tank, level) in levels:
    686             offset = _tank2offset[tank]
    687             value = long(level * 128.0 * 65536.0)
    688             data.append( (offset, "u", value) )
    689         self._handler.requestWrite(data, self._handleFuelWritten)
     681        if self._aircraftModel is not None:
     682            self._aircraftModel.setFuelLevel(self._handler, levels)
    690683
    691684    def enableTimeSync(self):
     
    999992        if disconnect:
    1000993            self._handler.disconnect()
    1001 
    1002     def _handleFuelRetrieved(self, data, callback):
    1003         """Callback for a fuel retrieval request."""
    1004         fuelWeight = data[0] / 256.0
    1005         result = []
    1006         for i in range(1, len(data), 2):
    1007             capacity = data[i+1] * fuelWeight * const.LBSTOKG
    1008             amount = data[i] * capacity / 128.0 / 65536.0
    1009             result.append( (amount, capacity) )
    1010 
    1011         callback(result)
    1012                                                  
    1013     def _handleFuelWritten(self, success, extra):
    1014         """Callback for a fuel setting request."""
    1015         pass
    1016994
    1017995    def _handleNumHotkeys(self, data, (id, generation)):
     
    13801358        super(GenericAircraftModel, self).addMonitoringData(data, fsType)
    13811359       
    1382         self._addOffsetWithIndexMember(data, 0x0af4, "H", "_monidx_fuelWeight")
    1383 
    1384         self._fuelStartIndex = len(data)
    1385         for tank in self._fuelTanks:
    1386             offset = _tank2offset[tank]
    1387             self._addOffsetWithIndexMember(data, offset, "u")    # tank level
    1388             self._addOffsetWithIndexMember(data, offset+4, "u")  # tank capacity
     1360        self._fuelStartIndex = self._addFuelOffsets(data, "_monidx_fuelWeight")
    13891361
    13901362        self._engineStartIndex = len(data)
     
    14061378                                                                   data)
    14071379
    1408         fuelWeight = data[self._monidx_fuelWeight]/256.0
    1409         state.fuel = []
    1410         for i in range(self._fuelStartIndex,
    1411                        self._fuelStartIndex + 2*len(self._fuelTanks), 2):
    1412             fuel = data[i+1]*data[i]*fuelWeight*const.LBSTOKG/128.0/65536.0
    1413             state.fuel.append(fuel)
    1414 
     1380        (state.fuel, state.totalFuel) = \
     1381            self._convertFuelData(data, index = self._monidx_fuelWeight)
    14151382
    14161383        state.n1 = [] if self._isN1 else None
     
    14311398        return state
    14321399
     1400    def getFuel(self, handler, callback):
     1401        """Get the fuel information for this model.
     1402
     1403        See Simulator.getFuel for more information. This
     1404        implementation simply queries the fuel tanks given to the
     1405        constructor."""
     1406        data = []
     1407        self._addFuelOffsets(data)
     1408
     1409        handler.requestRead(data, self._handleFuelRetrieved,
     1410                            extra = callback)
     1411
     1412    def setFuelLevel(self, handler, levels):
     1413        """Set the fuel level.
     1414
     1415        See the description of Simulator.setFuelLevel. This
     1416        implementation simply sets the fuel tanks as given."""
     1417        data = []
     1418        for (tank, level) in levels:
     1419            offset = _tank2offset[tank]
     1420            value = long(level * 128.0 * 65536.0)
     1421            data.append( (offset, "u", value) )
     1422
     1423        handler.requestWrite(data, self._handleFuelWritten)
     1424
     1425    def _addFuelOffsets(self, data, weightIndexName = None):
     1426        """Add the fuel offsets to the given data array.
     1427
     1428        If weightIndexName is not None, it will be the name of the
     1429        fuel weight index.
     1430
     1431        Returns the index of the first fuel tank's data."""
     1432        self._addOffsetWithIndexMember(data, 0x0af4, "H", weightIndexName)
     1433
     1434        fuelStartIndex = len(data)
     1435        for tank in self._fuelTanks:
     1436            offset = _tank2offset[tank]
     1437            self._addOffsetWithIndexMember(data, offset, "u")    # tank level
     1438            self._addOffsetWithIndexMember(data, offset+4, "u")  # tank capacity
     1439       
     1440        return fuelStartIndex
     1441
     1442    def _convertFuelData(self, data, index = 0, addCapacities = False):
     1443        """Convert the given data into a fuel info list.
     1444
     1445        The list consists of two or three-tuples of the following
     1446        items:
     1447        - the fuel tank ID,
     1448        - the amount of the fuel in kg,
     1449        - if addCapacities is True, the total capacity of the tank."""
     1450        fuelWeight = data[index] / 256.0
     1451        index += 1
     1452
     1453        result = []
     1454        totalFuel = 0
     1455        for fuelTank in self._fuelTanks:
     1456            capacity = data[index+1] * fuelWeight * const.LBSTOKG
     1457            amount = data[index] * capacity / 128.0 / 65536.0
     1458            index += 2
     1459           
     1460            result.append( (fuelTank, amount, capacity) if addCapacities
     1461                           else (fuelTank, amount))
     1462            totalFuel += amount
     1463
     1464        return (result, totalFuel)       
     1465
     1466    def _handleFuelRetrieved(self, data, callback):
     1467        """Callback for a fuel retrieval request."""
     1468        (fuelData, _totalFuel) = self._convertFuelData(data,
     1469                                                       addCapacities = True)
     1470        callback(fuelData)
     1471                                                         
     1472    def _handleFuelWritten(self, success, extra):
     1473        """Callback for a fuel setting request."""
     1474        pass
     1475
    14331476#------------------------------------------------------------------------------
    14341477
     
    14511494class B737Model(GenericAircraftModel):
    14521495    """Generic model for the Boeing 737 Classing and NG aircraft."""
     1496    fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_CENTRE, const.FUELTANK_RIGHT]   
     1497   
    14531498    def __init__(self):
    14541499        """Construct the model."""
    14551500        super(B737Model, self). \
    14561501            __init__(flapsNotches = [0, 1, 2, 5, 10, 15, 25, 30, 40],
    1457                      fuelTanks = acft.Boeing737.fuelTanks,
     1502                     fuelTanks = B737Model.fuelTanks,
    14581503                     numEngines = 2)
    14591504
     
    15191564class B767Model(GenericAircraftModel):
    15201565    """Generic model for the Boeing 767 aircraft."""
     1566    fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_CENTRE, const.FUELTANK_RIGHT]   
     1567
    15211568    def __init__(self):
    15221569        """Construct the model."""
    15231570        super(B767Model, self). \
    15241571            __init__(flapsNotches = [0, 1, 5, 15, 20, 25, 30],
    1525                      fuelTanks = acft.Boeing767.fuelTanks,
     1572                     fuelTanks = Boeing767Model.fuelTanks,
    15261573                     numEngines = 2)
    15271574
     
    15351582class DH8DModel(GenericAircraftModel):
    15361583    """Generic model for the Bombardier  Dash 8-Q400 aircraft."""
     1584    fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_RIGHT]   
     1585   
    15371586    def __init__(self):
    15381587        """Construct the model."""
    15391588        super(DH8DModel, self). \
    15401589            __init__(flapsNotches = [0, 5, 10, 15, 35],
    1541                      fuelTanks = acft.DH8D.fuelTanks,
     1590                     fuelTanks = DH8DModel.fuelTanks,
    15421591                     numEngines = 2)
    15431592
     
    15801629class CRJ2Model(GenericAircraftModel):
    15811630    """Generic model for the Bombardier CRJ-200 aircraft."""
     1631    fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_CENTRE, const.FUELTANK_RIGHT]   
     1632
    15821633    def __init__(self):
    15831634        """Construct the model."""
    15841635        super(CRJ2Model, self). \
    15851636            __init__(flapsNotches = [0, 8, 20, 30, 45],
    1586                      fuelTanks = acft.CRJ2.fuelTanks,
     1637                     fuelTanks = CRJ2Model.fuelTanks,
    15871638                     numEngines = 2)
    15881639
     
    15961647class F70Model(GenericAircraftModel):
    15971648    """Generic model for the Fokker F70 aircraft."""
     1649    fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_CENTRE, const.FUELTANK_RIGHT]   
     1650
    15981651    def __init__(self):
    15991652        """Construct the model."""
    16001653        super(F70Model, self). \
    16011654            __init__(flapsNotches = [0, 8, 15, 25, 42],
    1602                      fuelTanks = acft.F70.fuelTanks,
     1655                     fuelTanks = F70Model.fuelTanks,
    16031656                     numEngines = 2)
    16041657
     
    16121665class DC3Model(GenericAircraftModel):
    16131666    """Generic model for the Lisunov Li-2 (DC-3) aircraft."""
     1667    fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_CENTRE, const.FUELTANK_RIGHT]   
     1668    # fuelTanks = [const.FUELTANK_LEFT_AUX, const.FUELTANK_LEFT,
     1669    #              const.FUELTANK_RIGHT, const.FUELTANK_RIGHT_AUX]
     1670
    16141671    def __init__(self):
    16151672        """Construct the model."""
    16161673        super(DC3Model, self). \
    16171674            __init__(flapsNotches = [0, 15, 30, 45],
    1618                      fuelTanks = acft.DC3.fuelTanks,
     1675                     fuelTanks = DC3Model.fuelTanks,
    16191676                     numEngines = 2, isN1 = False)
    16201677
     
    16241681        return "FSUIPC/Generic Lisunov Li-2 (DC-3)"
    16251682
     1683    def _convertFuelData(self, data, index = 0, addCapacities = False):
     1684        """Convert the given data into a fuel info list.
     1685
     1686        It assumes to receive the 3 fuel tanks as seen above (left,
     1687        centre and right) and converts it to left aux, left, right,
     1688        and right aux. The amount in the left tank goes into left aux,
     1689        the amount of the right tank goes into right aux and the
     1690        amount of the centre tank goes into the left and right tanks
     1691        evenly distributed."""
     1692        (rawFuelData, totalFuel) = \
     1693            super(DC3Model, self)._convertFuelData(data, index, addCapacities)
     1694
     1695        centreAmount = rawFuelData[1][1]
     1696        if addCapacities:
     1697            centreCapacity = rawFuelData[1][2]
     1698            fuelData = [(const.FUELTANK_LEFT_AUX,
     1699                         rawFuelData[0][1], rawFuelData[0][2]),
     1700                        (const.FUELTANK_LEFT,
     1701                         centreAmount/2.0, centreCapacity/2.0),
     1702                        (const.FUELTANK_RIGHT,
     1703                         centreAmount/2.0, centreCapacity/2.0),
     1704                        (const.FUELTANK_RIGHT_AUX,
     1705                         rawFuelData[2][1], rawFuelData[2][2])]
     1706        else:
     1707            fuelData = [(const.FUELTANK_LEFT_AUX, rawFuelData[0][1]),
     1708                        (const.FUELTANK_LEFT, centreAmount/2.0),
     1709                        (const.FUELTANK_RIGHT, centreAmount/2.0),
     1710                        (const.FUELTANK_RIGHT_AUX, rawFuelData[2][1])]
     1711
     1712        return (fuelData, totalFuel)
     1713
     1714    def setFuelLevel(self, handler, levels):
     1715        """Set the fuel level.
     1716
     1717        See the description of Simulator.setFuelLevel. This
     1718        implementation assumes to get the four-tank representation,
     1719        as returned by getFuel()."""
     1720        leftLevel = 0.0
     1721        centreLevel = 0.0
     1722        rightLevel = 0.0
     1723       
     1724        for (tank, level) in levels:
     1725            if tank==const.FUELTANK_LEFT_AUX: leftLevel += level
     1726            elif tank==const.FUELTANK_LEFT or  tank==const.FUELTANK_RIGHT:
     1727                centreLevel += level
     1728            elif tank==const.FUELTANK_RIGHT_AUX: rightLevel += level
     1729
     1730        super(DC3Model, self).setFuelLevel([(const.FUELTANK_LEFT, leftLevel),
     1731                                            (const.FUELTANK_CENTRE,
     1732                                            centreLevel),
     1733                                            (const.FUELTANK_RIGHT, rightLevel)])
     1734
    16261735#------------------------------------------------------------------------------
    16271736
    16281737class T134Model(GenericAircraftModel):
    16291738    """Generic model for the Tupolev Tu-134 aircraft."""
     1739    fuelTanks = [const.FUELTANK_LEFT_TIP, const.FUELTANK_EXTERNAL1,
     1740                 const.FUELTANK_LEFT_AUX,
     1741                 const.FUELTANK_CENTRE,
     1742                 const.FUELTANK_RIGHT_AUX,
     1743                 const.FUELTANK_EXTERNAL2, const.FUELTANK_RIGHT_TIP]
     1744
    16301745    def __init__(self):
    16311746        """Construct the model."""
    16321747        super(T134Model, self). \
    16331748            __init__(flapsNotches = [0, 10, 20, 30],
    1634                      fuelTanks = acft.T134.fuelTanks,
     1749                     fuelTanks = T134Model.fuelTanks,
    16351750                     numEngines = 2)
    16361751
     
    16441759class T154Model(GenericAircraftModel):
    16451760    """Generic model for the Tupolev Tu-134 aircraft."""
     1761    fuelTanks = [const.FUELTANK_LEFT_AUX, const.FUELTANK_LEFT,
     1762                 const.FUELTANK_CENTRE, const.FUELTANK_CENTRE2,
     1763                 const.FUELTANK_RIGHT, const.FUELTANK_RIGHT_AUX]
     1764
    16461765    def __init__(self):
    16471766        """Construct the model."""
    16481767        super(T154Model, self). \
    16491768            __init__(flapsNotches = [0, 15, 28, 45],
    1650                      fuelTanks = acft.T154.fuelTanks,
     1769                     fuelTanks = T154Model.fuelTanks,
    16511770                     numEngines = 3)
    16521771
     
    16681787class YK40Model(GenericAircraftModel):
    16691788    """Generic model for the Yakovlev Yak-40 aircraft."""
     1789    fuelTanks = [const.FUELTANK_LEFT, const.FUELTANK_RIGHT]
     1790   
    16701791    def __init__(self):
    16711792        """Construct the model."""
    16721793        super(YK40Model, self). \
    16731794            __init__(flapsNotches = [0, 20, 35],
    1674                      fuelTanks = acft.YK40.fuelTanks,
     1795                     fuelTanks = YK40Model.fuelTanks,
    16751796                     numEngines = 2)
    16761797
Note: See TracChangeset for help on using the changeset viewer.