zap737

How to sense if Joystick Button is Being Held

Recommended Posts

Hi,

First, I am new to Linda so any guidance will help a lot.

I have a Thrustmaster Warthog throttle and am trying to set up the speed brake switch for use in a fighter aircraft.

In the F-18, the speed brake position is variable and can be stopped in any position.

The speed brake switch is a three position switch, spring loaded to rear (extend speed brake), centre and forward (non-spring loaded retract.)

I want to be able to extend the speed brake as long as the switch is being held in the rear position.   I want the speed brake to stop at the current position when the button is released.  Similarly, I want the speed brake to retract whilst the switch is held in the forward position, and to stop at the current position when the button is moved to the centre.

1.  How do I refer to the speed brake switch on my throttle in the script ?

2. How do i test if it is being held or released in the script ?

3.  Let's say the speed brake takes two seconds to extend from fully retracted to fully extended.   So If I held the switch for one second, it would be 50% extended.   What is the best way to sense how long the switch is being held?  For-to-next loops?  Timers?

Thanks

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

Hi, welcome to LINDA. I have moved your question under Support to answer it.

There is nothing quite as complicated required. You require only 2 functions for your airbrakes - extend and retract - and you will probably find the default functions (in FSX Lib) will do. There is no need for-next loops and LINDA will handle the timers. The function is just a single pass extend/retract command.

You assign the functions to the OnPress and OnRepeat of the desired button. This will cause the function to be called as long as the button is held pressed. 

 

Share this post


Link to post
Share on other sites

Hi Andrew, thanks for the reply.

I tried every assignment I could think of.  The only P3D assignment for the speed brake I could find is Spoilers.... See attached image at this link

https://imgur.com/DzFIReY

I assigned OnPress and OnRelease to every spoiler FSX control I could find and none would hold the speed brake at the position it was when the switch was released.

The aircraft I am trying to do this with is the F-16 which comes as a default aircraft in P3D.

 

Share this post


Link to post
Share on other sites

sorry I meant to say "..I assigned OnPress and OnRepeat to every spoiler FSX control

Share this post


Link to post
Share on other sites

Hi

I have looked at the default F16 for you. The airbrakes (spoilers) appear to only operate in the fully open or closed positions. There is a LVar SPEED_BRAKE_POS but it only reports 0 or 100 (closed or open).

You have found the correct FSX Controls for SPOILERS_ON (66064) to open, SPOILERS_OFF (66065) and SPOILERS_TOGGLE (65589). These can be called using the default function in Lib-FSX using _SPOILERS_ON(), .._OFF() and ..._TOGGLE(). No parameter is required.

The _SPOILERS_ON() and _SPOILERS_OFF() need to be assigned to OnPress for the Warthog button. The OnRepeat will have no effect as there are no intermediate positions.

Share this post


Link to post
Share on other sites
Posted (edited)

I found that writing a value to the Lvar:SPEED_BRAKE_POS, one cannot set a specific speed brake position, but you can read a position from that Lvar. by using ipc.readLvar("L:SPEED_BRAKE_POS")

I also found that passing a parameter between -16353 and +16353 to the function in Lib-FSX ......  _AXIS_SPOILER_SET () does in fact set the speed brake position.

So my thought is to read what position the speed brake is in  when the switch is released ( by using .ipc.readLvar("L:SPEED_BRAKE_POS"))  and then passing that parameter to _AXIS_SPOILER_SET () and then binding that to the OnRelease action.

Hence my question on how do you refer to the speed brake button in code.
 

Edited by zap737

Share this post


Link to post
Share on other sites

Ok. What you want then is 2 functions: airbrake_inc and _dec. The first reads the spoiler position and increments it by a value and then writes the new value. The second will just subtract the increment. Based on the 16000 range you will find that you need to add/subtract 500 or 1000 with each iteration. Then assign the 2 functions to the OnPress and OnRepeat as before.

You should write your functions in the aircraft’s user.lua file. This will avoid corrupting the supplied actions.lua file. With default aircraft like F16 it is a bit different and you can’t just click on the Edit button beside the aircraft name. You have to open the user.lua file in the LINDA Editor. 

When writing the functions there is quick way to test them. Save the changes and then right click on the function name. It will execute immediately and display its name on the top line of the Flt Sim.

 

Share this post


Link to post
Share on other sites
Posted (edited)

Hi Andrew, I tried all you suggested but the movement of the speed brake was jerky and far too slow even assigning it to the OnRepeat function.

I did however get a very smooth movement by using  a do..while loop as follows:

 

function SB_Inc ()
	n = ipc.readLvar("L:SPEED_BRAKE_POS") * 16353 / 100
	while n <= 16353 do
	_SPOILERS_SET (n + 5)
    n = n + 5
	end
end

 

The code above runs the speed brake from closed to open very smoothly and nicely.

However, when I tried to assign an action to OnRelease so that it would set the speed brake to the position when the switch was released,  the problem was it waited until the do..while loop finished executing before it set the position and the position was always at the fully extended position because it had to wait for the do..while loop to finish.

So what I need to do is test whether the joystick speed brake button is being held (or has been released) inside the do..while loop.

That way I can break out of the do..while loop when the switch is released and the speed brake wiill stay in whatever position it was when that happened.

Is there a way of doing that?

 

Edited by zap737

Share this post


Link to post
Share on other sites
Posted (edited)

Well done on the function but you have hit the problem of real time computing. You have to avoid long for/next or do/while loops because these will dominate the processing until complete.

Functions should only do an action of limited duration (less than 100th of a sec) that can be repeated when called from the timer. It may be impossible to get a fully smooth action of the airbrake while meeting these restrictions. I believe you will have to accept it moving in small steps (no one will be looking - it is the effect that is important). Remember that all the other LINDA code has to be run within the avaioable time.

Button presses are handled immediately by LINDA in queued sequence with the OnPress call. Continued presses are added to a queue by OnRepeat and actioned on the next iteration. This runs at 10Hz. It is a balance between smoothness and how far the air brakes should move every 10th of a second. 

Edited by ScotFlieger

Share this post


Link to post
Share on other sites

Hi Andrew, thanks for your help and patience.

I improved my solution by using the humble mouse macro. It now extends and retracts pretty smoothly.

I recorded a mouse macro by clicking on the speed brake switch in the cockpit and assigned the macro to the OnRepeat of the speed brake.

Share this post


Link to post
Share on other sites

Glad to hear that you have a working solution.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now