Game Controller and Air Player

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

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

Game Controller and Air Player

#1 Post by chefpilot »

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.

Code: Select all

-- Create a new variables
trimUp_id = si_variable_create("trim_up", "BOOL", false)
trimDown_id = si_variable_create("trim_down", "BOOL", false)

function callback(type, index, value)
print("type = " .. type .. ", index = " .. index .. ", value = " .. tostring(value))
if type == 1 then
if index == 4 then
if value == true then
-- print("trim down pressed")
si_variable_write(trimDown_id, true)
elseif value == false then
-- print("trim down released")
si_variable_write(trimDown_id, false)
end
elseif index == 2 then
if value == true then
-- print("trim up pressed")
si_variable_write(trimUp_id, true)
elseif value == false then
-- print("trim up released")
si_variable_write(trimUp_id, false)
end
end
end
end

function new_data_callback(data1, data2)
print("New data1: "..tostring(data1))
print("New data2: "..tostring(data2))
end

si_variable_subscribe("trim_up", "BOOL",
"trim_down", "BOOL", new_data_callback)
list = game_controller_list()

for k, v in pairs(list) do
game_controller_add(v, callback)
end
Marc

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

Re: Game Controller and Air Player

#2 Post by Tetrachromat »

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

Paul

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

Re: Game Controller and Air Player

#3 Post by chefpilot »

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

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

Re: Game Controller and Air Player

#4 Post by chefpilot »

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: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: Game Controller and Air Player

#5 Post by Tetrachromat »

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: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: Game Controller and Air Player

#6 Post by Tetrachromat »

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

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

Re: Game Controller and Air Player

#7 Post by chefpilot »

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: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: Game Controller and Air Player

#8 Post by Tetrachromat »

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: 90
Joined: Wed Aug 02, 2017 12:51 pm

Re: Game Controller and Air Player

#9 Post by chefpilot »

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: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: Game Controller and Air Player

#10 Post by Tetrachromat »

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