-
Notifications
You must be signed in to change notification settings - Fork 12
Lua Examples
codeforges edited this page Mar 8, 2026
·
1 revision
Worked examples for the Lua Graph Editor. Each example shows the nodes, connections, and generated Lua code. For the full node catalog, see Lua Node Reference.
Goal: Send a warning to the GCS when battery drops below 11.1V.
- Battery sensor -- reads voltage
-
Constant -- value
11.1 -
Compare -- operator
<(less than) - Debounce -- 5000ms delay to avoid spamming
- Send GCS Text -- message "LOW BATTERY", severity Warning
Battery [Voltage] --> Compare [A]
Constant [Value] --> Compare [B]
Compare [Result] --> Debounce [Input]
Debounce [Output] --> Send GCS Text [Trigger]
local WARN_SENT = false
function update()
local voltage = battery:voltage(0)
local is_low = voltage < 11.1
if is_low and not WARN_SENT then
gcs:send_text(4, "LOW BATTERY")
WARN_SENT = true
elseif not is_low then
WARN_SENT = false
end
return update, 1000
end
return update, 1000Goal: Alert the pilot when the vehicle is more than 500m from home.
- GPS Position sensor
-
Constant -- value
500 -
Compare -- operator
> - Send GCS Text -- message "GEOFENCE WARNING"
GPS Position [Distance from Home] --> Compare [A]
Constant [Value] --> Compare [B]
Compare [Result] --> Send GCS Text [Trigger]
GPS Position provides raw latitude/longitude. The compiler calculates distance from home using the ArduPilot ahrs:get_home() API.
Goal: Trigger a relay when RC channel 7 goes above 1700us.
- RC Channel sensor -- channel 7
-
Constant -- value
1700 -
Compare -- operator
> - Rising Edge -- fire only on the transition (not continuously)
- Trigger Relay -- relay 0, state ON
RC Channel [Value] --> Compare [A]
Constant [Value] --> Compare [B]
Compare [Result] --> Rising Edge [Input]
Rising Edge [Triggered] --> Trigger Relay [Trigger]
local prev_state = false
function update()
local ch7 = rc:get_pwm(7)
local is_high = ch7 > 1700
local rising = is_high and not prev_state
prev_state = is_high
if rising then
relay:on(0)
end
return update, 200
end
return update, 200Goal: Log rangefinder altitude to a CSV file every 2 seconds.
- Rangefinder sensor -- instance 0
- Run Every -- 2000ms interval
- Baro Altitude sensor
-
Log to File -- filename
terrain.csv, comma separator
Run Every [Flow] --> Log to File [Trigger]
Rangefinder [Distance] --> Log to File [Value 1]
Baro Altitude [Altitude] --> Log to File [Value 2]
The resulting CSV file on the SD card contains timestamped rangefinder and barometric altitude data, useful for post-flight terrain analysis.
Getting Started
Configuration
- Configuration
- PID Tuning
- Rates
- Flight Modes
- Receiver
- Serial Ports
- Safety and Failsafe
- MAVLink Signing
- Battery
- Tuning Presets
- All Parameters
Firmware
Lua Graph Editor