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.
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:
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.
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.
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.