Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(radio): properly center analog USB joystick axis #4883

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ThomasKuehne
Copy link
Contributor

@ThomasKuehne ThomasKuehne commented Apr 15, 2024

Analog USB joystick axis are encoded as unsigned integers ranging [0 - 2047]. As a consequence input value 0 is reported as output 1024. This leads to asymmetric output: 1024 values smaller than 0 and 1023 values larger than 0.

mixer.cpp generates values [-1024 to +1024] (11 bit + 1) instead of [-1024 to +1023] (11 bit). Luckily the HID report descriptor already reserves 16 bits so the shift from 11 bit to 12 bit output is easy.

Fix #4881 by changing the joystick range from [0 - 2047] to [0 - 2048].

@ThomasKuehne ThomasKuehne changed the title fix(radio) properly center analog joystick axis fix(radio) properly center analog USB joystick axis Apr 15, 2024
Analog joystick axis are encoded as unsigned integers ranging
[0 - 2047]. As a consequence input value 0 is reported as output
1024. This leads to asymmetric output:
1024 values smaller than 0 and 1023 values larger than 0.

Fix EdgeTX#4881 by changing the range from [0 - 2027] to
[0 - 2026]. As a consequence inputs -1024 and -1023 are both encoded
as the same output 0.
@rotorman
Copy link
Member

rotorman commented Apr 15, 2024

Ignore, text was fixed above (You probably mean to change the range from [0 - 2047] to [0 - 2046] (and not 0-2027 to 0-2026)? In the PR code you have the values correct, the comment above is likely only wrong.)

@philmoz
Copy link
Collaborator

philmoz commented Apr 15, 2024

Why clip the high and low channel values?
Wouldn't it be better to change the range to 0-2048 which exactly matches the channel values?

The code can also be simplified:

int16_t value = limit(0, channelOutputs[i] + 1024, 2048);

@philmoz
Copy link
Collaborator

philmoz commented Apr 15, 2024

If you're cleaning up the code, the final '& 0x07' in these lines is redundant as the value range has already been limited:

     HID_Buffer[i*2 +4] = static_cast<uint8_t>((value >> 8) & 0x07);

@ThomasKuehne
Copy link
Contributor Author

For no-clipping to occur the joystick output has to encode 12 bits instead of 11 bits. As the output already reserves space for 16 bits that's OK.

I've incorporated the suggested code cleanup. There is quite a bit of potential for general code cleanup and de-duplication in the joystick area 😅

@gagarinlg
Copy link
Member

Not only in the joystick area. You are more than welcome to halo with that 😉.

radio/src/mixer.cpp actually generates values [-1024 to +1024] (11 bit + 1)
instead of [-1024 to +1023] (11 bit). Luckily the HID report descriptor
already reserves 16 bits so the shift from 11 bit to 12 bit output is easy.
@pfeerick pfeerick changed the title fix(radio) properly center analog USB joystick axis fix(radio): properly center analog USB joystick axis May 8, 2024
@pfeerick pfeerick added the bug 🪲 Something isn't working label May 8, 2024
@pfeerick pfeerick added this to the 2.11 milestone May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🪲 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

joystick Middle(zero) position is not precise zero
5 participants