Author Topic: Joystick axis trigger  (Read 3782 times)

Mindeufair

  • Newbie
  • *
  • Posts: 3
Joystick axis trigger
« on: July 03, 2023, 01:31:17 PM »
Hi everyone,

Is it possible (using an already existing plugin or any other way) to trigger a command using an axis position (for instance call an action when my throttle is under 5%)?

I'm aware it is possible using "complex" commands, with loops etc, but this is really CPU intensive, that's why I was seeking an alternative method to achieve that.

If there is no other way, I might create and publish a plugin, but it will take time as I'm very busy right now (and I have yet another plugin - already done - to publish 👀).

Have a nice day!

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Joystick axis trigger
« Reply #1 on: July 03, 2023, 03:37:30 PM »
Which methods did you try, that were seemingly CPU-intensive?
« Last Edit: July 03, 2023, 05:10:51 PM by Pfeil »

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 291
  • Upstanding Lunatic
    • My AVCS Homepage
Re: Joystick axis trigger
« Reply #2 on: July 04, 2023, 02:04:18 PM »
A function style command such as this is not CPU intensive, and should serve as an example on how easy it would be to create a background function which is always watching one of the joystick axes in order to do something when it is below a certain threshold (where 65535 is the maximum value, or 100%). It is important to be mindful of such options as 'Do not execute this command if it is already running', and the fact that a "Stop all commands" button press or action in another command would end this infinite loop running in the background, but beyond that there is no reason to assume such things are CPU intensive unless they created with poorly designed flow control or functionality:



A plugin to accomplish this would be extreme overengineering unless there were other functions in the plugin or more global/background functionality was desired for this type of system consisting of 'axis < 5% = execute command', though these things would also be possible using an inline function if you wanted to avoid a plugin.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Joystick axis trigger
« Reply #3 on: July 04, 2023, 02:36:56 PM »
Note that in the above example, anything within the condition is executed approximately every 100 milliseconds ( assuming the command being executed doesn't take longer to do so than that), for as long as the axis value is below 5% (approximately, given that ADC resolution can bias that value).

If the intent is to execute a command once, every time the axis value changes from being higher than 5%, to being lower than 5% (or, from being unavailable to available), a modification like this could work:
Code: [Select]
Start Indefinite Loop
    Pause 0.1 seconds
    Set integer [~axisValue] value to the converted value of {STATE_JOYSTICK1Z}
    Begin Integer Compare : [~axisValue] Is Less Than 3277
        Begin Integer Compare : [~axisValue] Equals -1
            Loop Continue
        End Condition
        Begin Boolean Compare : [~triggered] Equals False
            Set Boolean [~triggered] to True
            // Example: DO SOMETHING HERE
            Write [Green] 'Z axis of Joystick #1 is LESS than 5%' to log
            Execute command, 'Example Command Name' (by name) (and wait until it completes)
        End Condition
    Else
        Begin Boolean Compare : [~triggered] Equals True
            Set Boolean [~triggered] to False
        End Condition
    End Condition
End Loop

I chose to move the check for -1 to within the loop as a separate condition; realistically it's not going to matter, but that way only one condition is evaluated while the value is higher than 3277 (which may well be negated by having more actions).
That's not something you'd notice in practice, performance/CPU usage-wise.

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 291
  • Upstanding Lunatic
    • My AVCS Homepage
Re: Joystick axis trigger
« Reply #4 on: July 04, 2023, 03:02:04 PM »
That would work, too - great example as always, Pfeil!!

Though to be honest, and not to detract from the lesson above by Pfeil, I had envisioned flow control under the actual 'Example Command Name' command, as the example I shared above waits for this command to complete thereby making it an appropriate place to handle the waiting for this axis to reset above 5% and easily executing the 'below 5% action' just one time.


This works of course because the action to execute this command inside the loop of the previous command MUST wait for THIS command to end before its loop continues, avoiding any need to poll that axis when it is "in" a "below 5%" state inside the first command example: