Skip to content

Commit

Permalink
Adds 'currentcap' pref to limit max current, sanity checks on PSU reads
Browse files Browse the repository at this point in the history
  • Loading branch information
t413 committed Oct 28, 2019
1 parent a6bc167 commit b777923
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
10 changes: 9 additions & 1 deletion lib/MPPTLib/solar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void Solar::setup() {
pub_.add("PSUperiod", psuperiod_ ).pref();
pub_.add("measperiod", measperiod_ ).pref();
pub_.add("autosweep", autoSweep_ ).pref();
pub_.add("currentcap", currentCap_ ).pref();
pub_.add("involt", inVolt_);
pub_.add("wh", wh_ );
pub_.add("collapses", [=](String) { return String(getCollapses()); });
Expand Down Expand Up @@ -135,6 +136,13 @@ void Solar::startSweep() {

void Solar::doSweepStep() {
newDesiredCurr_ = psu_.limitCurr_ + 0.02; //TODO maybe add a pref for sweep speed or use pgain?
if (newDesiredCurr_ >= currentCap_) {
setpoint_ = inVolt_ - 4 * pgain_;
newDesiredCurr_ = currentCap_;
sweeping_ = false;
Serial.printf("SWEEP DONE, currentcap reached (setpoint=%0.3f)\n", setpoint_);
return applyAdjustment();
}
applyAdjustment();

if (sweepPoints_.empty() || (psu_.outCurr_ > sweepPoints_.back().i))
Expand Down Expand Up @@ -174,7 +182,7 @@ void Solar::loop() {
double error = inVolt_ - setpoint_;
double dcurr = constrain(error * pgain_, -3, 2); //limit ramping speed
if (error > 0.3 || (-error > 0.2)) { //adjustment deadband, more sensitive when needing to ramp down
newDesiredCurr_ = psu_.limitCurr_ + dcurr;
newDesiredCurr_ = min(psu_.limitCurr_ + dcurr, currentCap_);
if (error < 0.6) { //ramp down, quick!
logme += "[QUICK] ";
needsQuickAdj_ = true;
Expand Down
1 change: 1 addition & 0 deletions lib/MPPTLib/solar.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Solar {
uint8_t pinInvolt_ = 36;
float inVolt_ = 0, wh_ = 0;
double setpoint_ = 0, pgain_ = 0.1;
double currentCap_ = 8.5;
CircularArray<uint32_t, 32> collapses_;
int measperiod_ = 200, printPeriod_ = 1000, psuperiod_ = 2000;
int autoSweep_ = 10 * 60; //every 10m
Expand Down
14 changes: 9 additions & 5 deletions lib/MPPTLib/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ bool PowerSupply::readVoltage() { return handleReply(cmdReply("aru")); }
bool PowerSupply::readCurrent() { return handleReply(cmdReply("ari")); }
bool PowerSupply::getOutputEnabled() { return handleReply(cmdReply("aro")); }

template<typename T>void setCheck(T &save, float in, float max) {
if (in < max) save = in;
}

bool PowerSupply::handleReply(String msg) {
if (!msg.length()) return false;
String hdr = msg.substring(0, 3);
String body = msg.substring(3);
if (hdr == "#ro") outEn_ = (body.toInt() == 1);
else if (hdr == "#ru") outVolt_ = body.toFloat() / 100.0;
else if (hdr == "#ri") outCurr_ = body.toFloat() / 100.0;
else if (hdr == "#rv") limitVolt_ = body.toFloat() / 100.0;
else if (hdr == "#ra") limitCurr_ = body.toFloat() / 100.0;
if (hdr == "#ro") setCheck(outEn_, (body.toInt() == 1), 2);
else if (hdr == "#ru") setCheck(outVolt_, body.toFloat() / 100.0, 80);
else if (hdr == "#ri") setCheck(outCurr_, body.toFloat() / 100.0, 15);
else if (hdr == "#rv") setCheck(limitVolt_, body.toFloat() / 100.0, 80);
else if (hdr == "#ra") setCheck(limitCurr_, body.toFloat() / 100.0, 15);
else {
Serial.println("got unknown msg > '" + hdr + "' / '" + body + "'");
return false;
Expand Down

0 comments on commit b777923

Please sign in to comment.