Skip to content

fix: clamp per-axis gyro deadzone distance to zero and fix boundary condition#1282

Open
Sohmsss wants to merge 1 commit intoAntiMicroX:masterfrom
Sohmsss:fix/gyro-deadzone-drift
Open

fix: clamp per-axis gyro deadzone distance to zero and fix boundary condition#1282
Sohmsss wants to merge 1 commit intoAntiMicroX:masterfrom
Sohmsss:fix/gyro-deadzone-drift

Conversation

@Sohmsss
Copy link
Copy Markdown

@Sohmsss Sohmsss commented Mar 21, 2026

Summary

Fixes gyro drift caused by two bugs in the deadzone math:

  • calculateX/Y/ZDistanceFromDeadZone() returned negative values when one axis magnitude was smaller than sqrt(dead_zone² - other_axes²). The negative distance was used directly as a mouse-movement speed, producing spurious movement opposite to the intended direction — visible as asymmetric drift (e.g. always drifting right slightly).
  • Off-by-one boundary condition in calculateSensorDirection(): used strict distance < m_dead_zone, so a reading whose 3-D magnitude was exactly equal to the configured deadzone was not treated as centered. Fixed to distance <= m_dead_zone.

Changes

File Change
src/joysensor.cpp Wrap abs(axis) - sqrt(discriminant) in std::max(0.0, ...) for all three axis distance functions
src/joygyroscopesensor.cpp distance < m_dead_zonedistance <= m_dead_zone
tests/testjoysensordead.cpp New Qt Test unit tests covering both regressions
tests/CMakeLists.txt Register new test target

Test plan

  • TestJoySensorDead::xDistanceNeverNegative — confirms the negative-return bug is fixed (x=0.1, y=0.3, z=0 with 20° deadzone previously returned −0.078)
  • TestJoySensorDead::yDistanceNeverNegative / zDistanceNeverNegative — same for Y and Z axes
  • TestJoySensorDead::xDistanceZeroInsideDeadZone / Y / Z — all-small vector returns 0
  • TestJoySensorDead::directionCenteredAtDeadZoneBoundary — sensor at exactly the deadzone radius returns SENSOR_CENTERED
  • TestJoySensorDead::directionCenteredInsideDeadZone — sensor inside deadzone returns SENSOR_CENTERED

Closes #1279

…ondition

Two bugs caused gyro drift even when a hardware deadzone was configured:

1. calculateX/Y/ZDistanceFromDeadZone() could return negative values when
   one axis magnitude was smaller than sqrt(dead_zone^2 - other_axes^2).
   That negative distance was passed to mouse-movement as a speed value,
   producing spurious movement opposite to the intended direction.
   Fixed by wrapping the result in std::max(0.0, ...).

2. calculateSensorDirection() used strict less-than (distance < m_dead_zone),
   so a reading whose 3-D magnitude was exactly equal to the configured
   deadzone was not treated as centered and could trigger movement.
   Fixed to use less-than-or-equal (distance <= m_dead_zone).

Adds tests/testjoysensordead.cpp covering both regressions.
@pktiuk
Copy link
Copy Markdown
Member

pktiuk commented Mar 23, 2026

@Sohmsss
Is this PR AI-generated?
If yes, then it should be clearly stated in the commit message and PR description (which tool and model was used)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gyro drift even when high deadzone

2 participants