Bluetooth on The Grid
The Grid. It's a place I've been fascinated with ever since I was a kid. I was a bit bummed out when I grew up enough to understand how computers actually worked and that programs weren't really tiny people in light-up suits. Regardless, the world of Tron is still one of the coolest fictional places I've ever seen portrayed in film. A while back when I started researching Virtual Reality I realized I might be able to capture at least a small portion of that world so that I could explore it.
The lightcycle racing scenes have always been one of my favorite parts of the Tron universe and I decided to create a small VR simulation that would let me ride one around. I used Unity to create a simple level and grabbed a free lightcycle model from the web. After that I managed to find a really good motorcycle controller script on the Unity Asset Store that I could apply to the model so it would have the feel of a motorcycle.
A more realistic controller
At first I drove it around using my Samsung Gear with a pretty basic Android bluetooth gamepad but decided I wanted to take the realism up a few notches. When I was shopping at an antique mall for some old hardware for use in art projects, I came across an old Yamaha Gemini Motorsport Playstation 2 controller that I realized I could repurpose.
I tried plugging the controller into a PS2 to USB adapter I had, but it wasn't working beyond lighting up. I had to open it up and check all of the connections before I discovered that the controller cable being wrapped around it for so many years was to blame. After some straightening and wiggling, I got it working with my laptop and had my first experience with driving the lightcycle with the controller. This was going to be a lot of fun.
Getting the motorcycle controller to work with the Gear VR would be another matter though because wired controllers weren’t an option. I'd need to figure out how to make a custom bluetooth controller. I knew that Arduino Micro (as well as Arduino Leonardo) boards understood a few things about acting as Human Interface Devices (HID) like mice, keyboards, and gamepads (ding-ding) and might be the right solution. After experimenting with a PS2 controller to Arduino library to no avail, it occurred to me that those boards only do HID because they have USB built into the processor... which does not help with Bluetooth in the slightest.
Thus began the long familiar journey of failure, discovery, and learning. Two bluetooth boards later I learned that not all bluetooth communication devices can do HID and that you need one with HID firmware on it(the HC-05 everyone uses with Arduinos doesn't, the RN-42 does)... Then, once you have that firmware, none of the Arduino libraries know how to create the correct communication packets which are called HID reports. SO, after learning more about low-level bluetooth modem communication than I'd ever thought possible, I was able to give the board a name that made sense, send out the correct button press event hex codes, and have them control my laptop over bluetooth! Great, all that was left was to put it on my Galaxy S7 for the Gear and it would work! Right? No... no it's never that easy. Unity had NO idea what the controller was telling it even after other gamepad testing apps clearly showed the controller as being connected and functional.
A little help from some friends
After much trial, error, and frustration I caved and asked the brilliant folks behind Unity's Rewired input plugin for some help, and they pointed me at just how to see what Unity was seeing rather than blindly assigning buttons until I was full of rageface. Turns out my phone was seeing different buttons from my Mac. A few minutes and a few platform specific mappings later, BOOM, custom Bluetooth motorcycle interfacing with my Gear VR app. I was on The Grid at last.
The feeling of driving that lightcycle the first time with the new controller was pretty amazing, and it only makes me think of how much farther I could take it. In a perfect world I'd love to make a full rideable motorcycle rig, complete with 4-directional leaning, rotation, and vibration. So consider this an on-going project, because I'm definitely not putting it on the shelf just yet!
comments powered by Disqus