Skip to content

Commit

Permalink
Development...
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Jun 9, 2024
1 parent f66ba23 commit f3bf1f2
Show file tree
Hide file tree
Showing 31 changed files with 682 additions and 725 deletions.
59 changes: 58 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,63 @@
"cmath": "cpp",
"limits": "cpp",
"soc_caps.h": "c",
"string": "cpp"
"string": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
}
}
8 changes: 6 additions & 2 deletions include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#include <MycilaNTP.h>
#include <MycilaPZEM004Tv3.h>
#include <MycilaRelay.h>
#include <MycilaRelayManager.h>
#include <MycilaRouter.h>
#include <MycilaRouterOutput.h>
#include <MycilaRouterRelay.h>
#include <MycilaSystem.h>
#include <MycilaTaskManager.h>
#include <MycilaTaskMonitor.h>
Expand Down Expand Up @@ -69,8 +69,11 @@ extern Mycila::Relay bypassRelayO1;
extern Mycila::Relay bypassRelayO2;
extern Mycila::Relay relay1;
extern Mycila::Relay relay2;
extern Mycila::Router router;
extern Mycila::RouterOutput output1;
extern Mycila::RouterOutput output2;
extern Mycila::RouterRelay routerRelay1;
extern Mycila::RouterRelay routerRelay2;
extern Mycila::TrafficLight lights;

extern Mycila::TaskManager ioTaskManager;
Expand Down Expand Up @@ -104,8 +107,9 @@ extern Mycila::Task pzemO2PairingTask;
extern Mycila::Task pzemTask;

extern Mycila::TaskManager routerTaskManager;
extern Mycila::Task relaysTask;
extern Mycila::Task relayTask;
extern Mycila::Task routerTask;
extern Mycila::Task routingTask;

// Tasks alone without a manager
extern Mycila::Task bootTask;
Expand Down
2 changes: 1 addition & 1 deletion include/YaSolRWebsite.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ namespace YaSolR {

void _outputDimmerSlider(Card& card, Mycila::RouterOutput& output);
void _outputBypassSwitch(Card& card, Mycila::RouterOutput& output);
void _relaySwitch(Card& card, const char* relayName);
void _relaySwitch(Card& card, Mycila::RouterRelay& relay);

void _pinout(Card& card, int32_t pin, std::map<int32_t, Card*>& pinout);
void _status(Card& card, const char* key, bool enabled, bool state = true, const char* err = "");
Expand Down
2 changes: 1 addition & 1 deletion lib/MycilaDimmer/MycilaDimmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void Mycila::Dimmer::setLevel(uint8_t newLevel) {

float Mycila::Dimmer::getPhaseAngle() const { return _delayToPhaseAngle(_dimmer->getDelay(), Thyristor::getFrequency()); }

float Mycila::Dimmer::computeDimmedVoltage(float inputVrms) const {
float Mycila::Dimmer::getDimmedVoltage(float inputVrms) const {
return _level == 0 ? 0 : (_level == MYCILA_DIMMER_MAX_LEVEL ? inputVrms : inputVrms * _lookupVrmsFactor(_level, Thyristor::getFrequency()));
}

Expand Down
4 changes: 2 additions & 2 deletions lib/MycilaDimmer/MycilaDimmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace Mycila {

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

inline void off() { setLevel(0); }
void off() { setLevel(0); }
bool isOff() const { return _level == 0; }
bool isOn() const { return _level > 0; }
bool isOnAtFullPower() const { return _level >= MYCILA_DIMMER_MAX_LEVEL; }
Expand Down Expand Up @@ -80,7 +80,7 @@ namespace Mycila {

// Returns the dimmed RMS voltage based on the current phase angle.
// This is a theoretical value. In reality, the real Vrms value will be more or less depending on the hardware and software speed.
float computeDimmedVoltage(float inputVrms) const;
float getDimmedVoltage(float inputVrms) const;

static void generateLUT(Print& out); // NOLINT

Expand Down
60 changes: 0 additions & 60 deletions lib/MycilaRouter/MycilaEnergySource.h

This file was deleted.

8 changes: 0 additions & 8 deletions lib/MycilaRouter/MycilaGrid.cpp

This file was deleted.

121 changes: 62 additions & 59 deletions lib/MycilaRouter/MycilaGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,81 @@
*/
#pragma once

#include <MycilaEnergySource.h>
#include <MycilaJSY.h>
#include <thyristor.h>

#ifdef MYCILA_JSON_SUPPORT
#include <ArduinoJson.h>
#endif

namespace Mycila {
class Grid : public EnergySource {
class Grid {
public:
void setExpiration(uint32_t seconds) { _delay = seconds * 1000; }
explicit Grid(JSY& jsy) : _jsy(&jsy) {}

void updateVoltage(float voltage) {
_voltage = voltage;
_lastUpdate = millis();
}
void updatePower(float power) {
_power = power;
_lastUpdate = millis();
// MQTT support
void setMQTTExpiration(uint32_t seconds) { _expiration = seconds * 1000; }
void setMQTTGridVoltage(float voltage) {
_mqttVoltage = voltage;
_mqttVoltageTime = millis();
}
void update(float current,
float energy,
float energyReturned,
float frequency,
float power,
float powerFactor) {
_current = current;
_energy = energy;
_energyReturned = energyReturned;
_frequency = frequency;
_power = power;
_powerFactor = powerFactor;
_lastUpdate = millis();
void setMQTTGridPower(float power) {
_mqttPower = power;
_mqttPowerTime = millis();
}

bool isExpired() const { return _delay > 0 && millis() - _lastUpdate > _delay; }
void invalidate() {
if (isExpired()) {
_current = 0;
_energy = 0;
_energyReturned = 0;
_frequency = 0;
_power = 0;
_powerFactor = 0;
_voltage = 0;
void applyExpiration() {
if (_mqttVoltageTime > 0 && millis() - _mqttVoltageTime >= _expiration) {
_mqttVoltage = 0;
_mqttVoltageTime = 0;
}
if (_mqttPowerTime > 0 && millis() - _mqttPowerTime >= _expiration) {
_mqttPower = 0;
_mqttPowerTime = 0;
}
}

// implement EnergySource
inline bool isConnected() const override { return getVoltage() > 0; }
float getFrequency() const override;
float getEnergy() const override { return _energy; }
float getEnergyReturned() const override { return _energyReturned; }
// returns the measured voltage or the nominal voltage if not connected
float getVoltage() const override { return _voltage; }
float getCurrent() const override { return _current; }
float getActivePower() const override { return _power; }
inline float getApparentPower() const override { return _powerFactor == 0 ? 0 : _power / _powerFactor; }
float getPowerFactor() const override { return _powerFactor; }
float getTHDi() const override {
// requires to know the Displacement Power Factor, cosφ,
// due to the phase shift between voltage and current
return 0;
bool isConnected() const { return getVoltage() > 0; }
float getEnergy() const { return _jsy->getEnergy2(); }
float getEnergyReturned() const { return _jsy->getEnergyReturned2(); }
float getCurrent() const { return _jsy->getCurrent2(); }
float getApparentPower() const { return _jsy->getApparentPower2(); }
float getPowerFactor() const { return _jsy->getPowerFactor2(); }
float getVoltage() const { return _mqttVoltageTime ? _mqttVoltage : _jsy->getVoltage2(); }
float getActivePower() const { return _mqttPowerTime ? _mqttPower : _jsy->getPower2(); }
float getFrequency() const {
// try first JSY
float f = _jsy->getFrequency();
// otherwise ZCD
return f > 0 ? f : Thyristor::getDetectedFrequency();
}

#ifdef MYCILA_JSON_SUPPORT
void toJson(const JsonObject& root) const {
root["apparent_power"] = getApparentPower();
root["current"] = getCurrent();
root["energy_returned"] = getEnergyReturned();
root["energy"] = getEnergy();
root["frequency"] = getFrequency();
root["jsy_grid_power"] = _jsy->getPower2();
root["jsy_grid_voltage"] = _jsy->getVoltage2();
root["mqtt_grid_power"] = _mqttPower;
root["mqtt_grid_voltage"] = _mqttVoltage;
root["online"] = isConnected();
root["power_factor"] = getPowerFactor();
root["power"] = getActivePower();
root["voltage"] = getVoltage();
}
#endif

private:
// metrics
volatile float _current = 0;
volatile float _energy = 0;
volatile float _energyReturned = 0;
volatile float _frequency = 0;
volatile float _power = 0;
volatile float _powerFactor = 0;
volatile float _voltage = 0;
JSY* _jsy;

// expiration
uint32_t _delay = 0;
volatile uint32_t _lastUpdate = 0;
// mqtt
volatile float _mqttPower = 0;
volatile float _mqttVoltage = 0;
volatile uint32_t _mqttPowerTime = 0;
volatile uint32_t _mqttVoltageTime = 0;
uint32_t _expiration = 0;
};
} // namespace Mycila
Loading

0 comments on commit f3bf1f2

Please sign in to comment.