Game Controller and Air Player

Help creating logic scripts for Air Manager Instruments

Moderators: Ralph, russ

Message
Author
chefpilot
Posts: 60
Joined: Wed Aug 02, 2017 12:51 pm

Game Controller and Air Player

#1 Post by chefpilot » Thu Nov 25, 2021 6:57 pm

Hi,
I'm just making first experience with the API function "Game Controller". I use the function to read out the trimswitch on the flightyoke to implement it to my trimsystem. Everything is working fine as long as I start the "instrument" with Air Manager. When starting it with Air Player nothing happens.
I've no clue to solve the problem.
  1. -- Create a new variables
  2.  
  3. trimUp_id = si_variable_create("trim_up", "BOOL", false)
  4.  
  5. trimDown_id = si_variable_create("trim_down", "BOOL", false)
  6.  
  7.  
  8.  
  9. function callback(type, index, value)
  10.  
  11.     print("type = " .. type .. ", index = " .. index .. ", value = " .. tostring(value))
  12.  
  13.     if type == 1 then
  14.  
  15.         if index == 4 then
  16.  
  17.             if value == true then
  18.  
  19. --          print("trim down pressed")
  20.  
  21.             si_variable_write(trimDown_id, true)
  22.  
  23.             elseif value == false then
  24.  
  25. --            print("trim down released")
  26.  
  27.             si_variable_write(trimDown_id, false)
  28.  
  29.             end
  30.  
  31.          elseif index == 2 then
  32.  
  33.             if value == true then
  34.  
  35. --              print("trim up pressed")
  36.  
  37.                 si_variable_write(trimUp_id, true)
  38.  
  39.             elseif  value == false then
  40.  
  41. --              print("trim up released")
  42.  
  43.                 si_variable_write(trimUp_id, false)
  44.  
  45.             end
  46.  
  47.          end
  48.  
  49.      end          
  50.  
  51. end
  52.  
  53.  
  54.  
  55. function new_data_callback(data1, data2)
  56.  
  57.     print("New data1: "..tostring(data1))
  58.  
  59.     print("New data2: "..tostring(data2))
  60.  
  61. end
  62.  
  63.  
  64.  
  65. si_variable_subscribe("trim_up", "BOOL",
  66.  
  67.                  "trim_down", "BOOL", new_data_callback)           
  68.  
  69. list = game_controller_list()
  70.  
  71.  
  72.  
  73. for k, v in pairs(list) do
  74.  
  75.     game_controller_add(v, callback)
  76.  
  77. end
Marc

Tetrachromat
Posts: 127
Joined: Sun Feb 14, 2021 6:55 pm

Re: Game Controller and Air Player

#2 Post by Tetrachromat » Fri Dec 03, 2021 4:07 pm

Hi @chefpilot
Try my plugin. It may solve your issue.

Paul

chefpilot
Posts: 60
Joined: Wed Aug 02, 2017 12:51 pm

Re: Game Controller and Air Player

#3 Post by chefpilot » Fri Dec 03, 2021 10:32 pm

Hi,
thank you very much. I'll try it during the weekend.
Marc

chefpilot
Posts: 60
Joined: Wed Aug 02, 2017 12:51 pm

Re: Game Controller and Air Player

#4 Post by chefpilot » Sun Dec 05, 2021 5:18 pm

Hi Paul,
I'm just trying to understand your post. Sorry but I do not have a chance how your post can solve my problem.
Any help is welcome!
Marc

Tetrachromat
Posts: 127
Joined: Sun Feb 14, 2021 6:55 pm

Re: Game Controller and Air Player

#5 Post by Tetrachromat » Sun Dec 05, 2021 6:05 pm

Hi Marc,

My understanding of the task that you want to achieves is:
- press the (elevator?) trim switch
- operate on the received input value
- send a trim command to the sim or write to a trim variable with the value
- such that the trim of your elevator move as intended

If this is your use case, then the plugin will help to achieve this without scripting in lua.

But if you have another use case, please explain further.

Paul

Tetrachromat
Posts: 127
Joined: Sun Feb 14, 2021 6:55 pm

Re: Game Controller and Air Player

#6 Post by Tetrachromat » Sun Dec 05, 2021 6:32 pm

Marc, are you using the Saitek/Logitech G Yoke?

chefpilot
Posts: 60
Joined: Wed Aug 02, 2017 12:51 pm

Re: Game Controller and Air Player

#7 Post by chefpilot » Sun Dec 05, 2021 9:47 pm

Hi,
my problem is related to the following post:
https://siminnovations.com/forums/viewt ... 5&start=20

Actually I have an Saitek pro Flight Yoke and an Cyborg X Joystick but that should not be the problem.
My intention is the following:
Whenever the toggle switch on the yoke or joystick is pressed in either one of the two directions the "trim_up" or "trim_down" command is transfered two the "trim-instrument". In that instrument the stepper-motor is turning the trimwheel connected to the potentiometer which sends the value to xplane as the elevator trim value.
It's roughly the same setup like Chuck has written in the former mentioned post. The only difference is that I do not have a seperate trimswitch on the yoke which can be wired to the arduino. I have to use one of the integrated switches which send the commands directly via the USB-cable from the controller to the sim and I've no chance to use them directly in my "Trim-instrument".
I nearly solved the problem by using two instruments. The first one use the game_controller-function and transfers the trim_up and trim_down comand via inter-instrument communications to the second instrument (trim-instrument). Everything works fine as long as I use Air Manger but does not work when using Air Player.
I hope I explained my problem in an unsterstanable way.
Marc

Tetrachromat
Posts: 127
Joined: Sun Feb 14, 2021 6:55 pm

Re: Game Controller and Air Player

#8 Post by Tetrachromat » Sun Dec 05, 2021 10:51 pm

OK Marc, that is not far from what I initially throught was your intention.

I call it a single producer - single consumer scenario. The difference in your intention is that that your consumer instrument is controlling the stepper motor.

Now that i understand your intention, I did refactor your code from the original post. I separated the code into the two instruments and applied some changes
- use tables to manage data
- use the log function
- use an anonymous function for the callback
- use two subscriptions, as the two commands will never occure at the same time.

This is the producer code: NOTE the button B2 has index = 5, not 2 as in your script.

Code: Select all

-- --------------------------------------------
-- Instrument with role 'Trim Command Producer'
-- --------------------------------------------
-- Constants
local GAME_CONTROLLER = "Flight Yoke System"
local BUTTON = 1

-- specify the index table for trim buttons B1 and B2
local index = { B1 = 4, B2 = 5 }

-- Create the var_id table for the buttons
local var_id = {}
var_id[index.B1] = si_variable_create( "trim_up", "BOOL", false)
var_id[index.B2] = si_variable_create( "trim_down", "BOOL", false)

-- add the controller
local gid = game_controller_add(GAME_CONTROLLER, function (type, index, value)
    -- log("INFO", string.format("type = %s, index = %s, value = %s", type, index, value))
    if type == BUTTON and value == true then
      local var = var_id[index]
      if var then
        si_variable_write(var, true)
      end
    end      
  end
)
And this is the consumer code:

Code: Select all

-- --------------------------------------------
-- Instrument with role 'Trim Command Consumer'
-- --------------------------------------------
local function trim_up()
  -- log("INFO", "command 'trim up' received" )
  -- add the code for controlling the stepper motor 'trim up' here
end

local function trim_down()
  -- log("INFO", "command 'trim down' received" )
  -- add the code for controlling the stepper motor 'trim down' here
end

si_variable_subscribe( "trim_up", "BOOL", trim_up )
si_variable_subscribe( "trim_down", "BOOL", trim_down )
How would my plugin help in this scenario?
My plugin (GCI) would eliminate the producer script completely. Instead you would configure GCI such that it should publish the 2 variables on the activation of the switch. With the 'timed' subtype you could even configure the repetition of the action. The consumer then only needs to advance the stepper motor on every receipt of the command.

The configuration would look like the following:

Code: Select all

{ "controllers": [ 
    { "name": "Flight Yoke System",
      "button": [
        { "id": [  4, "B1" ], 
          "subtype": { "name": "timed", "parameters": [0, 100] }, 
          "publish": { "variable": "trim_up", "unit": "BOOL", "initial": false }
        },
        { "id": [  5, "B2" ],
          "subtype": { "name": "timed", "parameters": [0, 100] }, 
          "publish": { "variable": "trim_down", "unit": "BOOL", "initial": false }
        } 
      ]
    }
  ]
}
Paul

chefpilot
Posts: 60
Joined: Wed Aug 02, 2017 12:51 pm

Re: Game Controller and Air Player

#9 Post by chefpilot » Mon Dec 06, 2021 1:41 pm

Hi Paul,
thank you very much.
Unfortunately I'm not as deep in the system as you are, shame on me!
I understand and being happy with the codes for both instruments but I do not have an idea where to put or what to do with your plugin(GCI).
I guess that would be the last step to the success.
Marc

Tetrachromat
Posts: 127
Joined: Sun Feb 14, 2021 6:55 pm

Re: Game Controller and Air Player

#10 Post by Tetrachromat » Mon Dec 06, 2021 3:40 pm

Hi Marc,

Probbably I did not explain the purpose of the plugin very well. Let me explain with my use case first. In my home cockpit, I use several inputs from 4 different game controllers in Air Manager. If I would go your way, I would need several "producer" instruments writing to SI bus, and the same number of "consumer" instruments subscribing to the SI bus.

Now, with the plugin, I have only one "producer", namely the plugin itself. I do not need to code any "producer" instrument. So what the plugin really does is replacing all those "producers". I just need to configure the plugin, to let it produce the intended actions (publishing to the SI bus is just one of the possible action).

I can understand that you are questioning the advantage of such a solution if you have just one single "consumer" instrument. To be honest I wouldn't do it myself either. But if you think into the future, you might use other controller inputs for other purposes beside just the trimming. Then it would be of more value to work with the plugin.

If you will stay with just this trimming use case, I would recommend that you stay away from the SI bus at all. It does not give you an advantage. There will be an easier solution than using the SI bus.

Paul

BTW:
What version of Air Manager / Air Player and X-plane plugin are you using? Are you on 4.0.2 version or on the 4.1 BETA?

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests