Calibrate distance calculation with temperature

This commit is contained in:
Ard Kuijpers
2020-03-08 22:49:22 +01:00
parent aa9fa938c4
commit d02b9a63f4
5 changed files with 28 additions and 9 deletions

View File

@@ -61,6 +61,7 @@ void PingNode::send()
setProperty(cStatusTopic).send(valid ? "ok" : "error");
if (valid) {
setProperty(cDistanceTopic).send(String(_distance));
setProperty(cPingTopic).send(String(_ping_us));
setProperty(cChangedTopic).send(changed ? "true": "false");
}
}
@@ -82,7 +83,7 @@ void PingNode::loop()
{
float ping_us = sonar->ping_median();
// Calculating the distance @ 10 °C from d = t_ping /2 * c => t_ping /2 * 337 [m/s] => t_ping_us / 1e-6 * 1/2 * 337
float newDistance = ping_us*0.0001685;
float newDistance = ping_us*_microseconds2meter;
fixRange(&newDistance, cMinDistance, cMaxDistance);
if (newDistance > 0) {
_ping_us = ping_us;
@@ -118,7 +119,11 @@ void PingNode::setup()
void PingNode::setMicrosecondsToMetersFactor(float temperatureCelcius)
{
//float soundSpeed = 337.0; // @ 10°C
float soundSpeed = 331.4 * 0.6*temperatureCelcius;
float soundSpeed = 331.4 + 0.6*temperatureCelcius;
printCaption();
Homie.getLogger() << cIndent
<< "SpeedOfSound: " << soundSpeed << " " << cUnitMetersPerSecond
<< " at " << temperatureCelcius << " " << cUnitDegrees << endl;
// Calculating the distance from d = t_ping /2 * c => t_ping /2 * 337 [m/s] => t_ping_us / 1e-6 * 1/2 * 337
_microseconds2meter = 0.5e-6 * soundSpeed;
}

View File

@@ -40,7 +40,7 @@ private:
NewPing* sonar;
float _distance = NAN;
int _ping_us = NAN;
int _ping_us = 0;
float _lastDistance = 0;
ChangeHandler _changeHandler = [](){};

View File

@@ -36,9 +36,15 @@ HomieInternals::Uptime relayUptime;
bool RelayNode::handleOnOff(const String &value)
{
if (value == "true" || value == "false")
if (value == "true" || value == "false" || value == "toggle")
{
setRelay(value == "true");
if (value == "toggle")
{
bool current = getRelayState();
setRelay(!current);
}
else
setRelay(value == "true");
return true;
}
else

View File

@@ -5,6 +5,7 @@
#define cUnitPercent "%"
#define cUnitVolt "V"
#define cUnitMeter "m"
#define cUnitMetersPerSecond "m/s"
#define cUnitMicrosecond "μs"
// Topics