Servers by jDrones

How to control relays with buttons already in use

Hi all,

I have connected a relay switch module to pixhawk (rover firmware 4.0.0) and I can change it with dedicated buttons of the Joystick, but I would like to switch it with the same buttons I use to arm and disarm the system but I can’t get it…

Any ideas with the parameters to do that?

Thanks

Which radio do you have ?

Happy flying :slightly_smiling_face:

I have no radio controller. I only use a Logitech F310 Joystick connected to the laptop and 4G telemetry throught raspberry connected to the pixhawk

You mean u want to control two different outputs of FC using one button off your joystick?

I believe there is way you can do that inside mission planner.

If you were using a TX then it is pretty simple channel assignment.

https://ardupilot.org/copter/docs/common-joystick.html

I think you can do it with lua scripting, when state is armed change the relay status. If you look at examples of lua you should get some ideas.

Lua scripting wiki:

https://ardupilot.org/copter/docs/common-lua-scripts.html

Get status with:

is_armed() -Returns a true if vehicle is currently armed.

Set relay with:

on(relay_num) - Turns the requested relay on.
off(relay_num) - Turns the requested relay off.

Yeah , that’s right

Happy flying :slightly_smiling_face:

Thanks for the suggestion. I Will try it.

It worked. Thank you Corrado_Steri,

Now I have another issue. I need to read a external button connected to AUX_OUTPUT 5. I tried to use the Lua script:

     -- This script is an example button functionality

       gcs:send_text(0, "Button check")
       local button_status = 0
       function update() -- this is the loop which periodically runs
       gcs:send_text(0, "Button check 1")
      button_status = button:get_button_state(1) 

     if (button_status == 1) then
         gcs:send_text(0, "Button on")
    else
        gcs:send_text(0, "Button off")
   end
   return update, 1000 -- reschedules the loop (1hz)
  end
  return update() -- run immediately before starting to reschedule

I have BTN_ENABLED = 1 and BTN_PIN1 = 54
I have connected the button betwen the ground pin and the signal pin of the Pixhawk but it doesn’t work.

Any ideas?

Thanks

I don’t think you can read an input on aux channel.

So, How can I read the button?

What about this parameters? I thought it must worked…

Sorry didn’t see that, so it should work.

Yes, I think so, even there is a example script:

But the script doesn’t work. Neither my modified versión…

With the example script I get a error…

image

at this line:

local button_new_state = button:get_button_state(button_number) == button_active_state

I think the command:

                           button:get_button_state(button_number)

doesn’t work, I don’t know why…

Yes, I think so, even there is a example script:

github.com

ArduPilot/ardupilot/blob/master/libraries/AP_Scripting/examples/button_test.lua.lua

-- This script is an example button functionality

local button_number = 1 -- the button numbber we want to read, as deffined in AP_Button

local button_active_state = true -- the 'pressed' state of the button

local last_button_state

function update() -- this is the loop which periodically runs

  local button_new_state = button:get_button_state(button_number) == button_active_state

  -- the button has changes since the last loop
  if button_new_state ~= last_button_state then
    last_button_state = button_new_state
    if button_new_state then
      gcs:send_text(0, "LUA: Button pressed")
    else
      gcs:send_text(0, "LUA: Button released")
    end

This file has been truncated. show original

But the script doesn’t work. Neither my modified versión…

With the example script I get a error…

image

at this line:

local button_new_state = button:get_button_state(button_number) == button_active_state

I think the command:

                           button:get_button_state(button_number)

doesn’t work, I don’t know why…

The button bindings are not in Rover 4.0 you will have to use dev.

You can debug if the button is working with the mavlink inspector in mission planners ctrl + f menu. You should see the button change message.

Thank you iampete. I cheked that the button Works in Mavlink inspector.

I am not sure how can I add the button bindings. Can you help me?

Thanks in advance

Why cant you use dev? Lost of other scripting improvements also. As ever in with dev firmware you should be more cautious setting everything up, but its not like a rover is going to fall out of the sky.

https://firmware.ardupilot.org/Rover/latest/

Ok iampete, I have loaded dev versión and the button finally Works. Thank you very much.

I need to test carefully this versión because is a dev versión but the button Works fine.

Thank you again.

Is there any doc on the lua implementation and bindings avail?

we are abit behind on the documentation I’m afraid.

The more or less complete list is here, there are a few exception such as logging that are not in that file.

For the most part for each new set of bindings going in we also add a example.

Servers by jDrones