Week 10 - Networking and Communication

Connecting my Xbox Wireless Controller XIAOESP32-C3

This week, I want to figure out how to connect my Xbox controller to ESP32 and take the input of the joysticks. This will be used to control the motors later.

From initial research, the range of esp32 bluetooth is 5-8 meters (maybe 10 in open field) ~ 30 feet. And the range of an xbox controller is also about 30 feet. This is a pretty nice distance for me to work with.

Bluepad32

Luckily, I found a GitLab repository just for what I want to do. Bluepad32 is "a Bluetooth gamepad "host" for the ESP32 / ESP32-S3 / ESP32-C3" with support for a lot of controllers, including my specific bluetooth xbox controller! Using Arduino IDE, I follow the guides in the tutortial video for my specific controller and ESP32-C3. I'm using Xbox Wireless (model 1914, 3 buttons) and the XIAOESP32-C3.

Like how I added the ESP32 package in Week 2, I added the Bluepad32 package: https://raw.githubusercontent.com/ricardoquesada/esp32-arduino-lib-builder/master/bluepad32_files/package_esp32_bluepad32_index.json. I select the board using the Bluepad23 dropdown and I opened the example Controller.ino from Bluepad23. I uploaded it to my ESP using a USB-C cable andddd that's it. The video shows their controller working but mine doesn't.

I spend some time trying to debug while I am waiting at Jake's OH. Maybe my firmware on the controller needs to be updated?

The firmware cannot update via Mac so I found a PC and installed Xbox accessories and updated it. Still didn't seem to fix anything. I played with a lot of different settings and it still did not seem to fix.

It's finally my turn at the office hour but Jake says I shouldn’t mess with the 2 complex systems (xbox controller and code) if I don’t know what’s going on, if I can’t debug it myself... no help was received but it was definitely a catalyst for a few events to come.

So... I will do something simpler, buy my own sliders and joysticks and do it from scratch

But is it really that much simpler? Slider potentiometer and joysticks are not listed in the class inventory and I do not have time to order them. I also realize that my main goal is the ornithopter and building a controller from scratch could be a final project on its whole. So I neglect his comment and go back to finishing my controller. OH was not helpful so I seek the help from the creator of the repository (which I should have done earlier).

I join the repository discord and from previous support threads, I locate a potential bug. In my case, the controller is definitely connected to the ESP32 (xbox lights indicate so and when I unplug the USBC, the lights start blinking, meaning not connected), but the only line that prints in the serial monitor is BTstack up and running at 34:85:18:7A:39:1D So it seems like setup() function is not getting called. I ask the discord for help.

And help was received!!!

Got a first reply in less than an hour and after a few messages back and forth. We found that the solution was super simple... I had the wrong board selected. I had to select the XIAO_ESP32C3 not ESP32C3 Dev Module... *facepalm* rookie mistake. Now it works perfectly. And it was that simple.

Getting inputs from my Xbox controller

Week 10 Final Thoughts

The process was a lot simpler than I imagined and I did not have to write much code or change much of the program that I found online. Thus, I did not learn much about it from just connectiing them. In the future though, when I am integrating my flight stablization and controller, I will be able to edit the code and hopefully learn more about how the code works.

Update: during my system integration, I was able to read through all of the code to understand it and integrate it into my flight control. Embedded below is the final version of just the xbox controller bluetooth input. I commented out the lines of code that are excessive for connecting to other devices. The serial monitor intakes the values of the left and right joystick and these values will be what I use to control the drone. The left joystick will control the elevation by controlling PWM (pulse width modulation) approximately evenly on the ESCs to make the motors output more thurst upwards. The IMU and PID (proportional integral derivative) code will help adjust the PWMs to balance the drone as it ascends or descends. Pushing the joystick up will make the drone go up and pushing the joystick down will make the drone go down. Then, the right joystick will control how to drone moves forwards/backwards/right/left. This is directly related to the roll and pitch of the drone.