An inverted pendulum is a classical problem for those who study mechanical engineering and feedback control theory. In this tutorial I will go through the steps of building an inverted pendulum on a cart stabilized with a DC motor. I will describe how to measure motor’s parameters and calculate coefficients for the feedback regulator.

Equations of motionEquations of motion can be obtained by differentiating of a Lagrangian which will give the following system:

To make sure I got the equations right I simulated free pendulum with this script.

In order to keep the pendulum in the upright position an external force should be applied depending on the state of the system. In this project full state control is used meaning that the control (u) equals to

where x is vector of state, K is a vector of coefficients obtained using LQR. I simulated controlled pendulum with this script.

In physical device we can measure motor's position and an angle of the rod using incremental rotary encoder, the velocities are then calculated as a derivative.

Motor controlIn order to stabilize pendulum we need to apply a certain force which we calculated in a previous section. But the control signal coming out of the Arduino is the width of the PWM signal, basically is the voltage applied to the DC motor. In order to calculate the voltage given the force required we need to model the DC motor and estimate it’s parameters.

The DC motor is modeled by the following equation:

We don’t need to know the exact parameters, instead we can simplify the equations with regards to friction as follows:

In order to determine parameters (a, b, c) we can record the relation of cart's velocity to time depending on different voltages. We’ll obtain the following plot:

Parameters then are found by brute force using the following script. Knowing the parameters and the required force we can calculate the exact voltage.

Now we have all the pieces, the last step is to calculate LQR parameters for the real device (using this script) and program the Arduino.

ConclusionBuilding an inverted pendulum may seem like a daunting task, it took me about two years to get from the idea to the working device. Now it comes down to understanding mechanics, manually measuring parameters of the motor and configuring the regulator. Those steps can be done during the class. This device can be used as an educational stand for experiments on different control techniques. I plan to further refine and productize it so it can be assembled easily and installed in a school or technical museum. Any collaboration on that is welcomed.

Thanks for reading!

## Comments