Skip to content

Instantly share code, notes, and snippets.

@Jamesscn
Created November 26, 2019 08:14
Show Gist options
  • Save Jamesscn/ca3e0ae6675bc0da5e6e5d4440e65c31 to your computer and use it in GitHub Desktop.
Save Jamesscn/ca3e0ae6675bc0da5e6e5d4440e65c31 to your computer and use it in GitHub Desktop.
This is the base code of a MPPT that will be implemented in a rural community in order to maximise the power output of their solar panels.
//This code will look to maximise the IV curve output of the solar panel
#include <stdbool.h> //To be able to use boolean variables and 'true' and 'false' values
//These will be replaced with a better model and finally the real readings
double readVoltage() {
return 24;
}
//These will be replaced with a better model and finally the real readings
double readCurrent() {
return 5;
}
int main() {
enum StateMachine {START, TRACK, SHIFT, HOLD}; //States used by the state machine
enum StateMachine state = START; //Initialize at the START state
double power = 0; //Calculated power delivered by the solar panels
double dutyCycle = 0.5; //Generated duty cycle
double holdPower; //Power that is to be hold during the HOLD state
double lastStablePower; //Last stable power processed in the SHIFT state
double prevPower; //Previous power averaged by the TRACK state
int trackCount, shiftCount, holdCount; //Counters to monitor iterations of the each state
double step = 0.2; //Size of the steps taken by the shift algorithm
int climb = 1; //Direction of the steps taken by the shift algorithm (1 for positive, -1 for negative)
while (true) {
switch(state) {
case START:
//Initialize variables
dutyCycle = 0.5;
step = 0.2;
climb = 1;
//Set the first state
prevPower = power;
power = 0;
trackCount = 0;
state = TRACK;
break;
case TRACK:
//Takes 5 measurements of the power supplied by the solar panesls
if (trackCount < 5) {
power += readVoltage() * readCurrent();
trackCount++;
} else {
trackCount = 0;
power = power / 5; //Averages the power of the 5 measurements
//Checks if the current power average has less than a 1% variation with respect to the previous
if (1.01 * prevPower >= power && 0.99 * prevPower <= power) {
state = SHIFT;
} else {
prevPower = power;
power = 0;
}
}
break;
case SHIFT:
//Determines if the last iteration of dutyCycle had a negative impact on power
if (power < lastStablePower) {
shiftCount = 0;
climb = -climb;
step = step / 2;
} else {
shiftCount++;
}
//Restarts the step size when too many iterations have occurred (it is stuck)
if (shiftCount >= 10) {
step = 0.2;
shiftCount = 0;
}
//Reverses the climb direction if algorithm tries to go of-limits
if (dutyCycle > 1) {
climb = -1;
dutyCycle = 1;
} else if (dutyCycle < 0) {
climb = 1;
dutyCycle = 0;
}
lastStablePower = power; //Saves this as the last stable power
dutyCycle = dutyCycle + climb * step; //Updates duty cycle
//Stop duty cycle from going off limits
if (dutyCycle >= 1) dutyCycle = 1;
if (dutyCycle <= 0) dutyCycle = 0;
//Check if it should hold this dutycycle or track for more changes in power
if (step <= 0.001) {
holdPower = power;
holdCount = 0;
power = 0;
state = HOLD;
} else {
prevPower = power;
power = 0;
trackCount = 0;
state = TRACK;
}
break;
case HOLD:
//Takes 5 measurements of the power supplied by the solar panesls
if (holdCount < 5) {
power += readVoltage() * readCurrent();
holdCount++;
} else {
holdCount = 0;
power = power / 5; //Averages the power of the 5 measurements
//Checks if the current power average has more than a 1% variation with respect to the max
if (1.01 * power > holdPower && 0.99 * power < holdPower) {
prevPower = power;
power = 0;
trackCount = 0;
state = TRACK;
}
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment