Skip to content

Commit

Permalink
Keep float operations on 32-bits and using std namespace for math fun…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
mathieucarbou committed Jan 27, 2025
1 parent 529e4ab commit 138b4cc
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 96 deletions.
4 changes: 2 additions & 2 deletions include/yasolr_dashboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ namespace YaSolR {

void _outputDimmerSlider(dash::SliderCard<float, 2>& card, Mycila::RouterOutput& output) {
card.onChange([&card, &output, this](float value) {
output.setDimmerDutyCycle(value / 100);
card.setValue(output.getDimmerDutyCycle() * 100);
output.setDimmerDutyCycle(value / 100.0f);
card.setValue(output.getDimmerDutyCycle() * 100.0f);
dashboard.refresh(card);
dashboardUpdateTask.requestEarlyRun();
});
Expand Down
1 change: 1 addition & 0 deletions include/yasolr_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#define YASOLR_DIMMER_ROBODYN "Robodyn 24A / 40A"
#define YASOLR_DIMMER_TRIAC "Triac + ZCD"
#define YASOLR_DIMMER_ZC_SSR "Zero-crossing Solid State Relay"
#define YASOLR_DS18_SEARCH_MAX_RETRY 30
#define YASOLR_GRAPH_POINTS 60
#define YASOLR_HIDDEN_PWD "********"
#define YASOLR_MQTT_KEEPALIVE 60
Expand Down
4 changes: 2 additions & 2 deletions lib/DimmableLight/src/thyristor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ float Thyristor::getFrequency() {
if (semiPeriodLength == 0) {
return 0;
}
return 1000000 / 2 / (float)(semiPeriodLength);
return 500000.0f / (float)(semiPeriodLength);
}

uint16_t Thyristor::getSemiPeriod() {
Expand All @@ -685,7 +685,7 @@ void Thyristor::setFrequency(float frequency) {
return;
}

semiPeriodLength = 1000000 / 2 / frequency;
semiPeriodLength = 500000.0f / frequency;
}
void Thyristor::setSemiPeriod(uint16_t semiPeriod) {
semiPeriodLength = semiPeriod;
Expand Down
20 changes: 10 additions & 10 deletions lib/MycilaRouter/MycilaGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ bool Mycila::Grid::updatePower() {
update = _localMetrics.get().power;
}

if (isnan(update) && _power.neverUpdated()) {
if (std::isnan(update) && _power.neverUpdated()) {
return false;
}

// all became unavailable ?
if (isnan(update)) {
if (std::isnan(update)) {
_power.reset();
return true;
}
Expand Down Expand Up @@ -175,21 +175,21 @@ void Mycila::Grid::toJson(const JsonObject& root) const {
}

void Mycila::Grid::toJson(const JsonObject& dest, const Metrics& metrics) {
if (!isnanf(metrics.apparentPower))
if (!std::isnan(metrics.apparentPower))
dest["apparent_power"] = metrics.apparentPower;
if (!isnanf(metrics.current))
if (!std::isnan(metrics.current))
dest["current"] = metrics.current;
if (!isnanf(metrics.energy))
if (!std::isnan(metrics.energy))
dest["energy"] = metrics.energy;
if (!isnanf(metrics.energyReturned))
if (!std::isnan(metrics.energyReturned))
dest["energy_returned"] = metrics.energyReturned;
if (!isnanf(metrics.frequency))
if (!std::isnan(metrics.frequency))
dest["frequency"] = metrics.frequency;
if (!isnanf(metrics.power))
if (!std::isnan(metrics.power))
dest["power"] = metrics.power;
if (!isnanf(metrics.powerFactor))
if (!std::isnan(metrics.powerFactor))
dest["power_factor"] = metrics.powerFactor;
if (!isnanf(metrics.voltage))
if (!std::isnan(metrics.voltage))
dest["voltage"] = metrics.voltage;
}
#endif
20 changes: 10 additions & 10 deletions lib/MycilaRouter/MycilaRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ void Mycila::Router::toJson(const JsonObject& root, float voltage) const {
}

void Mycila::Router::toJson(const JsonObject& dest, const Metrics& metrics) {
if (!isnanf(metrics.apparentPower))
if (!std::isnan(metrics.apparentPower))
dest["apparent_power"] = metrics.apparentPower;
if (!isnanf(metrics.current))
if (!std::isnan(metrics.current))
dest["current"] = metrics.current;
if (!isnanf(metrics.energy))
if (!std::isnan(metrics.energy))
dest["energy"] = metrics.energy;
if (!isnanf(metrics.power))
if (!std::isnan(metrics.power))
dest["power"] = metrics.power;
if (!isnanf(metrics.powerFactor))
if (!std::isnan(metrics.powerFactor))
dest["power_factor"] = metrics.powerFactor;
if (!isnanf(metrics.resistance))
if (!std::isnan(metrics.resistance))
dest["resistance"] = metrics.resistance;
if (!isnanf(metrics.thdi))
if (!std::isnan(metrics.thdi))
dest["thdi"] = metrics.thdi;
if (!isnanf(metrics.voltage))
if (!std::isnan(metrics.voltage))
dest["voltage"] = metrics.voltage;
}
#endif
Expand All @@ -84,7 +84,7 @@ void Mycila::Router::getRouterMetrics(Metrics& metrics, float voltage) const {

metrics.powerFactor = metrics.apparentPower == 0 ? NAN : metrics.power / metrics.apparentPower;
metrics.resistance = metrics.current == 0 ? NAN : metrics.power / (metrics.current * metrics.current);
metrics.thdi = metrics.powerFactor == 0 ? NAN : sqrt(1 / (metrics.powerFactor * metrics.powerFactor) - 1);
metrics.thdi = metrics.powerFactor == 0 ? NAN : std::sqrt(1.0f / (metrics.powerFactor * metrics.powerFactor) - 1.0f);

if (_localMetrics.isPresent()) {
metrics.energy = _localMetrics.get().energy;
Expand Down Expand Up @@ -118,7 +118,7 @@ void Mycila::Router::getRouterMeasurements(Metrics& metrics) const {

metrics.powerFactor = metrics.apparentPower == 0 ? NAN : metrics.power / metrics.apparentPower;
metrics.resistance = metrics.current == 0 ? NAN : metrics.power / (metrics.current * metrics.current);
metrics.thdi = metrics.powerFactor == 0 ? NAN : sqrt(1 / (metrics.powerFactor * metrics.powerFactor) - 1);
metrics.thdi = metrics.powerFactor == 0 ? NAN : std::sqrt(1.0f / (metrics.powerFactor * metrics.powerFactor) - 1.0f);
}
}

Expand Down
26 changes: 13 additions & 13 deletions lib/MycilaRouter/MycilaRouterOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void Mycila::RouterOutput::toJson(const JsonObject& root, float gridVoltage) con
root["enabled"] = isDimmerOnline();
root["state"] = getStateName();
float t = _temperature.orElse(NAN);
if (!isnanf(t)) {
if (!std::isnan(t)) {
root["temperature"] = t;
}

Expand All @@ -83,23 +83,23 @@ void Mycila::RouterOutput::toJson(const JsonObject& root, float gridVoltage) con
}

void Mycila::RouterOutput::toJson(const JsonObject& dest, const Metrics& metrics) {
if (!isnanf(metrics.apparentPower))
if (!std::isnan(metrics.apparentPower))
dest["apparent_power"] = metrics.apparentPower;
if (!isnanf(metrics.current))
if (!std::isnan(metrics.current))
dest["current"] = metrics.current;
if (!isnanf(metrics.dimmedVoltage))
if (!std::isnan(metrics.dimmedVoltage))
dest["energy"] = metrics.energy;
if (!isnanf(metrics.power))
if (!std::isnan(metrics.power))
dest["power"] = metrics.power;
if (!isnanf(metrics.powerFactor))
if (!std::isnan(metrics.powerFactor))
dest["power_factor"] = metrics.powerFactor;
if (!isnanf(metrics.resistance))
if (!std::isnan(metrics.resistance))
dest["resistance"] = metrics.resistance;
if (!isnanf(metrics.thdi))
if (!std::isnan(metrics.thdi))
dest["thdi"] = metrics.thdi;
if (!isnanf(metrics.voltage))
if (!std::isnan(metrics.voltage))
dest["voltage"] = metrics.voltage;
if (!isnanf(metrics.dimmedVoltage))
if (!std::isnan(metrics.dimmedVoltage))
dest["voltage_dimmed"] = metrics.dimmedVoltage;
}
#endif
Expand Down Expand Up @@ -162,7 +162,7 @@ float Mycila::RouterOutput::autoDivert(float gridVoltage, float availablePowerTo
return 0;
}

if (isnan(config.calibratedResistance) || config.calibratedResistance <= 0) {
if (std::isnan(config.calibratedResistance) || config.calibratedResistance <= 0) {
return 0;
}

Expand Down Expand Up @@ -313,11 +313,11 @@ void Mycila::RouterOutput::getOutputMetrics(Metrics& metrics, float gridVoltage)
const float dutyCycle = getDimmerDutyCycleLive();
const float maxPower = metrics.resistance == 0 ? 0 : metrics.voltage * metrics.voltage / metrics.resistance;
metrics.power = dutyCycle * maxPower;
metrics.powerFactor = sqrt(dutyCycle);
metrics.powerFactor = std::sqrt(dutyCycle);
metrics.dimmedVoltage = metrics.powerFactor * metrics.voltage;
metrics.current = metrics.resistance == 0 ? 0 : metrics.dimmedVoltage / metrics.resistance;
metrics.apparentPower = metrics.current * metrics.voltage;
metrics.thdi = dutyCycle == 0 ? 0 : sqrt(1 / dutyCycle - 1);
metrics.thdi = dutyCycle == 0 ? 0 : std::sqrt(1 / dutyCycle - 1);
}

bool Mycila::RouterOutput::getOutputMeasurements(Metrics& metrics) const {
Expand Down
2 changes: 1 addition & 1 deletion lib/MycilaRouter/MycilaRouterRelay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern Mycila::Logger logger;
// in percentage
// => 50W for a tri-phase 3000W resistance (1000W per phase)
// => 35W for a tri-phase 2100W resistance (700W per phase)
#define MYCILA_RELAY_TOLERANCE 0.05
#define MYCILA_RELAY_TOLERANCE 0.05f
#endif

#define TAG "RELAY"
Expand Down
8 changes: 4 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ lib_deps =
ESP32Async/AsyncTCP @ 3.3.2
ESP32Async/ESPAsyncWebServer @ 3.6.2
mathieucarbou/MycilaConfig @ 7.0.3
mathieucarbou/MycilaDS18 @ 4.1.2
mathieucarbou/MycilaDS18 @ 4.1.4
mathieucarbou/MycilaESPConnect @ 7.1.1
mathieucarbou/MycilaEasyDisplay @ 3.1.0
mathieucarbou/MycilaHADiscovery @ 6.0.1
mathieucarbou/MycilaJSY @ 13.1.0
mathieucarbou/MycilaJSY @ 13.1.1
mathieucarbou/MycilaLogger @ 3.2.1
mathieucarbou/MycilaMQTT @ 6.0.5
mathieucarbou/MycilaNTP @ 6.0.5
mathieucarbou/MycilaPulseAnalyzer @ 3.0.0
mathieucarbou/MycilaPZEM004Tv3 @ 5.0.0
mathieucarbou/MycilaPZEM004Tv3 @ 5.0.1
mathieucarbou/MycilaRelay @ 4.0.1
mathieucarbou/MycilaSystem @ 4.0.0
mathieucarbou/MycilaSystem @ 4.0.1
mathieucarbou/MycilaTaskManager @ 3.2.1
mathieucarbou/MycilaTaskMonitor @ 4.0.0
mathieucarbou/MycilaTrafficLight @ 2.0.2
Expand Down
12 changes: 6 additions & 6 deletions src/yasolr_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ void yasolr_init_config() {

} else if (key == KEY_OUTPUT1_DIMMER_MIN) {
if (output1)
output1->setDimmerDutyCycleMin(config.getFloat(KEY_OUTPUT1_DIMMER_MIN) / 100);
output1->setDimmerDutyCycleMin(config.getFloat(KEY_OUTPUT1_DIMMER_MIN) / 100.0f);

} else if (key == KEY_OUTPUT1_DIMMER_MAX) {
if (output1)
output1->setDimmerDutyCycleMax(config.getFloat(KEY_OUTPUT1_DIMMER_MAX) / 100);
output1->setDimmerDutyCycleMax(config.getFloat(KEY_OUTPUT1_DIMMER_MAX) / 100.0f);

} else if (key == KEY_OUTPUT1_DIMMER_LIMIT) {
if (output1)
output1->setDimmerDutyCycleLimit(config.getFloat(KEY_OUTPUT1_DIMMER_LIMIT) / 100);
output1->setDimmerDutyCycleLimit(config.getFloat(KEY_OUTPUT1_DIMMER_LIMIT) / 100.0f);

} else if (key == KEY_OUTPUT1_DIMMER_TEMP_LIMITER) {
if (output1)
Expand Down Expand Up @@ -205,15 +205,15 @@ void yasolr_init_config() {

} else if (key == KEY_OUTPUT2_DIMMER_MIN) {
if (output2)
output2->setDimmerDutyCycleMin(config.getFloat(KEY_OUTPUT2_DIMMER_MIN) / 100);
output2->setDimmerDutyCycleMin(config.getFloat(KEY_OUTPUT2_DIMMER_MIN) / 100.0f);

} else if (key == KEY_OUTPUT2_DIMMER_MAX) {
if (output2)
output2->setDimmerDutyCycleMax(config.getFloat(KEY_OUTPUT2_DIMMER_MAX) / 100);
output2->setDimmerDutyCycleMax(config.getFloat(KEY_OUTPUT2_DIMMER_MAX) / 100.0f);

} else if (key == KEY_OUTPUT2_DIMMER_LIMIT) {
if (output2)
output2->setDimmerDutyCycleLimit(config.getFloat(KEY_OUTPUT2_DIMMER_LIMIT) / 100);
output2->setDimmerDutyCycleLimit(config.getFloat(KEY_OUTPUT2_DIMMER_LIMIT) / 100.0f);

} else if (key == KEY_OUTPUT2_DIMMER_TEMP_LIMITER) {
if (output2)
Expand Down
34 changes: 17 additions & 17 deletions src/yasolr_dashboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ void YaSolR::Website::updateCards() {
_routerPower.setValue(routerMetrics.power);
_routerApparentPower.setValue(routerMetrics.apparentPower);
_routerPowerFactor.setValue(routerMetrics.powerFactor);
_routerTHDi.setValue(routerMetrics.thdi * 100);
_routerTHDi.setValue(routerMetrics.thdi * 100.0f);
_routerVoltage.setValue(gridMetrics.voltage);
_routerCurrent.setValue(routerMetrics.current);
_routerResistance.setValue(routerMetrics.resistance);
Expand Down Expand Up @@ -1438,7 +1438,7 @@ void YaSolR::Website::updateCards() {
break;
}
_output1DS18State.setValue(output1->temperature().orElse(NAN));
_output1DimmerSlider.setValue(output1->getDimmerDutyCycle() * 100);
_output1DimmerSlider.setValue(output1->getDimmerDutyCycle() * 100.0f);
_output1Bypass.setValue(output1->isBypassOn());
}

Expand All @@ -1460,7 +1460,7 @@ void YaSolR::Website::updateCards() {
break;
}
_output2DS18State.setValue(output2->temperature().orElse(NAN));
_output2DimmerSlider.setValue(output2->getDimmerDutyCycle() * 100);
_output2DimmerSlider.setValue(output2->getDimmerDutyCycle() * 100.0f);
_output2Bypass.setValue(output2->isBypassOn());
}

Expand All @@ -1476,11 +1476,11 @@ void YaSolR::Website::updateCards() {
Mycila::RouterOutput::Metrics output1Measurements;
output1->getOutputMeasurements(output1Measurements);

_output1DimmerSliderRO.setValue(output1->getDimmerDutyCycleLive() * 100);
_output1DimmerSliderRO.setValue(output1->getDimmerDutyCycleLive() * 100.0f);
_output1Power.setValue(output1Measurements.power);
_output1ApparentPower.setValue(output1Measurements.apparentPower);
_output1PowerFactor.setValue(output1Measurements.powerFactor);
_output1THDi.setValue(output1Measurements.thdi * 100);
_output1THDi.setValue(output1Measurements.thdi * 100.0f);
_output1Voltage.setValue(output1Measurements.dimmedVoltage);
_output1Current.setValue(output1Measurements.current);
_output1Resistance.setValue(output1Measurements.resistance);
Expand All @@ -1494,11 +1494,11 @@ void YaSolR::Website::updateCards() {
Mycila::RouterOutput::Metrics output2Measurements;
output2->getOutputMeasurements(output2Measurements);

_output2DimmerSliderRO.setValue(output2->getDimmerDutyCycleLive() * 100);
_output2DimmerSliderRO.setValue(output2->getDimmerDutyCycleLive() * 100.0f);
_output2Power.setValue(output2Measurements.power);
_output2ApparentPower.setValue(output2Measurements.apparentPower);
_output2PowerFactor.setValue(output2Measurements.powerFactor);
_output2THDi.setValue(output2Measurements.thdi * 100);
_output2THDi.setValue(output2Measurements.thdi * 100.0f);
_output2Voltage.setValue(output2Measurements.dimmedVoltage);
_output2Current.setValue(output2Measurements.current);
_output2Resistance.setValue(output2Measurements.resistance);
Expand Down Expand Up @@ -1541,9 +1541,9 @@ void YaSolR::Website::updateCharts() {
memmove(&_routerTHDiHistoryY[0], &_routerTHDiHistoryY[1], sizeof(_routerTHDiHistoryY) - sizeof(*_routerTHDiHistoryY));

// set new value
_gridPowerHistoryY[YASOLR_GRAPH_POINTS - 1] = round(grid.getPower().orElse(0));
_routedPowerHistoryY[YASOLR_GRAPH_POINTS - 1] = round(routerMetrics.power);
_routerTHDiHistoryY[YASOLR_GRAPH_POINTS - 1] = isnan(routerMetrics.thdi) ? 0 : round(routerMetrics.thdi * 100);
_gridPowerHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(grid.getPower().orElse(0));
_routedPowerHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(routerMetrics.power);
_routerTHDiHistoryY[YASOLR_GRAPH_POINTS - 1] = std::isnan(routerMetrics.thdi) ? 0 : std::round(routerMetrics.thdi * 100.0f);

// update charts
_gridPowerHistory.setY(_gridPowerHistoryY, YASOLR_GRAPH_POINTS);
Expand All @@ -1563,13 +1563,13 @@ void YaSolR::Website::updatePIDCharts() {
memmove(&_pidDTermHistoryY[0], &_pidDTermHistoryY[1], sizeof(_pidDTermHistoryY) - sizeof(*_pidDTermHistoryY));

// set new values
_pidInputHistoryY[YASOLR_GRAPH_POINTS - 1] = round(pidController.getInput());
_pidOutputHistoryY[YASOLR_GRAPH_POINTS - 1] = round(pidController.getOutput());
_pidErrorHistoryY[YASOLR_GRAPH_POINTS - 1] = round(pidController.getError());
_pidSumHistoryY[YASOLR_GRAPH_POINTS - 1] = round(pidController.getSum());
_pidPTermHistoryY[YASOLR_GRAPH_POINTS - 1] = round(pidController.getPTerm());
_pidITermHistoryY[YASOLR_GRAPH_POINTS - 1] = round(pidController.getITerm());
_pidDTermHistoryY[YASOLR_GRAPH_POINTS - 1] = round(pidController.getDTerm());
_pidInputHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(pidController.getInput());
_pidOutputHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(pidController.getOutput());
_pidErrorHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(pidController.getError());
_pidSumHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(pidController.getSum());
_pidPTermHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(pidController.getPTerm());
_pidITermHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(pidController.getITerm());
_pidDTermHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(pidController.getDTerm());

// update charts
_pidInputHistory.setY(_pidInputHistoryY, YASOLR_GRAPH_POINTS);
Expand Down
8 changes: 4 additions & 4 deletions src/yasolr_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ void yasolr_init_display() {
break;
}
case 5: {
display->home.printf("Grid P: %9d W", static_cast<int>(round(grid.getPower().orElse(0))));
display->home.printf("Grid P: %9d W", static_cast<int>(std::round(grid.getPower().orElse(0))));
wrote = true;
break;
}
case 6: {
Mycila::Router::Metrics routerMetrics;
router.getRouterMeasurements(routerMetrics);
display->home.printf("Router P: %9d W", static_cast<int>(round(routerMetrics.power)));
display->home.printf("Router P: %9d W", static_cast<int>(std::round(routerMetrics.power)));
wrote = true;
break;
}
Expand All @@ -143,7 +143,7 @@ void yasolr_init_display() {
}
case 9: {
if (output1 && output1->isDimmerEnabled()) {
display->home.printf("Output 1 Duty: %4d %%", static_cast<int>(round(output1->getDimmerDutyCycleLive() * 100)));
display->home.printf("Output 1 Duty: %4d %%", static_cast<int>(std::round(output1->getDimmerDutyCycleLive() * 100.0f)));
wrote = true;
}
break;
Expand All @@ -166,7 +166,7 @@ void yasolr_init_display() {
}
case 12: {
if (output2 && output2->isDimmerEnabled()) {
display->home.printf("Output 2 Duty: %4d %%", static_cast<int>(round(output2->getDimmerDutyCycleLive() * 100)));
display->home.printf("Output 2 Duty: %4d %%", static_cast<int>(std::round(output2->getDimmerDutyCycleLive() * 100.0f)));
wrote = true;
}
break;
Expand Down
Loading

0 comments on commit 138b4cc

Please sign in to comment.