Skip to content

Commit

Permalink
Updated docs and increased formula points removed unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
mp-se committed Oct 18, 2024
1 parent 0738306 commit df792cc
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 262 deletions.
7 changes: 3 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ build_flags =
#-D FORCE_GRAVITY_MODE
#-D COLLECT_PERFDATA
-D USE_LITTLEFS=true
-D CFG_APPVER="\"2.0.1\""
#-D CFG_GITREV=\""beta-3\""
-D CFG_APPVER="\"2.1.0\""
-D CFG_GITREV=\""beta-1\""
#-D ENABLE_REMOTE_UI_DEVELOPMENT
!python script/git_rev.py
#!python script/git_rev.py
lib_deps =
# Using local copy of these libraries
# https://github.com/mp-se/i2cdevlib.git#<document>
Expand All @@ -37,7 +37,6 @@ lib_deps =
https://github.com/mp-se/tinyexpr#v1.0.0
https://github.com/mp-se/Arduino-Log#1.1.2
https://github.com/mp-se/ArduinoJson#v6.21.5
https://github.com/mp-se/arduinoCurveFitting#v1.0.6
https://github.com/mp-se/arduino-mqtt#v2.5.2
https://github.com/mp-se/ESPAsyncWebServer#0.1.1
https://github.com/mp-se/ESPAsyncTCP#0.1.0
Expand Down
89 changes: 0 additions & 89 deletions src/calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,101 +21,12 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <curveFitting.h>
#include <tinyexpr.h>

#include <calc.hpp>
#include <log.hpp>
#include <utils.hpp>

int createFormula(RawFormulaData &fd, char *formulaBuffer,
int formulaBufferSize, int order) {
int noAngles = 0;
RawFormulaData fd2;

// Check how many valid values we have got and make sure we have a full
// series.
for (int i = 0; i < FORMULA_DATA_SIZE; i++) {
if (fd.a[i]) {
fd2.a[noAngles] = fd.a[i];
fd2.g[noAngles] = fd.g[i];
noAngles++;
}
}

#if LOG_LEVEL == 6
Log.verbose(
F("CALC: Trying to create formula using order = %d, found %d angles" CR),
order, noAngles);
#endif

if (noAngles < 3) {
writeErrorLog("CALC: Not enough values for deriving formula");
return ERR_FORMULA_NOTENOUGHVALUES;
} else {
double coeffs[order + 1];
int ret = fitCurve(order, noAngles, fd2.a, fd2.g,
sizeof(coeffs) / sizeof(double), coeffs);

// Returned value is 0 if no error
if (ret == 0) {
#if LOG_LEVEL == 6
Log.verbose(F("CALC: Finshied processing data points, order = %d." CR),
order);
#endif

// Print the formula based on 'order'
if (order == 4) {
snprintf(formulaBuffer, formulaBufferSize,
"%.8f*tilt^4+%.8f*tilt^3+%.8f*tilt^2+%.8f*tilt+%.8f",
coeffs[0], coeffs[1], coeffs[2], coeffs[3], coeffs[4]);
} else if (order == 3) {
snprintf(formulaBuffer, formulaBufferSize,
"%.8f*tilt^3+%.8f*tilt^2+%.8f*tilt+%.8f", coeffs[0], coeffs[1],
coeffs[2], coeffs[3]);
} else if (order == 2) {
snprintf(formulaBuffer, formulaBufferSize, "%.8f*tilt^2+%.8f*tilt+%.8f",
coeffs[0], coeffs[1], coeffs[2]);
} else { // order == 1
snprintf(formulaBuffer, formulaBufferSize, "%.8f*tilt+%.8f", coeffs[0],
coeffs[1]);
}

#if LOG_LEVEL == 6
Log.verbose(F("CALC: Formula: %s" CR), formulaBuffer);
#endif

bool valid = true;

for (int i = 0; i < 5; i++) {
if (fd.a[i] == 0 && valid) break;

double g = calculateGravity(fd.a[i], 0, formulaBuffer);
double dev = (g - fd.g[i]) < 0 ? (fd.g[i] - g) : (g - fd.g[i]);

// If the deviation is more than 2 degress we mark it as failed.
if (dev * 1000 > myConfig.getMaxFormulaCreationDeviation()) {
writeErrorLog(
"CALC: Validation failed on angle %.2f, deviation too large %.4f "
"SG, formula order %d",
fd.a[i], dev * 1000, order);
valid = false;
}
}

if (!valid) {
return ERR_FORMULA_UNABLETOFFIND;
}

Log.info(F("CALC: Found formula '%s'." CR), formulaBuffer);
return 0;
}
}

writeErrorLog("CALC: Internal error finding formula.");
return ERR_FORMULA_INTERNAL;
}

double calculateGravity(double angle, double temp, const char *tempFormula) {
const char *formula = myConfig.getGravityFormula();

Expand Down
6 changes: 0 additions & 6 deletions src/calc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,10 @@ SOFTWARE.

#include <config.hpp>

constexpr auto ERR_FORMULA_NOTENOUGHVALUES = -1;
constexpr auto ERR_FORMULA_INTERNAL = -2;
constexpr auto ERR_FORMULA_UNABLETOFFIND = -3;

double calculateGravity(double angle, double tempC,
const char *tempFormula = 0);
double gravityTemperatureCorrectionC(double gravity, double tempC,
double calTempC);
int createFormula(RawFormulaData &fd, char *formulaBuffer,
int formulaBufferSize, int order);

#endif // SRC_CALC_HPP_

Expand Down
2 changes: 1 addition & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void GravmonConfig::createJson(JsonObject& doc) {
doc[PARAM_GYRO_READ_COUNT] = this->getGyroReadCount();
// doc[PARAM_GYRO_READ_DELAY] = this->getGyroReadDelay();
doc[PARAM_GYRO_MOVING_THREASHOLD] = this->getGyroSensorMovingThreashold();
doc[PARAM_FORMULA_DEVIATION] = this->getMaxFormulaCreationDeviation();
doc[PARAM_FORMULA_DEVIATION] = serialized(String(this->getMaxFormulaCreationDeviation(), DECIMALS_TILT));
doc[PARAM_FORMULA_CALIBRATION_TEMP] = this->getDefaultCalibrationTemp();
doc[PARAM_PUSH_INTERVAL_POST] = this->getPushIntervalPost();
doc[PARAM_PUSH_INTERVAL_POST2] = this->getPushIntervalPost2();
Expand Down
6 changes: 3 additions & 3 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct RawGyroData {
};

// Used for holding formulaData (used for calculating formula on device)
#define FORMULA_DATA_SIZE 10
#define FORMULA_DATA_SIZE 20

struct RawFormulaData {
double a[FORMULA_DATA_SIZE];
Expand Down Expand Up @@ -109,8 +109,8 @@ class GravmonConfig : public BaseConfig {

// Gyro calibration and formula calculation data
RawGyroData _gyroCalibration = {0, 0, 0, 0, 0, 0};
RawFormulaData _formulaData = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
RawFormulaData _formulaData = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }};

float _maxFormulaCreationDeviation = 3; // SG
float _defaultCalibrationTemp = 20.0; // C
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void setup() {
} else {
// We cant use LED on ESP32C3 since that pin is connected to GYRO
ledOn(LedColor::RED); // Red or fast flashing to indicate connection
// error
// error
}

interval = 1000; // Change interval from 200ms to 1s
Expand Down
75 changes: 0 additions & 75 deletions src/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,78 +307,6 @@ void GravmonWebServer::webHandleSleepmode(AsyncWebServerRequest *request,
PERF_END("webserver-api-sleepmode");
}

void GravmonWebServer::webHandleFormulaCreate(AsyncWebServerRequest *request) {
if (!isAuthenticated(request)) {
return;
}

PERF_BEGIN("webserver-api-formula-create");
Log.notice(F("WEB : webServer callback for /api/formula." CR));

int e, createErr;
char buf[100];
RawFormulaData fd = myConfig.getFormulaData();

e = createFormula(fd, &buf[0], sizeof(buf), 2);

if (e) {
// If we fail with order=2 try with 3
Log.warning(F("WEB : Failed to find formula with order 3." CR), e);
e = createFormula(fd, &buf[0], sizeof(buf), 3);
}

if (e) {
// If we fail with order=3 try with 4
Log.warning(F("WEB : Failed to find formula with order 4." CR), e);
e = createFormula(fd, &buf[0], sizeof(buf), 4);
}

if (e) {
// If we fail with order=4 then we mark it as failed
Log.error(
F("WEB : Unable to find formula based on provided values err=%d." CR),
e);
createErr = e;
} else {
// Save the formula as succesful
Log.info(F("WEB : Found valid formula: '%s'" CR), &buf[0]);
myConfig.setGravityFormula(buf);
myConfig.saveFile();
createErr = 0;
}

AsyncJsonResponse *response =
new AsyncJsonResponse(false, JSON_BUFFER_SIZE_S);
JsonObject obj = response->getRoot().as<JsonObject>();

obj[PARAM_SUCCESS] = createErr ? false : true;
obj[PARAM_GRAVITY_FORMULA] = "";
obj[PARAM_MESSAGE] = "";

switch (createErr) {
case ERR_FORMULA_INTERNAL:
obj[PARAM_MESSAGE] = "Internal error creating formula.";
break;
case ERR_FORMULA_NOTENOUGHVALUES:
obj[PARAM_MESSAGE] =
"Not enough values to create formula, need at least 3 angles.";
break;
case ERR_FORMULA_UNABLETOFFIND:
obj[PARAM_MESSAGE] =
"Unable to find an accurate formula based on input, check error log "
"and graph below.";
break;
default:
obj[PARAM_GRAVITY_FORMULA] = myConfig.getGravityFormula();
obj[PARAM_MESSAGE] = "New formula created based on the entered values.";
break;
}

response->setLength();
request->send(response);
PERF_END("webserver-api-formula-create");
}

void GravmonWebServer::webHandleConfigFormatWrite(
AsyncWebServerRequest *request, JsonVariant &json) {
if (!isAuthenticated(request)) {
Expand Down Expand Up @@ -615,9 +543,6 @@ bool GravmonWebServer::setupWebServer() {
_server->on("/api/config", HTTP_GET,
std::bind(&GravmonWebServer::webHandleConfigRead, this,
std::placeholders::_1));
_server->on("/api/formula", HTTP_GET,
std::bind(&GravmonWebServer::webHandleFormulaCreate, this,
std::placeholders::_1));
_server->on("/api/calibrate/status", HTTP_GET,
std::bind(&GravmonWebServer::webHandleCalibrateStatus, this,
std::placeholders::_1));
Expand Down
1 change: 0 additions & 1 deletion src/webserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class GravmonWebServer : public BaseWebServer {
void webHandleConfigFormatWrite(AsyncWebServerRequest *request,
JsonVariant &json);
void webHandleSleepmode(AsyncWebServerRequest *request, JsonVariant &json);
void webHandleFormulaCreate(AsyncWebServerRequest *request);
void webHandleTestPush(AsyncWebServerRequest *request, JsonVariant &json);
void webHandleTestPushStatus(AsyncWebServerRequest *request);
void webHandleCalibrate(AsyncWebServerRequest *request);
Expand Down
4 changes: 2 additions & 2 deletions src_docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author = 'Magnus Persson'

# The full version, including alpha/beta/rc tags
release = '2.0.0-beta2'
release = '2.1.0'

# -- General configuration ---------------------------------------------------

Expand All @@ -48,7 +48,7 @@
#
html_theme = 'furo'
html_logo = "images/gravitymon_logo.png"
html_title = "GravityMon v2.0.0"
html_title = "GravityMon v2.1.0"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand Down
6 changes: 1 addition & 5 deletions src_docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,14 @@ the following libraries and without these this would have been much more difficu

Json parser/creator used in configuration files and API's

* https://github.com/PaulStoffregen/OneWire
* https://github.com/pstolarz/OneWireNg

Communication library used for interacting with temperature sensor.

* https://github.com/milesburton/Arduino-Temperature-Control-Library

Interaction with the DS18B20 sensor

* https://github.com/Rotario/arduinoCurveFitting

Create the gravity formula.

* https://github.com/256dpi/arduino-mqtt

Library for sending data to mqtt based on lightweight mqtt implementation.
Expand Down
21 changes: 20 additions & 1 deletion src_docs/source/releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@
Releases
########

v2.1.0
======

New features
++++++++++++
* Updated formula creator.

* Allow user more choice when it comes to selecting the order and varifying result.
* Will now correctly handle Plato data as input
* Formula will still be created for SG since this is the internal format used by Gravitymon
* Moved formula creation from device code to UI code to allow for more flexible formula creation.
* Removed dependant library and API.

* Extended formula points from 10 to 20

.. note::
Update the formula deviation (default 3) to a value according to your selected gravity format eg. SG = 0.003


v2.0.1
======

* Disable LED on esp8266 since it interfear with gyro communication.
* Disable LED on esp8266 since it might collide with gyro communication.

v2.0.0
======
Expand Down
Loading

0 comments on commit df792cc

Please sign in to comment.