Lint checked
This commit is contained in:
parent
299d2dd0fe
commit
5d9115137f
82
src/calc.cpp
82
src/calc.cpp
@ -21,35 +21,35 @@ 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
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include "calc.h"
|
#include "src/calc.h"
|
||||||
#include "helper.h"
|
|
||||||
#include "config.h"
|
|
||||||
#include <tinyexpr.h>
|
#include <tinyexpr.h>
|
||||||
#include "tempsensor.h"
|
|
||||||
#include <curveFitting.h>
|
#include <curveFitting.h>
|
||||||
|
#include "src/helper.h"
|
||||||
|
#include "src/config.h"
|
||||||
|
#include "src/tempsensor.h"
|
||||||
|
|
||||||
#define FORMULA_MAX_DEVIATION 1.5
|
#define FORMULA_MAX_DEVIATION 1.5
|
||||||
|
|
||||||
//
|
//
|
||||||
// Use values to derive a formula
|
// Use values to derive a formula
|
||||||
//
|
//
|
||||||
int createFormula( RawFormulaData& fd, char *formulaBuffer, int order ) {
|
int createFormula(RawFormulaData& fd, char *formulaBuffer, int formulaBufferSize, int order) {
|
||||||
|
|
||||||
int noAngles = 0;
|
int noAngles = 0;
|
||||||
|
|
||||||
// Check how many valid values we have got
|
// Check how many valid values we have got
|
||||||
if( fd.a[0]>0 && fd.a[1]>0 && fd.a[2]>0 && fd.a[3]>0 && fd.a[4]>0 )
|
if ( fd.a[0] > 0 && fd.a[1] > 0 && fd.a[2] > 0 && fd.a[3] > 0 && fd.a[4] > 0 )
|
||||||
noAngles = 5;
|
noAngles = 5;
|
||||||
else if( fd.a[0]>0 && fd.a[1]>0 && fd.a[2]>0 && fd.a[3]>0 )
|
else if ( fd.a[0] > 0 && fd.a[1] > 0 && fd.a[2] > 0 && fd.a[3] > 0 )
|
||||||
noAngles = 4;
|
noAngles = 4;
|
||||||
else if( fd.a[0]>0 && fd.a[1]>0 && fd.a[2]>0 )
|
else if ( fd.a[0] > 0 && fd.a[1] > 0 && fd.a[2] > 0 )
|
||||||
noAngles = 3;
|
noAngles = 3;
|
||||||
|
|
||||||
#if LOG_LEVEL==6
|
#if LOG_LEVEL == 6
|
||||||
Log.verbose(F("CALC: Trying to create formula using order = %d, found %d angles" CR), order, noAngles);
|
Log.verbose(F("CALC: Trying to create formula using order = %d, found %d angles" CR), order, noAngles);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if( !noAngles ) {
|
if ( !noAngles ) {
|
||||||
Log.error(F("CALC: Not enough values for deriving formula" CR));
|
Log.error(F("CALC: Not enough values for deriving formula" CR));
|
||||||
return ERR_FORMULA_NOTENOUGHVALUES;
|
return ERR_FORMULA_NOTENOUGHVALUES;
|
||||||
} else {
|
} else {
|
||||||
@ -57,48 +57,48 @@ int createFormula( RawFormulaData& fd, char *formulaBuffer, int order ) {
|
|||||||
double coeffs[order+1];
|
double coeffs[order+1];
|
||||||
int ret = fitCurve(order, noAngles, fd.a, fd.g, sizeof(coeffs)/sizeof(double), coeffs);
|
int ret = fitCurve(order, noAngles, fd.a, fd.g, sizeof(coeffs)/sizeof(double), coeffs);
|
||||||
|
|
||||||
//Returned value is 0 if no error
|
// Returned value is 0 if no error
|
||||||
if( ret == 0 ) {
|
if ( ret == 0 ) {
|
||||||
|
|
||||||
#if LOG_LEVEL==6
|
#if LOG_LEVEL == 6
|
||||||
Log.verbose(F("CALC: Finshied processing data points." CR));
|
Log.verbose(F("CALC: Finshied processing data points." CR));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Print the formula based on 'order'
|
// Print the formula based on 'order'
|
||||||
if( order == 4 ) {
|
if ( order == 4 ) {
|
||||||
sprintf( formulaBuffer, "%.8f*tilt^4+%.8f*tilt^3+%.8f*tilt^2+%.8f*tilt+%.8f", coeffs[0], coeffs[1], coeffs[2], coeffs[3], coeffs[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 ) {
|
} else if ( order == 3 ) {
|
||||||
sprintf( formulaBuffer, "%.8f*tilt^3+%.8f*tilt^2+%.8f*tilt+%.8f", coeffs[0], coeffs[1], coeffs[2], coeffs[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 ) {
|
} else if ( order == 2 ) {
|
||||||
sprintf( formulaBuffer, "%.8f*tilt^2+%.8f*tilt+%.8f", coeffs[0], coeffs[1], coeffs[2] );
|
snprintf(formulaBuffer, formulaBufferSize, "%.8f*tilt^2+%.8f*tilt+%.8f", coeffs[0], coeffs[1], coeffs[2]);
|
||||||
} else { // order == 1
|
} else { // order == 1
|
||||||
sprintf( formulaBuffer, "%.8f*tilt+%.8f", coeffs[0], coeffs[1] );
|
snprintf(formulaBuffer, formulaBufferSize, "%.8f*tilt+%.8f", coeffs[0], coeffs[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if LOG_LEVEL==6
|
#if LOG_LEVEL == 6
|
||||||
Log.verbose(F("CALC: Formula: %s" CR), formulaBuffer );
|
Log.verbose(F("CALC: Formula: %s" CR), formulaBuffer );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool valid = true;
|
bool valid = true;
|
||||||
|
|
||||||
for( int i = 0; i < 5; i++ ) {
|
for ( int i = 0; i < 5; i++ ) {
|
||||||
if( fd.a[i]==0 && valid )
|
if ( fd.a[i] == 0 && valid )
|
||||||
break;
|
break;
|
||||||
|
|
||||||
double g = calculateGravity( fd.a[i], 0, formulaBuffer );
|
double g = calculateGravity(fd.a[i], 0, formulaBuffer);
|
||||||
double dev = (g-fd.g[i])<0 ? (fd.g[i]-g) : (g-fd.g[i]);
|
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 the deviation is more than 2 degress we mark it as failed.
|
||||||
if( dev*1000 > FORMULA_MAX_DEVIATION )
|
if ( dev*1000 > FORMULA_MAX_DEVIATION )
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !valid ) {
|
if ( !valid ) {
|
||||||
Log.error(F("CALC: Deviation to large, formula rejected." CR));
|
Log.error(F("CALC: Deviation to large, formula rejected." CR));
|
||||||
return ERR_FORMULA_UNABLETOFFIND;
|
return ERR_FORMULA_UNABLETOFFIND;
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.info(F("CALC: Found formula '%s'." CR), formulaBuffer );
|
Log.info(F("CALC: Found formula '%s'." CR), formulaBuffer);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,22 +110,22 @@ int createFormula( RawFormulaData& fd, char *formulaBuffer, int order ) {
|
|||||||
//
|
//
|
||||||
// Calculates gravity according to supplied formula, compatible with iSpindle/Fermentrack formula
|
// Calculates gravity according to supplied formula, compatible with iSpindle/Fermentrack formula
|
||||||
//
|
//
|
||||||
double calculateGravity( double angle, double temp, const char *tempFormula ) {
|
double calculateGravity(double angle, double temp, const char *tempFormula) {
|
||||||
const char* formula = myConfig.getGravityFormula();
|
const char* formula = myConfig.getGravityFormula();
|
||||||
|
|
||||||
if( tempFormula != 0 ) {
|
if ( tempFormula != 0 ) {
|
||||||
#if LOG_LEVEL==6
|
#if LOG_LEVEL == 6
|
||||||
Log.verbose(F("CALC: Using temporary formula." CR));
|
Log.verbose(F("CALC: Using temporary formula." CR));
|
||||||
#endif
|
#endif
|
||||||
formula = tempFormula;
|
formula = tempFormula;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if LOG_LEVEL==6
|
#if LOG_LEVEL == 6
|
||||||
Log.verbose(F("CALC: Calculating gravity for angle %F, temp %F." CR), angle, temp);
|
Log.verbose(F("CALC: Calculating gravity for angle %F, temp %F." CR), angle, temp);
|
||||||
Log.verbose(F("CALC: Formula %s." CR), formula);
|
Log.verbose(F("CALC: Formula %s." CR), formula);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if( strlen(formula) == 0 )
|
if ( strlen(formula) == 0 )
|
||||||
return 0.0;
|
return 0.0;
|
||||||
|
|
||||||
// Store variable names and pointers.
|
// Store variable names and pointers.
|
||||||
@ -135,11 +135,11 @@ double calculateGravity( double angle, double temp, const char *tempFormula ) {
|
|||||||
// Compile the expression with variables.
|
// Compile the expression with variables.
|
||||||
te_expr *expr = te_compile(formula, vars, 2, &err);
|
te_expr *expr = te_compile(formula, vars, 2, &err);
|
||||||
|
|
||||||
if (expr) {
|
if ( expr ) {
|
||||||
double g = te_eval(expr);
|
double g = te_eval(expr);
|
||||||
te_free(expr);
|
te_free(expr);
|
||||||
|
|
||||||
#if LOG_LEVEL==6
|
#if LOG_LEVEL == 6
|
||||||
Log.verbose(F("CALC: Calculated gravity is %F." CR), g);
|
Log.verbose(F("CALC: Calculated gravity is %F." CR), g);
|
||||||
#endif
|
#endif
|
||||||
return g;
|
return g;
|
||||||
@ -152,13 +152,13 @@ double calculateGravity( double angle, double temp, const char *tempFormula ) {
|
|||||||
//
|
//
|
||||||
// Do a standard gravity temperature correction. This is a simple way to adjust for differnt worth temperatures
|
// Do a standard gravity temperature correction. This is a simple way to adjust for differnt worth temperatures
|
||||||
//
|
//
|
||||||
double gravityTemperatureCorrection( double gravity, double temp, char tempFormat, double calTemp) {
|
double gravityTemperatureCorrection(double gravity, double temp, char tempFormat, double calTemp) {
|
||||||
#if LOG_LEVEL==6
|
#if LOG_LEVEL == 6
|
||||||
Log.verbose(F("CALC: Adjusting gravity based on temperature, gravity %F, temp %F, calTemp %F." CR), gravity, temp, calTemp);
|
Log.verbose(F("CALC: Adjusting gravity based on temperature, gravity %F, temp %F, calTemp %F." CR), gravity, temp, calTemp);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if( tempFormat == 'C')
|
if ( tempFormat == 'C')
|
||||||
temp = convertCtoF( temp );
|
temp = convertCtoF(temp);
|
||||||
double calTempF = convertCtoF(calTemp); // calTemp is in C
|
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))";
|
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))";
|
||||||
|
|
||||||
@ -169,11 +169,11 @@ double gravityTemperatureCorrection( double gravity, double temp, char tempForma
|
|||||||
// Compile the expression with variables.
|
// Compile the expression with variables.
|
||||||
te_expr *expr = te_compile(formula, vars, 3, &err);
|
te_expr *expr = te_compile(formula, vars, 3, &err);
|
||||||
|
|
||||||
if (expr) {
|
if ( expr ) {
|
||||||
double g = te_eval(expr);
|
double g = te_eval(expr);
|
||||||
te_free(expr);
|
te_free(expr);
|
||||||
|
|
||||||
#if LOG_LEVEL==6
|
#if LOG_LEVEL == 6
|
||||||
Log.verbose(F("CALC: Corrected gravity is %F." CR), g);
|
Log.verbose(F("CALC: Corrected gravity is %F." CR), g);
|
||||||
#endif
|
#endif
|
||||||
return g;
|
return g;
|
||||||
|
16
src/calc.h
16
src/calc.h
@ -21,22 +21,22 @@ 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
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#ifndef _CALC_H
|
#ifndef SRC_CALC_H_
|
||||||
#define _CALC_H
|
#define SRC_CALC_H_
|
||||||
|
|
||||||
// Includes
|
// Includes
|
||||||
#include "helper.h"
|
#include "src/helper.h"
|
||||||
#include "config.h"
|
#include "src/config.h"
|
||||||
|
|
||||||
#define ERR_FORMULA_NOTENOUGHVALUES -1
|
#define ERR_FORMULA_NOTENOUGHVALUES -1
|
||||||
#define ERR_FORMULA_INTERNAL -2
|
#define ERR_FORMULA_INTERNAL -2
|
||||||
#define ERR_FORMULA_UNABLETOFFIND -3
|
#define ERR_FORMULA_UNABLETOFFIND -3
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
double calculateGravity( double angle, double temp, const char *tempFormula = 0 );
|
double calculateGravity(double angle, double temp, const char *tempFormula = 0);
|
||||||
double gravityTemperatureCorrection( double gravity, double temp, char tempFormat, double calTemp = 20 );
|
double gravityTemperatureCorrection(double gravity, double temp, char tempFormat, double calTemp = 20);
|
||||||
int createFormula( RawFormulaData& fd, char *formulaBuffer, int order);
|
int createFormula(RawFormulaData& fd, char *formulaBuffer, int formulaBufferSize, int order);
|
||||||
|
|
||||||
#endif // _CALC_H
|
#endif // SRC_CALC_H_
|
||||||
|
|
||||||
// EOF
|
// EOF
|
Loading…
Reference in New Issue
Block a user