Skip to content

Commit

Permalink
Merge pull request #29 from duncan-brown/calibration
Browse files Browse the repository at this point in the history
Add ability to calibrate a tilt and apply temperature correction
  • Loading branch information
thorrak authored Jun 7, 2020
2 parents 1db4077 + b9c835d commit dea0309
Show file tree
Hide file tree
Showing 10 changed files with 1,014 additions and 6 deletions.
5 changes: 5 additions & 0 deletions data/about.htm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<li class="nav-item">
<a class="nav-link" href="/settings/">Settings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/calibration/">Calibration</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="/about/">About <span class="sr-only">(current)</span></a>
</li>
Expand Down Expand Up @@ -69,6 +72,8 @@ <h5 class="card-header">
<a href="https://www.reddit.com/user/lordfili">lordfili</a> on Reddit,
<a href="https://www.homebrewtalk.com/forum/members/thorrak.221138/">Thorrak</a> on the HBT forums, or
<a href="https://github.com/thorrak">Thorrak</a> on GitHub.
Tilt calibration and temperature correction code
provided by <a href="https://github.com/duncan-brown">duncan-brown</a>.
</p>

<p class="card-text"> TiltBridge documentation and support can be found at the following links:<br>
Expand Down
763 changes: 763 additions & 0 deletions data/calibration.htm

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion data/index.htm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<li class="nav-item">
<a class="nav-link" href="/settings/">Settings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/calibration/">Calibration</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about/">About</a>
</li>
Expand Down Expand Up @@ -120,4 +123,4 @@ <h6 class="card-subtitle mb-2 text-muted">Google Sheets: {{sensor.gsheets_name}}
</script>

</body>
</html>
</html>
5 changes: 4 additions & 1 deletion data/restarting.htm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<li class="nav-item">
<a class="nav-link" href="/settings/">Settings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/calibration/">Calibration</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about/">About</a>
</li>
Expand All @@ -65,4 +68,4 @@ <h4>Restarting Tiltbridge</h4>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
</html>
36 changes: 36 additions & 0 deletions data/settings.htm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<li class="nav-item active">
<a class="nav-link" href="/settings/">Settings <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="/calibration/">Calibration</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about/">About</a>
</li>
Expand Down Expand Up @@ -76,6 +79,37 @@ <h5 class="card-header">
</div>
</div>

<div class="row row-padded">
<div class="col-12">
<div class="card">
<h5 class="card-header">
Calibration Settings
</h5>

<div class="card-body">
<p class="card-text">These settings control whether or not TiltBridge applies <a href="/calibration/">calibration</a> and temperature corrections to the Tilt reading before it is uploaded.</p>
<form action="/settings/update/" method="POST">
<div class="form-group row">
<label for="applyCalibration" class="col-sm-4 form-check-label" data-toggle="tooltip" title="Apply calibration to Tilt data">Enable Calibration</label>
<div class="col-sm-6">
<input class="form-check-input" type="checkbox" id="applyCalibration" name="applyCalibration">
</div>
<div class="col-sm-2"><button type="submit" class="btn btn-primary">Update</button></div>
</div>
<div class="form-group row">
<label for="tempCorrect" class="col-sm-4 form-check-label" data-toggle="tooltip" title="Apply temperature correction to Tilt data">Enable Temperature Correction to 60 &deg;F</label>
<div class="col-sm-6">
<input class="form-check-input" type="checkbox" id="tempCorrect" name="tempCorrect">
</div>
<div class="col-sm-2"><button type="submit" class="btn btn-primary">Update</button></div>
</div>
</form>
</div>
</div>

</div>
</div>

<div class="row row-padded">
<div class="col-12">
<div class="card">
Expand Down Expand Up @@ -364,6 +398,8 @@ <h5 class="card-header">
xhr.open('GET', '/settings/json/');
xhr.onload = function () {
self.settings = JSON.parse(xhr.responseText);
if ( self.settings.applyCalibration ) document.getElementById("applyCalibration").checked = true;
if ( self.settings.tempCorrect ) document.getElementById("tempCorrect").checked = true;
};
xhr.send()
}
Expand Down
5 changes: 4 additions & 1 deletion data/updating.htm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<li class="nav-item">
<a class="nav-link" href="/settings/">Settings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/calibration/">Calibration</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about/">About</a>
</li>
Expand All @@ -64,4 +67,4 @@ <h4>Updating Firmware</h4>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
</html>
5 changes: 4 additions & 1 deletion data/wifi_reset.htm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<li class="nav-item">
<a class="nav-link" href="/settings/">Settings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/calibration/">Calibration</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about/">About</a>
</li>
Expand All @@ -64,4 +67,4 @@ <h4>Resetting WiFi Settings</h4>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
</html>
105 changes: 104 additions & 1 deletion src/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ void processConfigError() {
redirectToConfig();
}

// This is to simplify the redirects in processCalibration
void redirectToCalibration() {
server.sendHeader("Location", "/calibration/");
server.sendHeader("Cache-Control", "no-cache");
server.send(301);
}

void processCalibrationError() {
#ifdef DEBUG_PRINTS
Serial.println("processCalibrationError!");
#endif
redirectToCalibration();
}


bool processSheetName(const char* varName, const char* colorName) {
if (server.hasArg(varName)) {
Expand Down Expand Up @@ -95,6 +109,17 @@ void processConfig() {
}
}

if (server.hasArg("applyCalibration")) {
app_config.config["applyCalibration"] = true;
} else {
app_config.config["applyCalibration"] = false;
}

if (server.hasArg("tempCorrect")) {
app_config.config["tempCorrect"] = true;
} else {
app_config.config["tempCorrect"] = false;
}

// Fermentrack Settings
if (server.hasArg("fermentrackURL")) {
Expand Down Expand Up @@ -253,7 +278,80 @@ void processConfig() {
}
}

constexpr unsigned int str2int(const char* str, int h = 0)
{
return !str[h] ? 5381 : (str2int(str, h+1) * 33) ^ str[h];
}

// we don't need to do much input checking on the calibration data as we are
// looking at numbers generated by the javascript and not a human
void processCalibration() {
int tilt_name = 0;
int degree;
double x0, x1, x2, x3;

if (server.hasArg("clearTiltColor")) {
tilt_name = str2int(server.arg("clearTiltColor").c_str());
degree = 1;
x1 = 1.0;
x0 = x2 = x3 = 0.0;
}

if (server.hasArg("updateTiltColor")) {
tilt_name = str2int(server.arg("updateTiltColor").c_str());
if (server.hasArg("linear")) {
degree = 1;
x0 = strtod(server.arg("linearFitx0").c_str(), nullptr);
x1 = strtod(server.arg("linearFitx1").c_str(), nullptr);
x2 = x3 = 0.0;
} else if (server.hasArg("quadratic")) {
degree = 2;
x0 = strtod(server.arg("quadraticFitx0").c_str(), nullptr);
x1 = strtod(server.arg("quadraticFitx1").c_str(), nullptr);
x2 = strtod(server.arg("quadraticFitx2").c_str(), nullptr);
x3 = 0.0;
} else if (server.hasArg("cubic")) {
degree = 2;
x0 = strtod(server.arg("cubicFitx0").c_str(), nullptr);
x1 = strtod(server.arg("cubicFitx1").c_str(), nullptr);
x2 = strtod(server.arg("cubicFitx2").c_str(), nullptr);
x3 = strtod(server.arg("cubicFitx3").c_str(), nullptr);
} else {
processCalibrationError();
}
}

switch( tilt_name ) {
case str2int("red"):
app_config.config["cal_red"] = {{"degree", degree}, {"x0", x0}, {"x1", x1}, {"x2", x2}, {"x3", x3}};
break;
case str2int("green"):
app_config.config["cal_green"] = {{"degree", degree}, {"x0", x0}, {"x1", x1}, {"x2", x2}, {"x3", x3}};
break;
case str2int("black"):
app_config.config["cal_black"] = {{"degree", degree}, {"x0", x0}, {"x1", x1}, {"x2", x2}, {"x3", x3}};
break;
case str2int("purple"):
app_config.config["cal_purple"] = {{"degree", degree}, {"x0", x0}, {"x1", x1}, {"x2", x2}, {"x3", x3}};
break;
case str2int("orange"):
app_config.config["cal_orange"] = {{"degree", degree}, {"x0", x0}, {"x1", x1}, {"x2", x2}, {"x3", x3}};
break;
case str2int("blue"):
app_config.config["cal_blue"] = {{"degree", degree}, {"x0", x0}, {"x1", x1}, {"x2", x2}, {"x3", x3}};
break;
case str2int("yellow"):
app_config.config["cal_yellow"] = {{"degree", degree}, {"x0", x0}, {"x1", x1}, {"x2", x2}, {"x3", x3}};
break;
case str2int("pink"):
app_config.config["cal_pink"] = {{"degree", degree}, {"x0", x0}, {"x1", x1}, {"x2", x2}, {"x3", x3}};
break;
default:
processCalibrationError();
}

redirectToCalibration();
}


//This function is overkill for how we're handling things, but
Expand Down Expand Up @@ -291,10 +389,12 @@ void root_from_spiffs() {
void settings_from_spiffs() {
loadFromSpiffs("/settings.htm");
}
void calibration_from_spiffs() {
loadFromSpiffs("/calibration.htm");
}
void about_from_spiffs() {
loadFromSpiffs("/about.htm");
}

void favicon_from_spiffs() {
loadFromSpiffs("/favicon.ico");
}
Expand Down Expand Up @@ -351,6 +451,9 @@ void httpServer::init(){
server.on("/settings/", settings_from_spiffs);
server.on("/settings/update/", processConfig);

server.on("/calibration/", calibration_from_spiffs);
server.on("/calibration/update/", processCalibration);

server.on("/json/", http_json);
server.on("/settings/json/", settings_json);
#ifndef DISABLE_OTA_UPDATES
Expand Down
14 changes: 14 additions & 0 deletions src/jsonConfigHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ void jsonConfigHandler::initialize() {
{"mdnsID", "tiltbridge"},
{"update_spiffs", false},

// Global Calibration settings
{"applyCalibration", false},
{"tempCorrect", false},

// Tilt Calibration Settings
{"cal_red", { {"degree", 1}, {"x0", 0.0}, {"x1", 1.0}, {"x2", 0.0}, {"x3", 0.0}}},
{"cal_green", { {"degree", 1}, {"x0", 0.0}, {"x1", 1.0}, {"x2", 0.0}, {"x3", 0.0}}},
{"cal_black", { {"degree", 1}, {"x0", 0.0}, {"x1", 1.0}, {"x2", 0.0}, {"x3", 0.0}}},
{"cal_purple", { {"degree", 1}, {"x0", 0.0}, {"x1", 1.0}, {"x2", 0.0}, {"x3", 0.0}}},
{"cal_orange", { {"degree", 1}, {"x0", 0.0}, {"x1", 1.0}, {"x2", 0.0}, {"x3", 0.0}}},
{"cal_blue", { {"degree", 1}, {"x0", 0.0}, {"x1", 1.0}, {"x2", 0.0}, {"x3", 0.0}}},
{"cal_yellow", { {"degree", 1}, {"x0", 0.0}, {"x1", 1.0}, {"x2", 0.0}, {"x3", 0.0}}},
{"cal_pink", { {"degree", 1}, {"x0", 0.0}, {"x1", 1.0}, {"x2", 0.0}, {"x3", 0.0}}},

// Fermentrack Settings
{"fermentrackURL", ""},
{"fermentrackPushEvery", 30},
Expand Down
77 changes: 76 additions & 1 deletion src/tilt/tiltHydrometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,83 @@ std::string tiltHydrometer::gsheets_beer_name() {


bool tiltHydrometer::set_values(uint32_t i_temp, uint32_t i_grav){
double d_temp = (double) i_temp;
double d_grav = (double) i_grav / 1000.0;
nlohmann::json cal_params;

#if DEBUG_PRINTS
Serial.print("Tilt gravity = ");
Serial.println(d_grav);
#endif

if (app_config.config["applyCalibration"]) {
double x0 = 0.0;
double x1 = 1.0;
double x2 = 0.0;
double x3 = 0.0;

switch(m_color) {
case TILT_COLOR_RED:
cal_params = app_config.config["cal_red"];
break;
case TILT_COLOR_GREEN:
cal_params = app_config.config["cal_green"];
break;
case TILT_COLOR_BLACK:
cal_params = app_config.config["cal_black"];
break;
case TILT_COLOR_PURPLE:
cal_params = app_config.config["cal_purple"];
break;
case TILT_COLOR_ORANGE:
cal_params = app_config.config["cal_orange"];
break;
case TILT_COLOR_BLUE:
cal_params = app_config.config["cal_blue"];
break;
case TILT_COLOR_YELLOW:
cal_params = app_config.config["cal_yellow"];
break;
case TILT_COLOR_PINK:
cal_params = app_config.config["cal_pink"];
break;
}

for (auto& el : cal_params.items()) {
std::string coeff = el.key();
double val = el.value().get<double>();
#if DEBUG_PRINTS
Serial.print("Calibration coefficient ");
Serial.print(coeff.c_str());
Serial.print(" = ");
Serial.println(val);
#endif
if (!coeff.compare("x0")) x0 = val;
if (!coeff.compare("x1")) x1 = val;
if (!coeff.compare("x2")) x2 = val;
if (!coeff.compare("x3")) x3 = val;
}

d_grav = x0 + x1 * d_grav + x2 * d_grav * d_grav + x3 * d_grav * d_grav * d_grav;

#if DEBUG_PRINTS
Serial.print("Calibrated gravity = ");
Serial.println(d_grav);
#endif
}

if (app_config.config["tempCorrect"]) {
double ref_temp = 60.0;
d_grav = d_grav * ((1.00130346 - 0.000134722124 * d_temp + 0.00000204052596 * d_temp * d_temp - 0.00000000232820948 * d_temp * d_temp * d_temp) / (1.00130346 - 0.000134722124 * ref_temp + 0.00000204052596 * ref_temp * ref_temp - 0.00000000232820948 * ref_temp * ref_temp * ref_temp));

#if DEBUG_PRINTS
Serial.print("Temperature corrected gravity = ");
Serial.println(d_grav);
#endif
}

temp = i_temp;
gravity = i_grav;
gravity = (int) round(d_grav * 1000.0);
m_loaded = true; // Setting loaded true now that we have gravity/temp values
m_lastUpdate = xTaskGetTickCount();
return true;
Expand Down

0 comments on commit dea0309

Please sign in to comment.