Skip to content

Commit

Permalink
Fixed NaN + resistance + TDHi
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Feb 24, 2024
1 parent a3d6e6f commit 0466ef8
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/MycilaPZEM004Tv3.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,22 @@ namespace Mycila {
// note :this is the active power, not the apparent power
float getPower() const { return _power; }
float getPowerFactor() const { return _powerFactor; }
float getApparentPower() const { return _power / _powerFactor; }
float getApparentPower() const { return _powerFactor == 0 ? 0 : _power / _powerFactor; }
float getVoltage() const { return _voltage; }

float getResistance() const {
const float c = _current;
return c == 0 ? 0 : _voltage * _powerFactor / c;
}

float getTHDi(float phi) const {
const float pf = _powerFactor;
if (pf == 0)
return 0;
// https://fr.electrical-installation.org/frwiki/Indicateur_de_distorsion_harmonique_:_facteur_de_puissance
return sqrt(pow(cos(phi), 2) / pow(pf, 2) - 1);
}

// get the uptime in milliseconds of the last successful read
uint32_t getTime() const { return _lastReadSuccess; }

Expand Down

0 comments on commit 0466ef8

Please sign in to comment.