Cleric-K / vJoySerialFeeder

Feed Virtual Joystick driver with data from a serial port
GNU General Public License v3.0
258 stars 55 forks source link

Proper read sizes for CRSF 500Hz operation #50

Closed CapnBry closed 3 years ago

CapnBry commented 3 years ago

Thanks to you, Cleric-K, I've fixed the read sizes to avoid the "Read beyond end of frame" issue that discouraged me from using them before. 500Hz now works perfectly, no debug message spam.

CapnBry commented 3 years ago

I've cleaned up those things you've suggested and just removed the addr. I had meant to test that instead of Buffer[0] but I guess my brain forgot to use the variable when I wrote the if statement. Tested again and working at 500Hz

Cleric-K commented 3 years ago

Great job! Thanks a lot! I'll compile new release in the coming days.

Best~

CapnBry commented 3 years ago

Awesome, thanks for your guidance! Once you've got a new build posted I'll link it to our ExpressLRS users and the TBS Lounge so Crossfire users will also have a usable wireless joystick option.

Cleric-K commented 3 years ago

Great. Btw I have a working solution for wireless joystick which I haven't published yet. It's a firmware for STM32 bluepill. It currently autodetects pretty much the same protocols that vjsf supports. It's zero conf. Just attach the data wire of the receiver and plug in the device where it's recognized as standard USB HID joystick. It's nice because it works with any device supporting HID (PC, Mac, Linux, Andoid, probably even game consoles) Maybe I can base CRSF implementation based on your code. Do you have such a board to do some tests eventually?

CapnBry commented 3 years ago

I do have an STM bluepill or two lying around. I did something similar a while back with an Teensy where I turned it into an IBUS, SBUS, and SPI CC2500 receiver all in one that just showed up as a HID joystick. I wasn't cool and fancy though and just had them all running on different input pins. Note that CSRF is 420000 baud not inverted serial (I hate that I have to say that) if you plan to support it, but you probably already know.

It has been asked on the ELRS discord what sort of latency using vJoy+vJoySerialFeeder adds versus the direct USB HID solution. Have you performed any tests comparing physical stick to joystick stick latency as part of your development? It must be worthwhile if you're creating a hardware solution.

Cleric-K commented 3 years ago

Hm, the high bitrate could be a problem. Unfortunately STM32F1 doesn't have hardware UART support for inversion. In order not to require any external inversion components I do the following. I connect the Rx at say pin 1. I install interrupt handler that triggers on change. The handler is very simply it sets either the same or the inverted value as output on pin 2. Pin 2 is externally connected to pin 3. This is the only modification needed to the board - a jumper between 2 and 3. 3 is the UART RX. That's how I implement software inversion. Practically I output what I read at 1 (the real signal) by applying inversion if needed, to pin 2, which feeds externally to UART RX at 3. This works pretty well with the 100k speeds but this is four times greater. We'll see if it'll be able to handle it.

I haven't done latency any strict latency tests. Certainly I can't detect humanly any difference between HID and vjsf.

CapnBry commented 3 years ago

That's actually a pretty clever way to do software-based inversion without having to actually decode inverted serial. Believe me, we deal with all sorts of inverted/non-inverted pairings and all manner of hardware in between that tries to mess it up for us. Up until recently ELRS hardware has mostly been hardware designed for another system so the circuits I've seen for dealing with inverted and/or half-duplex serial is just mind-boggling.

So do you have any data regarding latency of using USB HID directly vs vJoy+Feeder? I'd be especially interested if you had any tools you use for measuring it since most of our testing involves using our cellphones to record stick movements in slow-motion and seeing how long it takes for a game to show movement. It is fairly trivial to measure stick to receiver or to a host board providing the USB HID, but including the USB report polling and other windows overhead isn't something I'm familiar with profiling.

Cleric-K commented 3 years ago

No I haven't done any testing. Personally (by feel) I can't detect any difference between hid and vjsf. If I had to place a bet I would guess the hid should be slightly quicker but I doubt that the difference could have any humanly detectible significance.