lhr-solar / MPPT

Max Powerpoint Tracker Code
MIT License
2 stars 2 forks source link

Add the dV/dP FeedbackControl MPPT class #21

Closed dimembermatt closed 4 years ago

dimembermatt commented 4 years ago

The code for the dV/dP Feedback Control MPPT algorithm needs to be ported over from Python into the mppt rewrite code. The base for the code can be taken from MPPTV3/mppt/IC.h. The file should be named FC.h, and the class named FC. The code to port is in /home/rasbox/UTSVTBox/MPPT/sim/src/mppt_algorithms/mppt_dP_dV_feedback_control.py. The relevant sections are presented below:

error = .05
p_in = v_in * i_in
dP = p_in - self.p_old
dV = v_in - self.v_old

dV_ref = self.calc_perturb_amt(self.v_ref, v_in, i_in, t_in)
if dV == 0: # we reached the mppt in the last iteration, but we need to keep moving in case mppt changes
    self.v_ref += .005
elif abs(dP/dV) < error: # we reached the mppt in this iteration, don't move
    pass
else:
if dP/dV > 0:
    self.v_ref += dV_ref
else:
    self.v_ref -= dV_ref

# update values
self.v_old = v_in
self.i_old = i_in
self.p_old = p_in

We also need to add the class to the main.cpp. Follow how it is done for the PandO and IC mppt objects. First, declare the import in the includes section:

#include <chrono>
#include "mbed.h"
#include "sensor/currentSensor.h"
#include "sensor/voltageSensor.h"
#include "mppt/mppt.h"
#include "mppt/PandO.h"
#include "mppt/IC.h"
#include "CAN/CAN.h"
#include "dcdcconverter/DcDcConverter.h"

Then we declare it the globals:

// initialize voltage and current sensors
VoltageSensor sensorArrayVoltage(PA_0);
VoltageSensor sensorBattVoltage(PA_0);
CurrentSensor sensorArrayCurrent(PA_0);
CurrentSensor sensorBattCurrent(PA_0);
// initialize MPPT
PandO pando(PA_0);
IC ic(PA_0);
Mppt* mppt;
// initialize DC-DC converter
Dcdcconverter converter(PA_0);

Then we assign it in the CAN command parsing:

} else if (strcmp(msg, "MPPT_IC") == 0) {
    // shut down current algorithm
    mppt->disable_tracking();
    // swap reference
    mppt = &ic;
    // start up new algorithm
    mppt->enable_tracking(MPPT_INT_PERIOD);
} else if (strcmp(msg, "MPPT_FC") == 0) {
    // shut down current algorithm
    mppt->disable_tracking();
    // TODO: swap reference
    // start up new algorithm
   mppt->enable_tracking(MPPT_INT_PERIOD);
}