Theory

A PID controller takes in information of the error and issues an efficient trajectory for the return to the goal state. The proportional term (P) returns the corrective action associated with the current error of the system. The integral term (I) projects the end state of the corrective action and adjusts the output so the end state matches the goal state. The derivative term (D) accounts for the rate of change in error compared to the expected rate of change. Its equation is given by,

where e(t) is the error from the goal state, and u(t) is the total corrective output. Kp is the gain constant associated with how much the proportional term contributes to the corrective output.

Ki is the integral constant and Kd is the derivative gain constant.

To start, we restrict the model of our system to the pendulum. The pendulum includes two forces that cause torques about its pivot: gravity and the force by the control arm. The tangential component of the acceleration from the control arm is a non-inertial force that, from the perspective of the pendulum, allows for the construction of the pendulum's projected movement.

The sum of the torques on the pendulum is then given by,

where

and

The acceleration of the control arm will come in the form of a change in frequency of steps made by the stepper motor or,

For the angular velocity of the control arm, we know

Since 400 steps of the stepper motor is equivalent to one revolution of the control arm, we have

Taking the derivative of both sides yields,

and we have the relation,

Since our desired state is when the angular acceleration of the pendulum is zero, we can predict the acceleration needed by the control arm to keep the pendulum upright for a given error, θ.

Therefore,

Converted into a change in frequency by,

Small angle approximation allows the removal of the tangent function

The Arduino has a 10-bit resolution, which allows for the the position of the pendulum to be read as an integer value of 0 through 1023.

The digital reading of the error by the Arduino can be converted to an angle by,

So the necessary change in frequency of the control arm for a given amount of error to keep the pendulum from following is

or written in a form more conducive for the PID algorithm,

where deltat is our sampling period and the period at which we update the PID corrective value.

A change in frequency will occur every time the corrective output is evaluated so

Since this value is proportional to the current error error of the pendulum, we have the theoretical proportional term,

where

This value however would only keep control of the pendulum at a given angle from the upright position if the control arm could accelerate endlessly. This is obviously not conducive for a real mechanical system.

The integral and derivative terms are expected to increase the acceleration until the error in the system is zero.

There exists a critical angle the control arm cannot correct for due to the physical limitations of the stepper motor.

The critical angle is a simulated angle achieved by altering the set point by which the pendulum computes its error.

Starting in the upright position, the PID algorithm will attempt to control the pendulum about its new set point. Trials were taken simulating the position designated by the red arrow below.

Our model is not dependent on the length of the pendulum. The critical angle was measured as various lengths of the pendulum.