Skip to content

Commit

Permalink
📌 new extended PSU syntax for custom pins, SWSerial
Browse files Browse the repository at this point in the history
  • Loading branch information
t413 committed May 20, 2021
1 parent aa0a362 commit 63224d2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
46 changes: 40 additions & 6 deletions lib/MPPTLib/powerSupplies.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
#include "powerSupplies.h"
#include <stdexcept>
#include <SoftwareSerial.h>
#include <ModbusMaster.h> // ModbusMaster
#include "utils.h"

PowerSupply* PowerSupply::make(const String &type) {
//form: rxpin,txpin[sw]:baud
Stream* makeStream(String s, int baud) {
auto sp1 = split(s, ":");
if (sp1.second.length()) //specify baud rate
baud = sp1.second.toInt();
int rx = -1, tx = -1;
if (sp1.first.length()) { //specify pins
bool useSw = suffixed(& sp1.first, "sw");
auto pins = split(sp1.first, ",");
rx = pins.first.length()? pins.first.toInt() : -1;
tx = pins.second.length()? pins.second.toInt() : -1;
if (useSw) {
auto ret = new SoftwareSerial;
ret->begin(baud, SWSERIAL_8N1, rx, tx, false);
return ret;
}
}
Serial2.begin(baud, SERIAL_8N1, rx, tx, false, 1000);
return &Serial2;
}

PowerSupply* PowerSupply::make(String type) {
type.toLowerCase();
auto sp1 = split(type, ":");
PowerSupply* ret = NULL;
String typeUp = type;
typeUp.toUpperCase();
if (typeUp.startsWith("DP")) {
ret = new DPS(&Serial2, typeUp.equals("DPS5020"));
Serial2.begin(19200, SERIAL_8N1, -1, -1, false, 1000);
ret = new DPS(makeStream(sp1.second, 19200), typeUp.equals("DPS5020"));
} else if (typeUp.startsWith("DROK")) {
ret = new Drok(&Serial2);
Serial2.begin(4800, SERIAL_8N1, -1, -1, false, 1000);
ret = new Drok(makeStream(sp1.second, 4800));
} else { //default
ret = NULL;
}
Expand All @@ -26,7 +48,19 @@ PowerSupply* PowerSupply::make(const String &type) {
// ----------------------- //

PowerSupply::PowerSupply() { }
PowerSupply::~PowerSupply() { }
PowerSupply::~PowerSupply() {
String ret;
if (auto hw = dynamic_cast<HardwareSerial*>(port_)) {
hw->end(); ret += "ended HW ";
} else if (auto sw = dynamic_cast<SoftwareSerial*>(port_)) {
sw->end(); ret += "ended SW ";
}
if ((port_ != &Serial) && (port_ != &Serial1) && (port_ != &Serial2)) {
delete(port_);
ret += "deleted ";
}
log("~PowerSupply " + ret);
}

String PowerSupply::toString() const {
return str("PSU-out[%0.2fV %0.2fA]-lim[%0.2fV %0.2fA]", outVolt_, outCurr_, limitVolt_, limitCurr_)
Expand Down
2 changes: 1 addition & 1 deletion lib/MPPTLib/powerSupplies.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PowerSupply {
bool outEn_ = false;
uint32_t lastSuccess_ = 0, lastAmpUpdate_ = 0;

static PowerSupply* make(const String &type);
static PowerSupply* make(String type);
PowerSupply();
virtual ~PowerSupply();
virtual bool begin() = 0;
Expand Down
7 changes: 4 additions & 3 deletions lib/MPPTLib/solar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ String Solar::setPSU(String s) {
measperiod_ = 500; //slow down, DSP5005 meas does full update()
ckPSUs();
psu_->begin();
return "created PSU=" + psu_->getType();
return "created psu " + psu_->getType();
}
return psu_->getType();
}
Expand Down Expand Up @@ -305,6 +305,8 @@ void Solar::doSweepStep() {

bool Solar::hasCollapsed() const {
if (!psu_ || !psu_->outEn_) return false;
if (!psu_->isDrok() && psu_->isCollapsed()) //DP* psu is darn accurate
return true;
bool simpleClps = (inVolt_ < (psu_->outVolt_ * 1.11)); //simple voltage match method
float collapsePct = (inVolt_ - psu_->outVolt_) / psu_->outVolt_;
if (simpleClps && psu_->isCollapsed())
Expand Down Expand Up @@ -617,8 +619,7 @@ String LowVoltageProtect::toString() const {
LowVoltageProtect::~LowVoltageProtect() { log("~LVProtect " + toString()); }
LowVoltageProtect::LowVoltageProtect(String config) {
StringPair sp1 = split(config, ":");
invert_ = sp1.first.endsWith("i");
if (invert_) sp1.first.remove(sp1.first.lastIndexOf("i"));
invert_ = suffixed(& sp1.first, "i");
pin_ = sp1.first.length()? sp1.first.toInt() : 22;
if (digitalPinToAnalogChannel(pin_) > 7)
throw std::runtime_error("sorry, lv-protect pin can't use an ADC2 pin");
Expand Down
6 changes: 6 additions & 0 deletions lib/MPPTLib/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ StringPair split(const String &str, const String &del) {
return StringPair(str, "");
}

bool suffixed(String *str, const String &suff) {
if (!str) return false;
bool res = str->endsWith(suff);
if (res) str->remove(str->lastIndexOf(suff));
return res;
}

// float lifepo4_soc[] = {13.4, 13.3, 13.28, 13.};

Expand Down
1 change: 1 addition & 0 deletions lib/MPPTLib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ String str(bool v);

typedef std::pair<String,String> StringPair;
StringPair split(const String &str, const String &del);
bool suffixed(String *str, const String &suff);

template<typename T, uint16_t Size>
class CircularArray {
Expand Down
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ monitor_speed = 115200
lib_deps =
PubSubClient
ModbusMaster
plerup/espsoftwareserial
src_build_flags =
!python utils.py version
!python utils.py version ;injects version into main
extra_scripts = pre:utils.py
build_unflags = -fno-rtti ;allow dynamic_cast

0 comments on commit 63224d2

Please sign in to comment.