-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2632a0c
commit 1580343
Showing
28 changed files
with
234 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.