# Connecting Stretch to xbox adaptive controller

**URL:** <https://forum.hello-robot.com/t/connecting-stretch-to-xbox-adaptive-controller/531>\
**Category:** Ask\
**Created:** [November 11, 2022, 1:08am UTC](https://forum.hello-robot.com/t/connecting-stretch-to-xbox-adaptive-controller/531 "2022-11-11T01:08:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafael\_MM](https://avatars.discourse-cdn.com/v4/letter/r/a698b9/32.png) [@Rafael\_MM](https://forum.hello-robot.com/u/Rafael_MM)\
**Post date:** [November 11, 2022, 1:08am UTC](https://forum.hello-robot.com/t/connecting-stretch-to-xbox-adaptive-controller/531/1 "2022-11-11T01:08:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bshah](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.hello-robot.com/bshah/32/55_2.png) [@bshah](https://forum.hello-robot.com/u/bshah)\
**Post date:** [November 11, 2022, 7:23am UTC](https://forum.hello-robot.com/t/connecting-stretch-to-xbox-adaptive-controller/531/2 "2022-11-11T07:23:07Z")

</div>

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](https://docs.hello-robot.com/0.2/stretch-hardware-guides/docs/hardware_guide_re2/#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](https://docs.hello-robot.com/0.2/stretch-tutorials/getting_started/quick_start_guide_re2/#hello-world-demo) 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](https://pypi.org/project/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](https://github.com/hello-robot/stretch_body/blob/master/body/stretch_body/xbox_controller.py) source file you found in Stretch Body. Here’s what’s `XboxController`’s API looks like:

```auto
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](https://github.com/hello-robot/stretch_body/blob/master/tools/bin/stretch_xbox_controller_teleop.py). This script is automatically started when the robot boots up unless you [turn it off](https://docs.hello-robot.com/0.2/stretch-tutorials/getting_started/quick_start_guide_re2/#start-coding). In a [loop](https://github.com/hello-robot/stretch_body/blob/master/tools/bin/stretch_xbox_controller_teleop.py#L456-L468), this script reads state for the controller, and uses our python library, [Stretch Body](https://docs.hello-robot.com/0.2/stretch-tutorials/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](https://www.pcgamer.com/linux-microsoft-adaptive-controller-support-drivers/), 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!

---

<div class="post-metadata">

**Author:** ![Rafael\_MM](https://avatars.discourse-cdn.com/v4/letter/r/a698b9/32.png) [@Rafael\_MM](https://forum.hello-robot.com/u/Rafael_MM)\
**Post date:** [November 18, 2022, 1:05am UTC](https://forum.hello-robot.com/t/connecting-stretch-to-xbox-adaptive-controller/531/3 "2022-11-18T01:05:59Z")

</div>

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](https://www.pcgamingwiki.com/wiki/Controller:Xbox_Adaptive_Controller)  
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](https://www.pcgamingwiki.com/wiki/Controller:Xbox_Adaptive_Controller)) 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.

---

<div class="post-metadata">

**Author:** ![bshah](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.hello-robot.com/bshah/32/55_2.png) [@bshah](https://forum.hello-robot.com/u/bshah)\
**Post date:** [November 18, 2022, 5:11pm UTC](https://forum.hello-robot.com/t/connecting-stretch-to-xbox-adaptive-controller/531/4 "2022-11-18T17:11:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rafael\_MM](https://avatars.discourse-cdn.com/v4/letter/r/a698b9/32.png) [@Rafael\_MM](https://forum.hello-robot.com/u/Rafael_MM)\
**Post date:** [November 21, 2022, 2:20am UTC](https://forum.hello-robot.com/t/connecting-stretch-to-xbox-adaptive-controller/531/5 "2022-11-21T02:20:58Z")

</div>

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

[http://programarcadegames.com/python\_examples/f.php?file=joystick\_calls.py](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.

---

<div class="post-metadata">

**Author:** ![bshah](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.hello-robot.com/bshah/32/55_2.png) [@bshah](https://forum.hello-robot.com/u/bshah)\
**Post date:** [November 21, 2022, 9:42pm UTC](https://forum.hello-robot.com/t/connecting-stretch-to-xbox-adaptive-controller/531/6 "2022-11-21T21:42:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rafael\_MM](https://avatars.discourse-cdn.com/v4/letter/r/a698b9/32.png) [@Rafael\_MM](https://forum.hello-robot.com/u/Rafael_MM)\
**Post date:** [November 25, 2022, 11:13pm UTC](https://forum.hello-robot.com/t/connecting-stretch-to-xbox-adaptive-controller/531/7 "2022-11-25T23:13:16Z")

</div>

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

I can confirm this code works with the adaptive controller.

> [@Running Stretch RE2 with adaptive controller](https://forum.hello-robot.com/t/running-stretch-re2-with-adaptive-controller/539):
>
> Hey everyone, My name is Rafael Morales I am a Robotics master’s student at Oregon State University. I focus on HRI (human-robot interaction) and I am currently working with children who have a type of motor impairment. We are trying to use Stretch to see if they can benefit from using the robot (increasing their ADL and independence). We are currently trying to implement an Xbox adaptive controller in order to facilitate the use of Stretch for the children, but since the original code used to…

---

<div class="post-metadata">

**Author:** ![bshah](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.hello-robot.com/bshah/32/55_2.png) [@bshah](https://forum.hello-robot.com/u/bshah)\
**Post date:** [November 28, 2022, 4:55pm UTC](https://forum.hello-robot.com/t/connecting-stretch-to-xbox-adaptive-controller/531/8 "2022-11-28T16:55:54Z")

</div>

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