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

As an additional contribution to the thread I have learnt some lessons.

I am using a Mateksys H743-slim V1.5 and V3

Objective was to use a relay to control a 3W Led (via driver board) to flash at 1 Sec intervals, (brighter that a ws2812) for remote orientation.

Never used LUA scripts before so a reason to learn!

Immediately hit the same problem Alex (Alex010721) did!

My setting were;

RELAY_PIN2=54 Shown as AUXOUT5 but correlates to S5 on the Mateksys H743

Why Relay2, because RELAY_PIN is set to 81, the ws2812 Status LED on S13! (There is no RELAY_PIN1)

SERVO5_FUNCTION=-1 GPIO

RELAY_DEFAULT=0 Initial Relay low

SCR_ENABLE=1

Long story short, after using all the variants in the thread, modifying content and adding gcs:send messages to see the progress in the Messages on MP and 12 hours later I discovered that RELAY_PIN2 is, as far as I have deduced used to switch between Camera 1 and 2, and is not assessable on the board, I may be wrong?

So early one morning after a restless night on contemplation I changed to:

RELAY_PIN3=54

BUT,BUT wait for it using RELAY_NUM= 2 in the script because after further frustration I have learnt that RELAY 3 is infarct relay number 2 in the script!

Now my S5 pad switches high low, I have set it at 1 Sec, but it does not TOGGLE it Blips for a very short mSec???

What I now need to learn is how to turn it on for say 50mSec then off for 1 Sec.

Any one willing to help?

And while I am pulling what hair I have left, anyone know what the max current can be drawn from a relay pin?

The relays are numbered 0-5 not 1-6. so the first relay is relay 0, that’s probably what’s causing the confusion.

Thanks for the input.

I suspected as much.

I think it’s unique to the Mateksys H743 that there is no RELAY_NUM1 so this adds to the confusion!

what do you mean no relay1?

The relay list (Mateksys H743) is
RELAY_PIN (I think this is allocated to VTX power on/off, I may be wrong)
RELAY_PIN2 (I think this is allocated to Camera switch 1 or 2, I may be wrong)
RELAY_PIN3
ETC:

There is No RELAY_PIN1 listed. It probably exsists, it’s the number system that is odd!

Just to clarify i’m running 4.4.4!

EDIT Ignor comments on RELAY_PIN1 ::JUST PUT MY LOGIC DOWN TO LACK OF SLEEP AND OLD AGE!

DUH! Late night logic! Removed graphic!
Working script.

local RELAY_NUM = 2 – actual relay 3 on Maytex H743

gcs:send_text(2, “Relay Active on relay 3”) – send the message

function update()
relay:toggle(RELAY_NUM)
return update, 1000
end

return update()