# Self balancing robot

Ugly! Accelerometer in wii nunchuck, Gyro in Wii motion plus (right side)

Control system:

My current control system. Edit: I calculate angle from the accelerometer right after the offset and scaling.

I’m using a complementary filter, which is used to combine a gyro (accurate but drifts over time) and an accelerometer (not so accurate but doesn’t drift). Anyway look at the control picture to see exactly how I’m doing the calculations. Everything underlined in blue I want to get rid of/move. Possibly I will want to keep the MA so that I can do a standard deviation. The standard deviation could help the platform tune the complementary filter. A high standard deviation for the accelerometer would tune the complimentary filter constants to .999(gyro)/.001(accel) where as a low standard deviation may set it to .95(gyro)/.05(accel) and make it a continuous tuning function.  I’ve also been brainstorming on methods that would allow robot calculate the balancing angle for the platform. As far as phase shift: I’m not too worried about the accelerometer’s phase shift due to the filters, but the gyro needs the phase shift to be an absolute minimum!

Talking to two I2C devices with the same address. Had to include my new tool - saleae logic analyzer

How I would improve results:

The wii motion plus is a ~20\$ three axis 2000deg/sec gyro with a plastic case, sockets, PCB, and a I2C pass through chip. This really isn’t the right type of gyro for this type of robot, and I seriously question the quality of the gyros on board. I can say the same for the accelerometer on the wii nunchuck too. I’ve also thought of a few ways to calibrate the system and get the gyro/accel units to match up with each other. Regardless the results are pretty good and I may just need to tune everything. Those servos are also slow as beans! My plans are to replace the gyros with less than 500deg/sec gyros, clean up the algorithm and work on perfecting it! I also need to put the IMU on the pivoting point of the platform.

Code:

segway05 – I probably won’t get around to taking another wack at this till summer, so here’s the most recent code. Not many comments other than the blatant mistakes pointed out.

Great resources:

Complementary filter by MIT

Tilt with accelerometer

Calculating angles with accelerometer

Using wii nunchuck and Accelerometer together – Just an FYI if you disable the Wii motion plus to talk to the nunchuck via passthrough port, the wii motion plus takes about 100ms to turn back on which is SLOW. I don’t  have a wiimote/wii, so I can’t listen in on them talking with my logic analyzer. This method is good since both devices have the same I2C address…

Videos:

``` ```

Electrical Engineer who loves to bike!

### 24 Responses to Self balancing robot

1. Gary says:

Nice work! I´m working in a similar project for my Final Project. What microcontroller did you use?

• Moser says:

Ohh just an Arduino using an atmega328. more specifically it’s the BoArduino

• tyde says:

Beau travail! Je travaille à un projet similaire pour mon projet final. Qu’est-ce microcontrôleur avez-vous utilisé?

Bonjour je travaille sur un projet similaire avec Arduino 328, Serait il possible d’avoir votre code par mp pour pouvoir comparer.

Excusez moi pour mon anglais

• Moser says:

Not sure why it wouldn’t let me reply to your comment tyde,

Désolé je n’ai pas vu votre commentaire! J’ai posté le code sur mon poste. J’ai été en utilisant le ATmega328 avec bootloader Arduino. J’ai j’utilisais Arduino 0022

2. aEx155 says:

I’ve done some work on getting these sensors to work for a balancing platform as well. Unfortunately there isn’t much documentation on any projects that use the sensors (besides quadrotor controllers – MultiWii and such) for segway-platforms, so I was glad to see someone else working on it.

I’ve detailed some of my experiences in a few blogposts here: http://makerandomstuff.blogspot.com/2011/12/introduction-fail-way-crappy-balancing.html
http://makerandomstuff.blogspot.com/2011/12/update-fail-way-v01.html
http://makerandomstuff.blogspot.com/2011/12/update-fail-way-gets-new-accelerometer.html

Some other have been able to get readings off of both the Nunchuck and the WM+ together using the pass-through port, which I believe works better than having to turn them off alternately. The code I’ve managed to get together is here, based on a lot of other programs as I stated in my posts:
http://arduino.cc/forum/index.php/topic,84668.0.html

I haven’t been able to get my segway working perfectly either, but maybe this will help.

• Moser says:

Hey thanks I’ll look into those links. I’ll try to get some source code up soon

• Moser says:

I saw you mentioned that you had some sensor drifting issues due to nunchuck mounting: you may want to use a MA filter as it really helped mine. Have you found a way for the platform to detect the best balancing angle? I have been thinking of a few ways to do it, but i’m more going for stability at the moment.

I think our main problem is that the gyro on the wm+ is a 1500deg/sec where as most people use a 100-300 deg/sec gyro for these types of platforms.

“ITG-3200™ gyroscope meets these requirements with its ±2,000°/sec (dps)”

• aEx155 says:

In my most recent post I detail some of changes I made to the sensor setup. More specifically, I picked up a cheap nunchuck clone to take apart so I could mount the board inside directly.

3. ayushsinha says:

Hi! Great stuff. I worked on a balancing bot as well and a Comlementary Filter does a fine job. Looking at your second video, I had similar problems. I used a Kalman Filter and that helped a lot to fuse the noisy accelerometer and drifting gyro. Maybe you could give it a try to remove those oscillations.

http://autopilot.sourceforge.net/kalman.html

4. Scot says:

are you familiar with http://www.multiwii.com/
Alex has built a great website with a huge amount of documentation on the code (http://code.google.com/p/multiwii/source/browse/#svn%2Ftags) used thats all available for anyone to use.
http://www.multiwii.com/forum is a great resource, even if you dont want to use their code in its entirety.

5. DonM says:

Interesting project. Have you looked into ballbots? They might be a better model for your quadrocopter goal.

• Moser says:

A ballbot would probably be more complicated to build than a quadrocopter! A segway type robot is 1 axis, ballbot is 2 axis. If I can get it to balance in 1 axis then 2 axis shouldn’t be a problem assuming that my micro controller can keep up with the computations.

This was just a quick project whipped up from parts which I had laying around already, and to convince myself that I could probably get a quad built when needed.

6. Hi, Moser!
It’s very interesing project with arduino + WMP + NC.
Sonner or later I’ll try to build this balancing bot.
Thanks!

7. batista1987 says:

excusme, why you use this code ? Setpoint = -11.0 + 30*analogRead(A0)/1023.; //remake this
why is not costant?

• Moser says:

That was just to adjust the target angle that the PID was trying to achieve. I wasn’t sure what the angle was, so there was a potentiometer to adjust it. This really isn’t a good way and I believe that most self balancing platforms use encoders on the wheel and use something like this logic: If i’m balanced, but my wheels are moving me forward, then my angle needs to be adjusted – the goal is to maintain my balance while minimalizing wheel movement.

Here’s a great project that used a kalman filter
http://blog.tkjelectronics.dk/2012/03/the-balancing-robot/

8. Oliv' says:

Hi, I’m reading a lot on self balancing bot and found your project amazing, built on “everyday parts”. You said the motor are not adapted, but what kind of motor did you use ?
I’m searching about what I should use, how to choose rpm and torque…
Thanks !

9. Mantaj singh says:

Hello sir I am from India making balansing robot mostely similer is sige as yours but it is not balanced their is no one how can help me please sir can you gave me yours personal contact I want to ask many things from you.. I has only one three axis aclrometer and no garo can it works with other sensors. Ex gp2 or ir