You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
14 KiB

  1. # INAV Programming Framework
  2. INAV Programming Framework (abbr. IPF) is a mechanism that allows to evaluate cenrtain flight parameters (RC channels, switches, altitude, distance, timers, other logic conditions) and use the value of evaluated expression in different places of INAV. Currently, the result of LCs can be used in:
  3. * [Servo mixer](Mixer.md) to activate/deactivate certain servo mix rulers
  4. * To activate/deactivate system overrides
  5. INAV Programming Framework coinsists of:
  6. * Logic Conditions - each Logic Condition can be understood as a single command, a single line of code
  7. * Global Variables - variables that can store values from and for LogiC Conditions and servo mixer
  8. * Programming PID - general purpose, user configurable PID controllers
  9. IPF can be edited using INAV Configurator user interface, of via CLI
  10. ## Logic Conditions
  11. ### CLI
  12. `logic <rule> <enabled> <activatorId> <operation> <operand A type> <operand A value> <operand B type> <operand B value> <flags>`
  13. * `<rule>` - ID of Logic Condition rule
  14. * `<enabled>` - `0` evaluates as disabled, `1` evaluates as enabled
  15. * `<activatorId>` - the ID of _LogicCondition_ used to activate this _Condition_. _Logic Condition_ will be evaluated only then Activator evaluates as `true`. `-1` evaluates as `true`
  16. * `<operation>` - See `Operations` paragraph
  17. * `<operand A type>` - See `Operands` paragraph
  18. * `<operand A value>` - See `Operands` paragraph
  19. * `<operand B type>` - See `Operands` paragraph
  20. * `<operand B value>` - See `Operands` paragraph
  21. * `<flags>` - See `Flags` paragraph
  22. ### Operations
  23. | Operation ID | Name | Notes |
  24. |---- |---- |---- |
  25. | 0 | TRUE | Always evaluates as true |
  26. | 1 | EQUAL | Evaluates `false` if `false` or `0` |
  27. | 2 | GREATER_THAN | |
  28. | 3 | LOWER_THAN | |
  29. | 4 | LOW | `true` if `<1333` |
  30. | 5 | MID | `true` if `>=1333 and <=1666` |
  31. | 6 | HIGH | `true` if `>1666` |
  32. | 7 | AND | |
  33. | 8 | OR | |
  34. | 9 | XOR | |
  35. | 10 | NAND | |
  36. | 11 | NOR | |
  37. | 12 | NOT | |
  38. | 13 | STICKY | `Operand A` is activation operator, `Operand B` is deactivation operator. After activation, operator will return `true` until Operand B is evaluated as `true`|
  39. | 14 | ADD | Add `Operand A` to `Operand B` and returns the result |
  40. | 15 | SUB | Substract `Operand B` from `Operand A` and returns the result |
  41. | 16 | MUL | Multiply `Operand A` by `Operand B` and returns the result |
  42. | 17 | DIV | Divide `Operand A` by `Operand B` and returns the result |
  43. | 18 | GVAR SET | Store value from `Operand B` into the Global Variable addressed by `Operand B`. Bear in mind, that operand `Global Variable` means: Value stored in Global Variable of an index! To store in GVAR 1 use `Value 1` not `Global Variable 1` |
  44. | 19 | GVAR INC | Increase the GVAR indexed by `Operand A` with value from `Operand B` |
  45. | 20 | GVAR DEC | Decrease the GVAR indexed by `Operand A` with value from `Operand B` |
  46. | 21 | IO PORT SET | Set I2C IO Expander pin `Operand A` to value of `Operand B`. `Operand A` accepts values `0-7` and `Operand B` accepts `0` and `1` |
  47. | 22 | OVERRIDE_ARMING_SAFETY | Allows to arm on any angle even without GPS fix |
  48. | 23 | OVERRIDE_THROTTLE_SCALE | Override throttle scale to the value defined by operand. Operand type `0` and value `50` means throttle will be scaled by 50%. |
  49. | 24 | SWAP_ROLL_YAW | basically, when activated, yaw stick will control roll and roll stick will control yaw. Required for tail-sitters VTOL during vertical-horizonral transition when body frame changes |
  50. | 25 | SET_VTX_POWER_LEVEL | Sets VTX power level. Accepted values are `0-3` for SmartAudio and `0-4` for Tramp protocol |
  51. | 26 | INVERT_ROLL | Inverts ROLL axis input for PID/PIFF controller |
  52. | 27 | INVERT_PITCH | Inverts PITCH axis input for PID/PIFF controller |
  53. | 28 | INVERT_YAW | Inverts YAW axis input for PID/PIFF controller |
  54. | 29 | OVERRIDE_THROTTLE | Override throttle value that is fed to the motors by mixer. Operand is scaled in us. `1000` means throttle cut, `1500` means half throttle |
  55. | 30 | SET_VTX_BAND | Sets VTX band. Accepted values are `1-5` |
  56. | 31 | SET_VTX_CHANNEL | Sets VTX channel. Accepted values are `1-8` |
  57. | 32 | SET_OSD_LAYOUT | Sets OSD layout. Accepted values are `0-3` |
  58. | 33 | SIN | Computes SIN of `Operand A` value in degrees. Output is multiplied by `Operand B` value. If `Operand B` is `0`, result is multiplied by `500` |
  59. | 34 | COS | Computes COS of `Operand A` value in degrees. Output is multiplied by `Operand B` value. If `Operand B` is `0`, result is multiplied by `500` |
  60. | 35 | TAN | Computes TAN of `Operand A` value in degrees. Output is multiplied by `Operand B` value. If `Operand B` is `0`, result is multiplied by `500` |
  61. | 36 | MAP_INPUT | Scales `Operand A` from [`0` : `Operand B`] to [`0` : `1000`]. Note: input will be constrained and then scaled |
  62. | 37 | MAP_OUTPUT | Scales `Operand A` from [`0` : `1000`] to [`0` : `Operand B`]. Note: input will be constrained and then scaled |
  63. | 38 | RC_CHANNEL_OVERRIDE | Overrides channel set by `Operand A` to value of `Operand B` |
  64. | 39 | SET_HEADING_TARGET | Sets heading-hold target to `Operand A`, in degrees. Value wraps-around. |
  65. | 40 | MOD | Divide `Operand A` by `Operand B` and returns the remainder |
  66. ### Operands
  67. | Operand Type | Name | Notes |
  68. |---- |---- |---- |
  69. | 0 | VALUE | Value derived from `value` field |
  70. | 1 | GET_RC_CHANNEL | `value` points to RC channel number, indexed from 1 |
  71. | 2 | FLIGHT | `value` points to flight parameter table |
  72. | 3 | FLIGHT_MODE | `value` points to flight modes table |
  73. | 4 | LC | `value` points to other logic condition ID |
  74. | 5 | GVAR | Value stored in Global Variable indexed by `value`. `GVAR 1` means: value in GVAR 1 |
  75. | 5 | PID | Output of a Programming PID indexed by `value`. `PID 1` means: value in PID 1 |
  76. #### FLIGHT
  77. | Operand Value | Name | Notes |
  78. |---- |---- |---- |
  79. | 0 | ARM_TIMER | in `seconds` |
  80. | 1 | HOME_DISTANCE | in `meters` |
  81. | 2 | TRIP_DISTANCE | in `meters` |
  82. | 3 | RSSI | |
  83. | 4 | VBAT | in `Volts * 100`, eg. `12.1V` is `1210` |
  84. | 5 | CELL_VOLTAGE | in `Volts * 100`, eg. `12.1V` is `1210` |
  85. | 6 | CURRENT | in `Amps * 100`, eg. `9A` is `900` |
  86. | 7 | MAH_DRAWN | in `mAh` |
  87. | 8 | GPS_SATS | |
  88. | 9 | GROUD_SPEED | in `cm/s` |
  89. | 10 | 3D_SPEED | in `cm/s` |
  90. | 11 | AIR_SPEED | in `cm/s` |
  91. | 12 | ALTITUDE | in `cm` |
  92. | 13 | VERTICAL_SPEED | in `cm/s` |
  93. | 14 | TROTTLE_POS | in `%` |
  94. | 15 | ATTITUDE_ROLL | in `degrees` |
  95. | 16 | ATTITUDE_PITCH | in `degrees` |
  96. | 17 | IS_ARMED | boolean `0`/`1` |
  97. | 18 | IS_AUTOLAUNCH | boolean `0`/`1` |
  98. | 19 | IS_ALTITUDE_CONTROL | boolean `0`/`1` |
  99. | 20 | IS_POSITION_CONTROL | boolean `0`/`1` |
  100. | 21 | IS_EMERGENCY_LANDING | boolean `0`/`1` |
  101. | 22 | IS_RTH | boolean `0`/`1` |
  102. | 23 | IS_WP | boolean `0`/`1` |
  103. | 24 | IS_LANDING | boolean `0`/`1` |
  104. | 25 | IS_FAILSAFE | boolean `0`/`1` |
  105. | 26 | STABILIZED_ROLL | Roll PID controller output `[-500:500]` |
  106. | 27 | STABILIZED_PITCH | Pitch PID controller output `[-500:500]` |
  107. | 28 | STABILIZED_YAW | Yaw PID controller output `[-500:500]` |
  108. | 29 | ACTIVE_WAYPOINT_INDEX | Indexed from `1`. To verify WP is in progress, use `IS_WP` |
  109. | 30 | ACTIVE_WAYPOINT_ACTION | See ACTIVE_WAYPOINT_ACTION paragraph |
  110. | 31 | 3D HOME_DISTANCE | in `meters`, calculated from HOME_DISTANCE and ALTITUDE using Pythagorean theorem |
  111. | 32 | CROSSFIRE LQ | Crossfire Link quality as returned by the CRSF protocol |
  112. | 33 | CROSSFIRE SNR | Crossfire SNR as returned by the CRSF protocol |
  113. | 34 | GPS_VALID | boolean `0`/`1`. True when the GPS has a valid 3D Fix |
  114. #### ACTIVE_WAYPOINT_ACTION
  115. | Action | Value |
  116. |---- |---- |
  117. | WAYPOINT | 1 |
  118. | HOLD_TIME | 3 |
  119. | RTH | 4 |
  120. | SET_POI | 5 |
  121. | JUMP | 6 |
  122. | SET_HEAD | 7 |
  123. | LAND | 8 |
  124. #### FLIGHT_MODE
  125. | Operand Value | Name | Notes |
  126. |---- |---- |---- |
  127. | 0 | FAILSAFE | |
  128. | 1 | MANUAL | |
  129. | 2 | RTH | |
  130. | 3 | POSHOLD | |
  131. | 4 | CRUISE | |
  132. | 5 | ALTHOLD | |
  133. | 6 | ANGLE | |
  134. | 7 | HORIZON | |
  135. | 8 | AIR | |
  136. | 9 | USER1 | |
  137. | 10 | USER2 | |
  138. ### Flags
  139. All flags are reseted on ARM and DISARM event.
  140. | bit | Decimal | Function |
  141. |---- |---- |---- |
  142. | 0 | 1 | Latch - after activation LC will stay active until LATCH flag is reseted |
  143. ## Global variables
  144. ### CLI
  145. `gvar <index> <default value> <min> <max>`
  146. ## Programming PID
  147. `pid <index> <enabled> <setpoint type> <setpoint value> <measurement type> <measurement value> <P gain> <I gain> <D gain> <FF gain>`
  148. * `<index>` - ID of PID Controller, starting from `0`
  149. * `<enabled>` - `0` evaluates as disabled, `1` evaluates as enabled
  150. * `<setpoint type>` - See `Operands` paragraph
  151. * `<setpoint value>` - See `Operands` paragraph
  152. * `<measurement type>` - See `Operands` paragraph
  153. * `<measurement value>` - See `Operands` paragraph
  154. * `<P gain>` - P-gain, scaled to `1/1000`
  155. * `<I gain>` - I-gain, scaled to `1/1000`
  156. * `<D gain>` - D-gain, scaled to `1/1000`
  157. * `<FF gain>` - FF-gain, scaled to `1/1000`
  158. ## Examples
  159. ### Dynamic THROTTLE scale
  160. `logic 0 1 0 23 0 50 0 0 0`
  161. Limits the THROTTLE output to 50% when Logic Condition `0` evaluates as `true`
  162. ### Set VTX power level via Smart Audio
  163. `logic 0 1 0 25 0 3 0 0 0`
  164. Sets VTX power level to `3` when Logic Condition `0` evaluates as `true`
  165. ### Invert ROLL and PITCH when rear facing camera FPV is used
  166. Solves the problem from [https://github.com/iNavFlight/inav/issues/4439](https://github.com/iNavFlight/inav/issues/4439)
  167. ```
  168. logic 0 1 0 26 0 0 0 0 0
  169. logic 1 1 0 27 0 0 0 0 0
  170. ```
  171. Inverts ROLL and PITCH input when Logic Condition `0` evaluates as `true`. Moving Pitch stick up will cause pitch down (up for rear facing camera). Moving Roll stick right will cause roll left of a quad (right in rear facing camera)
  172. ### Cut motors but keep other throttle bindings active
  173. `logic 0 1 0 29 0 1000 0 0 0`
  174. Sets Thhrottle output to `0%` when Logic Condition `0` evaluates as `true`
  175. ### Set throttle to 50% and keep other throttle bindings active
  176. `logic 0 1 0 29 0 1500 0 0 0`
  177. Sets Thhrottle output to about `50%` when Logic Condition `0` evaluates as `true`
  178. ### Set throttle control to different RC channel
  179. `logic 0 1 0 29 1 7 0 0 0`
  180. If Logic Condition `0` evaluates as `true`, motor throttle control is bound to RC channel 7 instead of throttle channel
  181. ### Set VTX channel with a POT
  182. Set VTX channel with a POT on the radio assigned to RC channel 6
  183. ```
  184. logic 0 1 -1 15 1 6 0 1000 0
  185. logic 1 1 -1 37 4 0 0 7 0
  186. logic 2 1 -1 14 4 1 0 1 0
  187. logic 3 1 -1 31 4 2 0 0 0
  188. ```
  189. Steps:
  190. 1. Normalize range `[1000:2000]` to `[0:1000]` by substracting `1000`
  191. 2. Scale range `[0:1000]` to `[0:7]`
  192. 3. Increase range by `1` to have the range of `[1:8]`
  193. 4. Assign LC#2 to VTX channel function
  194. ### Set VTX power with a POT
  195. Set VTX power with a POT on the radio assigned to RC channel 6. In this example we scale POT to 4 power level `[1:4]`
  196. ```
  197. logic 0 1 -1 15 1 6 0 1000 0
  198. logic 1 1 -1 37 4 0 0 3 0
  199. logic 2 1 -1 14 4 1 0 1 0
  200. logic 3 1 -1 25 4 2 0 0 0
  201. ```
  202. Steps:
  203. 1. Normalize range [1000:2000] to [0:1000] by substracting `1000`
  204. 2. Scale range [0:1000] to [0:3]
  205. 3. Increase range by `1` to have the range of [1:4]
  206. 4. Assign LC#2 to VTX power function