Repository navigation
motion: fix Android hid-nintendo axes, clock skew, and gyro bias - #545
Conversation
…1-beta-mali redeploy [skip ci]
mamcjr
left a comment
There was a problem hiding this comment.
Thanks, this is the right fix for genuine Nintendo pads on Android: the raw Nintendo frame passed through as SDL's moves all three axes, and none of the four swaps in Settings > Controls > Controller gyro axes (f72bf98) can express (-y, z, -x). Keeping that setting after your normalisation, for clones like the DataFrog, is the right order. It merges cleanly with main.
One thing to fix before merging:
- Tilt with only the accelerometer stops working. On main,
own_samplepasses the phone's accelerometer toadd_sampleon every read, so with no gyroscope events the attitude still follows gravity. Hereown_samplecallsadd_sample(w, m_last_accel, testing, dt)(motion.c, inown_sample), so outsideLARECOMP_MOTION_TESTit integrates zero, and the phone accelerometer path only levels while!q_set. On a phone without a gyroscope (the log says "no gyroscope among N sensors"), shrine tilt puzzles and the bow follow the phone on main and freeze after the first frame here. The same happens on any phone when the gyroscope pauses for more than 250 ms. Passingm_phone_have_accel(or "the phone's accelerometer is live") instead oftestingwhen the phone is the source should fix it, and a test for the accelerometer-only case would keep it fixed.
Smaller ones:
- The phone's bias learner is rewritten too, not only the controller path. It looks better than main's (main absorbed a slow bow aim held for 1.5 s into the bias), but it changes motion on every phone, so I'll check drift and slow aim on the POCO.
pad_clock_dtscales every controller on every platform. The ±0.08 deadband should leave HIDAPI and DualSense at 1.0, but could it be limited to the Nintendo Android path, or log the measured ratio for every pad?- The Auto-mode wake rule now counts up on a change and down on a repeat instead of needing 20 changes in a row. The
turnedcheck still guards it; just noting it for the GameSir-style reports (#115).
f72bf98 isn't released yet, so this can go out in the same release and no player gets a saved swap applied twice.
|
Rebased onto
Verified after the final edit on
|
08963d5 to
6f30e81
Compare
|
Thanks, the accelerometer-only fix works and
The phone's bias learner rewrite I'll check on the POCO (drift, slow aim, the 180 degree turn) once these are in. |
6f30e81 to
d038d38
Compare
|
Updated
|
Fixes Switch-mode controller motion aiming on Android (
057evia kernelhid-nintendo+ AndroidSensorInputMapper), resolving wrong gyro/accel axes, aim rebound, resting/aiming drift, and Auto-mode wakeup/lockout bugs.Related to #338, #288, #521, #446.
Root Causes Found on Device
Tested on Galaxy Tab S11 (
SM-X730, Android 16 / API 36, Mali-G925-Immortalis MC121.3.278, BOTW 1.9.0) with an 8BitDo Pro 2 paired over Bluetooth in Switch (S) mode (Nintendo Switch Pro Controller,vendor 057e product 2009, type 7, path -, sensor rate 0 Hz):(-y, z, -x)on Android:Linux
hid-nintendoemits IMU axes in Nintendo coordinates (+Xforward,+Yleft,+Zup). Android'sVendor_057e_Product_*.kland SDL3'sAndroid_OnJoySensor(path == NULL) pass those raw values through untouched, unlike SDL's Linux evdev (CorrectSensorData) and HIDAPI Switch (SDL_hidapi_switch.c) backends which remap to(-y, z, -x). The 4 swaps inSettings > Controls > Controller gyro axes(f72bf98) cannot express(-y, z, -x).~1.92x–1.96xMSC_TIMESTAMPclock skew):A direct kernel
/dev/input/event13capture on Tab S11 showedhid-nintendoMSC_TIMESTAMPadvancing 1.92x real wall time (3.8093 shw vs1.9830 swall; in-app measured1.96x). Feeding unscaledsensor_timestampdeltas nearly doubles integrated angular velocity during tilts, causing the Mahony gravity filter (KP = 0.5f) to pulldirbackward as soon as movement stops (identical to the rebound symptom in#521).The previous resting bias learner used the controller's
sensor_timestamp(~0.78 sreal window), accepted motion up to0.06 rad/s(~3.44 deg/s) without checking accelerometer stability (allowing slow bow tracking to pull bias off-center and create reverse drift), shared one bias/clock state across phone and controllers, and left phone bias in screen-rotated coordinates (to_screen()), where a 180-deg display flip could invert bias error past the lock threshold.Changes Made
runtime/hle/motion.c:hid-nintendoframe normalization: For__ANDROID__pads withvendor == 0x057e,path == NULL, and Switch 1 IMU PIDs (0x2006,0x2007,0x2008,0x2009,0x200e— excluding Switch 2 and retro NSO pads), automatically apply(-y, z, -x)to both accel and gyro beforeSettings > Controls > Controller gyro axes(0..3). A validLARECOMP_PAD_GYROenv override still takes direct precedence over raw axes (LARECOMP_PAD_GYRO=x,y,zacts as a zero-build rollback).pad_clock_dt): Measureshw_ts / host_tsover 0.25 s initial / 0.50 s sliding windows (with a±0.08deadband around1.0x,0.7/0.3smoothing, USB HIDAPI same-host burstdt > 0preservation, and 32-bitMSC_TIMESTAMPwrap handling) and scales controllerdtto real seconds. Rollback env:LARECOMP_NO_CLOCK_SCALE=1.MotionPadisolation + single-active-pad arbitration: TracksPadClock,GyroBias,accel[3],have_accel, and Auto-mode liveness per controller slot (m_pads[]), preventing alternating multi-controller events (e.g., Bluetooth Joy-Con L+R or grip + BT pad) from resetting clock/bias/q_seton every packet, ignoring stale post-close events, and leaving phone attitude untouched when an unrelated pad disconnects.GyroBias+ recovery: Separatem_phone_bias(estimated/subtracted in raw MEMS coordinates beforeto_screen_turns()so 180-deg screen rotations preserve bias cleanly) and per-padbias. Uses realhost_tswith a 250 ms pause-gap check, running-mean gyro + accel stillness bounds, gentle 3 s drift tracking, and a 6 s dead-still recovery path (rollback:LARECOMP_NO_GYRO_BIAS=1).NaN/Inf, Auto-mode & underflow hardening: Passes explicithave_accelintoadd_sample()soGYRO-before-ACCELpackets after axis change, pad switch, ormotion_reset()wait cleanly for fresh accel; addsvec3_finite()/ negated range checks; caps Auto-modechangingcounters (19pre-turn /20overall) so coarse-LSB pads wake while#115noise cannot stick; guards orphan accel andhid_sixaxis_states()unsignednow - m_live_nsunderflow.runtime/hle/input.c: Disables gamepad gyro/accel sensors beforeSDL_CloseGamepad()and notifiesmotion_pad_closed(id)afterward.tools/test_motion.c,tools/test_android_runtime.ps1: Addstest_motion(15 isolated Bionic/POSIX regression cases covering axes, negative controls, env rollbacks,pad_gyro_axes0..3withGYRO-before-ACCEL,1.92xclock skew +LARECOMP_NO_CLOCK_SCALE=1negative control, mid-burstdt_host == 032-bit wrap, bias immunity/recovery, phone 180-deg flip, multi-pad/post-close arbitration, Auto-mode wakeup/timeouts/NaNrejection, and underflow/reboot persistence), skipped cleanly ifandroid/deps/outSDL3 headers have not been built yet.Rollback & Off-Path guarantees
!g_settings.motion), behavior and cost are unchanged (motion_event()returns immediately; non-Nintendo pads seescale = 1.0xinside the±0.08deadband with zero remap and zeronintendo_androidpath on desktop).Settings > Driver compatibility > Driver variables):LARECOMP_PAD_GYRO=x,y,z— bypasses Nintendo Android auto-remap.LARECOMP_NO_CLOCK_SCALE=1— disableshw_ts / host_tsclock scaling.LARECOMP_NO_GYRO_BIAS=1— disables resting bias subtraction.Verification (Exact PR Commit
08963d5)1. Native Android Unit Tests (
tools/test_android_runtime.ps1)SM-X730):9/9Bionic test binaries PASS (15/15isolatedtest_motionscenarios PASS).aarch64-linux-android30and-U__ANDROID__non-Android parity,-Wall -Wextra -Werroronruntime/hle/motion.candtools/test_motion.c): 0 warnings.2. Isolated Live Verification on Galaxy Tab S11 (
SM-X730, Android 16, Mali-G925 r49p11.3.278, BOTW1.9.0)Verified with
driver_envempty andpad_gyro_axes = 0(LARECOMP_LOG_MOTION=1):Controller (
motion_source = 2, 8BitDo Pro 2 in SwitchSmode):0.000turns drift across 4,694 resting samples (~53 s), real sample period11.2–11.5 ms,0errors. Also confirmed in-hand in-game with bow aiming & rune tilts (no manualLARECOMP_PAD_GYRO, no rebound, no drift).Tablet built-in gyro (
motion_source = 1,LSM6DSVTR):0.000turns drift across ~52 s,0errors.3. Hardware / Driver Matrix
SM-X730, Android 16) + 8BitDo Pro 2 (Smode, BT)(-y,z,-x),1.96xclock scaling, resting bias lock,0drift / reboundLSM6DSVTRIMU0drift / clean lock; 180-deg flip verified in unit test0x2069) / NSO (0x2017) / Desktoptest_motion)#521)057ehid-nintendopath)CPH2653)