I recently completed a final project for an undergrad circuits class, throughout which this forum was a huge help, and so I just wanted to try and give back a little by presenting our code here. The project was, as you may have guessed, a two-wheeled self-balancing robot via a PID controller:
We used an MSP430G2553 with the EXP430G2 launchpad, although the code should of course be applicable to any similar MCU. We also used the apparently popular MPU6050 IMU to produce angle estimates, and a simple H-bridge using TIP-102 transistors for motor control. The schematic that I've attached is a complete reproduction of our setup (including our pin configuration), and can also be found on the github linked to at the bottom of this post.
The files that describe the primary control sequences are main.c, which of course has the setup and main loop, as well as the PID controller code; and StateEstimate.c, which describes how the raw IMU data is combined into a more accurate estimate of the robot's current angle with the ground (we used the so-called "complementary filter" with reasonable success, which just sums a low-passed accelerometer reading with a high-passed gyroscope reading). Furthermore, the parameters for the PID controller are located in Config.h (the parameters currently in the repo are just toy values), and the parameters for the state estimator are located in StateEstimate.h.
The two biggest frustrations we faced when developing the project was successfully communicating with the MPU6050 over I2C (included in the github is the i2c device library, separately available at http://www.i2cdevlib.com),and getting useful information sent over a UART connection. So, I hope that by releasing this code we can make those tasks at least a little bit smoother for people tackling the same problems. The code is set up, by default, to use pins 1.1 and 1.2 for UART communication over the hardware serial interface. The MSP430 will send the angle estimate and PID control signal over UART, which can be captured and saved as a CSV, or even displayed in an OpenGL gui, using the supplied serialReader.py python utility (instructions are in the repository README). Using the supplied C code as a template, sending and receiving arbitrary data for post-processing should hopefully be trivial. UART is configured in main.c, and the functions for sending data over the connection are located in hwuart.c.
Link to github: https://github.com/austensatterlee/robotbuddy
Anyway, I hope this is helpful or at least interesting to some of you. I'd be happy to answer any questions about how we got this working, or receive any criticisms from more knowledgeable members!