Bug fixes gravitycorr + option to use gyro temp

This commit is contained in:
Magnus
2021-04-11 11:12:46 +02:00
parent 876718602f
commit c1c325a2bc
13 changed files with 85 additions and 53 deletions

View File

@ -64,17 +64,18 @@ double calculateGravity( double angle, double temp ) {
//
// Do a standard gravity temperature correction. This is a simple way to adjust for differnt worth temperatures
//
double gravityTemperatureCorrection( double gravity, double temp, double calTemp) {
double gravityTemperatureCorrection( double gravity, double temp, char tempFormat, double calTemp) {
#if LOG_LEVEL==6
Log.verbose(F("CALC: Adjusting gravity based on temperature, gravity %F, temp %F, calTemp %F." CR), gravity, temp, calTemp);
#endif
double tempF = convertCtoF( temp );
double calTempF = convertCtoF(calTemp);
if( tempFormat == 'C')
temp = convertCtoF( temp );
double calTempF = convertCtoF(calTemp); // calTemp is in C
const char* formula = "gravity*((1.00130346-0.000134722124*temp+0.00000204052596*temp^2-0.00000000232820948*temp^3)/(1.00130346-0.000134722124*cal+0.00000204052596*cal^2-0.00000000232820948*cal^3))";
// Store variable names and pointers.
te_variable vars[] = {{"gravity", &gravity}, {"temp", &tempF}, {"cal", &calTempF}};
te_variable vars[] = {{"gravity", &gravity}, {"temp", &temp}, {"cal", &calTempF}};
int err;
// Compile the expression with variables.

View File

@ -29,7 +29,7 @@ SOFTWARE.
// Functions
double calculateGravity( double angle, double temp );
double gravityTemperatureCorrection( double gravity, double temp, double calTemp = 20 );
double gravityTemperatureCorrection( double gravity, double temp, char tempFormat, double calTemp = 20 );
#endif // _CALC_H

View File

@ -48,10 +48,10 @@ SOFTWARE.
#define CFG_PARAM_PUSH_BREWFATHER "brewfather-push" // URL (brewfather format)
#define CFG_PARAM_PUSH_HTTP "http-push" // URL (iSpindle format)
#define CFG_PARAM_PUSH_HTTP2 "http-push2" // URL (iSpindle format)
#define CFG_PARAM_PUSH_INFLUXDB2 "influxdb2-push" // URL (iSpindle format)
#define CFG_PARAM_PUSH_INFLUXDB2_ORG "influxdb2-org" // URL (iSpindle format)
#define CFG_PARAM_PUSH_INFLUXDB2_BUCKET "influxdb2-bucket" // URL (iSpindle format)
#define CFG_PARAM_PUSH_INFLUXDB2_AUTH "influxdb2-auth" // URL (iSpindle format)
#define CFG_PARAM_PUSH_INFLUXDB2 "influxdb2-push" // URL
#define CFG_PARAM_PUSH_INFLUXDB2_ORG "influxdb2-org" // URL
#define CFG_PARAM_PUSH_INFLUXDB2_BUCKET "influxdb2-bucket" // URL
#define CFG_PARAM_PUSH_INFLUXDB2_AUTH "influxdb2-auth" // URL
#define CFG_PARAM_SLEEP_INTERVAL "sleep-interval" // Sleep interval
// TODO: @deprecated setting
#define CFG_PARAM_PUSH_INTERVAL "push-interval" // Time between push

View File

@ -67,7 +67,7 @@ class GyroSensor {
void calibrateSensor();
double getAngle() { return angle; };
double getSensorTempC() { return sensorTemp; };
float getSensorTempC() { return sensorTemp; };
bool isConnected() { return sensorConnected; };
bool hasValue() { return validValue; };
void enterSleep();

View File

@ -52,6 +52,9 @@ void printBuildOptions() {
#ifdef SKIP_SLEEPMODE
"SKIP_SLEEP "
#endif
#ifdef USE_GYRO_TEMP
"GYRO_TEMP "
#endif
#ifdef EMBED_HTML
"EMBED_HTML "
#endif

View File

@ -197,7 +197,7 @@ void loop() {
#endif
if( myConfig.isGravityTempAdj() ) {
LOG_PERF_START("loop-gravity-corr");
gravity = gravityTemperatureCorrection( gravity, temp); // Use default correction temperature of 20C
gravity = gravityTemperatureCorrection( gravity, temp, myConfig.getTempFormat() ); // Use default correction temperature of 20C
LOG_PERF_STOP("loop-gravity-corr");
#if LOG_LEVEL==6
Log.verbose(F("Main: Temp adjusted gravity=%F." CR), gravity );

View File

@ -24,6 +24,7 @@ SOFTWARE.
#include "tempsensor.h"
#include "helper.h"
#include "config.h"
#include "gyro.h"
#include <onewire.h>
#include <DallasTemperature.h>
#include <Wire.h>
@ -35,11 +36,13 @@ float convertCtoF( float t ) {
return (t * 1.8 ) + 32.0;
}
#if !defined( USE_GYRO_TEMP )
OneWire myOneWire(D6);
DallasTemperature mySensors(&myOneWire);
TempSensor myTempSensor;
#define TEMPERATURE_PRECISION 9
#endif
TempSensor myTempSensor;
//
// Setup temp sensors
@ -51,6 +54,10 @@ void TempSensor::setup() {
return;
#endif
#if defined( USE_GYRO_TEMP )
Log.notice(F("TSEN: Using temperature from gyro." CR));
#else
// This code is used to read the DS18 temp sensor
if( mySensors.getDS18Count() )
return;
@ -63,6 +70,7 @@ void TempSensor::setup() {
Log.notice(F("TSEN: Found %d sensors." CR), mySensors.getDS18Count());
mySensors.setResolution(TEMPERATURE_PRECISION);
}
#endif
float t = myConfig.getTempSensorAdj();
@ -81,20 +89,30 @@ void TempSensor::setup() {
}
//
// Retrieving value from sensor
// Retrieving value from sensor, value is in Celcius
//
float TempSensor::getValue() {
float c = 0;
#if defined( SIMULATE_TEMP )
return 21;
#endif
#if defined( USE_GYRO_TEMP )
//LOG_PERF_START("temp-get");
float c = myGyro.getSensorTempC();
//LOG_PERF_STOP("temp-get");
hasSensor = true;
return c;
#if LOG_LEVEL==6
Log.verbose(F("TSEN: Reciving temp value for gyro sensor %F C." CR), c);
#endif
#else
// Read the sensors
//LOG_PERF_START("temp-request");
mySensors.requestTemperatures();
//LOG_PERF_STOP("temp-request");
float c = 0;
if( mySensors.getDS18Count() >= 1) {
//LOG_PERF_START("temp-get");
c = mySensors.getTempCByIndex(0);
@ -107,6 +125,7 @@ float TempSensor::getValue() {
}
return c;
#endif
}
// EOF

View File

@ -88,9 +88,12 @@ void webHandleConfig() {
double temp = myTempSensor.getValueCelcius();
double gravity = calculateGravity( angle, temp );
doc[ CFG_PARAM_ANGLE ] = reduceFloatPrecision( angle);
doc[ CFG_PARAM_GRAVITY ] = reduceFloatPrecision( gravityTemperatureCorrection( gravity, temp ), 4);
doc[ CFG_PARAM_BATTERY ] = reduceFloatPrecision( myBatteryVoltage.getVoltage());
doc[ CFG_PARAM_ANGLE ] = reduceFloatPrecision( angle);
if( myConfig.isGravityTempAdj() )
doc[ CFG_PARAM_GRAVITY ] = reduceFloatPrecision( gravityTemperatureCorrection( gravity, temp, myConfig.getTempFormat() ), 4);
else
doc[ CFG_PARAM_GRAVITY ] = reduceFloatPrecision( gravity, 4);
doc[ CFG_PARAM_BATTERY ] = reduceFloatPrecision( myBatteryVoltage.getVoltage());
#if LOG_LEVEL==6
serializeJson(doc, Serial);
@ -224,15 +227,18 @@ void webHandleStatus() {
double temp = myTempSensor.getValueCelcius();
double gravity = calculateGravity( angle, temp );
doc[ CFG_PARAM_ID ] = myConfig.getID();
doc[ CFG_PARAM_ANGLE ] = reduceFloatPrecision( angle);
doc[ CFG_PARAM_GRAVITY ] = reduceFloatPrecision( gravityTemperatureCorrection( gravity, temp ), 4);
doc[ CFG_PARAM_TEMP_C ] = reduceFloatPrecision( temp, 1);
doc[ CFG_PARAM_TEMP_F ] = reduceFloatPrecision( myTempSensor.getValueFarenheight(), 1);
doc[ CFG_PARAM_BATTERY ] = reduceFloatPrecision( myBatteryVoltage.getVoltage());
doc[ CFG_PARAM_TEMPFORMAT ] = String( myConfig.getTempFormat() );
doc[ CFG_PARAM_SLEEP_MODE ] = sleepModeAlwaysSkip;
doc[ CFG_PARAM_RSSI ] = WiFi.RSSI();
doc[ CFG_PARAM_ID ] = myConfig.getID();
doc[ CFG_PARAM_ANGLE ] = reduceFloatPrecision( angle);
if( myConfig.isGravityTempAdj() )
doc[ CFG_PARAM_GRAVITY ] = reduceFloatPrecision( gravityTemperatureCorrection( gravity, temp, myConfig.getTempFormat() ), 4);
else
doc[ CFG_PARAM_GRAVITY ] = reduceFloatPrecision( gravity, 4);
doc[ CFG_PARAM_TEMP_C ] = reduceFloatPrecision( temp, 1);
doc[ CFG_PARAM_TEMP_F ] = reduceFloatPrecision( myTempSensor.getValueFarenheight(), 1);
doc[ CFG_PARAM_BATTERY ] = reduceFloatPrecision( myBatteryVoltage.getVoltage());
doc[ CFG_PARAM_TEMPFORMAT ] = String( myConfig.getTempFormat() );
doc[ CFG_PARAM_SLEEP_MODE ] = sleepModeAlwaysSkip;
doc[ CFG_PARAM_RSSI ] = WiFi.RSSI();
#if LOG_LEVEL==6
serializeJson(doc, Serial);
Serial.print( CR );