very urgent help needed regarding algorithm for self balancing of two wheeled robot

I am new to this forum and I need urgent help regarding algorithm for self balancing of two wheeled robot.
I am constructing a simple two wheeled robot which will balance on its own
I am using accelerometer for tilt angle calculation.accelerometer will give tilt anagle
Can any body tell me algorithm for balancing this robot with this tilt information.
I am using two dc motors and to control them i am applying PWM wave.

Thanking you in advance.

With regards,
S Param

Wow. That is a very large request. And, not so easily answered.

My recommendation is for you to look at this page of links, and use Google to search the web. That will get you well on your way.

Good Luck.


Ok, a quick glance of this makes me think that you need more sensors. Below is a link talking about the segway. It says that there are 5 oscilloscopes in it. I’m no expert, but I think if they could have made it with less, they would have. Just curious, what do you need this for? It sounds kind of cool.

A last second thought. How are you planning on balancing it when it is off center? Moving a weight or changing wheel speed? I would think that the first could be done a bit easier then the second. Though that really depends on the application.

I heard that we can balance only with one accelerometer.I am going to balance with controlling the speed of motors.
if you know any algorithm for this let me know

From what I know about dynamics here is a picture of what is happening.


…<---------- …__.|…O…|_____
… --…–

Ok, so basically. The C is the center of mass, O is the center of the axle, and U is the distance between C and O. There is a moment created by gravity anytime that C isn’t directly over O. The accelerometer provides you with an angle that can be used with geometry to find the displacement in the horizontal plane. The formula for the moment created is massgravitydisplacement. Now, you have the amount of torque that needs to be corrected against. The way you plan on fixing this is by adding another moment in the opposite direction created by the movement of the axle. The only way this will work well is if the axle supports the frame with bearings so that the actual turning does not create a torque about the axle. All you want is a push along the arrow marked above. Now, the way to calculate this moment is to use the angle found by the accelerometer and geometry to find the vertical displacement between the axle and the C. This moment is going to be displacementpushing force. The pushing force is going to be massgravitymupidiameter of wheelrpm. The rpm may take a sensor of sorts.(not sure on that part) So, in essence:

massgravitymupiUsin(90-N)diameter of wheelrpm=massgravityUcos(90-N)
I go ahead and cancel the mass, gravity and U. This leaves:

sin(90-N)diameter of wheelrpm=cos(90-N)

So, rpm=cot(90-N)/diameter of wheel.

**OK, now for the disclaimer: **All the math posted above was done at 3 in the morning while watching tv and being half asleep. I am not by any means confident in all of the math I did. I think that it should give you a rough estimate of what you are looking for. Anyone that can confirm or improve my work would be greatly appreciated.