Connecting Stretch to xbox adaptive controller

Hello,

I am planning on using stretch RE2 with an xbox adaptive controller.

At first glance I was considering connecting the controller via blue-tooth or connecting it with an xbox dongle and using the same code stretch is currently running for teleoperated, but I have stumble into some problems.

It seems like there is no way to detect other controllers (even those with dongle) without changing the internal code
- On that note: I haven’t found the actual code that stretch uses to connect to the remote when booting up. I am assuming is probably a C++ code somewhere?
- I did find a stretch_body file in the github that had an “xbox_controller.py” python program, which seems like is the code stretch is using, but once again I am not sure what code Stretch is calling when booting up.

What way would be the easiest/less trouble to implement the already excising code with a new controller. I have very little C++ knowledge, so if possible implementing this with python.

Thank you for the assistance,

Rafael Morales

1 Like

Hi @Rafael_MM, welcome to the forum! I’ll detail how the standard xbox controller commands the robot and this should help make it clear where you can make modifications to get the adaptive xbox controller working instead.

The standard xbox controller has a USB dongle that plugs into the USB hub in the trunk of the robot. When the robot’s on-board computer boots up, the USB dongle is connected to by the standard xbox controller once you’ve hit the “Connect” button on controller. Then the controller’s current state (e.i. which triggers/buttons are pressed and the joystick’s readout) are available from a Python library called inputs. Specifically, from a method called inputs.get_gamepad(), which returns a fairly low level interpretation of the standard controller’s state. We wanted to make a nicer API to use this controller, so we wrapped inputs’ get_gamepad method in a class called XboxController, and this is the xbox_controller.py source file you found in Stretch Body. Here’s what’s XboxController’s API looks like:

class XboxController:

    def start(): # call this method at the start of your program

    def get_state() -> dict: # this method returns the current state of the controller as a Python dictionary

    def stop(): # call this method at the end of your program

In total, there are three methods that you would use to interact with the standard xbox controller. In the case of the adaptive controller, once you have it connected to the on-board computer, you can choose to use the inputs library or write a similar wrapper class for the adaptive controller.

Finally, the script that actually maps controller state to robot commands is called stretch_xbox_controller_teleop.py. This script is automatically started when the robot boots up unless you turn it off. In a loop, this script reads state for the controller, and uses our python library, Stretch Body, to issue commands to the robot’s various joints. The easiest way to reuse the stretch_xbox_controller_teleop.py script is to write your own wrapper class for the adaptive controller and modify this script to use your class in place of XboxController.

Unfortunately, I do not have a Xbox Adaptive Controller so I cannot test this, but based on recent articles, it seems like on-board computer may be able to treat the adaptive controller exactly like the standard controller. If this is the case, you may not need to write any code since the XboxController class will work for the adaptive controller as well.

Let me know if you have any questions!

2 Likes

Hey Binit,

So here is a quick update.

I could “connect” to Stretch using a wire connection (USB) from the adaptive controller to Stretch sometimes…

It appears that most of the time inputs (inputs.get_gamepad()) does not register that the Xbox adaptive controller is connected. This is strange, it’s about a 20/80 split with 20 % being the times it connects successfully.

I know the Xbox adaptive controller isn’t the problem since I “pygames” can detect the controller it seems like it is an input error.

Currently, I am attaching an “Xbox wireless adapter dongle” Controller:Xbox Adaptive Controller - PCGamingWiki PCGW - bugs, fixes, crashes, mods, guides and improvements for every PC game
to get the adaptive controller to work wirelessly.
I was able to get Pygames to detect the adaptive controller using the dongle via Xone (I was told to via this website: Controller:Xbox Adaptive Controller - PCGamingWiki PCGW - bugs, fixes, crashes, mods, guides and improvements for every PC game) but I am still running into the same problem where input does not seem to register the controller.

(still troubleshooting and will post updates if I find anything more)
My next step is to uninstall and reinstall inputs to see if that makes a difference.

Hi @Rafael_MM, thanks for the update. Could you describe a bit more how the performance is with pygame? One workaround you could use is to create the XboxController wrapper class around pygame’s input logic.

For the performance test, I ran a simple pygame script.

http://programarcadegames.com/python_examples/f.php?file=joystick_calls.py

Basically, this test just shows if it detects a device and if it what inputs are being pressed at what time.

It is similar to the Xbox_controller.py code included in “stretch_body” the only difference is that the pygame code detects the adaptive controller always (even when running from the dongle).

Currently, I am working on making a wrapper code using pygames logic.

Glad to hear it @Rafael_MM, that sounds like the right approach since pygame is able to detect the adapative controller 100% of the time.

Here is the link to the updated code working with pygame.

I can confirm this code works with the adaptive controller.

1 Like

Glad to hear it works @Rafael_MM! Thanks for sharing the code!