From 9bb615723c919cc7b2929b3c3a11e0167c3a9a62 Mon Sep 17 00:00:00 2001 From: Tim O'Brien Date: Wed, 24 Jun 2020 21:54:41 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92=20Fixes=20mutex,=20human-readable?= =?UTF-8?q?=20uptime,=20formatting,=20wh=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 +----- lib/MPPTLib/publishable.cpp | 14 +++++++------- lib/MPPTLib/solar.cpp | 32 +++++++++++++++++--------------- lib/MPPTLib/utils.cpp | 10 ++++++++++ lib/MPPTLib/utils.h | 1 + 5 files changed, 36 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index e944d7e..0e4591e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,5 @@ .pio .piolibdeps -.vscode/.browse.c_cpp.db* -.vscode/c_cpp_properties.json -.vscode/launch.json -.vscode/ipch - +.vscode .DS_Store wiki/ diff --git a/lib/MPPTLib/publishable.cpp b/lib/MPPTLib/publishable.cpp index 6662a59..71bcde0 100644 --- a/lib/MPPTLib/publishable.cpp +++ b/lib/MPPTLib/publishable.cpp @@ -37,7 +37,7 @@ template<> void Pub::load(Preferences&p) { (*value) = String(buf); } -Publishable::Publishable() : lock_(xSemaphoreCreateCounting( 10, 0 )) { +Publishable::Publishable() : lock_(xSemaphoreCreateMutex()) { add("save", [this](String s){ return str("saved %d prefs", this->savePrefs()); }).hide(); @@ -50,13 +50,13 @@ Publishable::Publishable() : lock_(xSemaphoreCreateCounting( 10, 0 )) { void Publishable::log(const String &s) { Serial.println(s); - if (xSemaphoreTake(lock_, (TickType_t) 10) == pdTRUE) { + if (xSemaphoreTake(lock_, (TickType_t) 100) == pdTRUE) { logPub_.push_back(s); xSemaphoreGive(lock_); } } bool Publishable::popLog(String *s) { - if (xSemaphoreTake(lock_, (TickType_t) 10) == pdTRUE) { + if (xSemaphoreTake(lock_, (TickType_t) 100) == pdTRUE) { bool got = logPub_.size() > 0; if (got) (*s) = logPub_.pop_front(); xSemaphoreGive(lock_); @@ -65,13 +65,13 @@ bool Publishable::popLog(String *s) { return false; } void Publishable::logNote(const String &s) { - if (xSemaphoreTake(lock_, (TickType_t) 10) == pdTRUE) { - logNote_ += s; + if (xSemaphoreTake(lock_, (TickType_t) 100) == pdTRUE) { + logNote_ += " " + s; xSemaphoreGive(lock_); - } + } else Serial.println("LOGNOTE couldn't get mutex! " + s); } String Publishable::popNotes() { - if (xSemaphoreTake(lock_, (TickType_t) 10) == pdTRUE) { + if (xSemaphoreTake(lock_, (TickType_t) 100) == pdTRUE) { String ret = logNote_; logNote_ = ""; xSemaphoreGive(lock_); diff --git a/lib/MPPTLib/solar.cpp b/lib/MPPTLib/solar.cpp index 28d5693..29c72d0 100644 --- a/lib/MPPTLib/solar.cpp +++ b/lib/MPPTLib/solar.cpp @@ -71,7 +71,7 @@ void Solar::setup() { pub_.add("clear",[=](String s){ pub_.clearPrefs(); return "cleared"; }).hide(); pub_.add("debug",[=](String s){ psu_.debug_ = !(s == "off"); return String(psu_.debug_); }).hide(); pub_.add("version",[=](String){ log("Version " GIT_VERSION); return GIT_VERSION; }).hide(); - pub_.add("uptime",[=](String){ String ret = str("Uptime %lud", millis()); log(ret); return ret; }).hide(); + pub_.add("uptime",[=](String){ String ret = "Uptime " + timeAgo(millis()/1000); log(ret); return ret; }).hide(); server_.on("/", HTTP_ANY, [=]() { log("got req " + server_.uri() + " -> " + server_.hostHeader()); @@ -157,9 +157,9 @@ void Solar::doConnect() { if (i->pref_) db_.client.subscribe((db_.feed + "/prefs/" + i->key).c_str()); //subscribe to preference changes db_.client.subscribe((db_.feed + "/cmd").c_str()); //subscribe to cmd topic for any actions - } else Serial.println("PubSub connect ERROR! " + db_.client.state()); - } else Serial.println("no MQTT user / pass / server / feed set up!"); - } else Serial.printf("can't pub connect, wifi %d pub %d\n", WiFi.isConnected(), db_.client.connected()); + } else pub_.logNote("[PubSub connect ERROR]" + db_.client.state()); + } else pub_.logNote("[no MQTT user / pass / server / feed set up]"); + } else pub_.logNote(str("[can't pub connect, wifi %d pub %d]", WiFi.isConnected(), db_.client.connected())); } String SPoint::toString() const { @@ -169,7 +169,7 @@ String SPoint::toString() const { void Solar::applyAdjustment() { if (newDesiredCurr_ != psu_.limitCurr_) { if (psu_.setCurrent(newDesiredCurr_)) - pub_.logNote(str("[adjusting %0.2fA (from %0.2fA)] ", newDesiredCurr_ - psu_.limitCurr_, psu_.limitCurr_)); + pub_.logNote(str("[adjusting %0.2fA (from %0.2fA)]", newDesiredCurr_ - psu_.limitCurr_, psu_.limitCurr_)); else log("error setting current"); delay(50); psu_.readCurrent(); @@ -213,7 +213,7 @@ void Solar::doSweepStep() { int collapsedPoints = 0; for (int i = 0; i < sweepPoints_.size(); i++) if (sweepPoints_[i].collapsed) collapsedPoints++; - if (isCollapsed) pub_.logNote(str("COLLAPSED[%d] ", collapsedPoints)); + if (isCollapsed) pub_.logNote(str("COLLAPSED[%d]", collapsedPoints)); if (isCollapsed && collapsedPoints >= 2) { //great, sweep finished int maxIndex = 0; @@ -279,8 +279,8 @@ void Solar::loop() { double dcurr = constrain(error * pgain_, -ramplimit_ * 2, ramplimit_); //limit ramping speed if (error > 0.3 || (-error > 0.2)) { //adjustment deadband, more sensitive when needing to ramp down newDesiredCurr_ = min(psu_.limitCurr_ + dcurr, currentCap_); - if (error < 0.6) { //ramp down, quick! - pub_.logNote("[QUICK] "); + if ((error < 0.6) && (state_ == States::mppt)) { //ramp down, quick! + pub_.logNote("[QUICK]"); nextSolarAdjust_ = now; } } @@ -356,7 +356,8 @@ void Solar::loop() { if (res) { wh_ += psu_.outVolt_ * psu_.outCurr_ * (now - lastPSUpdate_) / 1000.0 / 60 / 60; currFilt_ = currFilt_ - 0.1 * (currFilt_ - psu_.outCurr_); - pub_.setDirty({"outvolt", "outcurr", "outputEN", "wh", "outpower", "currFilt"}); + pub_.setDirty({"outvolt", "outcurr", "outputEN", "outpower", "currFilt"}); + if (millis() > ignoreSubsUntil_) pub_.setDirtyAddr(&wh_); //don't publish at first lastPSUSuccess_ = now; } else { log("psu update fail" + String(psu_.debug_? " serial debug output enabled" : "")); @@ -365,9 +366,10 @@ void Solar::loop() { nextPSUpdate_ = now + 5000; lastPSUpdate_ = now; } - if (getCollapses() > 2) { + + if (getCollapses() > 2) nextAutoSweep_ = lastAutoSweep_ + autoSweep_ / 3.0 * 1000; - } + if (autoSweep_ > 0 && (now > nextAutoSweep_)) { if (state_ == States::capped) { log(str("Skipping auto-sweep. Already at currentCap (%0.1fA)", currentCap_)); @@ -385,12 +387,12 @@ void Solar::loop() { void Solar::publishTask() { doConnect(); db_.client.loop(); - ignoreSubsUntil_ = millis() + 3000; + ignoreSubsUntil_ = millis() + 10000; db_.client.setCallback([=](char*topicbuf, uint8_t*buf, unsigned int len){ String topic(topicbuf), val = str(std::string((char*)buf, len)); log("got sub value " + topic + " -> " + val); if (topic == (db_.feed + "/wh")) { - wh_ += val.toFloat(); + wh_ = (millis() > ignoreSubsUntil_)? val.toFloat() : wh_ + val.toFloat(); log("restored wh value to " + val); db_.client.unsubscribe((db_.feed + "/wh").c_str()); } else if (millis() > ignoreSubsUntil_) { //don't load old values @@ -414,10 +416,10 @@ void Solar::publishTask() { wins += db_.client.publish((db_.feed + "/" + (i->pref_? "prefs/":"") + i->key).c_str(), i->toString().c_str(), true)? 1 : 0; if (i->pref_) ignoreSubsUntil_ = now + 3000; } - pub_.logNote(str("[published %d] ", wins)); + pub_.logNote(str("[pub-%d]", wins)); pub_.clearDirty(); } else { - pub_.logNote("[pub disconnected] "); + pub_.logNote("[pub disconnected]"); doConnect(); } heap_caps_check_integrity_all(true); diff --git a/lib/MPPTLib/utils.cpp b/lib/MPPTLib/utils.cpp index 932d8a8..4079583 100644 --- a/lib/MPPTLib/utils.cpp +++ b/lib/MPPTLib/utils.cpp @@ -30,6 +30,16 @@ Publishable* pub_; //static void log(const String &s) { pub_->log(s); } void addLogger(Publishable* p) { pub_ = p; } +String timeAgo(int sec) { + int days = (sec / (3600 * 24)); + String ret = str("%ds", sec % 60); + if (sec >= 60 ) ret = str("%dm ", (sec % 3600) / 60) + ret; + if (sec >= 3600) ret = str("%dh ", ((sec % (3600 * 24)) / 3600)) + ret; + if (days) ret = str("%dd ", days % 365) + ret; + if (days >= 365) ret = str("%dy ", days / 365) + ret; + return ret; +} + PowerSupply::PowerSupply(Stream &port) : _port(&port), debug_(false) { } diff --git a/lib/MPPTLib/utils.h b/lib/MPPTLib/utils.h index f712239..e853197 100644 --- a/lib/MPPTLib/utils.h +++ b/lib/MPPTLib/utils.h @@ -34,6 +34,7 @@ void log(const String &); void addLogger(Publishable*); String getResetReasons(); +String timeAgo(int seconds); float mapfloat(long x, long in_min, long in_max, long out_min, long out_max);