Skip to content

Commit

Permalink
detectGridFrequency()
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Sep 20, 2024
1 parent 8fa99fc commit a2fb1e7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 56 deletions.
2 changes: 2 additions & 0 deletions include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,5 @@ extern Mycila::Task initRestApiTask;
extern Mycila::Task initWebTask;

extern Mycila::CircularBuffer<float, 50> udpMessageRateBuffer;

extern float detectGridFrequency();
33 changes: 33 additions & 0 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include <YaSolR.h>

float detectGridFrequency() {
// first get res from the measurement tools
Mycila::Grid::Metrics gridMetrics;
grid.getMeasurements(gridMetrics);

float frequency = round(gridMetrics.frequency);
if (frequency)
return frequency;

frequency = round(pzemO1.getFrequency());
if (frequency)
return frequency;

frequency = round(pzemO2.getFrequency());
if (frequency)
return frequency;

frequency = config.get(KEY_GRID_FREQUENCY).toInt();
if (frequency)
return frequency;

frequency = round(pulseAnalyzer.getFrequency());
if (frequency)
return frequency / 2;

return 0;
}
7 changes: 1 addition & 6 deletions src/Website.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,11 +773,6 @@ void YaSolR::WebsiteClass::updateCards() {
Mycila::RouterOutput::Metrics output2Measurements;
output2.getMeasurements(output2Measurements);

float freq = gridMetrics.frequency;
if (freq == 0 && zcd.isEnabled()) {
freq = 1000000.0f / 2 / zcd.getSemiPeriod();
}

// stats
Mycila::System::Memory memory;
Mycila::System::getMemory(memory);
Expand All @@ -788,7 +783,7 @@ void YaSolR::WebsiteClass::updateCards() {
_deviceHeapUsed.set((String(memory.used) + " bytes").c_str());
_gridEnergy.set((String(gridMetrics.energy, 3) + " kWh").c_str());
_gridEnergyReturned.set((String(gridMetrics.energyReturned, 3) + " kWh").c_str());
_gridFrequency.set((String(freq) + " Hz").c_str());
_gridFrequency.set((String(detectGridFrequency(), 0) + " Hz").c_str());
_networkAPIP.set(espConnect.getIPAddress(Mycila::ESPConnect::Mode::AP).toString().c_str());
_networkEthIP.set(espConnect.getIPAddress(Mycila::ESPConnect::Mode::ETH).toString().c_str());
_networkInterface.set(mode == Mycila::ESPConnect::Mode::AP ? "AP" : (mode == Mycila::ESPConnect::Mode::STA ? "WiFi" : (mode == Mycila::ESPConnect::Mode::ETH ? "Ethernet" : "")));
Expand Down
63 changes: 13 additions & 50 deletions src/tasks/ZCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,11 @@
*/
#include <YaSolR.h>

static uint32_t findBestSemiPeriod() {
float frequency = 0;
uint32_t semiPeriod = 0;

// first get res from the measurement tools
Mycila::Grid::Metrics gridMetrics;
grid.getMeasurements(gridMetrics);
if (!frequency)
frequency = round(gridMetrics.frequency);
if (!frequency)
frequency = round(pzemO1.getFrequency());
if (!frequency)
frequency = round(pzemO2.getFrequency());
if (frequency) {
semiPeriod = 1000000 / 2 / frequency;
logger.info(TAG, "Semi-period detected by a measurement device: %" PRIu32 " us", semiPeriod);
return semiPeriod;
}

// otherwise try get res from the configuration
frequency = config.get(KEY_GRID_FREQUENCY).toInt();
if (frequency) {
semiPeriod = 1000000 / 2 / frequency;
logger.info(TAG, "Semi-period derived from configured frequency: %" PRIu32 " us", semiPeriod);
return semiPeriod;
}

// otherwise use auto-detection
frequency = round(pulseAnalyzer.getFrequency());
if (frequency) {
semiPeriod = 1000000 / frequency;
logger.info(TAG, "Semi-period detected by pulse analyzer: %" PRIu32 " us", semiPeriod);
return semiPeriod;
}

return 0;
}

Mycila::Task zcdTask("ZCD", [](void* params) {
// if the user is asking to disable ZCD, and it is starting or running, we stop it
if (!config.getBool(KEY_ENABLE_ZCD)) {
if (zcd.isEnabled() || pulseAnalyzer.isEnabled()) {
logger.warn(TAG, "ZCD disabled, stopping ZCD and PulseAnalyzer");
logger.warn(TAG, "ZCD disabled, stopping ZCD and Pulse Analyzer");
zcd.end();
pulseAnalyzer.end();
}
Expand All @@ -62,27 +24,28 @@ Mycila::Task zcdTask("ZCD", [](void* params) {
// => ZCD disabled.

// try to find the semiPeriod
uint32_t semiPeriod = findBestSemiPeriod();
const float frequency = detectGridFrequency();

if (frequency) {
const uint32_t semiPeriod = 1000000 / 2 / frequency;
logger.info(TAG, "Semi-period: %" PRIu32 " us, Grid Frequency: %.2f Hz", semiPeriod, frequency);

// if we have a semiPeriod, we start ZCD
if (semiPeriod) {
// if we have a semiPeriod, we start ZCD
pulseAnalyzer.end();
zcd.begin(config.get(KEY_PIN_ZCD).toInt(), semiPeriod);
if (zcd.isEnabled()) {
logger.info(TAG, "ZCD started");
dimmer1Task.resume();
dimmer2Task.resume();
}
return;
}

// => ZCD switch turned + ZCD disabled + no semiPeriod => we need start analyser or wait for results

if (!pulseAnalyzer.isEnabled()) {
} else if (!pulseAnalyzer.isEnabled()) {
// => ZCD switch turned + ZCD disabled + no semiPeriod + analyzer off => we need start analyser
logger.info(TAG, "Starting PulseAnalyzer");
pulseAnalyzer.begin(config.get(KEY_PIN_ZCD).toInt());
return;
}

logger.warn(TAG, "Waiting for ZCD pulse analysis to complete...");
} else {
// => ZCD switch turned + ZCD disabled + no semiPeriod + analyzer running => we need to wait
logger.warn(TAG, "Waiting for ZCD pulse analysis to complete...");
}
});

0 comments on commit a2fb1e7

Please sign in to comment.