FreeJoy-Team / FreeJoy

STM32F103 USB HID game device controller with flexible configuration
GNU General Public License v3.0
701 stars 134 forks source link

Improve latency #41

Open misteraddons opened 3 years ago

misteraddons commented 3 years ago

I work on the MiSTerFPGA project. We have a testing suite used to measure input latency using an FPGA in the loop.

FreeJoy is something I'm very intersted in using in some upcoming projects, but it would be awesome to find a way to decrease the input latency.

The best controllers have 0-5ms of latency, where FreeJoy currently has ~60ms: Average: 60.1666 ms Max: 63.804 ms Min: 56.408 ms Std Dev: 1.591 ms I set up all buttons to be BTN_GND, and ran the test with more than 2000 samples.

Let me know if I can be of any assistance!

vostrenkov commented 3 years ago

Hello, i guess you get 60ms latency because of debouncing algorithm for the buttons. You can decrease debouncing time to improve input latency but there are ghost presses possible at to low values

vostrenkov commented 3 years ago

image

misteraddons commented 3 years ago

Excellent, thank you! I haven't tested without debounce so it may still need to be tweaked but here are my current results: Capture Also, full spreadsheet of results here: https://docs.google.com/spreadsheets/d/1KlRObr3Be4zLch7Zyqg6qCJzGuhyGmXaOIUrpfncXIM/edit?usp=sharing

Looking forward to the possibility of 2P support :)

Avalonnw commented 3 years ago

Ideally debounce should happen in the hardware. So, assuming it is, what would be the latency with software debounce off?

misteraddons commented 3 years ago

The Notes column of the results shows that the top 4 results were with software debounce disabled.

Avalonnw commented 3 years ago

The Notes column of the results shows that the top 4 results were with software debounce disabled.

I can see that. I meant I was curious to see FreeJoy's latency with debounce off. I am aasuming it would be in 6.2-13.9 range?

misteraddons commented 3 years ago

Sorry, I don't follow. Those are the results with debounce off.

Avalonnw commented 3 years ago

FreeJoy results are with debounce set to 50ms. I says so in your table.

misteraddons commented 3 years ago

That's only the bottom result. The other 4 are with "0 ms debounce".

Avalonnw commented 3 years ago

That's only the bottom result. The other 4 are with "0 ms debounce".

Ah, sorry, I thought the top 4 were done with other controllers. My bad.

misteraddons commented 3 years ago

No problem :)

vostrenkov commented 3 years ago

You can also try to set higher USB exchange rate in advanced settings. It should reduce input latency even more. But i think 30-60ms is quite enough for most of applications. Also i should say that button processing have lowest priority in the software. So adding analog axes and digital sensors to the system may significantly increase button input latency

misteraddons commented 3 years ago

I forgot to mention, this is with 1 ms/1000 Hz polling enabled on the MiSTer FPGA system. The lowest latency devices are around 0.75 ms. I see you're looking into bootsector's STM32 HID bootloader. In talking with him, that should help significantly. Let me know if you'd like me to retest with any experimental firmware.

vostrenkov commented 3 years ago

Thank you! I'm satisfied with the testing results enough, so I don't think there is a reason to improve input performance, at least for now.

I used to analyze some lightweight bootloader implementations but finally made a fat one using stm usb library. It does everything i wanted though

misteraddons commented 3 years ago

I ended up working with bootsector and he was able to get his own firmware down to 0.73 ms on MiSTer. If you ever want to improve latency, that might be something to look into. Cheers and thanks for the awesome, flexible code!