Skip to content

Commit

Permalink
Grid Source refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Jun 9, 2024
1 parent 2632a0c commit 1580343
Show file tree
Hide file tree
Showing 28 changed files with 234 additions and 245 deletions.
1 change: 0 additions & 1 deletion data/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ <h1>YaSolR Configuration</h1>
disp_angle: ["Display Rotation", "select", "0,90,180,270"],
disp_speed: ["Display Speed (s)", "select", "1,2,3,4,5,6,7,8,9,10"],
grid_freq: ["Grid frequency (default)", "select", "50,60"],
grid_volt: ["Grid voltage (default)", "select", "230,110"],

MQTT: "TITLE",
mqtt_server: ["Server", "string"],
Expand Down
1 change: 1 addition & 0 deletions include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ extern Mycila::DS18 ds18O1;
extern Mycila::DS18 ds18O2;
extern Mycila::DS18 ds18Sys;
extern Mycila::EasyDisplay display;
extern Mycila::Grid grid;
extern Mycila::HADiscovery haDiscovery;
extern Mycila::JSY jsy;
extern Mycila::Logger logger;
Expand Down
1 change: 0 additions & 1 deletion include/YaSolRDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
#define KEY_DISPLAY_TYPE "disp_type"
#define KEY_GRID_FREQUENCY "grid_freq"
#define KEY_GRID_POWER_MQTT_TOPIC "grid_pow_mqtt"
#define KEY_GRID_VOLTAGE "grid_volt"
#define KEY_GRID_VOLTAGE_MQTT_TOPIC "grid_volt_mqtt"
#define KEY_HA_DISCOVERY_TOPIC "ha_disco_topic"
#define KEY_MQTT_PASSWORD "mqtt_pwd"
Expand Down
5 changes: 2 additions & 3 deletions include/YaSolRWebsite.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ namespace YaSolR {
Card _mqttServerCert = Card(&dashboard, FILE_UPLOAD_CARD, "Server Certificate", ".pem,crt,der");
Card _mqttPublishInterval = Card(&dashboard, SLIDER_CARD, "Publish Interval", "s", 5, 30, 1);
Card _mqttTopic = Card(&dashboard, TEXT_INPUT_CARD, "Base Topic");
Card _mqttGridVoltage = Card(&dashboard, TEXT_INPUT_CARD, "Grid Voltage from MQTT Topic");
Card _mqttGridPower = Card(&dashboard, TEXT_INPUT_CARD, "Grid Power from MQTT Topic");
Card _haDiscovery = Card(&dashboard, BUTTON_CARD, "Home Assistant Integration");
Card _haDiscoveryTopic = Card(&dashboard, TEXT_INPUT_CARD, "Home Assistant Discovery Topic");
Card _mqttGridVoltage = Card(&dashboard, TEXT_INPUT_CARD, "Grid Voltage from MQTT Topic");
Card _mqttGridPower = Card(&dashboard, TEXT_INPUT_CARD, "Grid Power from MQTT Topic");

Tab _pinConfigTab = Tab(&dashboard, "\u21C6 GPIO");
Card _pinDimmerO1 = Card(&dashboard, TEXT_INPUT_CARD, "Dimmer for Output 1");
Expand Down Expand Up @@ -214,7 +214,6 @@ namespace YaSolR {

Tab _hardwareConfigTab = Tab(&dashboard, "\u2699 Hardware Config");
Card _gridFreq = Card(&dashboard, DROPDOWN_CARD, "Nominal Grid Frequency");
Card _gridVolt = Card(&dashboard, DROPDOWN_CARD, "Nominal Grid Voltage");
Card _displayType = Card(&dashboard, DROPDOWN_CARD, "Display Type");
Card _displaySpeed = Card(&dashboard, SLIDER_CARD, "Display Speed", "s", 1, 10, 1);
Card _displayRotation = Card(&dashboard, DROPDOWN_CARD, "Display Rotation");
Expand Down
10 changes: 0 additions & 10 deletions lib/MycilaGrid/LICENSE

This file was deleted.

112 changes: 0 additions & 112 deletions lib/MycilaGrid/MycilaGrid.cpp

This file was deleted.

56 changes: 0 additions & 56 deletions lib/MycilaGrid/MycilaGrid.h

This file was deleted.

5 changes: 0 additions & 5 deletions lib/MycilaGrid/library.properties

This file was deleted.

60 changes: 60 additions & 0 deletions lib/MycilaRouter/MycilaEnergySource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: MIT
/*
* Copyright (C) 2023-2024 Mathieu Carbou and others
*/
#pragma once

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

namespace Mycila {
class EnergySource {
public:
static const int X = 10;

EnergySource(){};
~EnergySource(){};

// Electricity connected
virtual bool isConnected() const = 0;

// Current frequency in Hz
virtual float getFrequency() const = 0;

// Active energy in kWh
virtual float getEnergy() const = 0;
// Active energy returned in kWh
virtual float getEnergyReturned() const = 0;

// Current voltage in V
virtual float getVoltage() const = 0;
// Current used in A
virtual float getCurrent() const = 0;

// Active power in W (can be negative if energy is returned)
virtual float getActivePower() const = 0;
// Apparent power in VA
virtual float getApparentPower() const = 0;
// Power factor (0 - 1)
virtual float getPowerFactor() const = 0;

// Total harmonic distortion of current, if available
virtual float getTHDi() const = 0;

#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["online"] = isConnected();
root["power_factor"] = getPowerFactor();
root["power"] = getActivePower();
root["thdi"] = getTHDi();
root["voltage"] = getVoltage();
}
#endif
};
} // namespace Mycila
8 changes: 8 additions & 0 deletions lib/MycilaRouter/MycilaGrid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
/*
* Copyright (C) 2023-2024 Mathieu Carbou and others
*/
#include <MycilaGrid.h>
#include <thyristor.h>

float Mycila::Grid::getFrequency() const { return _frequency > 0 ? _frequency : Thyristor::getDetectedFrequency(); }
81 changes: 81 additions & 0 deletions lib/MycilaRouter/MycilaGrid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: MIT
/*
* Copyright (C) 2023-2024 Mathieu Carbou and others
*/
#pragma once

#include <MycilaEnergySource.h>

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

void updateVoltage(float voltage) {
_voltage = voltage;
_lastUpdate = millis();
}
void updatePower(float power) {
_power = power;
_lastUpdate = 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();
}

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;
}
}

// 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;
}

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;

// expiration
uint32_t _delay = 0;
volatile uint32_t _lastUpdate = 0;
};
} // namespace Mycila
Loading

0 comments on commit 1580343

Please sign in to comment.