Help for a noob please....

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

Message
Author
Dave_K
Posts: 38
Joined: Thu Nov 03, 2022 8:44 am

Help for a noob please....

#1 Post by Dave_K »

Hi all,

I have been using airmanager for some years now but want to build the AP panel for my CJ4 in MSFS.

I have an Arduino Mega 2560, I've connected and flashed it, before that I had been practising code with a button, LED and an encoder knob.

Is there a simple and clear video somewhere that shows exactly how to connect a physical encoder via the Mega to my plane in MSFS - maybe start with something simple like the HDG bug or VS knob??

I'm a bit lost as to just what needs to be done here.

I already have the functionality via a knobster but really want to go custom panel now and the AP panel has 6 encoders and 15 buttons :D

Any help would be much appreciated
Thanks
i9 12900K, RTX 3080ti, triple 32" 1440p screens plus double 1080p touch screens

frumpy
Posts: 361
Joined: Sat Jan 30, 2016 12:01 pm

Re: Help for a noob please....

#2 Post by frumpy »

You can start here: https://siminnovations.com/forums/viewtopic.php?t=826

However, this is only for X-Plane and FSX. You need to change the function to make it compatible with MSFS, like fs2020_variable_subscribe and fs2020_event, but I cannot help here.

Dave_K
Posts: 38
Joined: Thu Nov 03, 2022 8:44 am

Re: Help for a noob please....

#3 Post by Dave_K »

frumpy wrote: Mon Feb 26, 2024 4:36 pm You can start here: https://siminnovations.com/forums/viewtopic.php?t=826

However, this is only for X-Plane and FSX. You need to change the function to make it compatible with MSFS, like fs2020_variable_subscribe and fs2020_event, but I cannot help here.
Thanks for that, i'm looking at it now.

I generally find once i get a working example i'm ok, but i'm not there yet :)

I'll keep digging......
i9 12900K, RTX 3080ti, triple 32" 1440p screens plus double 1080p touch screens

Dave_K
Posts: 38
Joined: Thu Nov 03, 2022 8:44 am

Re: Help for a noob please....

#4 Post by Dave_K »

I'm missing one step ( I think)

I have got my Arduino connected to AirManager, I created a new hardware entry and cut/pasted some encoder code from the help file into the editor, when run I can select my Arduino, the channel, and also the encoder pins, it then displays output in the console taht shows which way the encoder was turned - this all seems positive so far.

I can add the hardware into my CJ4 panel but - here's where the story ends - there is no code to connect the encoder output to the FS2020 VARS etc

I can't see any examples listed for this, there must be something surely??

Anyone got any tips?

Edit...

I think the answer lies in here, but not sure if this is all that is needed or there is more to add and also where to put it :D

Code: Select all

-----VS Dial-------------
--Get VS State
function vsstate_callback(state)
    vs_state = state  
end
fs2020_variable_subscribe("AUTOPILOT VERTICAL HOLD", "bool", vsstate_callback)


function callback_VS_DIAL(direction)
     if direction ==  1 then
         if vs_state then fs2020_event("AP_VS_VAR_DEC")
         else fs2020_event("AP_PITCH_REF_INC_DN")             
         end
         sound_play(snd_dial)
     elseif direction == -1 then
         if vs_state then fs2020_event("AP_VS_VAR_INC")
         else fs2020_event("AP_PITCH_REF_INC_UP")
         end
         sound_play(snd_dial)
     end
end
i9 12900K, RTX 3080ti, triple 32" 1440p screens plus double 1080p touch screens

Dave_K
Posts: 38
Joined: Thu Nov 03, 2022 8:44 am

Re: Help for a noob please....

#5 Post by Dave_K »

I'm not sure if I'm on the right track here so I can't really go much further without verification but from some cut-n-paste plus what minimal understanding of the code I have so far maybe this is the way I need to go.....

Code: Select all

-----VS Dial-------------
--Get VS State
function vsstate_callback(state)
    vs_state = state  
end
fs2020_variable_subscribe("AUTOPILOT VERTICAL HOLD", "bool", vsstate_callback)


function dial_change(direction)
    if direction ==  1 then
        if vs_state then fs2020_event("AP_VS_VAR_DEC")
        else fs2020_event("AP_PITCH_REF_INC_DN")             
        end
        sound_play(snd_dial)
    elseif direction == -1 then
        if vs_state then fs2020_event("AP_VS_VAR_INC")
        else fs2020_event("AP_PITCH_REF_INC_UP")
        end
        sound_play(snd_dial)
    end
end
-- Create a new rotary encoder
hw_dial_add("vs scroll", dial_change)

I'm not sure what function "vs_state" provides but I can see it affects the events outcome?

Is this a reasonable code block to try?
I have no idea what happens if its wrong, hopefull it doesn't upset my FS2020 install?
i9 12900K, RTX 3080ti, triple 32" 1440p screens plus double 1080p touch screens

JackZ
Posts: 2267
Joined: Mon Feb 22, 2016 1:02 pm

Re: Help for a noob please....

#6 Post by JackZ »

Vs_state needs to be declared as a global variable (ie accessible throughout the whole code), not as a local variable (only in the function)
In Lua the « scope » (read visibility) of a variable is determined by the location of the first occurrence of a variable in the code. If it’s declared or used in the main code early on the variable scope is global and the variable can be accessed everywhere, if the first occurrence of the variable is in a function the scope is limited to the function only.

Since here you want to reuse vs_state elsewhere you have to declare vs_state early in your main code. Add a vs_stat = false line at the beginning of your code to achieve this.
vs_state is a Boolean (true/false values) variable that stores the current state of the autopilot vertical speed mode.

You retrieve and set this value in the vsstate_callback() function, and since the variable is declared with a global scope using the trick above mentioned, its current value is now accessible from the other function dial_change(), that sends to the sim a different event according to its value (true or false)
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

Dave_K
Posts: 38
Joined: Thu Nov 03, 2022 8:44 am

Re: Help for a noob please....

#7 Post by Dave_K »

Thanks for that

makes a lot of sense, have dealt with scope when i was messing with MicroPython a while ago.

So apart from adding declaration trick, the code looks normal?


I'll try this out later today. :D
i9 12900K, RTX 3080ti, triple 32" 1440p screens plus double 1080p touch screens

Dave_K
Posts: 38
Joined: Thu Nov 03, 2022 8:44 am

Re: Help for a noob please....

#8 Post by Dave_K »

It almost works :) :) :)

The new knob now alters the VS dial in the cockpit, but its very erratic

My encoders have two pulses per detent, i tried adding the counter code listed in the quickstart guide which is supposed to make the logic only recognise every other pulse but it still multi-clicks, however if i press the "turn CW" or "turn CCW" buttons in the create tab of AM, it does actually only use every other press so the logic seems fine.

Is there another way to reduce the pulses or have i bought the wrong encoders??

It's still a success though and really bolsters my confidence so thanks for the help so far :D
i9 12900K, RTX 3080ti, triple 32" 1440p screens plus double 1080p touch screens

Dave_K
Posts: 38
Joined: Thu Nov 03, 2022 8:44 am

Re: Help for a noob please....

#9 Post by Dave_K »

I added the 'TYPE" argument to the dial_add line and restarted AM, it seems to work perfectly now, i'm not sure if the counter code is needed now or not though??

I also fixed the lack of sounds as well.

The current code is thus.....

Code: Select all

--SOUNDS
snd_click = sound_add("click.wav")
snd_dial = sound_add("dial.wav")

-----VS Dial-------------
--Get VS State
local vs_state = nil
vs_counter = 0

function vsstate_callback(state)
    vs_state = state  
end
fs2020_variable_subscribe("AUTOPILOT VERTICAL HOLD", "bool", vsstate_callback)


function dial_change(direction)
    vs_counter = vs_counter + 1
    if vs_counter == 2 then vs_counter = 0
    else
    if direction ==  1 then
        if vs_state then fs2020_event("AP_VS_VAR_DEC")
        else fs2020_event("AP_PITCH_REF_INC_DN")             
        end
        sound_play(snd_dial)
    elseif direction == -1 then
        if vs_state then fs2020_event("AP_VS_VAR_INC")
        else fs2020_event("AP_PITCH_REF_INC_UP")
        end
        sound_play(snd_dial)
    end
    end
end
-- Create a new rotary encoder
hw_dial_add("vs scroll", "TYPE_2_DETENT_PER_PULSE", dial_change)
i9 12900K, RTX 3080ti, triple 32" 1440p screens plus double 1080p touch screens

JackZ
Posts: 2267
Joined: Mon Feb 22, 2016 1:02 pm

Re: Help for a noob please....

#10 Post by JackZ »

1-What is the use of vs_counter in your code? It apparently does nothing but slowing the code.
If you look at the examples here, there is no such piece of code.
https://siminnovations.com/wiki/index.p ... w_dial_add
no need to add an elseif either.

2- The sound play() can be placed outside of the tests. Be careful for this though, the sound track should be really short (less than one second duration) otherwise the play buffer will add up lots of sounds if you turn the encoder real fast. A safe bet is to stop the previous sound BEFORE playing a new one.
3- i suggest you add the acceleration and debounce parameters as standard to your encoder callback function

see modified code attached

Code: Select all

--SOUNDS
snd_click = sound_add("click.wav")
snd_dial = sound_add("dial.wav")

-----VS Dial-------------
--Get VS State
local vs_state = nil -- <--- nil is probably not the best value for vs_state, as it "types" the variable and Lua will have to change it afterwards. Better set it to false."local" here is not useful
vs_counter = 0


function vsstate_callback(state)
    vs_state = state  
end
fs2020_variable_subscribe("AUTOPILOT VERTICAL HOLD", "bool", vsstate_callback)


function dial_change(direction)
    if direction ==  1 then
        if vs_state then 
              fs2020_event("AP_VS_VAR_DEC")
        else 
              fs2020_event("AP_PITCH_REF_INC_DN")             
        end
    end --if
    if direction == -1 then
        if vs_state then
             fs2020_event("AP_VS_VAR_INC")
        else 
             fs2020_event("AP_PITCH_REF_INC_UP")
        end

    end -- if 
    sound_stop(snd_dial)
    sound_play(snd_dial) 
end -- function

-- Create a new rotary encoder
acceleration_factor = 2 -- Multiplier for rapid rotation (try different values such as 3, 5, etc...)
debounce_time = 4 -- standard debounce time, increase if it's an encoder of poor quality
hw_dial_add("vs scroll", "TYPE_2_DETENT_PER_PULSE", acceleration_factor, debounce_time, dial_change)
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

Post Reply