|
| 1 | +######################################################################################### |
| 2 | +## |
| 3 | +## JSBSim Wrapper Block |
| 4 | +## |
| 5 | +######################################################################################### |
| 6 | + |
| 7 | +# IMPORTS =============================================================================== |
| 8 | + |
| 9 | +from pathsim.blocks import Function |
| 10 | +from ISA import ISAtmosphere |
| 11 | +import math |
| 12 | + |
| 13 | +# BLOCKS ================================================================================ |
| 14 | + |
| 15 | +class CAStoMach(Function): |
| 16 | + """Convert calibrated airspeed (CAS) to Mach value. |
| 17 | + CAS in m/s altitude in m. |
| 18 | + """ |
| 19 | + |
| 20 | + input_port_labels = { |
| 21 | + "cas": 0, |
| 22 | + "altitude": 1 |
| 23 | + } |
| 24 | + |
| 25 | + output_port_labels = { |
| 26 | + "mach": 0 |
| 27 | + } |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + super().__init__(func=self._eval) |
| 31 | + |
| 32 | + def _eval(self, cas, altitude): |
| 33 | + """Convert Calibrated airspeed to Mach value. |
| 34 | +
|
| 35 | + Assume m/s for cas and m for altitude. |
| 36 | +
|
| 37 | + Based on the formulas in the US Air Force Aircraft Performance Flight |
| 38 | + Testing Manual (AFFTC-TIH-99-01), in particular sections 4.6 to 4.8. |
| 39 | +
|
| 40 | + The subsonic and supersonic Mach number equations are used with the simple |
| 41 | + substitutions of (Vc/asl) for M and Psl for P. However, the condition for |
| 42 | + which the equations are used is no longer subsonic (M < 1) or supersonic |
| 43 | + (M > 1) but rather calibrated airspeed being less or greater than the |
| 44 | + speed of sound ( asl ), standard day, sea level (661.48 knots). |
| 45 | + """ |
| 46 | + ISA = ISAtmosphere() |
| 47 | + |
| 48 | + pressure, _, _, _ = ISA._eval(altitude) |
| 49 | + |
| 50 | + if cas < ISAtmosphere.StdSL_speed_of_sound: |
| 51 | + # Bernoulli's compressible equation (4.11) |
| 52 | + qc = ISAtmosphere.StdSL_pressure * ( |
| 53 | + math.pow(1 + 0.2 * math.pow(cas / ISAtmosphere.StdSL_speed_of_sound, 2), 3.5) - 1 |
| 54 | + ) |
| 55 | + else: |
| 56 | + # Rayleigh's supersonic pitot equation (4.16) |
| 57 | + qc = ISAtmosphere.StdSL_pressure * ( |
| 58 | + ( |
| 59 | + (166.9215801 * math.pow(cas / ISAtmosphere.StdSL_speed_of_sound, 7)) |
| 60 | + / math.pow(7 * math.pow(cas / ISAtmosphere.StdSL_speed_of_sound, 2) - 1, 2.5) |
| 61 | + ) |
| 62 | + - 1 |
| 63 | + ) |
| 64 | + |
| 65 | + # Solving for M in equation (4.11), also used as initial condition for supersonic case |
| 66 | + mach = math.sqrt(5 * (math.pow(qc / pressure + 1, 2 / 7) - 1)) |
| 67 | + |
| 68 | + if mach > 1: |
| 69 | + # Iterate equation (4.22) since M appears on both sides of the equation |
| 70 | + for i in range(7): |
| 71 | + mach = 0.88128485 * math.sqrt((qc / pressure + 1) * math.pow(1 - 1 / (7 * mach * mach), 2.5)) |
| 72 | + |
| 73 | + return mach |
| 74 | + |
| 75 | + |
| 76 | +class CAStoTAS(Function): |
| 77 | + """Convert calibrated airspeed (CAS) to true airspeed (TAS). |
| 78 | + CAS and TAS in m/s altitude in m. |
| 79 | + """ |
| 80 | + |
| 81 | + input_port_labels = { |
| 82 | + "cas": 0, |
| 83 | + "altitude": 1 |
| 84 | + } |
| 85 | + |
| 86 | + output_port_labels = { |
| 87 | + "tas": 0 |
| 88 | + } |
| 89 | + |
| 90 | + def __init__(self): |
| 91 | + super().__init__(func=self._eval) |
| 92 | + |
| 93 | + def _eval(self, cas, altitude): |
| 94 | + """Assume m/s for input and output velocities and m for altitude.""" |
| 95 | + |
| 96 | + mach = CAStoMach()._eval(cas, altitude) |
| 97 | + ISA = ISAtmosphere() |
| 98 | + _, _, _, speed_of_sound = ISA.state(altitude) |
| 99 | + return mach * speed_of_sound |
| 100 | + |
| 101 | + |
| 102 | +class TAStoCAS(Function): |
| 103 | + """Convert true airspeed (TAS) to calibrated airspeed (CAS). |
| 104 | + TAS and CAS in m/s altitude in m. |
| 105 | + """ |
| 106 | + |
| 107 | + input_port_labels = { |
| 108 | + "tas": 0, |
| 109 | + "altitude": 1 |
| 110 | + } |
| 111 | + |
| 112 | + output_port_labels = { |
| 113 | + "cas": 0 |
| 114 | + } |
| 115 | + |
| 116 | + def __init__(self): |
| 117 | + super().__init__(func=self._eval) |
| 118 | + |
| 119 | + def _eval(self, tas, altitude): |
| 120 | + """Assume m/s for input and output velocities and m for altitude.""" |
| 121 | + |
| 122 | + ISA = ISAtmosphere() |
| 123 | + pressure, _, _, speed_of_sound = ISA.state(altitude) |
| 124 | + |
| 125 | + mach = tas / speed_of_sound |
| 126 | + qc = pressure * ( math.pow(1 + 0.2*mach**2, 7/2) - 1) |
| 127 | + cas = ISA.StdSL_speed_of_sound * math.sqrt( 5 * ( math.pow(qc/ISA.StdSL_pressure + 1, 2/7) - 1) ) |
| 128 | + return cas |
| 129 | + |
| 130 | + |
| 131 | +class CAStoEAS(Function): |
| 132 | + """Convert calibrated airspeed (CAS) to equivalent airspeed (EAS). |
| 133 | + CAS and EAS in m/s altitude in m. |
| 134 | + """ |
| 135 | + |
| 136 | + input_port_labels = { |
| 137 | + "cas": 0, |
| 138 | + "altitude": 1 |
| 139 | + } |
| 140 | + |
| 141 | + output_port_labels = { |
| 142 | + "eas": 0 |
| 143 | + } |
| 144 | + |
| 145 | + def __init__(self): |
| 146 | + super().__init__(func=self._eval) |
| 147 | + |
| 148 | + def _eval(self, cas, altitude): |
| 149 | + """Assume m/s for input and output velocities and m for altitude.""" |
| 150 | + ISA = ISAtmosphere() |
| 151 | + _, density, _, _ = ISA.state(altitude) |
| 152 | + _, rho0, _, _ = ISA.state(0) # Standard sea level density |
| 153 | + eas = CAStoTAS()._eval(cas, altitude) * math.sqrt(density / rho0) |
| 154 | + return eas |
| 155 | + |
| 156 | + |
| 157 | +class EAStoTAS(Function): |
| 158 | + """Convert equivalent airspeed (EAS) to true airspeed (TAS). |
| 159 | + EAS and TAS in m/s altitude in m. |
| 160 | + """ |
| 161 | + |
| 162 | + input_port_labels = { |
| 163 | + "eas": 0, |
| 164 | + "altitude": 1 |
| 165 | + } |
| 166 | + |
| 167 | + output_port_labels = { |
| 168 | + "tas": 0 |
| 169 | + } |
| 170 | + |
| 171 | + def __init__(self): |
| 172 | + super().__init__(func=self._eval) |
| 173 | + |
| 174 | + def _eval(self, eas, altitude): |
| 175 | + """Assume m/s for input and output velocities and m for altitude.""" |
| 176 | + ISA = ISAtmosphere() |
| 177 | + _, density, _, _ = ISA.state(altitude) |
| 178 | + _, rho0, _, _ = ISA.state(0) # Standard sea level density |
| 179 | + tas = eas * math.sqrt(rho0 / density) |
| 180 | + return tas |
| 181 | + |
| 182 | + |
| 183 | +class MachtoCAS(Function): |
| 184 | + """Convert Mach value to calibrated airspeed (CAS). |
| 185 | + CAS in m/s altitude in m. |
| 186 | + """ |
| 187 | + |
| 188 | + input_port_labels = { |
| 189 | + "mach": 0, |
| 190 | + "altitude": 1 |
| 191 | + } |
| 192 | + |
| 193 | + output_port_labels = { |
| 194 | + "cas": 0 |
| 195 | + } |
| 196 | + |
| 197 | + def __init__(self): |
| 198 | + super().__init__(func=self._eval) |
| 199 | + |
| 200 | + def _eval(mach, altitude): |
| 201 | + """Assume m for altitude.""" |
| 202 | + ISA = ISAtmosphere() |
| 203 | + _, _, _, speed_of_sound = ISA.state(altitude) |
| 204 | + return TAStoCAS()._eval(mach * speed_of_sound) |
| 205 | + |
0 commit comments