Help with the math for balancing a pendulum

I am working on inverted pendulum balancer based on the Arduino UNO.

Mechanically, the system uses a pendulum pivot point that is on a cart which itself is on a rail. There is a single 12V DC motor with gear reduction which moves the cart horizontally along the rail to keep the pendulum in balance. The motor is driven by PWM signal into an L298N motor driver.

There are 2 optical encoders. One encoder is used to determine the pendulum angle and the pendulum angular acceleration. The other encoder is used to determine the cart distance from the rail center and the speed of the cart..

So here is the data I currently have:
p_len = 0.57 meters, the pendulum length
p_radians = the pendulum angle in radians, 0 = straight up (balanced) and 3.1459 = straight down
p_accel = angular acceleration of the pendulum in radians/sec
dist = distance in meters that the cart is from the center of the rail
speed = speed of the cart in meters/sec

Now I need help figuring out how to combine this data so it forms the input to the PID library.
I could do something as simple as
input = p_radians + p_accel + dist + speed, but I’m not certain that is the right way to do that and then there is the problem of the input sign (+/-). What determines the sign of the input? Is it a natural flow from the math or is it dependent on one or more of the other variables?
I also don’t know what to do with the length of the pendulum and how it fits in the formula.

I admit I have somewhat limited math skills and it was a real challenge to get this far.

Can someone help me with this stuff?

is the goal to move the cart to the center of the track or make the pendulum angle some specific value? don't see a need for both

if either is the goal, the PID algorithm will need a value for the distance from a target, the angle error or track distance and the rate of change of that distance, the rate of change of the angle or the effectively the speed of the cart.

i don't believe there is a need for an integral term

The goal is to keep the pendulum in balance while keeping it near the center so it doesn't drift over to one end or the other of the rail. In this case, with this setup, the setpoint would be 0 degrees (straight up). Hope that helps.

Okay, when I figure out how to calculate the input, I will use a PD controler.

BTW: When the cart moves to the right the encoder count increases and vice versa. The pendulum angle increases when it rotates on it's pivot CW.

What data does the PID library need?
You need to read about pendulum law.
(I think you need the pendulum speed to be zero)

Speed is:

speed=sqrt(2gl*(cos(finalAngle)-cos(startAngle))

where g is gravity, l is pendulum length, .

Thanks robertoxyz20.

I have been reading all I can about balancing an inverted pendulum. and I have this book on the way as well. So far the math escapes me.

The formula you offer looks appropriate for determining the angular acceleration of the pendulum.

The PID controller library uses input, output and setpoint where setpoint is the desired angle (0 in this case), input is the system feedback which us the results of the position calculations, and output is the what drives the cart motor control. The PID library uses Proportional, Integral, and Derivative variables to adjust the system to keep the pendulum in balance.

PickyBiker:
The goal is to keep the pendulum in balance while keeping it near the center so it doesn't drift over to one end or the other of the rail.

Then you need two nested control loops, a fast one to balance it, and a slow one to control the balance point very slightly and thus allow it to drift along the track to the centre without losing balance.

MarkT, okay, I should be able to figure out how to cascade a slow PID into a Fast one. Thank you.

robertoxyz20 in your formula is the angle in degrees, radians, or grads?