Skip to content

Commit

Permalink
More and more dev...
Browse files Browse the repository at this point in the history
(cherry picked from commit 68b60aef350ba8426f8bef412174d1cda6539170)
  • Loading branch information
mathieucarbou committed Jul 5, 2024
1 parent 1e9f92e commit 95ba6e8
Show file tree
Hide file tree
Showing 36 changed files with 935 additions and 431 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"complex": "cpp",
"iostream": "cpp"
}
}
25 changes: 22 additions & 3 deletions data/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ <h1>YaSolR Configuration</h1>
o1_resistance: ["Output 1 Resistance (Ohm)", "uint"],
o1_dim_max_d: ["Dimmer Duty Limiter (limit dimmer duty 0-4095)", "uint"],
o1_dim_max_t: ["Dimmer Temperature Limiter (stop routing when temperature reached)", "uint"],
o1_excess_ratio: ["Grid Excess Reserved to Output 1. Output 2 will take the remaining. If 100%, Output 2 will be activated only if there is some remaining excess.", "percent"],
o1_ad_enable: ["Dimmer Automatic Control", "switch"],
o1_ab_enable: ["Bypass Automatic Control", "switch"],
o1_days: ["Bypass Week Days", "string"],
Expand All @@ -78,6 +79,7 @@ <h1>YaSolR Configuration</h1>
o2_resistance: ["Output 2 Resistance (Ohm)", "uint"],
o2_dim_max_d: ["Dimmer Duty Limiter (limit dimmer duty 0-4095)", "uint"],
o2_dim_max_t: ["Dimmer Temperature Limiter (stop routing when temperature reached)", "uint"],
o2_excess_ratio: ["Grid Excess Reserved to Output 2. Output 2 will take the remaining after output 1. If 100%, Output 2 will try to consume 100% of the remaining.", "percent"],
o2_ad_enable: ["Dimmer Automatic Control", "switch"],
o2_ab_enable: ["Bypass Automatic Control", "switch"],
o2_days: ["Bypass Week Days", "string"],
Expand All @@ -86,9 +88,6 @@ <h1>YaSolR Configuration</h1>
o2_temp_start: ["Bypass Start Temperature (C)", "uint"],
o2_temp_stop: ["Bypass Stop Temperature (C)", "uint"],

"Output 1 / Output 2 Split": "TITLE",
o1_o2_split: ["Grid Excess Reserved to Output 1. Output 2 will take the remaining. If 100%, Output 2 will be activated only if there is some remaining excess.", "percent"],

"Relays": "TITLE",
relay1_load: ["Relay 1 Automatic Control: Connected Load (Watts)", "uint"],
relay2_load: ["Relay 2 Automatic Control: Connected Load (Watts)", "uint"],
Expand Down Expand Up @@ -165,6 +164,17 @@ <h1>YaSolR Configuration</h1>
o2_relay_type: ["Output 2 Bypass Relay Type", "select", "NO,NC"],
relay1_type: ["Relay 1 Type", "select", "NO,NC"],
relay2_type: ["Relay 2 Type", "select", "NO,NC"],

"PID Calibration": "TITLE",
pid_pmode: ["Proportional Mode (1: on Error, 2: on Input, 3: Both)", "select", "1,2,3"],
pid_dmode: ["Derivative Mode (1: on Error, 2: on Input)", "select", "1,2"],
pid_icmode: ["Integral Correction Mode (0: Off, 1: Clamp, 2: Anti-windup)", "select", "0,1,2"],
pid_setpoint: ["Setpoint (Target Grid Power)", "float"],
pid_kp: ["Kp", "float"],
pid_ki: ["Ki", "float"],
pid_kd: ["Kd", "float"],
pid_out_min: ["Output Min", "float"],
pid_out_max: ["Output Max", "float"],
};

async function onInputChange(event) {
Expand Down Expand Up @@ -211,6 +221,15 @@ <h1>YaSolR Configuration</h1>
percent: function (cell, key, value, options) {
addInputNumber(cell, key, value, 0, 100);
},
float: function (cell, key, value, options) {
const input = document.createElement("input");
input.setAttribute("id", key);
input.setAttribute("type", "text");
input.setAttribute("autocomplete", "off");
input.setAttribute("value", value);
cell.appendChild(input);
input.addEventListener("change", onInputChange);
},
string: function (cell, key, value, options) {
const input = document.createElement("input");
input.setAttribute("id", key);
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
426 changes: 426 additions & 0 deletions doc/pid/PID.log

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions doc/pid/commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
curl -v -X POST -F "pid_kp=0.2" -F "pid_ki=0" -F "pid_kd=0.1" -F "pid_iterm_min=0" http://192.168.125.123/api/config
curl -v -X POST -F "pid_kp=0.3" -F "pid_ki=0" -F "pid_kd=0.1" -F "pid_iterm_min=0" http://192.168.125.123/api/config
curl -v -X POST -F "pid_kp=0.3" -F "pid_ki=0" -F "pid_kd=0.2" -F "pid_iterm_min=0" http://192.168.125.123/api/config
curl -v -X POST -F "pid_kp=0.4" -F "pid_ki=0.01" -F "pid_kd=0.2" -F "pid_iterm_min=-5" http://192.168.125.123/api/config
curl -v -X POST -F "pid_kp=0.4" -F "pid_ki=0" -F "pid_kd=0.2" -F "pid_iterm_min=0" http://192.168.125.123/api/config
curl -v -X POST -F "pid_kp=0.6" -F "pid_ki=0.01" -F "pid_kd=0.8" -F "pid_iterm_min=-10" http://192.168.125.123/api/config
curl -v -X POST -F "pid_kp=0.6" -F "pid_ki=0" -F "pid_kd=0.8" -F "pid_iterm_min=0" http://192.168.125.123/api/config
13 changes: 8 additions & 5 deletions include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <MycilaLogger.h>
#include <MycilaMQTT.h>
#include <MycilaNTP.h>
#include <MycilaPID.h>
#include <MycilaPZEM004Tv3.h>
#include <MycilaRelay.h>
#include <MycilaRouter.h>
Expand Down Expand Up @@ -51,8 +52,9 @@

#include <YaSolRDefines.h>

extern AsyncWebServer webServer;
extern AsyncUDP udp;
extern AsyncWebServer webServer;
extern AsyncWebSocket wsDebugPID;
extern ESPDash dashboard;

extern Mycila::Config config;
Expand All @@ -67,6 +69,7 @@ extern Mycila::HADiscovery haDiscovery;
extern Mycila::JSY jsy;
extern Mycila::Logger logger;
extern Mycila::MQTT mqtt;
extern Mycila::PID pidController;
extern Mycila::PZEM pzemO1;
extern Mycila::PZEM pzemO2;
extern Mycila::Relay bypassRelayO1;
Expand All @@ -90,20 +93,20 @@ extern Mycila::Task mqttPublishConfigTask;
extern Mycila::TaskManager jsyTaskManager;
extern Mycila::Task jsyTask;

extern Mycila::TaskManager coreTaskManager;
extern Mycila::TaskManager core0TaskManager;
extern Mycila::Task carouselTask;
extern Mycila::Task dashboardTask;
extern Mycila::Task displayTask;
extern Mycila::Task ds18Task;
extern Mycila::Task lightsTask;

extern Mycila::TaskManager core1TaskManager;
extern Mycila::Task dashboardTask;
extern Mycila::Task mqttConfigTask;
extern Mycila::Task networkManagerTask;
extern Mycila::Task networkUpTask;
extern Mycila::Task otaTask;
extern Mycila::Task profilerTask;
extern Mycila::Task resetTask;
extern Mycila::Task restartTask;
extern Mycila::Task routerDebugTask;
#ifdef APP_MODEL_TRIAL
extern Mycila::Task trialTask;
#endif
Expand Down
13 changes: 10 additions & 3 deletions include/YaSolRDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@
#define KEY_MQTT_USERNAME "mqtt_user"
#define KEY_NTP_SERVER "ntp_server"
#define KEY_NTP_TIMEZONE "ntp_timezone"
#define KEY_OUTPUT_SPLIT "o1_o2_split"
#define KEY_OUTPUT1_DAYS "o1_days"
#define KEY_OUTPUT1_DIMMER_MAX_DUTY "o1_dim_max_d"
#define KEY_OUTPUT1_DIMMER_MAX_TEMP "o1_dim_max_t"
#define KEY_OUTPUT1_RELAY_TYPE "o1_relay_type"
#define KEY_OUTPUT1_RESERVED_EXCESS "o1_excess_ratio"
#define KEY_OUTPUT1_RESISTANCE "o1_resistance"
#define KEY_OUTPUT1_TEMPERATURE_START "o1_temp_start"
#define KEY_OUTPUT1_TEMPERATURE_STOP "o1_temp_stop"
Expand All @@ -123,15 +123,22 @@
#define KEY_OUTPUT2_DIMMER_MAX_DUTY "o2_dim_max_d"
#define KEY_OUTPUT2_DIMMER_MAX_TEMP "o2_dim_max_t"
#define KEY_OUTPUT2_RELAY_TYPE "o2_relay_type"
#define KEY_OUTPUT2_RESERVED_EXCESS "o2_excess_ratio"
#define KEY_OUTPUT2_RESISTANCE "o2_resistance"
#define KEY_OUTPUT2_TEMPERATURE_START "o2_temp_start"
#define KEY_OUTPUT2_TEMPERATURE_STOP "o2_temp_stop"
#define KEY_OUTPUT2_TIME_START "o2_time_start"
#define KEY_OUTPUT2_TIME_STOP "o2_time_stop"
#define KEY_OUTPUT2_TIME_STOP "o2_time_stop"
#define KEY_PID_KP "pid_kp"
#define KEY_PID_KI "pid_ki"
#define KEY_PID_D_MODE "pid_dmode"
#define KEY_PID_IC_MODE "pid_icmode"
#define KEY_PID_KD "pid_kd"
#define KEY_PID_KI "pid_ki"
#define KEY_PID_KP "pid_kp"
#define KEY_PID_OUT_MAX "pid_out_max"
#define KEY_PID_OUT_MIN "pid_out_min"
#define KEY_PID_P_MODE "pid_pmode"
#define KEY_PID_SETPOINT "pid_setpoint"
#define KEY_RELAY1_LOAD "relay1_load"
#define KEY_RELAY1_TYPE "relay1_type"
#define KEY_RELAY2_LOAD "relay2_load"
Expand Down
2 changes: 1 addition & 1 deletion include/YaSolRWebsite.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ namespace YaSolR {
Card _output2Resistance = Card(&dashboard, ENERGY_CARD, YASOLR_LBL_058, "Ω");
Card _output2Energy = Card(&dashboard, ENERGY_CARD, YASOLR_LBL_059, "kWh");
Card _output2DimmerAuto = Card(&dashboard, BUTTON_CARD, YASOLR_LBL_060);
Card _output2DimmerRatio = Card(&dashboard, SLIDER_CARD, YASOLR_LBL_061, "%", 0, 100, 1);
Card _output2DimmerRatio = Card(&dashboard, SLIDER_CARD, YASOLR_LBL_158, "%", 0, 100, 1);
Card _output2DimmerDutyLimiter = Card(&dashboard, SLIDER_CARD, YASOLR_LBL_062, "", 0, YASOLR_DIMMER_MAX_LEVEL, 1);
Card _output2DimmerTempLimiter = Card(&dashboard, TEXT_INPUT_CARD, YASOLR_LBL_063, "°C");
Card _output2BypassAuto = Card(&dashboard, BUTTON_CARD, YASOLR_LBL_064);
Expand Down
4 changes: 2 additions & 2 deletions include/i18n/en.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#define YASOLR_LBL_058 "Resistance"
#define YASOLR_LBL_059 "Energy"
#define YASOLR_LBL_060 "Dimmer Automatic Control"
#define YASOLR_LBL_061 "Grid Excess Sharing"
#define YASOLR_LBL_061 "Grid Excess Reserved"
#define YASOLR_LBL_062 "Dimmer Duty Limiter"
#define YASOLR_LBL_063 "Dimmer Temperature Limiter"
#define YASOLR_LBL_064 "Bypass Automatic Control"
Expand Down Expand Up @@ -161,7 +161,7 @@
#define YASOLR_LBL_155 "Input Only"
#define YASOLR_LBL_156 "I/O"
#define YASOLR_LBL_157 "JSY Remote UDP: Message Rate"
#define YASOLR_LBL_158
#define YASOLR_LBL_158 "Grid Excess Remaining"
#define YASOLR_LBL_159
#define YASOLR_LBL_160
#define YASOLR_LBL_161
Expand Down
4 changes: 2 additions & 2 deletions include/i18n/fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#define YASOLR_LBL_058 "Résistance"
#define YASOLR_LBL_059 "Énergie"
#define YASOLR_LBL_060 "Routage automatique"
#define YASOLR_LBL_061 "Partage du surplus réseau"
#define YASOLR_LBL_061 "Surplus réseau réservé"
#define YASOLR_LBL_062 "Limitation du routage"
#define YASOLR_LBL_063 "Température de consigne d'arrêt du routage"
#define YASOLR_LBL_064 "Marche forcée automatique"
Expand Down Expand Up @@ -161,7 +161,7 @@
#define YASOLR_LBL_155 "Entrée uniquement"
#define YASOLR_LBL_156 "E/S"
#define YASOLR_LBL_157 "JSY Remote UDP: Débit"
#define YASOLR_LBL_158
#define YASOLR_LBL_158 "Surplus réseau restant"
#define YASOLR_LBL_159
#define YASOLR_LBL_160
#define YASOLR_LBL_161
Expand Down
8 changes: 1 addition & 7 deletions lib/MycilaDimmer/MycilaDimmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void Mycila::Dimmer::end() {
}
}

void Mycila::Dimmer::setPowerDuty(uint16_t newDuty) {
void Mycila::Dimmer::setDuty(uint16_t newDuty) {
if (!_enabled)
return;

Expand All @@ -82,9 +82,6 @@ void Mycila::Dimmer::setPowerDuty(uint16_t newDuty) {
if (_duty == newDuty)
return;

// save old level
const uint16_t oldDuty = _duty;

const uint16_t semiPeriod = _zcd->getSemiPeriod();

if (newDuty == 0) {
Expand All @@ -105,7 +102,4 @@ void Mycila::Dimmer::setPowerDuty(uint16_t newDuty) {
}

_duty = newDuty;

if (_callback && (oldDuty == 0 || oldDuty == MYCILA_DIMMER_MAX_DUTY || newDuty == 0 || newDuty == MYCILA_DIMMER_MAX_DUTY))
_callback(newDuty == 0 ? DimmerState::OFF : (newDuty == MYCILA_DIMMER_MAX_DUTY ? DimmerState::FULL : DimmerState::DIM));
}
22 changes: 7 additions & 15 deletions lib/MycilaDimmer/MycilaDimmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
#define MYCILA_DIMMER_MAX_DUTY 4095 // ((1 << MYCILA_DIMMER_RESOLUTION) - 1)

namespace Mycila {
enum class DimmerState { OFF,
FULL,
DIM };

typedef std::function<void(DimmerState event)> DimmerStateCallback;

class Dimmer {
public:
explicit Dimmer(ZCD& zcd) : _zcd(&zcd) {}
Expand All @@ -30,9 +24,7 @@ namespace Mycila {
void begin(const int8_t pin);
void end();

void listen(DimmerStateCallback callback) { _callback = callback; }

void off() { setPowerDuty(0); }
void off() { setDuty(0); }
bool isOff() const { return _duty == 0; }
bool isOn() const { return _duty > 0; }
bool isOnAtFullPower() const { return _duty >= MYCILA_DIMMER_MAX_DUTY; }
Expand All @@ -46,20 +38,21 @@ namespace Mycila {
root["angle_d"] = angle * RAD_TO_DEG;
root["delay"] = getFiringDelay();
root["duty"] = _duty;
root["duty_cycle"] = getPowerDutyCycle();
root["duty_cycle"] = getDutyCycle();
root["enabled"] = _enabled;
root["state"] = _duty > 0 ? "on" : "off";
_zcd->toJson(root["zcd"].to<JsonObject>());
}

// Power Duty Cycle [0, MYCILA_DIMMER_MAX_DUTY]
void setPowerDuty(uint16_t duty);
uint16_t getPowerDuty() const { return _duty; }
void setDuty(uint16_t duty);
uint16_t getDuty() const { return _duty; }

// Power Duty Cycle [0, 1]
// At 0% power, duty == 0
// At 100% power, duty == 1
void setPowerDutyCycle(float dutyCycle) { setPowerDuty(dutyCycle * MYCILA_DIMMER_MAX_DUTY); }
float getPowerDutyCycle() const { return static_cast<float>(_duty) / MYCILA_DIMMER_MAX_DUTY; }
void setDutyCycle(float dutyCycle) { setDuty(dutyCycle * MYCILA_DIMMER_MAX_DUTY); }
float getDutyCycle() const { return static_cast<float>(_duty) / MYCILA_DIMMER_MAX_DUTY; }

// Delay [0, semi-period] us
// Where semi-period = 1000000 / 2 / frequency (50h: 10000 us, 60Hz: 8333 us)
Expand All @@ -79,7 +72,6 @@ namespace Mycila {
ZCD* _zcd;
gpio_num_t _pin = GPIO_NUM_NC;
bool _enabled = false;
DimmerStateCallback _callback = nullptr;
Thyristor* _dimmer = nullptr;
uint16_t _duty = 0;
};
Expand Down
23 changes: 4 additions & 19 deletions lib/MycilaRouter/MycilaGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#pragma once

#include <MycilaJSY.h>
#include <MycilaPZEM004Tv3.h>

#ifdef MYCILA_JSON_SUPPORT
#include <ArduinoJson.h>
Expand Down Expand Up @@ -37,8 +36,6 @@ namespace Mycila {

class Grid {
public:
Grid(PZEM& pzem1, PZEM& pzem2) : _pzem1(&pzem1), _pzem2(&pzem2) {}

// expiration for remote data
void setExpiration(uint32_t seconds) { _expiration = seconds * 1000; }

Expand Down Expand Up @@ -151,21 +148,13 @@ namespace Mycila {
return updated;
}

bool isConnected() const { return _meterConnected ||
_meterRemoteConnected ||
_mqttConnected ||
_pzem1->getVoltage() > 0 ||
_pzem2->getVoltage() > 0; }
bool isConnected() const { return _meterConnected || _meterRemoteConnected || _mqttConnected; }

// get the current grid voltage
// - if JSY/PZEM are connected, they have priority
// - if JSY are connected, they have priority
// - if JSY remote is connected, it has second priority
// - if MQTT is connected, it has lowest priority
float getVoltage() const {
if (_pzem1->getVoltage() > 0)
return _pzem1->getVoltage();
if (_pzem2->getVoltage() > 0)
return _pzem2->getVoltage();
if (_meterConnected)
return _meter.voltage;
if (_meterRemoteConnected)
Expand Down Expand Up @@ -203,7 +192,7 @@ namespace Mycila {
return SOURCE_NONE;
}

void getMetrics(GridMetrics& metrics) const {
void getMeasurements(GridMetrics& metrics) const {
metrics.power = getPower();
metrics.voltage = getVoltage();
switch (getGridSource()) {
Expand Down Expand Up @@ -233,7 +222,7 @@ namespace Mycila {
#ifdef MYCILA_JSON_SUPPORT
void toJson(const JsonObject& root) const {
GridMetrics metrics;
getMetrics(metrics);
getMeasurements(metrics);
root["online"] = isConnected();
_toJson(root["metrics"].to<JsonObject>(), metrics);
_toJson(root["source"]["jsy"].to<JsonObject>(), _meter);
Expand All @@ -243,10 +232,6 @@ namespace Mycila {
#endif

private:
// For voltage reading
PZEM* _pzem1;
PZEM* _pzem2;

// Local JSY
volatile GridMetrics _meter;
volatile uint32_t _meterTime = 0;
Expand Down
Loading

0 comments on commit 95ba6e8

Please sign in to comment.