Skip to content

Commit fd9ca51

Browse files
committed
Docs: Add new JavaScript features (PID, flight modes, let/const, ternary)
Adds documentation for new INAV 9.0 JavaScript programming features: New Features: - PID controller output access (inav.pid[0-3].output) - Flight mode detection (inav.flight.mode.poshold, .rth, etc.) - Let/const variables for compile-time named expressions - Ternary operator for conditional value assignment Changes: - Add Variables section with let/const and ternary operator docs - Update Available Objects to list PID and flight modes - Add Flight Mode Detection subsection with examples - Add PID Controller Outputs subsection with examples - Update index.md feature list - Update Tips section for consistency All examples use namespaced syntax (inav.flight.*, inav.override.*, etc.) for clarity and explicitness.
1 parent 8ff1820 commit fd9ca51

File tree

2 files changed

+91
-15
lines changed

2 files changed

+91
-15
lines changed

docs/javascript_programming/JAVASCRIPT_PROGRAMMING_GUIDE.md

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,28 +198,101 @@ inav.events.sticky(
198198

199199
---
200200

201+
## Variables
202+
203+
### Let/Const Variables
204+
205+
Use `let` or `const` to define reusable expressions that are compiled into the logic:
206+
207+
```javascript
208+
// Define reusable calculations
209+
let distanceThreshold = 500;
210+
let altitudeLimit = 100;
211+
let combinedCondition = inav.flight.homeDistance > distanceThreshold && inav.flight.altitude > altitudeLimit;
212+
213+
// Use in conditions
214+
if (combinedCondition) {
215+
inav.override.vtx.power = 4;
216+
}
217+
```
218+
219+
**Benefits:**
220+
- Makes code more readable with named values
221+
- Compiler automatically optimizes duplicate expressions
222+
- Variables preserve their custom names through compile/decompile cycles
223+
224+
**Important:** `let`/`const` variables are **compile-time substituted**, not runtime variables. For runtime state, use `inav.gvar[]`.
225+
226+
### Ternary Operator
227+
228+
Use ternary expressions for conditional values:
229+
230+
```javascript
231+
// Assign based on condition
232+
let throttleLimit = inav.flight.cellVoltage < 330 ? 25 : 50;
233+
234+
if (inav.flight.cellVoltage < 350) {
235+
inav.override.throttleScale = throttleLimit;
236+
}
237+
238+
// Inline in expressions
239+
inav.override.vtx.power = inav.flight.homeDistance > 500 ? 4 : 2;
240+
```
241+
242+
**Use when:** You need conditional value assignment in a single expression.
243+
244+
---
245+
201246
## Available Objects
202247

248+
The `inav` namespace provides access to all flight controller data and control functions:
249+
250+
- `inav.flight` - Flight telemetry (including `flight.mode.*`)
251+
- `inav.override` - Override flight parameters
252+
- `inav.rc` - RC channels
253+
- `inav.gvar` - Global variables (0-7)
254+
- `inav.pid` - Programming PID outputs (`pid[0-3].output`)
255+
- `inav.waypoint` - Waypoint navigation
256+
- `inav.events.edge` - Edge detection
257+
- `inav.events.sticky` - Latching conditions
258+
- `inav.events.delay` - Delayed execution
259+
260+
### Flight Mode Detection
261+
262+
Check which flight modes are currently active via `inav.flight.mode.*`:
263+
264+
```javascript
265+
if (inav.flight.mode.poshold === 1) {
266+
inav.gvar[0] = 1; // Flag: in position hold
267+
}
268+
269+
if (inav.flight.mode.rth === 1) {
270+
inav.override.vtx.power = 4; // Max power during RTH
271+
}
272+
```
273+
274+
**Available modes:** `failsafe`, `manual`, `rth`, `poshold`, `cruise`, `althold`, `angle`, `horizon`, `air`, `acro`, `courseHold`, `waypointMission`, `user1` through `user4`
275+
276+
### PID Controller Outputs
277+
278+
Read output values from the 4 programming PID controllers (configured in Programming PID tab):
279+
203280
```javascript
204-
const {
205-
flight, // Flight telemetry
206-
override, // Override flight parameters
207-
rc, // RC channels
208-
gvar, // Global variables (0-7)
209-
waypoint, // Waypoint navigation
210-
edge, // Edge detection
211-
sticky, // Latching conditions
212-
delay // Delayed execution
213-
} = inav;
281+
if (inav.pid[0].output > 500) {
282+
inav.override.throttle = 1600;
283+
}
214284

285+
inav.gvar[0] = inav.pid[0].output; // Store for OSD display
215286
```
216287

288+
**Available:** `inav.pid[0].output` through `inav.pid[3].output`
289+
217290
---
218291

219292
## Tips
220293

221-
1. **Initialize variables on arm** using `edge()` with `flight.armTimer > 1000`
222-
2. **Use gvars for state** - they persist between logic condition evaluations
294+
1. **Initialize variables on arm** using `inav.events.edge()` with `inav.flight.armTimer > 1000`
295+
2. **Use inav.gvar for state** - they persist between logic condition evaluations
223296
3. **edge() duration = 0** means instant trigger on condition becoming true
224297
4. **edge() duration > 0** adds debounce time
225298
5. **if statements are continuous** - they execute every cycle

docs/javascript_programming/index.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,18 @@ Instructions for:
178178
- Waypoint navigation
179179

180180
**JavaScript Features:**
181-
- `const` destructuring: `const { flight, override } = inav;`
182-
- `let` variables: compile-time constant substitution
181+
- Namespaced API access: `inav.flight.*`, `inav.override.*`, `inav.events.*`
182+
- `let`/`const` variables: compile-time constant substitution
183183
- `var` variables: allocated to global variables
184+
- Ternary operator: `condition ? value1 : value2`
184185
- Arrow functions: `() => condition`
185-
- Object property access: `flight.altitude`, `rc[0].value`
186+
- Object property access: `inav.flight.altitude`, `inav.rc[0].value`
186187
- Binary expressions: `+`, `-`, `*`, `/`, `%`
187188
- Comparison operators: `>`, `<`, `===`
188189
- Logical operators: `&&`, `||`, `!`
189190
- Math methods: `Math.min()`, `Math.max()`, `Math.sin()`, etc.
191+
- Flight mode detection: `inav.flight.mode.poshold`, `inav.flight.mode.rth`, etc.
192+
- PID controller outputs: `inav.pid[0-3].output`
190193

191194
### Validation
192195

0 commit comments

Comments
 (0)