I am trying to make an inverted rotary pendulum that essentially consists of a motor turning a bar with a pendulum on the end. The pendulum and the motor both have an optical encoder on them. I have a basic code using interrupts to pulse two channels on each encoder and using the position and the known time I have the velocity of the pendulum at any given time. Now here is the problem, I am trying to figure out how to make a PID controller for this to balance the bar upright. The Arduino Mega is using serial output to tell a Pololu motor controller what to do, but the code is essentially telling the controller to input max voltage all the time.
So here is my question:
If I have a properly working code that tells me the position (rad) and velocity (rad/s) of a pendulum (I wrote a little serial write code that shows me in the serial monitor what appears to be the correct data) how do I implement a tunable PID controller from this data?
What I have so far:
void loop()
{
u = - (kp*proportional + kd*derivative + ki*integral);
motor_input = (3200 / 12 * u); // because a 12V power supply is used to power the motor at max 3200 rate and u is motor input
motor_input = (int)motor_input;
if (motor_input >= INPUT_MAX){
motor_input = INPUT_MAX;
}
else if (motor_input <= -INPUT_MAX){
motor_input = -INPUT_MAX;
}
Where kp, ki, kd are the gains. But I am not sure what I am supposed to be multiplying them by. What are proportional, derivative, and integral here? 3200 is 100% duty cycle so the value for motor_input should fall below it. I think proportional is supposed to be the angle of the pendulum, derivative is supposed to be the velocity of the pendulum, but I'm not really sure what integral is supposed to be.
Sounds like a very non-linear dynamic system - the angle of the shaft affects the orientation
of the control movements to the pendulum's base. Start by analysing small movements about the
top-dead-centre position.
There are two things to control - first getting the pendulum vertical by rapidly getting its base below its
top, then more slowly correcting for translation of the whole pendulum (which requires controlling
the setpoint of the first loop slightly off vertical to decelerate such drifting motions.
From your sensors you should be able to calculate good estimates of the angle of the
pendulum and of its overall translation (motion of the centre-of-mass of the pendulum).
From the angle of the motor shaft you can calculate the effect of small angle change of the motor
on the horizontal position of the pendulum's base too.
papayadialog:
I think proportional is supposed to be the angle of the pendulum, derivative is supposed to be the velocity of the pendulum, but I'm not really sure what integral is supposed to be.
Not quite on the right track. In auto control systems theory.... the gains kp ki and kd are multiplied (respectively) with the error signal, the integral of the ERROR signal and the derivative of the error signal. The multiplication results are summed together and the composite (summed) signal drives your raw motor system....and the output (typically angle in the case of single output for this kind of case) is used for negative feedback. The 'error' is a function of time..... it's the difference between the input to the 'differencing block' and the feedback signal..... see feedback control system block diagram from a auto control system text book.
The integrator stops the system from getting stuck in a rut.... such as the system getting stuck due to some friction etc. If the error becomes constant.... then the integrator prevents the error from getting stuck as a non-zero constant. If no integrator.... then the output can sometimes get stuck close to the desired output.... but not quite get there. The integrator sorts this out.