Developed by Kristian Lauszus, TKJ Electronics 2012
The code is released under the GNU General Public License.
This project is deprecated
This project is no longer maintained as I will instead focus on a Balancing robot kit, the Balanduino. For more information see the new repository: https://github.com/TKJElectronics/Balanduino and the Kickstarter campaign: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit.
This is the code for my balancing robot/segway. It's a ported version of the code for the mbed board which I original used. The original code can be found at the following link: https://github.com/TKJElectronics/BalancingRobot.
The code will work for all boards that features an ATmega328p (Duemilanove, Uno, Pro, Pro Mini etc.) - it's not directly pin compatible with the larger Arduinos (Mega, Mega 2560 etc.) as I use the port registers to save processing resources - see http://www.arduino.cc/en/Reference/PortManipulation. But just take a look at the pinMapping pages for comparison: http://arduino.cc/en/Hacking/PinMapping168 and http://arduino.cc/en/Hacking/PinMapping2560 and figure the pins out yourself - for instance OC1A and OC1B are not located on pin 9 and 10, but at pin 11 and 12 on the Arduino Mega.
I have used the registers to set up 20kHz PWM, Phase and Frequency Correct on pin 9 (OC1A) & pin 10 (OC1B) with ICR1 as TOP using Timer1 - see the datasheet page 128-135.
I use a 6DOF IMU from Sparkfun: http://www.sparkfun.com/products/10010, though I only use one of the gyro axis, but any IMU can be used. For instance the very popular MPU-6050, see this example code.
For more info about calculating the pitch see my post at the Arduino forum: http://arduino.cc/forum/index.php/topic,58048.0.html.
For more information about the Kalman filter see my blog post: http://blog.tkjelectronics.dk/2012/09/a-practical-approach-to-kalman-filter-and-how-to-implement-it/ and the source code: https://github.com/TKJElectronics/KalmanFilter.
To steer the robot, I use a USB Host Shield together with my SPP Bluetooth Library for Arduino: https://github.com/felis/USB_Host_Shield_2.0/blob/master/SPP.cpp. More information can be found at the blog psot: http://blog.tkjelectronics.dk/2012/07/rfcommspp-library-for-arduino/.
You can either use an Android app I wrote: https://github.com/TKJElectronics/BalanduinoAndroidApp or the Processing Application to control the robot.
For information about the hardware, see the wiki: https://github.com/TKJElectronics/BalancingRobot/wiki/Hardware.
Also check out the youtube video of it in action: http://www.youtube.com/watch?v=N28C_JqVhGU - this is actually the mbed version, but they behave the same way.
For more information see my blog post at http://blog.tkjelectronics.dk/2012/03/the-balancing-robot/ or send me an email at kristianl@tkjelectronics.dk.