Everything You Wanted to Know About the Reverse Pendulum

The article serves as a cheat sheet for those who want to make their reverse pendulum. Here are the problems due to which I reworked several times, a brief overview of the theory is needed to understand how to stabilize the system.





Why did I need this?



In short: I wanted to expand my CNC machine, but something went wrong ...

Full story
Since childhood, I wanted to have my own CNC machine, because dealt with models of aircraft where you need to make a lot of small repeating details. First I bought a ready-made DIY kit , and then I decided to increase it. I played for two months, but still the machine is small, the working area was only 18 by 10 cm, it does not have positioning sensors. I decided to buy a larger guide, put the limit switches and install the carriage in the middle with a stepper motor. I did it in half a day, but you canโ€™t go straight to the dream - to a large CNC, you need to complicate the task and put the pendulum on the carriage, then it seemed easy to me, but I had to remember the institute years and get acquainted with TAU.





Unsuccessful attempts



The project took almost two years of trial and error, redesigning, waiting for details and incomplete days off so that those who wish to repeat saved their time and nerves, I consider it necessary to talk about unsuccessful decisions.





Free pendulum



To complete the picture, we simulate a pendulum on a free carriage without friction.



The equations of motion can be obtained by differentiating the Lagrangian with respect to generalized coordinates. We obtain the following equations:





 begincasesL cdot ddot theta+g cdotsin( theta)โˆ’ ddotx cdotcos(th)=0(m+M) cdot ddotx+m cdot ddot theta cdotL cdotcos( theta)โˆ’m cdotL dot theta2 cdotsin( theta)=0 endcases







from which you can find how the state vector changes:





 begincases dot theta=w dotw= fracg cdotsin( theta)+b cdotL cdotw2 cdotsin( theta) cdotcos( theta)L cdot(1+b cdotcos2( theta)) dotx=v dotv=b cdot fracL cdotw2 cdotsin( theta)โˆ’g cdotsin( theta) cdotcos( theta)1+b cdotcos2( theta) endcases,b= fracmM+m







and simulate the system. The code is here .





Why is the system unstable?



Common sense and visualization tell us that the pendulum itself will not stand. But how to verify this mathematically?

In general terms, the linearized system and solution are as follows:





 dot mathbfx=A mathbfx, mathbfx(t)=eAt mathbfx(0)







An exponent in the power of the matrix looks clearer if you go to the coordinate system from eigenvectors, then the matrix A will be diagonal ( D ), and the exhibitor will look like:





e ^ {Dt} = \ begin {bmatrix} e ^ {\ lambda_1t} & 0 & \ dots & 0 \\ 0 & e ^ {\ lambda_2t} & \ dots & 0 \\ \ vdots & \ vdots & \ ddots & \ vdots \\ 0 & 0 & \ dots & e ^ {\ lambda_nt} \\ \ end {bmatrix}







Now it is seen, in the presence of eigenvalues โ€‹โ€‹(  lambdai ) with a positive real part, the corresponding component of the state vector will tend to infinity, and the system will fall apart. The above applies to continuous systems, more about sustainability is described in this video lecture.

Check if this is the case for the reverse pendulum. We linearize our system near the equilibrium position for  theta=0,sin( theta) approx theta,cos( theta) approx1,w2 approx0 :





\ begin {bmatrix} \ dot \ theta \\ \ dot \ omega \\ \ dot {x} \\ \ dot {v} \ end {bmatrix} = \ begin {bmatrix} 0 & 1 & 0 & 0 \\ \ frac {g} {L (1 + b)} {\ theta} & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ -g \ frac {b} {1 + b} \ theta & 0 & 0 & 0 \ end {bmatrix} \ begin {bmatrix} \ theta \\ \ omega \\ x \\ v \ end {bmatrix}







Nonzero eigenvalues โ€‹โ€‹have the form  pm sqrt fracgL(1+b) , thus we became convinced of instability.



Add feedback



Now the force will act on the carriage f , one of the equations can be rewritten in the form: (m+M) cdot ddotx+m cdot ddot theta cdotL cdotcos( theta)โˆ’m cdotL dot theta2 cdotsin( theta)=f , and the linearized system will take the form:





\ begin {bmatrix} \ dot \ theta \\ \ dot \ omega \\ \ dot {x} \\ \ dot {v} \ end {bmatrix} = \ begin {bmatrix} 0 & 1 & 0 & 0 \\ \ frac {g} {L (1 + b)} {\ theta} & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ -g \ frac {b} {1 + b} \ theta & 0 & 0 & 0 \ end {bmatrix} \ begin {bmatrix} \ theta \\ \ omega \\ x \\ v \ end {bmatrix} + \ begin {bmatrix} 0 \\ \ frac {1} {L} \ frac {1} {2m + M} \\ 0 \\ \ frac {1} {2m + M} \ end {bmatrix} \ cdot {f}







Now the system (  dot mathbfx=A mathbfx+Bu ) became controllable , this can be verified by checking that the rank of the matrix \ begin {bmatrix} B && AB && A ^ 2B && A ^ 3B \ end {bmatrix} equal to the dimension of the state vector, i.e. 4. To keep the pendulum in an upright position, I used a linear-quadratic state controller, ie control (u or f) is the product of the state vector [ theta, dot theta,x, dotx] by a vector of parameters that are found once by minimizing the quadratic functional . The simulation code is here .





Engine control



Now you need to control the DC motor, it contains many parameters that I do not know, so I took it for a "black box", described by the following equations, taking into account friction:





 begincases dotx=v dotv=โˆ’a cdotv+b cdotU+c cdotsign(v) endcases







You can read about the derivation of equations and parameter estimation here . Below are my graphs of the acceleration of the motor with the carriage depending on the voltage (in reality, the PWM signal is output from the controller) and the fitted curves.



I also found the coefficients of the model by brute force, the code .

Thus, the controller gives us the required acceleration, and from the 2nd equation, knowing all the constants, we find the voltage.



Putting the real device together



Now we have all the knowledge to collect and stabilize the pendulum. I used the following iron:



Thus, we explicitly measure the angle of the pendulum, the position of the carriage, calculate the angular velocity of the pendulum and the speed of the carriage - we get the full state, I found the controller parameters with this script. To my surprise, everything worked pretty quickly as it is. I am satisfied with the result, it stands and even holds a glass!





The code for Arduino is here

What could be improved compared to many of the options that can be found on youtube - this pendulum is quiet because the PWM is tuned outside the auditory range and plastic wheels are used.

Now this task looks like a laboratory work: measure the parameters of the motor and find the coefficients of the regulator, simultaneously understanding what is happening.



What's next?



I plan to produce a pendulum: make a swing, get rid of a coil of wires, make a shield with convenient connectors, so as not to be ashamed to present to some school or museum. If someone wants to join, I will be glad there are many more ambitious ideas.



References







Thanks for attention!



All Articles