Is there a good, popular and reliable algorithm I can use by taking input from a gyroscope and using this to control two independant wheels to keep such a balanced robot reliably upright? I'm looking for an algorithm that will let me use it to drive a robot around as well as keep it upright when stationary. The ability to deal with inclines and people nudging it would also be a bonus, but not essential.

  • $\begingroup$ I am going to be working on this as well. You have to use a kalman filter to filter your raw data. And use a PID algorithm to tune it. $\endgroup$
    – Huzo
    Commented Feb 9, 2017 at 15:38

3 Answers 3


The simplest controller is a linear state feedback controller. There are essentially 4 different states that you need a gain for. These are tilt angle, tilt rate, speed and position.

LQR (linear quadratic regulator) is a method to design these gains (after obtaining a linearized state-space representation of your system). If you do not have a state space representation (you probably don't), you can get equations of motion and measure the parameters. If you do not have a state space representation, you should just tune the gains manually (without LQR or other methods such as pole placement).

Tuning gains manually:

Assuming tilt angle, position/speed and wheel torques are all directed forwards (if positive), you want a positive gain on your tilt angle and tilt rate, as well as a positive gain on position and speed.

Start with a gain on tilt angle, and tilt rate. This will get it to balance initially. Once it remains balanced, you can control position and speed by adding a gain to them. If it is unstable, increase the gain on tilt rate (which helps to damp the system).

The position/speed control will control both states to zero. To control to some other value, you just need a reference tracking controller, by replacing the states with their errors before feeding it into your controller (eg. current speed - speed reference).

Yaw control can be done independently (with the differences in wheel torques added to the main balance/speed/position controller).


I believe the most popular solution to this problem is an LQR controller. The problem you are trying to solve is the inverted pendulum problem. Using those keywords, you should be able to Google someone's open-source code. The next problem will be mapping most of the relevant physics quantities to your application (weight, motor torque, etc...)


Since I don't know your skills in control engineering/theory, I recommend you to start with a PID controller. It is a simple controller and you will find many code implementations of it. The drawback of the PID is that you probably will end up spending some time tuning the parameters by hand.

Some years ago I used it to control a two wheeled Lego Mindstorm robot based on its odometry and it worked well enough. To make it move around, you will need to play with the set points.

Of course, later on you can improve the the quality of your controller (stability, robustness etc.) by using more advanced control laws like the LQR mentioned before.

Good luck!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.