Skip to content

feat: anti-jitter on the lever position, and 0.6.0 - #17

Merged
FilipNone merged 1 commit into
mainfrom
feat/anti-jitter
Nov 26, 2026
Merged

FilipNone merged 1 commit into
mainfrom
feat/anti-jitter

Conversation

@FilipNone

Copy link
Copy Markdown
Owner

Problem

The lever's hall sensor reads noise even when the lever is still. Measured on a unit: x and y move by about 2 units (sd), up to 18 peak to peak, about 330 times a second at rest, and z flickers by one step of its high byte. The handbrake axis twitches by up to 2 % while held, and gear 7, which is held by field strength, can drop and come back right at its edge.

Approach

Firmware (sensor_math in shifter.c). Anti-jitter (24 36 v: 0 none, 1 low, 2 medium, 3 high; saved in option bits 5 and 6) steadies x, y and z (s14) before anything is worked out from them, so it covers gears, sequential shifts, the handbrake axis, report 0x24 and calibration. For each value:

  • A running average over 2 samples (low, medium) or about 4 (high), then a hold band of 4/8/16 units for x and y, and 13/16/20 for z.
  • If the average stays more than half a band away for 128 ms, the output moves to it. A still lever reads where it is, and gear 7 settles on one side of its limit.
  • A change over 150 (x, y) or 80 (z) passes at once, so shifts and yanks are never late.
  • One frame more than 600 away from the last is skipped as a bad read. Seen once on the unit: y 6598 at rest.

None gives stock's raw values.

Raw report. While anti-jitter is on, input report 0x26 (26 x y, big-endian) takes every other report 0x24 slot. The host gets no more reports than before, and report 0x24 comes every 4 ms instead of 2. Two earlier designs failed on the unit:

  • Reading 0x26 as a feature report every 15 ms froze the unit during a settings write (see below).
  • Asking for it with a command every 400 ms made Chrome hold input back for 60 to 90 ms after each command. Python on Linux saw no gap with the same commands.

Freeze fix (flash.c, usb.c). Control requests arriving during a settings erase locked up the unit: no USB, no LEDs, no watchdog. It also happened with interrupts off. Settings writes and enter_bootloader now keep interrupts off and pause the USB block while the flash is busy, about 25 ms:

  • EP0 ignores the host, so a request then fails.
  • EP1 answers NAK.

The emulator now models erase and program time, and flags any interrupt or USB packet taken while the flash is busy. That check failed on the build that froze. The differential tests turn the timing off, because stock writes for about 70 ms and its timing would no longer line up.

Web tool. A new Anti-jitter section has four choices and a live trace: Lever x and Lever y (plus the handbrake axis in handbrake mode), with the position games see in colour over the raw reading in grey. Each lane zooms in on its own range. The page sends nothing to the shifter for the trace.

CLI and docs. openshifter_ctl.py anti-jitter none|low|medium|high, plus docs/protocol.md, docs/testing.md, the wiki and the README. Version 0.6.0.

Cost: about 900 bytes of code and 28 bytes of RAM.

Automated validation

  • make -C firmware builds without warnings
  • make -C firmware test passes: 94 tests, differential tests ran (stock image), nothing skipped
  • CI passes on the latest commit
  • Code review of the diff done and findings resolved

New tests:

  • test_shifter.AntiJitter (9):
    • noise on x, y and z at neutral, in gear 3 and on the handbrake
    • gear 7 at its limit: the raw reading chatters, the filtered one doesn't
    • shifts and yanks give the same reports at the same samples as none
    • slow moves, calibration on a noisy sensor, a bad read, report 0x26, and a sensor restart
  • test_emulated (3):
    • anti-jitter on the real binary in H and handbrake mode
    • report 0x26 rate and slots
    • control requests every 15 ms through a settings write
  • test_ctl.test_anti_jitter, and a web test for the session and the simulator

Replaying this unit's real recordings through the C core:

Level At rest: x/y changes Holding the handbrake: axis reversals Pull delay
None 389/s 75/s 0 ms
Low 1.4/s 2.3/s 2 ms
Medium 0 0 2 ms
High 0 0 7 ms

Hardware testing

  • Tested on a real shifter (TBRS004-20241224, Linux, Chrome), over several builds of this branch:
    • At rest in H: x/y changes per second went from 326 (none) to 0.3 (low) and 0 (medium, high).
    • Gear 7 held in the hand: from 442/s to 3.1, 0.6 and 0.
    • The handbrake held halfway: axis reversals from 11.7/s to 1.1, 0 and 0.3. No gear changes at any level.
    • The level survives a replug.
    • Report 0x26 at 227/s next to report 0x24 at 227/s and report 1 at 454/s, longest gap 2 to 4 ms.
    • 3 minutes of level changes about every 1.7 s (about 20 flash writes after the save limit) with no stall. The build without the USB pause froze at the first write.
    • The web trace in Chrome: smooth after the page stopped sending commands.

Limitations and follow-up

  • The bands are tuned on one unit. Others may have different noise.
  • With anti-jitter on, report 0x24 comes at 250 Hz instead of 500. Report 1, which games read, is unchanged.
  • In Chrome, any command the web tool sends (the colour preview while dragging, for example) holds input back for a moment. The trace avoids this; the rest of the tool is unchanged.

The lever's hall sensor reads about 2 units of noise in x and y even when
the lever is still, and z flickers by one step, which can drop gear 7 at
its edge. Anti-jitter (none, low, medium, high) steadies x, y and z before
anything is worked out from them: gears, shifts, the handbrake axis,
report 0x24 and calibration. Each runs through a short running average
and holds inside a band; a still lever settles on its average after
128 ms, large moves pass at once, and a single frame far from the last
is skipped as a bad read. None (the default) gives the raw values.

While anti-jitter is on, input report 0x26 carries the raw x and y in
every other report 0x24 slot. The web tool's new Anti-jitter section
draws the position games see over the raw reading, live.

Settings writes now take no USB packet and no interrupt while the flash
is busy. A unit froze when control requests arrived during an erase. The
emulator now models erase and program time and flags both.

Also: openshifter_ctl anti-jitter, docs, wiki, version 0.6.0.
@FilipNone
FilipNone merged commit 98a4ee5 into main Nov 26, 2026
8 checks passed
@FilipNone
FilipNone deleted the feat/anti-jitter branch November 26, 2026 12:33
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.

1 participant