Hi,
I set myself a challenge try and and make a program in LUA, this is my first attempt!
My objective was to apply a BRIGHT flashing LED, 3W or bigger, as an orientation indicator, via a LED CC driver board!
The, available: Github.com/ArduPilot/ardupilot/blob/master/libraries/AP_Scripting/examples/relay_control.lua script, (thanks to who ever provided the example) with the Toggle only gave a 50:50 ratio. I wanted a variable, the script below gives 6:1 but can be varied as required.
After at least 12 Hours of hair pulling, and remember this is my first attempt I am publishing my first script. It seems so short for such effort.
Others may find better ways to do it, improve on it or point out possible options.
(It’s called F**K it WORKS slow.lua)
local RELAY_NUM = 2 – Relay number 3 in Mateksys H743
local relay_status = true
function led_action() –
if relay_status then
relay:off(RELAY_NUM)
relay_status = false
return led_action, 1200 – off time
else –
relay:on(RELAY_NUM)
relay_status = true
end
return led_action, 200 – on time
end
return led_action() – run immediately
end
return update() –
Appoligies if posted in the wrong place?
Yuri_Rage
(Yuri Rage)
February 11, 2024, 3:56am
2
Your last two lines do nothing and are in error.
You have a bunch of unnecessary hyphens for comments that don’t exist.
This is functionally equivalent and does not rely on a global state variable:
local RELAY_NUM = 2
function on()
relay:on(RELAY_NUM)
return off, 200
end
function off()
relay:off(RELAY_NUM)
return on, 1200
end
return on()
And this should also work:
local RELAY_NUM = 2
local INTERVAL_MS = {
[0] = 1200,
[1] = 200
}
function toggle()
relay:toggle(RELAY_NUM)
return toggle, INTERVAL_MS[relay:get(RELAY_NUM)]
end
return toggle()
I have several videos on the subject that you may find useful:
Yuri’s Homebrew & DIY - YouTube
Thanks for the reply, I am always willing to learn. (And i had fun trying out my problem solving logic).
I have alway found it easier to learn a programming language when you have a personal objective. I started with ASM in ATMEL in 2002!
The hyphens were a doulble - - comment block but when put in the post would only show a single (above my pay grade!)
Thanks agin i’ll check your Video.
Yuri_Rage
(Yuri Rage)
February 11, 2024, 4:34am
4
You can use triple grave accents (`) to surround code blocks on the forum. And I see that your comments only have single dashes as a result of copy/paste auto formatting. But you also have pointless trailing dashes on a bunch of lines.
You’re in good company. My degree predates your ASM experiments by a few years…
Just for grins, here it is in 5 (ugly) lines:
function update()
relay:toggle(2)
return update, (function() return ({[0] = 1200, [1] = 200})[relay:get(2)] end)()
end
return update()
So much to learn, so few years!
Now you’re showing off
We were taught BASIC in college, and thermionic valve theroy, transistors were way off!
My next LUA script is to develope this in to Aircraft navigation lights, it could take a few more years.
I ended up teaching Radio Communications and Industrial Electronics, built my first Quad in 2009!
Yuri_Rage
(Yuri Rage)
February 11, 2024, 3:00pm
6
Don’t reinvent the wheel…
-- Hexsoon EDU 450 LED script
-- LEDs will be fixed colour when disarmed, when armed LEDs will also strobe
--[[ INSTRUCTIONS:
LEDs have two servo headers, plug into AUX 5 and 6 on cube.
Note white is ground, black and brown are signal
black into AUX 5 and brown into AUX 6
Other aux pins could be used but 5 and 6 make best use of the available groups
BRD_PWM_COUNT must be 6
Aux 5 and 6 are servo outs 13 and 14
SERVO13_FUNCITON 132 Profi LED Clock
SERVO14_FUNCTION 94 Script 1
setup scripting:
SCR_ENABLE 1
SCR_HEAP_SIZE 88032
Reboot
Use MP config tab -> MAVFtp to place this script in 'APM/scripts' folder
Reboot
Check messages tab should see:
LEDs strip left: chan=14
This file has been truncated. show original
-- leds_on_a_switch.lua: control led brightness with a radio switch
--
-- constants
local AuxSwitchPos = {LOW=0, MIDDLE=1, HIGH=2}
local NTF_LED_BRIGHT = Parameter("NTF_LED_BRIGHT")
local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7}
-- state
local prev_pos = -1
function update()
local sw_pos = rc:get_aux_cached(300)
if sw_pos ~= prev_pos then
if sw_pos == AuxSwitchPos.LOW then
NTF_LED_BRIGHT:set(0)
gcs:send_text(MAV_SEVERITY.INFO, "LEDs turned OFF")
elseif sw_pos == AuxSwitchPos.MIDDLE then
NTF_LED_BRIGHT:set(1)
gcs:send_text(MAV_SEVERITY.INFO, "LEDs dimmed")
This file has been truncated. show original
Please don’t take this the wrong way, this is excellent support, and thank you, but sometimes the journey is more important than the destination.
Maybe it’s my way of learning.
Kind regards,
Roy
PS my intention is to use 3W or 5W fixed color LEDS, pulsed to keep the current draw low, but the intensity high.