| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGFunction.h |
| 4 | Author: Jon Berndt |
| 5 | Date started: August 25 2004 |
| 6 | |
| 7 | ------------- Copyright (C) 2001 Jon S. Berndt ([email protected]) ------------- |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify it under |
| 10 | the terms of the GNU Lesser General Public License as published by the Free |
| 11 | Software Foundation; either version 2 of the License, or (at your option) any |
| 12 | later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 17 | details. |
| 18 | |
| 19 | You should have received a copy of the GNU Lesser General Public License along |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 21 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | Further information about the GNU Lesser General Public License can also be |
| 24 | found on the world wide web at http://www.gnu.org. |
| 25 | |
| 26 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 27 | SENTRY |
| 28 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 29 | |
| 30 | #ifndef FGFUNCTION_H |
| 31 | #define FGFUNCTION_H |
| 32 | |
| 33 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 34 | INCLUDES |
| 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 36 | |
| 37 | #include "FGParameter.h" |
| 38 | #include "input_output/FGPropertyManager.h" |
| 39 | |
| 40 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 41 | FORWARD DECLARATIONS |
| 42 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 43 | |
| 44 | namespace JSBSim { |
| 45 | |
| 46 | class Element; |
| 47 | class FGPropertyValue; |
| 48 | class FGFDMExec; |
| 49 | |
| 50 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 51 | CLASS DOCUMENTATION |
| 52 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 53 | |
| 54 | /** Represents a mathematical function. |
| 55 | The FGFunction class is a powerful and versatile resource that allows |
| 56 | algebraic functions to be defined in a JSBSim configuration file. It is |
| 57 | similar in concept to MathML (Mathematical Markup Language, www.w3.org/Math/), |
| 58 | but simpler and more terse. |
| 59 | A function definition consists of an operation, a value, a table, or a property |
| 60 | (which evaluates to a value). The currently supported operations are: |
| 61 | - sum (takes n args) |
| 62 | - difference (takes n args) |
| 63 | - product (takes n args) |
| 64 | - quotient (takes 2 args) |
| 65 | - pow (takes 2 args) |
| 66 | - sqrt (takes one argument) |
| 67 | - toradians (takes one argument) |
| 68 | - todegrees (takes one argument) |
| 69 | - exp (takes 2 args) |
| 70 | - log2 (takes 1 arg) |
| 71 | - ln (takes 1 arg) |
| 72 | - log10 (takes 1 arg) |
| 73 | - abs (takes 1 arg) |
| 74 | - sin (takes 1 arg) |
| 75 | - cos (takes 1 arg) |
| 76 | - tan (takes 1 arg) |
| 77 | - asin (takes 1 arg) |
| 78 | - acos (takes 1 arg) |
| 79 | - atan (takes 1 arg) |
| 80 | - atan2 (takes 2 args) |
| 81 | - min (takes n args) |
| 82 | - max (takes n args) |
| 83 | - avg (takes n args) |
| 84 | - fraction |
| 85 | - mod |
| 86 | - floor (takes 1 arg) |
| 87 | - ceil (takes 1 arg) |
| 88 | - fmod (takes 2 args) |
| 89 | - lt (less than, takes 2 args) |
| 90 | - le (less equal, takes 2 args) |
| 91 | - gt (greater than, takes 2 args) |
| 92 | - ge (greater than, takes 2 args) |
| 93 | - eq (equal, takes 2 args) |
| 94 | - nq (not equal, takes 2 args) |
| 95 | - and (takes n args) |
| 96 | - or (takes n args) |
| 97 | - not (takes 1 args) |
| 98 | - if-then (takes 2-3 args) |
| 99 | - switch (takes 2 or more args) |
| 100 | - random (Gaussian distribution random number) |
| 101 | - urandom (Uniform random number between -1 and +1) |
| 102 | - pi |
| 103 | - integer |
| 104 | - interpolate 1-dimensional (takes a minimum of five arguments, odd number) |
| 105 | |
| 106 | An operation is defined in the configuration file as in the following example: |
| 107 | |
| 108 | @code |
| 109 | <sum> |
| 110 | <value> 3.14159 </value> |
| 111 | <property> velocities/qbar </property> |
| 112 | <product> |
| 113 | <value> 0.125 </value> |
| 114 | <property> metrics/wingarea </property> |
| 115 | </product> |
| 116 | </sum> |
| 117 | @endcode |
| 118 | |
| 119 | A full function definition, such as is used in the aerodynamics section of a |
| 120 | configuration file includes the function element, and other elements. It should |
| 121 | be noted that there can be only one non-optional (non-documentation) element - |
| 122 | that is, one operation element - in the top-level function definition. |
| 123 | Multiple value and/or property elements cannot be immediate child |
| 124 | members of the function element. Almost always, the first operation within the |
| 125 | function element will be a product or sum. For example: |
| 126 | |
| 127 | @code |
| 128 | <function name="aero/moment/Clr"> |
| 129 | <description>Roll moment due to yaw rate</description> |
| 130 | <product> |
| 131 | <property>aero/qbar-area</property> |
| 132 | <property>metrics/bw-ft</property> |
| 133 | <property>aero/bi2vel</property> |
| 134 | <property>velocities/r-aero-rad_sec</property> |
| 135 | <table> |
| 136 | <independentVar>aero/alpha-rad</independentVar> |
| 137 | <tableData> |
| 138 | 0.000 0.08 |
| 139 | 0.094 0.19 |
| 140 | </tableData> |
| 141 | </table> |
| 142 | </product> |
| 143 | </function> |
| 144 | @endcode |
| 145 | |
| 146 | The "lowest level" in a function is always a value or a property, which cannot |
| 147 | itself contain another element. As shown, operations can contain values, |
| 148 | properties, tables, or other operations. In the first above example, the sum |
| 149 | element contains all three. What is evaluated is written algebraically as: |
| 150 | |
| 151 | @code 3.14159 + qbar + (0.125 * wingarea) @endcode |
| 152 | |
| 153 | Some operations can take only a single argument. That argument, however, can be |
| 154 | an operation (such as sum) which can contain other items. The point to keep in |
| 155 | mind is that it evaluates to a single value - which is just what the |
| 156 | trigonometric functions require (except atan2, which takes two arguments). |
| 157 | |
| 158 | <h2>Specific Function Definitions</h2> |
| 159 | |
| 160 | Note: In the definitions below, a "property" refers to a single property |
| 161 | specified within either the <property></property> tag or the shortcut tag, |
| 162 | \<p>\</p>. The keyword "value" refers to a single numeric value specified either |
| 163 | within the \<value>\</value> tag or the shortcut <v></v> tag. The keyword |
| 164 | "table" refers to a single table specified either within the \<table>\</table> |
| 165 | tag or the shortcut <t></t> tag. The plural form of any of the three words |
| 166 | refers to one or more instances of a property, value, or table. |
| 167 | |
| 168 | - @b sum, sums the values of all immediate child elements: |
| 169 | @code |
| 170 | <sum> |
| 171 | {properties, values, tables, or other function elements} |
| 172 | </sum> |
| 173 | |
| 174 | Example: Mach + 0.01 |
| 175 | |
| 176 | <sum> |
| 177 | <p> velocities/mach </p> |
| 178 | <v> 0.01 </v> |
| 179 | </sum> |
| 180 | @endcode |
| 181 | - @b difference, subtracts the values of all immediate child elements from the |
| 182 | value of the first child element: |
| 183 | @code |
| 184 | <difference> |
| 185 | {properties, values, tables, or other function elements} |
| 186 | </difference> |
| 187 | |
| 188 | Example: Mach - 0.01 |
| 189 | |
| 190 | <difference> |
| 191 | <p> velocities/mach </p> |
| 192 | <v> 0.01 </v> |
| 193 | </difference> |
| 194 | @endcode |
| 195 | - @b product multiplies together the values of all immediate child elements: |
| 196 | @code |
| 197 | <product> |
| 198 | {properties, values, tables, or other function elements} |
| 199 | </product> |
| 200 | |
| 201 | Example: qbar*S*beta*CY_beta |
| 202 | |
| 203 | <product> |
| 204 | <property> aero/qbar-psf </property> |
| 205 | <property> metrics/Sw-sqft </property> |
| 206 | <property> aero/beta-rad </property> |
| 207 | <property> aero/coefficient/CY_beta </property> |
| 208 | </product> |
| 209 | @endcode |
| 210 | - @b quotient, divides the value of the first immediate child element by the |
| 211 | second immediate child element: |
| 212 | @code |
| 213 | <quotient> |
| 214 | {property, value, table, or other function element} |
| 215 | {property, value, table, or other function element} |
| 216 | </quotient> |
| 217 | |
| 218 | Example: (2*GM)/R |
| 219 | |
| 220 | <quotient> |
| 221 | <product> |
| 222 | <v> 2.0 </v> |
| 223 | <p> guidance/executive/gm </p> |
| 224 | </product> |
| 225 | <p> position/radius-to-vehicle-ft </p> |
| 226 | </quotient> |
| 227 | @endcode |
| 228 | - @b pow, raises the value of the first immediate child element to the power of |
| 229 | the value of the second immediate child element: |
| 230 | @code |
| 231 | <pow> |
| 232 | {property, value, table, or other function element} |
| 233 | {property, value, table, or other function element} |
| 234 | </pow> |
| 235 | |
| 236 | Example: Mach^2 |
| 237 | |
| 238 | <pow> |
| 239 | <p> velocities/mach </p> |
| 240 | <v> 2.0 </v> |
| 241 | </pow> |
| 242 | @endcode |
| 243 | - @b sqrt, takes the square root of the value of the immediate child element: |
| 244 | @code |
| 245 | <sqrt> |
| 246 | {property, value, table, or other function element} |
| 247 | </sqrt> |
| 248 | |
| 249 | Example: square root of 25 |
| 250 | |
| 251 | <sqrt> <v> 25.0 </v> </sqrt> |
| 252 | @endcode |
| 253 | - @b toradians, converts a presumed argument in degrees to radians by |
| 254 | multiplying the value of the immediate child element by pi/180: |
| 255 | @code |
| 256 | <toradians> |
| 257 | {property, value, table, or other function element} |
| 258 | </toradians> |
| 259 | |
| 260 | Example: convert 45 degrees to radians |
| 261 | |
| 262 | <toradians> <v> 45 </v> </toradians> |
| 263 | @endcode |
| 264 | - @b todegrees, converts a presumed argument in radians to degrees by |
| 265 | multiplying the value of the immediate child element by 180/pi: |
| 266 | @code |
| 267 | <todegrees> |
| 268 | {property, value, table, or other function element} |
| 269 | </todegrees> |
| 270 | |
| 271 | Example: convert 0.5*pi radians to degrees |
| 272 | |
| 273 | <todegrees> |
| 274 | <product> <v> 0.5 </v> <pi/> </product> |
| 275 | </todegrees> |
| 276 | @endcode |
| 277 | - @b exp, raises "e" to the power of the immediate child element: |
| 278 | @code |
| 279 | <exp> |
| 280 | {property, value, table, or other function element} |
| 281 | </exp> |
| 282 | |
| 283 | Example: raise "e" to the 1.5 power, e^1.5 |
| 284 | |
| 285 | <exp> <v> 1.5 </v> </exp> |
| 286 | @endcode |
| 287 | - @b log2, calculates the log base 2 value of the immediate child element: |
| 288 | @code |
| 289 | <log2> |
| 290 | {property, value, table, or other function element} |
| 291 | </log2> |
| 292 | |
| 293 | Example: |
| 294 | <log2> <v> 128 </v> </log2> |
| 295 | @endcode |
| 296 | - @b ln, calculates the natural logarithm of the value of the immediate child |
| 297 | element: |
| 298 | @code |
| 299 | <ln> |
| 300 | {property, value, table, or other function element} |
| 301 | </ln> |
| 302 | |
| 303 | Example: ln(128) |
| 304 | |
| 305 | <ln> <v> 200 </v> </ln> |
| 306 | @endcode |
| 307 | - @b log10, calculates the base 10 logarithm of the value of the immediate child |
| 308 | element |
| 309 | @code |
| 310 | <log10> |
| 311 | {property, value, table, or other function element} |
| 312 | </log10> |
| 313 | |
| 314 | Example log(Mach) |
| 315 | |
| 316 | <log10> <p> velocities/mach </p> </log10> |
| 317 | @endcode |
| 318 | - @b abs calculates the absolute value of the immediate child element |
| 319 | @code |
| 320 | <abs> |
| 321 | {property, value, table, or other function element} |
| 322 | </abs> |
| 323 | |
| 324 | Example: |
| 325 | |
| 326 | <abs> <p> flight-path/gamma-rad </p> </abs> |
| 327 | @endcode |
| 328 | - @b sin, calculates the sine of the value of the immediate child element (the |
| 329 | argument is expected to be in radians) |
| 330 | @code |
| 331 | <sin> |
| 332 | {property, value, table, or other function element} |
| 333 | </sin> |
| 334 | |
| 335 | Example: |
| 336 | |
| 337 | <sin> <toradians> <p> fcs/heading-true-degrees </p> </toradians> </sin> |
| 338 | @endcode |
| 339 | - @b cos, calculates the cosine of the value of the immediate child element (the |
| 340 | argument is expected to be in radians) |
| 341 | @code |
| 342 | <cos> |
| 343 | {property, value, table, or other function element} |
| 344 | </cos> |
| 345 | |
| 346 | Example: |
| 347 | |
| 348 | <cos> <toradians> <p> fcs/heading-true-degrees </p> </toradians> </cos> |
| 349 | @endcode |
| 350 | - @b tan, calculates the tangent of the value of the immediate child element |
| 351 | (the argument is expected to be in radians) |
| 352 | @code |
| 353 | <tan> |
| 354 | {property, value, table, or other function element} |
| 355 | </tan> |
| 356 | |
| 357 | Example: |
| 358 | |
| 359 | <tan> <toradians> <p> fcs/heading-true-degrees </p> </toradians> </tan> |
| 360 | @endcode |
| 361 | - @b asin, calculates the arcsine (inverse sine) of the value of the immediate |
| 362 | child element. The value provided should be in the range from -1 to |
| 363 | +1. The value returned will be expressed in radians, and will be in |
| 364 | the range from -pi/2 to +pi/2. |
| 365 | @code |
| 366 | <asin> |
| 367 | {property, value, table, or other function element} |
| 368 | </asin> |
| 369 | |
| 370 | Example: |
| 371 | |
| 372 | <asin> <v> 0.5 </v> </asin> |
| 373 | @endcode |
| 374 | - @b acos, calculates the arccosine (inverse cosine) of the value of the |
| 375 | immediate child element. The value provided should be in the range |
| 376 | from -1 to +1. The value returned will be expressed in radians, and |
| 377 | will be in the range from 0 to pi. |
| 378 | @code |
| 379 | <acos> |
| 380 | {property, value, table, or other function element} |
| 381 | </acos> |
| 382 | |
| 383 | Example: |
| 384 | |
| 385 | <acos> <v> 0.5 </v> </acos> |
| 386 | @endcode |
| 387 | - @b atan, calculates the inverse tangent of the value of the immediate child |
| 388 | element. The value returned will be expressed in radians, and will |
| 389 | be in the range from -pi/2 to +pi/2. |
| 390 | @code |
| 391 | <atan> |
| 392 | {property, value, table, or other function element} |
| 393 | </atan> |
| 394 | |
| 395 | Example: |
| 396 | |
| 397 | <atan> <v> 0.5 </v> </atan> |
| 398 | @endcode |
| 399 | - @b atan2 calculates the inverse tangent of the value of the immediate child |
| 400 | elements, Y/X (in that order). It even works for X values near zero. |
| 401 | The value returned will be expressed in radians, and in the range |
| 402 | -pi to +pi. |
| 403 | @code |
| 404 | <atan2> |
| 405 | {property, value, table, or other function element} {property, value, table, or other function element} |
| 406 | </atan2> |
| 407 | |
| 408 | Example: inverse tangent of 0.5/0.25, evaluates to: 1.107 radians |
| 409 | |
| 410 | <atan2> <v> 0.5 </<v> <v> 0.25 </v> </atan2> |
| 411 | @endcode |
| 412 | - @b min returns the smallest value from all the immediate child elements |
| 413 | @code |
| 414 | <min> |
| 415 | {properties, values, tables, or other function elements} |
| 416 | </min> |
| 417 | |
| 418 | Example: returns the lesser of velocity and 2500 |
| 419 | |
| 420 | <min> |
| 421 | <p> velocities/eci-velocity-mag-fps </p> |
| 422 | <v> 2500.0 </v> |
| 423 | </min> |
| 424 | @endcode |
| 425 | - @b max returns the largest value from all the immediate child elements |
| 426 | @code |
| 427 | <max> |
| 428 | {properties, values, tables, or other function elements} |
| 429 | </max> |
| 430 | |
| 431 | Example: returns the greater of velocity and 15000 |
| 432 | |
| 433 | <max> |
| 434 | <p> velocities/eci-velocity-mag-fps </p> |
| 435 | <v> 15000.0 </v> |
| 436 | </max> |
| 437 | @endcode |
| 438 | - @b avg returns the average value of all the immediate child elements |
| 439 | @code |
| 440 | <avg> |
| 441 | {properties, values, tables, or other function elements} |
| 442 | </avg> |
| 443 | |
| 444 | Example: returns the average of the four numbers below, evaluates to 0.50. |
| 445 | |
| 446 | <avg> |
| 447 | <v> 0.25 </v> |
| 448 | <v> 0.50 </v> |
| 449 | <v> 0.75 </v> |
| 450 | <v> 0.50 </v> |
| 451 | </avg> |
| 452 | @endcode |
| 453 | - @b fraction, returns the fractional part of the value of the immediate child |
| 454 | element |
| 455 | @code |
| 456 | <fraction> |
| 457 | {property, value, table, or other function element} |
| 458 | </fraction> |
| 459 | |
| 460 | Example: returns the fractional part of pi - or, roughly, 0.1415926... |
| 461 | |
| 462 | <fraction> <pi/> </fraction> |
| 463 | @endcode |
| 464 | - @b integer, returns the integer portion of the value of the immediate child |
| 465 | element |
| 466 | @code |
| 467 | <integer> |
| 468 | {property, value, table, or other function element} |
| 469 | </integer> |
| 470 | @endcode |
| 471 | - @b mod returns the remainder from the integer division of the value of the |
| 472 | first immediate child element by the second immediate child element, |
| 473 | X/Y (X modulo Y). The value returned is the value X-I*Y, for the |
| 474 | largest integer I such that if Y is nonzero, the result has the |
| 475 | same sign as X and magnitude less than the magnitude of Y. For |
| 476 | instance, the expression "5 mod 2" would evaluate to 1 because 5 |
| 477 | divided by 2 leaves a quotient of 2 and a remainder of 1, while |
| 478 | "9 mod 3" would evaluate to 0 because the division of 9 by 3 has a |
| 479 | quotient of 3 and leaves a remainder of 0. |
| 480 | @code |
| 481 | <mod> |
| 482 | {property, value, table, or other function element} |
| 483 | {property, value, table, or other function element} |
| 484 | </mod> |
| 485 | |
| 486 | Example: 5 mod 2, evaluates to 1 |
| 487 | |
| 488 | <mod> <v> 5 </v> <v> 2 </v> </mod> |
| 489 | @endcode |
| 490 | - @b floor returns the largest integral value that is not greater than X. |
| 491 | @code |
| 492 | <floor> |
| 493 | {property, value, table, or other function element} |
| 494 | </floor> |
| 495 | @endcode |
| 496 | Examples: floor(2.3) evaluates to 2.0 while floor(-2.3) evaluates to -3.0 |
| 497 | - @b ceil returns the smallest integral value that is not less than X. |
| 498 | @code |
| 499 | <ceil> |
| 500 | {property, value, table, or other function element} |
| 501 | </ceil> |
| 502 | @endcode |
| 503 | Examples: ceil(2.3) evaluates to 3.0 while ceil(-2.3) evaluates to -2.0 |
| 504 | - @b fmod returns the floating-point remainder of X/Y (rounded towards zero) |
| 505 | @code |
| 506 | <fmod> |
| 507 | {property, value, table, or other function element} |
| 508 | {property, value, table, or other function element} |
| 509 | </fmod> |
| 510 | @endcode |
| 511 | Example: fmod(18.5, 4.2) evaluates to 1.7 |
| 512 | - @b lt returns a 1 if the value of the first immediate child element is less |
| 513 | than the value of the second immediate child element, returns 0 |
| 514 | otherwise |
| 515 | @code |
| 516 | <lt> |
| 517 | {property, value, table, or other function element} |
| 518 | {property, value, table, or other function element} |
| 519 | </lt> |
| 520 | |
| 521 | Example: returns 1 if thrust is less than 10,000, returns 0 otherwise |
| 522 | |
| 523 | <lt> |
| 524 | <p> propulsion/engine[2]/thrust-lbs </p> |
| 525 | <v> 10000.0 </v> |
| 526 | </lt> |
| 527 | @endcode |
| 528 | - @b le, returns a 1 if the value of the first immediate child element is less |
| 529 | than or equal to the value of the second immediate child element, |
| 530 | returns 0 otherwise |
| 531 | @code |
| 532 | <le> |
| 533 | {property, value, table, or other function element} |
| 534 | {property, value, table, or other function element} |
| 535 | </le> |
| 536 | |
| 537 | Example: returns 1 if thrust is less than or equal to 10,000, returns 0 otherwise |
| 538 | |
| 539 | <le> |
| 540 | <p> propulsion/engine[2]/thrust-lbs </p> |
| 541 | <v> 10000.0 </v> |
| 542 | </le> |
| 543 | @endcode |
| 544 | - @b gt returns a 1 if the value of the first immediate child element is greater |
| 545 | than the value of the second immediate child element, returns 0 |
| 546 | otherwise |
| 547 | @code |
| 548 | <gt> |
| 549 | {property, value, table, or other function element} |
| 550 | {property, value, table, or other function element} |
| 551 | </gt> |
| 552 | |
| 553 | Example: returns 1 if thrust is greater than 10,000, returns 0 otherwise |
| 554 | |
| 555 | <gt> |
| 556 | <p> propulsion/engine[2]/thrust-lbs </p> |
| 557 | <v> 10000.0 </v> |
| 558 | </gt> |
| 559 | @endcode |
| 560 | - @b ge, returns a 1 if the value of the first immediate child element is |
| 561 | greater than or equal to the value of the second immediate child |
| 562 | element, returns 0 otherwise |
| 563 | @code |
| 564 | <ge> |
| 565 | {property, value, table, or other function element} |
| 566 | {property, value, table, or other function element} |
| 567 | </ge> |
| 568 | |
| 569 | Example: returns 1 if thrust is greater than or equal to 10,000, returns 0 |
| 570 | otherwise |
| 571 | |
| 572 | <ge> |
| 573 | <p> propulsion/engine[2]/thrust-lbs </p> |
| 574 | <v> 10000.0 </v> |
| 575 | </ge> |
| 576 | @endcode |
| 577 | - @b eq returns a 1 if the value of the first immediate child element is |
| 578 | equal to the second immediate child element, returns 0 |
| 579 | otherwise |
| 580 | @code |
| 581 | <eq> |
| 582 | {property, value, table, or other function element} |
| 583 | {property, value, table, or other function element} |
| 584 | </eq> |
| 585 | |
| 586 | Example: returns 1 if thrust is equal to 10,000, returns 0 otherwise |
| 587 | |
| 588 | <eq> |
| 589 | <p> propulsion/engine[2]/thrust-lbs </p> |
| 590 | <v> 10000.0 </v> |
| 591 | </eq> |
| 592 | @endcode |
| 593 | - @b nq returns a 1 if the value of the first immediate child element is not |
| 594 | equal to the value of the second immediate child element, returns 0 |
| 595 | otherwise |
| 596 | @code |
| 597 | <nq> |
| 598 | {property, value, table, or other function element} |
| 599 | {property, value, table, or other function element} |
| 600 | </nq> |
| 601 | |
| 602 | Example: returns 1 if thrust is not 0, returns 0 otherwise |
| 603 | |
| 604 | <nq> |
| 605 | <p> propulsion/engine[2]/thrust-lbs </p> |
| 606 | <v> 0.0 </v> |
| 607 | </nq> |
| 608 | @endcode |
| 609 | - @b and returns a 1 if the values of the immediate child elements are all 1, |
| 610 | returns 0 otherwise. Values provided are expected to be either 1 or 0 |
| 611 | within machine precision. |
| 612 | @code |
| 613 | <and> |
| 614 | {properties, values, tables, or other function elements} |
| 615 | </and> |
| 616 | |
| 617 | Example: returns 1 if the specified flags are all 1 |
| 618 | |
| 619 | <and> |
| 620 | <p> guidance/first-stage-flight-flag </p> |
| 621 | <p> control/engines-running-flag </p> |
| 622 | </and> |
| 623 | @endcode |
| 624 | - @b or returns a 1 if the values of any of the immediate child elements 1, |
| 625 | returns 0 otherwise. Values provided are expected to be either 1 or 0 |
| 626 | within machine precision. |
| 627 | @code |
| 628 | <or> |
| 629 | {properties, values, tables, or other function elements} |
| 630 | </or> |
| 631 | |
| 632 | Example: returns 1 if any of the specified flags are 1 |
| 633 | |
| 634 | <or> |
| 635 | <p> guidance/first-stage-flight-flag </p> |
| 636 | <p> control/engines-running-flag </p> |
| 637 | </or> |
| 638 | @endcode |
| 639 | - @b not, returns the inverse of the value of the supplied immediate child |
| 640 | element (e.g., returns 1 if supplied a 0) |
| 641 | @code |
| 642 | <not> |
| 643 | {property, value, table, or other function element} |
| 644 | </not> |
| 645 | |
| 646 | Example: returns 0 if the value of the supplied flag is 1 |
| 647 | |
| 648 | <not> <p> guidance/first-stage-flight-flag </p> </not> |
| 649 | @endcode |
| 650 | - @b ifthen if the value of the first immediate child element is 1, then the |
| 651 | value of the second immediate child element is returned, otherwise |
| 652 | the value of the third child element is returned |
| 653 | @code |
| 654 | <ifthen> |
| 655 | {property, value, table, or other function element} |
| 656 | {property, value, table, or other function element} |
| 657 | {property, value, table, or other function element} |
| 658 | </ifthen> |
| 659 | |
| 660 | Example: if flight-mode is greater than 2, then a value of 0.00 is |
| 661 | returned, otherwise the value of the property control/pitch-lag is |
| 662 | returned. |
| 663 | |
| 664 | <ifthen> |
| 665 | <gt> <p> executive/flight-mode </p> <v> 2 </v> </gt> |
| 666 | <v> 0.00 </v> |
| 667 | <p> control/pitch-lag </p> |
| 668 | </ifthen> |
| 669 | @endcode |
| 670 | - @b switch uses the integer value of the first immediate child element as an |
| 671 | index to select one of the subsequent immediate child elements to |
| 672 | return the value of |
| 673 | @code |
| 674 | <switch> |
| 675 | {property, value, table, or other function element} |
| 676 | {property, value, table, or other function element} |
| 677 | {property, value, table, or other function element} |
| 678 | ... |
| 679 | </switch> |
| 680 | |
| 681 | Example: if flight-mode is 2, the switch function returns 0.50 |
| 682 | <switch> |
| 683 | <p> executive/flight-mode </p> |
| 684 | <v> 0.25 </v> |
| 685 | <v> 0.50 </v> |
| 686 | <v> 0.75 </v> |
| 687 | <v> 1.00 </v> |
| 688 | </switch> |
| 689 | @endcode |
| 690 | - @b random Returns a normal distributed random number. |
| 691 | The function, without parameters, returns a normal distributed |
| 692 | random value with a distribution defined by the parameters |
| 693 | mean = 0.0 and standard deviation (stddev) = 1.0 |
| 694 | The Mean of the distribution (its expected value, μ). |
| 695 | Which coincides with the location of its peak. |
| 696 | Standard deviation (σ): The square root of variance, |
| 697 | representing the dispersion of values from the distribution mean. |
| 698 | This shall be a positive value (σ>0). |
| 699 | @code |
| 700 | <random/> |
| 701 | <random seed="1234"/> |
| 702 | <random seed="time_now"/> |
| 703 | <random seed="time_now" mean="0.0" stddev="1.0"/> |
| 704 | @endcode |
| 705 | - @b urandom Returns a uniformly distributed random number. |
| 706 | The function, without parameters, returns a random value |
| 707 | between the minimum value -1.0 and the maximum value of 1.0 |
| 708 | The two maximum and minimum values can be modified using the |
| 709 | lower and upper parameters. |
| 710 | @code |
| 711 | <urandom/> |
| 712 | <random seed="1234"/> |
| 713 | <random seed="time_now"/> |
| 714 | <random seed="time_now" lower="-1.0" upper="1.0"/> |
| 715 | @endcode |
| 716 | - @b pi Takes no argument and returns the value of Pi |
| 717 | @code<pi/>@endcode |
| 718 | - @b interpolate1d returns the result from a 1-dimensional interpolation of the |
| 719 | supplied values, with the value of the first immediate child |
| 720 | element representing the lookup value into the table, and the |
| 721 | following pairs of values representing the independent and |
| 722 | dependent values. The first provided child element is |
| 723 | expected to be a property. The interpolation does not |
| 724 | extrapolate, but holds the highest value if the provided |
| 725 | lookup value goes outside of the provided range. |
| 726 | @code |
| 727 | <interpolate1d> |
| 728 | {property, value, table, or other function element} |
| 729 | {property, value, table, or other function element} {property, value, table, or other function element} |
| 730 | ... |
| 731 | </interpolate1d> |
| 732 | |
| 733 | Example: If mach is 0.4, the interpolation will return 0.375. If mach is |
| 734 | 1.5, the interpolation will return 0.60. |
| 735 | |
| 736 | <interpolate1d> |
| 737 | <p> velocities/mach </p> |
| 738 | <v> 0.00 </v> <v> 0.25 </v> |
| 739 | <v> 0.80 </v> <v> 0.50 </v> |
| 740 | <v> 0.90 </v> <v> 0.60 </v> |
| 741 | </interpolate1d> |
| 742 | @endcode |
| 743 | @author Jon Berndt |
| 744 | */ |
| 745 | |
| 746 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 747 | DECLARATION: FGFunction |
| 748 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 749 | |
| 750 | // Todo: Does this class need a copy constructor, like FGLGear? |
| 751 | |
| 752 | class FGFunction : public FGParameter, public FGJSBBase |
| 753 | { |
| 754 | public: |
| 755 | /// Default constructor. |
| 756 | FGFunction() |
| 757 | : cached(false), cachedValue(-HUGE_VAL), PropertyManager(nullptr), |
| 758 | pNode(nullptr), pCopyTo(nullptr) {} |
| 759 | |
| 760 | explicit FGFunction(FGPropertyManager* pm) |
| 761 | : FGFunction() |
| 762 | { PropertyManager = pm; } |
| 763 | |
| 764 | /** Constructor. |
| 765 | When this constructor is called, the XML element pointed to in memory by the |
| 766 | element argument is traversed. If other FGParameter-derived objects (values, |
| 767 | functions, properties, or tables) are encountered, this instance of the |
| 768 | FGFunction object will store a pointer to the found object and pass the |
| 769 | relevant Element pointer to the constructor for the new object. In other |
| 770 | words, each FGFunction object maintains a list of "child" |
| 771 | FGParameter-derived objects which in turn may each contain its own list, and |
| 772 | so on. At runtime, each object evaluates its child parameters, which each |
| 773 | may have its own child parameters to evaluate. |
| 774 | |
| 775 | @param PropertyManager a pointer to the property manager instance. |
| 776 | @param element a pointer to the Element object containing the function |
| 777 | definition. |
| 778 | @param prefix an optional prefix to prepend to the name given to the |
| 779 | property that represents this function (if given). |
| 780 | */ |
| 781 | FGFunction(FGFDMExec* fdmex, Element* element, const std::string& prefix="", |
| 782 | FGPropertyValue* var=0L); |
| 783 | |
| 784 | /** Destructor |
| 785 | Make sure the function is untied before destruction. |
| 786 | */ |
| 787 | ~FGFunction(void) override; |
| 788 | |
| 789 | /** Retrieves the value of the function object. |
| 790 | @return the total value of the function. */ |
| 791 | double GetValue(void) const override; |
| 792 | |
| 793 | /** The value that the function evaluates to, as a string. |
| 794 | @return the value of the function as a string. */ |
| 795 | std::string GetValueAsString(void) const; |
| 796 | |
| 797 | /// Retrieves the name of the function. |
| 798 | std::string GetName(void) const override {return Name;} |
| 799 | |
| 800 | /** Does the function always return the same result (i.e. does it apply to |
| 801 | constant parameters) ? */ |
| 802 | bool IsConstant(void) const override; |
| 803 | |
| 804 | /** Specifies whether to cache the value of the function, so it is calculated |
| 805 | only once per frame. |
| 806 | If shouldCache is true, then the value of the function is calculated, and a |
| 807 | flag is set so further calculations done this frame will use the cached |
| 808 | value. In order to turn off caching, cacheValue must be called with a false |
| 809 | argument. |
| 810 | @param shouldCache specifies whether the function should cache the computed |
| 811 | value. */ |
| 812 | void cacheValue(bool shouldCache); |
| 813 | |
| 814 | enum class OddEven {Either, Odd, Even}; |
| 815 | |
| 816 | protected: |
| 817 | bool cached; |
| 818 | double cachedValue; |
| 819 | std::vector <FGParameter_ptr> Parameters; |
| 820 | FGPropertyManager* PropertyManager; |
| 821 | FGPropertyNode_ptr pNode; |
| 822 | |
| 823 | void Load(Element* element, FGPropertyValue* var, FGFDMExec* fdmex, |
| 824 | const std::string& prefix=""); |
| 825 | virtual void bind(Element*, const std::string&); |
| 826 | void CheckMinArguments(Element* el, unsigned int _min); |
| 827 | void CheckMaxArguments(Element* el, unsigned int _max); |
| 828 | void CheckOddOrEvenArguments(Element* el, OddEven odd_even); |
| 829 | std::string CreateOutputNode(Element* el, const std::string& Prefix); |
| 830 | |
| 831 | private: |
| 832 | std::string Name; |
| 833 | FGPropertyNode_ptr pCopyTo; // Property node for CopyTo property string |
| 834 | |
| 835 | void Debug(int from); |
| 836 | }; |
| 837 | |
| 838 | } // namespace JSBSim |
| 839 | |
| 840 | #endif |