SOLVED: LUA relay:enabled and relay:toggle issues

Hi,

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.

I’ve tried to upload the LUA sample relay script ardupilot/relay_control.lua at master · ArduPilot/ardupilot · GitHub and a slightly modified version of it:

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:
image

The 1st one is not togglilng the relay according to my voltmeter.

Any ideas what I’m doing wrong here?

Thanks in advance!

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()
1 Like

Hi Mustafa,

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 :slightly_smiling_face:
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()

This should work.
Let us know if it doesn’t.

1 Like

oh… i thought relay:enabled() checks the current state of the relay… thanks for your help very much!

from the docs:

on(relay_num) - Turns the requested relay on
enabled(relay_num) - Returns true if the requested relay is currently turned on

@Mustafa_Gokce

any ideas why this works as described?

PRs are always welcome!

1 Like

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.

1 Like

No it isn’t.
It represents whether the relay is enabled or not. Not current on/off state.

It is wrong, I raised an issue on github.

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 :slightly_smiling_face:, not only the enabled status… I don’t know if they exist or not though…

I think so too, toggle is a bit dangerous :slightly_smiling_face:

Thanks for that!

Yes, just checked, it works perfectly. So, it appears, togglilng is not working well from MP interface only.

1 Like