beta current transformer card

This commit is contained in:
Siwat Sirichai 2024-02-10 16:20:36 +07:00
parent 8232bce927
commit 8244ad1c8a
2 changed files with 139 additions and 0 deletions

View File

@ -0,0 +1,93 @@
#include <CurrentTransformer.hpp>
CurrentTransformer::CurrentTransformer(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function<float(uint16_t)> adcToCurrent, uint32_t conversionInterval)
{
this->analogCard = analogCard;
this->pin = pin;
this->voltage = voltage;
this->adcToCurrent = adcToCurrent;
}
void CurrentTransformer::bindFRAM(FRAM *fram, uint8_t framAddress)
{
this->fram = fram;
this->framAddress = framAddress;
}
void CurrentTransformer::begin()
{
this->beginConversion();
}
void CurrentTransformer::loop()
{
if (this->lastConversionTime == 0) {
this->lastConversionTime = millis();
}
static uint32_t lastConversionLoopTime = 0;
if (millis() - lastConversionLoopTime > this->conversionInterval) {
this->beginConversion();
lastConversionLoopTime = millis();
}
}
void CurrentTransformer::beginConversion()
{
uint16_t adcValue = this->analogCard->analogRead(this->pin);
this->current = this->adcToCurrent(adcValue);
this->setEnergy(this->energy + this->current * *this->voltage * (millis() - this->lastConversionTime) / 3600000); // in Wh
this->lastConversionTime = millis();
for (auto const& callback : this->callbacks) {
callback.second(this->current, this->energy);
}
}
void CurrentTransformer::setEnergy(float energy)
{
this->energy = energy;
if (this->autoSave) {
this->saveEnergy();
}
}
void CurrentTransformer::resetEnergy()
{
this->setEnergy(0);
}
float CurrentTransformer::getCurrent()
{
return this->current;
}
long double CurrentTransformer::getEnergy()
{
return this->energy;
}
uint8_t CurrentTransformer::registerCallback(std::function<void(float, long double)> callback) {
this->callbacks[this->handler_count] = callback;
return this->handler_count++;
}
void CurrentTransformer::unregisterCallback(uint8_t handler) {
this->callbacks.erase(handler);
}
void CurrentTransformer::saveEnergy(){
this->fram->write(this->framAddress, (uint8_t*)&this->energy, 16);
}
void CurrentTransformer::loadEnergy(){
this->fram->read(this->framAddress, (uint8_t*)&this->energy, 16);
}
void CurrentTransformer::setEnergyAutoSave(bool autoSave){
this->autoSave = autoSave;
}
float CurrentTransformer::getVoltage(){
return *this->voltage;
}
float CurrentTransformer::getPower(){
return this->current * *this->voltage;
}

View File

@ -0,0 +1,46 @@
#pragma once
#include <AnalogCard.hpp>
#include <FRAM.h>
#include <map>
/**
* @brief The CurrentTransformer class is a class for reading the current and energy from a current transformer that's connected to the AnalogCard.
* Also supports storing energy to FRAM.
*/
class CurrentTransformer
{
public:
CurrentTransformer(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function<float(uint16_t)> adcToCurrent, uint32_t conversionInterval);
void bindFRAM(FRAM *fram, uint8_t framAddress); // Takes 16 bytes of FRAM (long double energy)
void begin();
void loop();
void beginConversion();
void setEnergy(float energy);
void resetEnergy();
float getCurrent();
long double getEnergy();
float getPower();
void saveEnergy();
void loadEnergy();
void setEnergyAutoSave(bool autoSave);
float getVoltage();
uint8_t registerCallback(std::function<void(float, long double)> callback);
void unregisterCallback(uint8_t handler);
private:
AnalogCard* analogCard;
uint8_t pin;
uint8_t framAddress;
FRAM *fram;
uint32_t conversionInterval;
bool autoSave;
float calibration;
long double energy;
float current;
float *voltage;
uint32_t lastConversionTime;
std::function<float(uint16_t)> adcToCurrent; // std::function that convert adc value to current in amps
uint8_t handler_count = 0;
std::map<uint8_t,std::function<void(float, long double)>> callbacks;
};