Hi,
I'm using a PD controller to control the closed loop design for my DC/DC converter. I am converting 20V down to 2V and have the 2V output be the input to the Arduino. I want to use the output to control the PWM duty cycle so that 2V is always the output no matter what the input voltage is.
The problem that I am experiencing is that the PWM signal isn't being changed. I know that Error should be around 0 but it doesn't want to change. I am not very experience with PD controllers and I want to avoid the PID Library option. My code:
int Vo, Input, Err=0, ErrOld=0, VRef=102, PWM=25, PWMRef=33;
double Kp=.03, Kd=.06;
float P, D;
void setup() {
pinMode(9,OUTPUT);
pinMode(A5, INPUT);
TCCR2B = TCCR2B & B11111000 | B00000001;
Serial.begin(9600);
analogWrite(9,PWMRef);
}
void loop() {
Input=analogRead(A5);
Serial.print("Input=");
Serial.print(Input);
Vo=map(Input,0,1023,0,255);
Serial.print(" Vo=");
Serial.print(Vo);
Err=VRef-Vo;
P=Kp*Err;
Serial.print(" P=");
Serial.print(P);
D=Kd*(Err-ErrOld);
Serial.print(" D=");
Serial.print(D);
PWM=int(PWMRef+double(P+D));
ErrOld=Err;
if(PWM>255)
PWM=255;
if(PWM<1)
PWM=1;
analogWrite(9,PWM);
delay(100);
Serial.print("Error= ");
Serial.print(Err);
Serial.print(" PWM= ");
Serial.println(PWM);
}
An example output that I am receiving is:
Input=218 Vo=54 P=1.44 D=2.88 Error= 48 PWM= 37
Input=531 Vo=132 P=-0.90 D=-4.68 Error= -30 PWM= 27
Input=419 Vo=104 P=-0.06 D=1.68 Error= -2 PWM= 34
Input=495 Vo=123 P=-0.63 D=-1.14 Error= -21 PWM= 31
Input=461 Vo=114 P=-0.36 D=0.54 Error= -12 PWM= 33
Input=484 Vo=120 P=-0.54 D=-0.36 Error= -18 PWM= 32
Input=473 Vo=117 P=-0.45 D=0.18 Error= -15 PWM= 32
Input=472 Vo=117 P=-0.45 D=0.00 Error= -15 PWM= 32
Input=473 Vo=117 P=-0.45 D=0.00 Error= -15 PWM= 32
Input=473 Vo=117 P=-0.45 D=0.00 Error= -15 PWM= 32
Input=472 Vo=117 P=-0.45 D=0.00 Error= -15 PWM= 32
Input=472 Vo=117 P=-0.45 D=0.00 Error= -15 PWM= 32
Input=472 Vo=117 P=-0.45 D=0.00 Error= -15 PWM= 32