I’m currently using Mission Planner 1.3.77 build 1.3.8113.41594, ArduRover V4.2.0-beta2 (also tried on V4.1.5-stable), Pixhack 2.8.4 (a clone of Pixhawk).
I’ve enabled GPIO on AUX5 (SERVO13_FUNCTION=-1), RELAY_DEFAULT=0, RELAY_PIN=54, SCR_ENABLE=1.
I’m able to switch the RELAY0 between LOW and HIGH (checked with the voltmeter: there’s about 3.2V on HIGH), but when I toggle it, the pin goes HIGH for a moment and then goes LOW right after that.
local RELAY_NUM = 0
function update() -- this is the loop which periodically runs
if relay:enabled(RELAY_NUM) then
gcs:send_text(2, 'Relay: Off')
relay:off(RELAY_NUM)
else
gcs:send_text(2, 'Relay: On')
relay:on(RELAY_NUM)
end
return update, 5000 -- reschedules the loop at 50Hz
end
return update() -- run immediately before starting to reschedule
They do not work as expected: the latter just prints “Relay: Off” all the time:
The 1st one is not togglilng the relay according to my voltmeter.
local RELAY_NUM = 0
local RELAY_SHOULD_BE_ON = true
function update()
if RELAY_SHOULD_BE_ON then
relay:on(RELAY_NUM)
RELAY_SHOULD_BE_ON = false
else
relay:off(RELAY_NUM)
RELAY_SHOULD_BE_ON = true
end
return update, 5000
end
return update()
Thanks for your quick reply! Your code works as expected: the relay switches on and off. Any ideas why relay:enabled() and toggle() functions do not work?
Because you enabled the relay and checked the enabled status with an if statement.
The statement is always true since you enabled it
Toggle code should also work like in the docs but don’t know why they give an example at such a high rate.
Change the line return update, 20
to return update, 5000
So it should look like:
local RELAY_NUM = 0
function update()
relay:toggle(RELAY_NUM)
return update, 5000
end
return update()
I’ve never gotten the toggle function to work as expected, and I never bothered to raise an issue because it leaves ambiguity in the relay state anyway.
I think “enabled” might be documented incorrectly. It was my understanding that “enabled” meant that the relay was configured, not that it was in the “on” state.
I could be wrong about that, and I will have a look at the source code later to confirm.
Probably a scripting issue, not firmware.
Send the code that you tried.
local RELAY_NUM = 0
function update()
relay:toggle(RELAY_NUM)
return update, 5000
end
return update()
With this code, relay should be turned on 5sec, and turned off 5sec periodically.
Doesn’t this work as I said?
It would be really nice to expose the current status of the relay with both scripting and from some mavlink message I think , not only the enabled status… I don’t know if they exist or not though…