Im working on a dc-dc buck boost converter, with 2 different switching elements for bidirectional operation:
#include <PID_v1.h>
#include <math.h>
float pwPin1 = 5;
float pwPin2 = 6;
float analogPin1 = 1;
float analogPin2 = 2;
float analogPin3 = 3;
float value1 = 0;
float value2 = 0;
float boostDuty = 0;
float buckDuty = 0;
int v1 = 0;
double Input1;
double Output1;
double Setpoint1;
double Input2;
double Output2;
double Setpoint2;
double aggKp1 = 1, aggKi1 = 0.5, aggKd1 = 1; // settiing aggressive values of Kp Ki Kd for PID1
double consKp1 = 0.1, consKi1 = 0.05, consKd1 = 0.25; // settiing aggressive values of Kp Ki Kd for PID1
double aggKp2 = 1, aggKi2 = 0.5, aggKd2 = 1; // settiing aggressive values of Kp Ki Kd for PID2
double consKp2 = 0.1, consKi2 = 0.05, consKd2 = 0.25; // settiing aggressive values of Kp Ki Kd for PID2
PID myPID1(&Input1, &Output1, &Setpoint1, consKp1, consKi1, consKd1, DIRECT); //PID1
PID myPID2(&Input2, &Output2, &Setpoint2, consKp2, consKi2, consKd2, DIRECT); //PID2
void setup()
{
pinMode(pwPin1, OUTPUT); //set PWM 5 to output
pinMode(pwPin2, OUTPUT); //set PWM 6 to output
Input1 = analogRead(analogPin1); //take feedback of output in boost mode to PID1
Input2 = analogRead(analogPin2); //take feedback of output in boost mode to PID2
Setpoint1 = 777; // set point for PID1
Setpoint2 = 491; // set point for PID2
myPID1.SetMode(AUTOMATIC); // turn ON PID1
myPID2.SetMode(AUTOMATIC); //turn on PID2
TCCR0B = TCCR0B & B11111000 | B00000001; // set freq. of PWM to 62.5kHz
}
void loop()
{
TCCR0B = TCCR0B & B11111000 | B00000001;
Input1 = analogRead(analogPin1);
Input2 = analogRead(analogPin2);
v1 = analogRead(analogPin3);
double gap1 = abs(Setpoint1 - Input1); //distance away from setpoint
double gap2 = abs(Setpoint2 - Input2); //distance away from setpoint
if (gap1 < 10)
{
myPID1.SetTunings(consKp1, consKi1, consKd1);
}
else
{
myPID1.SetTunings(aggKp1, aggKi1, aggKd1);
}
if (gap2 < 10)
{
myPID2.SetTunings(consKp2, consKi2, consKd2);
}
else
{
myPID2.SetTunings(aggKp2, aggKi2, aggKd2);
}
if (v1 <= 511 && v1 > 306)
{
do // boost converter fn.
{
Input1 = analogRead(analogPin1); //read A2
boostDuty = 1 - (Input1 / 778); //calculate boost duty ratio
myPID1.Compute();
analogWrite(pwPin2, (boostDuty * 255 * Output1)); //write to D6
}
while (v1 > 306);
}
else
{
do //buck converter fn.
{
Input2 = analogRead(analogPin2);
value1 = Input2 / 4.01;
buckDuty = value1 / 202;
myPID2.Compute();
analogWrite(pwPin1, buckDuty * Output2);
}
while (v1 < 511);
v1 = analogRead(analogPin3);
if (v1 = 0)
analogWrite(pwPin1, 0);
}
}
As per value of v1 the buck mode or boost mode gets selected.Im facing the following problems:
-
The buck mode does not seem to generate a PWM??
-
Ive used 2 seperate PID fns. ...can someone please check wether ive applied correctly? especially the output statement?
-
I based the duty ration of buck & boost fn on the basic voltage eqn. for buck and boost converter...is that fine...and ive multipled it with output1 & output 2 of PID??
-
The processor is heating up??why?
-
Is the if ..else statement used for selecting boost and buck function correct???
Ive attached the power circuit diagram for your reference..
Please help have a project submission.....
Thanks & regards
Emmanuel
