It’s been a while since I’ve posted here, not because I haven’t been working on projects but more because I’ve spent the better part of a year trying to add some diversity to my hobbies. Last April I decided to finally lose the weight I’d gained since high school so I did a little self-improvement and lost forty pounds while exploring some other interests. Aside from the weight loss I took up running and biking, started learning to play violin, wrote a novel for NaNoWriMo, and am currently experimenting with gardening and cooking. Luckily many of these activities provide opportunities for electronics hacks and designs so I anticipate a few smaller hacks centered around these new hobbies in addition to the large, ongoing projects I have going.
Since I last posted, I’ve redesigned the OpenSCAD to make the robot much smaller. I haven’t documented any of this and I’m pretty sure it’s a dead end, but I’ll try and write up the changes I’ve made and my results soon. I haven’t really done anything else on the PiGRRL Switch. Everything I need is sitting on my desk, I just need to buckle down and put it all together. I plan on starting with the console and verifying it works with a regular USB controller and then finishing the custom controllers.
As a follow-up to my last post, I wanted to go over the Arduino firmware I used for my primary controller test. Essentially what it does is read the states of multiple buttons and print a sequence of 1’s or 0’s (1 for unpressed and 0 for pressed) to the Bluetooth module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
The whole program is embedded above and I’ll go through each section individually and explain what it does below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
These enums serve the purpose of defining human-readable names for the pins that are hooked up to the buttons. That way their usage in the code is immediately obvious upon reading.
Nasty literals!
This isn’t strictly necessary due to the simplicity of this program, but the general best practice is to not use integer literals in code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This is the pin mode setup in the Arduino setup() function. The buttons are hooked up directly to ground so that when the button is pressed, the pin sees 0V. When not pressed the pin is floating so here I’m enabling the Arduino’spullup resistors so that when the button isn’t pressed the pin sees 5V.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Here the state of the buttons is read and printed to the Bluetooth module over UART. Because of the way pullups are handled, ‘1’ corresponds to unpressed and ‘0’ corresponds to pressed. The printing of the button states is preceded by a “K:” to act as a key-value pair. This means that when the controller driver on the Raspberry Pi sees a “K” it knows it’s about to get a list of digital buttons. I’m planning on using this in the future to enable more complex data than just a few buttons. For example, I could use the keys “JX” and “JY” to denote the two axis values of an analog joystick. The last serial print is a Serial.println() function call to print a newline character. This is an easy way to segment the data so the controller driver can differentiate between multiple packets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Lastly is the delay that sets the update rate of the program. With a delay of 10 milliseconds at the end of the loop, new button states will be sent to the driver at an update rate of roughly 100Hz. Right now this rate was arbitrarily decided, but in the future I’ll have to tweak it to balance several factors.
First off is the bandwidth of the Bluetooth connection. As far as I can tell, the HC-05 module uses Bluetooth 2.0 which has a data throughput of of 2.1 Mbit/s or 275251.2 bytes per second. Divide this by the update rate and you get the total amount of data that can be sent in one cycle of the program. This also assumes that the connection is perfect with no lost packets. Bluetooth’s serial port profile uses re-transmission to verify that packets sent are received. This means that wireless noise can cause the connection to slow down while the Bluetooth module is attempting to resend a bad packet. Additionally, if I go back to the BLE module and am able to get it working, BLE only has a throughput of 35389.44 bytes per second, or 13% of Bluetooth 2.0. So while I’d be saving power I’d be restricting the update rate and amount of data I can send from the controller.
Another thing to consider is the update rate of the controller driver running on the Raspberry Pi. Serial ports have buffers to store incoming data. This means that if the driver is updating slower than the controller, the driver won’t be able to process all of the data coming in and the buffer will overflow. This problem will exhibit itself as lag in the controls. The driver needs to be updating at the same rate, or faster, than the controller.
The last consideration is debouncing. This is a topic that has been extensively covered elsewhere, but suffice it to say that if the controller updates too fast it will accidentally send phantom bounces in the switch contacts to the driver and cause erroneous button presses.
Once everything has come together and is working to a degree I’m happy with, I’ll come back to these small issues and tweak everything until it works perfectly. Until then, my next step is writing the controller driver! As always, my code is available on my GitHub in the PiGRRL_Switch repository.