Repository navigation
USB HID host for joystick/gamepad trainer input #7844
RobertoD91
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
This is probably a bad idea, that adds unnecessary complexity to the codebase, but I was thinking: could the radio USB port be used in host mode to connect a joystick/gamepad directly and use it as the "student" input in the trainer system?
The idea came from a completely unnecessary experiment I wanted to try: controlling an FPV airplane with a joystick.
So I put together an ESP32-S3 USB HID → SBUS/CPPM converter:
https://github.com/RobertoD91/esp32-joystick2trainer
NB: Please don't use it: it is just an MVP and was build with AI (Fable5).
I originally preferred this external approach because it seemed relatively safe for experimentation. I would use it with a friend acting as the master pilot, so if anything went wrong they could simply disable trainer mode and take control again. The model I would use also has a flight controller with failsafe/RTH.
After testing the converter, I realised that, at least in theory, the same thing could be done directly inside EdgeTX: use the radio USB port in host mode, read a HID device and feed its axes/buttons into the trainer channels.
Out of curiosity I built a prototype on my fork and, surprisingly, it works:
https://github.com/RobertoD91/edgetx/tree/2.12-feature-usb-host-joystick-trainer
I tested it on a RadioMaster TX16S MkII with a Microsoft SideWinder Force Feedback 2.
Limitations, besides common sense:
🤖 Technical details of the prototype
The STM32F4 OTG core can also operate as a USB host. The prototype adds a new trainer mode, "Master/USB HID", where the USB port runs in host mode and a standard HID joystick/gamepad can provide the trainer channels
TR1..TR16.The HID report descriptor is parsed at runtime, with the goal of supporting standard HID joystick/gamepad report descriptors. Axes, hat and buttons are then mapped to the normal EdgeTX trainer channels, so everything after that point uses the existing trainer/mixer machinery.
Relevant commits:
f8d051b,82a510d(diff against 2.12).Current implementation:
OTG_FS, ~600 lines): port reset, enumeration,SET_CONFIGURATION,SET_IDLE, HID report descriptor fetch and interrupt IN polling atbInterval, running in a dedicated low-priority task. Single device, no hubs.radio/src/usbh/, with gtests using the real 1343-byte SideWinder descriptor, a generic gamepad descriptor and array-style buttons.MASTER_USB_HID), YAML entry, translations, Companion support, a status line on the color LCD trainer page and a CMake optionUSB_HOST_JOYSTICK, currently enabled only for the TX16S.usbPlugged()reports unplugged). Switching away from the USB HID trainer mode gives the peripheral back to the normal USB device stack.Bench testing so far:
The same
OTG_FSperipheral is used on several other F407/F429 radios, so supporting some of them may require only relatively small board-specific changes, but I have not tested any of them.Conclusion
To be clear about who did what: the idea, the architecture choices (new trainer mode, host in a separate task, runtime descriptor parsing) and all the bench testing are mine; the code itself was largely written by the AI assistant from my descriptions and test feedback. I have not reviewed the USB host implementation line by line against the STM32 OTG reference manual, and I would not consider the current code PR-ready.
There is also plenty still to clean up: trainer channel mapping is currently fixed (axes, then hat, then buttons), the status line still contains debug information and there is no user configurability.
The external ESP32 → SBUS/CPPM converter is probably still the most pragmatic solution, and adding a USB host stack, another task and configuration UI to EdgeTX may simply not be worth the complexity.
I'm mostly posting this to understand whether the idea is interesting at all before spending more time on it, and to leave the prototype somewhere others can find it.
If there is interest, I can keep testing it on the TX16S and MT12, clean it up based on feedback and eventually turn it into a proper PR.
All reactions