Synchronize switches with xplane not working

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

Message
Author
xplanearg
Posts: 44
Joined: Mon May 04, 2020 10:37 am

Re: Synchronize switches with xplane not working

#21 Post by xplanearg »

Sling wrote: Wed Apr 14, 2021 12:42 pm Yes it is tested working code.
Hi, ive tested this and it does not work for me

steps to reproduce:
1) start airmanager
2) start x-plane
3) my panel physical battery switch is in the ON position
4) x-plane is set to start cold and dark everything off

x-plane launches, and the battery is off. Airmanager wont turn on the battery matching the switch position.
i have to move the battery switch to off and then on again for it to work

actual code im using:

Code: Select all

local switch_36_pos
function switch_36_callback(position)
  print("The battery switch got changed to position " .. position)
  if position == 0 then
    xpl_command("sim/electrical/battery_1_off")
    xpl_command("laminar/c90/fltctrl/switch/elev_trim_control_dn") -- c90 laminar
  elseif position == 1 then
    xpl_command("sim/electrical/battery_1_on")
    xpl_command("laminar/c90/fltctrl/switch/elev_trim_control_up") -- c90 laminar
  end
  switch_36_pos = position
end
switch_36_id = hw_switch_add("ARDUINO_MEGA2560_A_D36", switch_36_callback)
switch_36_pos = hw_switch_get_position(switch_36_id)

function switch_36_subscribe(position)
  if position ~= switch_36_pos then
    switch_36_callback(switch_36_pos)
  end
end
xpl_dataref_subscribe("sim/cockpit/electrical/battery_on", "INT", switch_36_subscribe)
not sure what im doing wrong here

xplanearg
Posts: 44
Joined: Mon May 04, 2020 10:37 am

Re: Synchronize switches with xplane not working

#22 Post by xplanearg »

Corjan wrote: Wed Apr 14, 2021 7:15 am Hi,


To be honest, your code seems flawed ;)
Doing a xpl_subscribe using the same switch callback means that the INT[] will be placed in the position argument of the switch_36_callback function.
This is probably not what you want to happen.

You can use the timer if you want to simulate a loop:

Code: Select all

function switch_36_callback(position)
  -- Ignore
end
switch_36_id = hw_switch_add("ARDUINO_MEGA2560_A_D36", function()
  -- ignore events from switch
end)

-- Called every 100ms
timer_start(0, 100, function()
  local position = hw_switch_get_position(switch_36_id)
  print("The battery switch got changed to position " .. position) 
  if position == 0 then
    xpl_command("sim/electrical/battery_1_off")
    xpl_command("laminar/c90/fltctrl/switch/elev_trim_control_dn") -- c90 laminar
  elseif position == 1 then
    xpl_command("sim/electrical/battery_1_on")
    xpl_command("laminar/c90/fltctrl/switch/elev_trim_control_up") -- c90 laminar
  end
end)

Corjan
Hi, ive tested your code and it works however its fundamentally flawed. This is executing the command over and over even when the position of the switch is correct.
the result is the network traffic goes over the roof. Im sure when i scale this up to more than 100 switches it will slow down either x-plane or airmanager. x-plane commands are not designed to be send every 100 ms...

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Synchronize switches with xplane not working

#23 Post by Keith Baxter »

Hi

I thought I would try and help. But I am fundamentally confused.

Perhaps a short clip indicating what you are doing would assist us better to help you.

Keith
Last edited by Keith Baxter on Wed Apr 14, 2021 9:10 pm, edited 1 time in total.
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Synchronize switches with xplane not working

#24 Post by Sling »

The code was tested with the landing light switch as in your example now you’ve switched to the battery switch. If you test with the exact code I posted you will find it works.

As

You have to make sure the hardware is available before running this else the hardware will not get added.

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Synchronize switches with xplane not working

#25 Post by Keith Baxter »

Sling wrote: Wed Apr 14, 2021 9:06 pm The code was tested with the landing light switch as in your example now you’ve switched to the battery switch. If you test with the exact code I posted you will find it works.

As

You have to make sure the hardware is available before running this else the hardware will not get added.
Tony,

Not only that, The switch is firing two commands. Cannot understand why.
The c90 commands are not standard Xplane commands. Are they in fact commands or datarefs??? What Developer are we talking about and what Air-Frame?
So many unknowns. That is why I ask for a clip or something so that we can assist better.

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Synchronize switches with xplane not working

#26 Post by Sling »

Yes my thoughts exactly. When trying to diagnose an issue or to get some code working you should keep the environment the same for each test as it just introduces more and more variances each time you test. I always recommend testing with default c172 if at all possible. Once working you can then start to introduce new things.

xplanearg
Posts: 44
Joined: Mon May 04, 2020 10:37 am

Re: Synchronize switches with xplane not working

#27 Post by xplanearg »

Sling wrote: Wed Apr 14, 2021 9:06 pm The code was tested with the landing light switch as in your example now you’ve switched to the battery switch. If you test with the exact code I posted you will find it works.

As

You have to make sure the hardware is available before running this else the hardware will not get added.
Hi, thanks for your answer.
I believe you have found the problem

My setup is the following:
one PC with airmanager and all arduinos connected via usb. Airmanager is configured to launched at the computer startup.
when i turn on the computer, airmanager starts and automatically loads my panel. Airmanager executes the code, and i know this because for example i have set a command to change view at startup, and this works inmediately.
However arduinos take time to become online, and i think this is where the problem comes from.
When airmanager is started and executes the lua code the arduinos are not yet available, that is why its not working i presume.
the question now is how to get around this limitation
Last edited by xplanearg on Thu Apr 15, 2021 9:36 am, edited 1 time in total.

xplanearg
Posts: 44
Joined: Mon May 04, 2020 10:37 am

Re: Synchronize switches with xplane not working

#28 Post by xplanearg »

Keith Baxter wrote: Wed Apr 14, 2021 8:53 pm Hi

I thought I would try and help. But I am fundamentally confused.

Perhaps a short clip indicating what you are doing would assist us better to help you.

Keith
hi, i will try to explain.
1) set up airmanager to load automatically on the computer startup
2) configure your panel to load at airmanager startup.
3) use the code i provided in the example above
4) turn off the pc running airmanger
5) put your phisicall switch in the on position (battery for instance)
6) start the pc running airmanager, it will load airmanger automatically and load your panel automatically
7) start x-plane (airplane previously set up to load cold and dark)
8) the battery will not be turned on as it should

xplanearg
Posts: 44
Joined: Mon May 04, 2020 10:37 am

Re: Synchronize switches with xplane not working

#29 Post by xplanearg »

Keith Baxter wrote: Wed Apr 14, 2021 9:13 pm
Sling wrote: Wed Apr 14, 2021 9:06 pm The code was tested with the landing light switch as in your example now you’ve switched to the battery switch. If you test with the exact code I posted you will find it works.

As

You have to make sure the hardware is available before running this else the hardware will not get added.
Tony,

Not only that, The switch is firing two commands. Cannot understand why.
The c90 commands are not standard Xplane commands. Are they in fact commands or datarefs??? What Developer are we talking about and what Air-Frame?
So many unknowns. That is why I ask for a clip or something so that we can assist better.

Keith
Hi, its executing 2 commands as a workaround as i typically fly the c90 (2 actions assigned to only 1 switch). Don't be distracted by it, this issue happens with any switch
airplane is C90 from X-Plane (default)
it does not matter what commands or how many you configure, airmanger wont fire any commands in the circumstances described

xplanearg
Posts: 44
Joined: Mon May 04, 2020 10:37 am

Re: Synchronize switches with xplane not working

#30 Post by xplanearg »

Sling wrote: Thu Apr 15, 2021 7:11 am Yes my thoughts exactly. When trying to diagnose an issue or to get some code working you should keep the environment the same for each test as it just introduces more and more variances each time you test. I always recommend testing with default c172 if at all possible. Once working you can then start to introduce new things.
Hi, please try to reproduce like so:

1) set up airmanager to load automatically on the computer startup
2) configure your panel to load at airmanager startup.
3) use the code i provided in the example above
4) turn off the pc running airmanger
5) put your phisicall switch in the on position (battery for instance)
6) start the pc running airmanager, it will load airmanger automatically and load your panel automatically
7) start x-plane (airplane previously set up to load cold and dark)
8) the battery will not be turned on as it should

Post Reply