From 8719b7afe9e7efa878f1b02f862f0243909ed808 Mon Sep 17 00:00:00 2001 From: Ta180m Date: Fri, 21 Feb 2020 16:08:42 -0600 Subject: Added libraries --- .../arduinoCurveFitting-master/.gitattributes | 2 + .../libraries/arduinoCurveFitting-master/LICENSE | 21 +++ .../libraries/arduinoCurveFitting-master/README.md | 7 + .../examples/fitCurve/fitCurve.ino | 37 ++++ .../arduinoCurveFitting-master/keywords.txt | 1 + .../arduinoCurveFitting-master/library.properties | 10 ++ .../src/curveFitting.cpp | 195 +++++++++++++++++++++ .../arduinoCurveFitting-master/src/curveFitting.h | 40 +++++ .../detectorBuilding/detectorBuilding.cpp | 61 +++++++ .../libraries/detectorBuilding/detectorBuilding.h | 14 ++ Arduino/libraries/readme.txt | 1 + 11 files changed, 389 insertions(+) create mode 100644 Arduino/libraries/arduinoCurveFitting-master/.gitattributes create mode 100644 Arduino/libraries/arduinoCurveFitting-master/LICENSE create mode 100644 Arduino/libraries/arduinoCurveFitting-master/README.md create mode 100644 Arduino/libraries/arduinoCurveFitting-master/examples/fitCurve/fitCurve.ino create mode 100644 Arduino/libraries/arduinoCurveFitting-master/keywords.txt create mode 100644 Arduino/libraries/arduinoCurveFitting-master/library.properties create mode 100644 Arduino/libraries/arduinoCurveFitting-master/src/curveFitting.cpp create mode 100644 Arduino/libraries/arduinoCurveFitting-master/src/curveFitting.h create mode 100644 Arduino/libraries/detectorBuilding/detectorBuilding.cpp create mode 100644 Arduino/libraries/detectorBuilding/detectorBuilding.h create mode 100644 Arduino/libraries/readme.txt diff --git a/Arduino/libraries/arduinoCurveFitting-master/.gitattributes b/Arduino/libraries/arduinoCurveFitting-master/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/Arduino/libraries/arduinoCurveFitting-master/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/Arduino/libraries/arduinoCurveFitting-master/LICENSE b/Arduino/libraries/arduinoCurveFitting-master/LICENSE new file mode 100644 index 0000000..646b41f --- /dev/null +++ b/Arduino/libraries/arduinoCurveFitting-master/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Andrey Fedorov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +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. diff --git a/Arduino/libraries/arduinoCurveFitting-master/README.md b/Arduino/libraries/arduinoCurveFitting-master/README.md new file mode 100644 index 0000000..59e2831 --- /dev/null +++ b/Arduino/libraries/arduinoCurveFitting-master/README.md @@ -0,0 +1,7 @@ +# arduinoCurveFitting +Fit polynomial curves to given points using least squares regression. The max order of polynomial fitting is 20, this should be more than enough to fit most practical problems. All values are kept as double for precision, this works well on a Teensy due to its floating point unit and large (64 bit) double precision. the numbers required increase exponentially as the number of points or order increases. + +This library solves the least squares problem using Cramer's rule and a small function to calculate the determinant of each matrix. + +More explained in this article +https://medium.com/@rowaner111/fitting-curves-to-data-on-an-arduino-part-1-how-to-use-arduinocurvefitting-a3173c6dd4ef diff --git a/Arduino/libraries/arduinoCurveFitting-master/examples/fitCurve/fitCurve.ino b/Arduino/libraries/arduinoCurveFitting-master/examples/fitCurve/fitCurve.ino new file mode 100644 index 0000000..cbc8966 --- /dev/null +++ b/Arduino/libraries/arduinoCurveFitting-master/examples/fitCurve/fitCurve.ino @@ -0,0 +1,37 @@ +#include + +void setup(){ + Serial.begin(9600); + while(!Serial); + Serial.println("Starting"); + + char buf[100]; + int xpower = 3; + int order = 3; + snprintf(buf, 100, "Fitting curve of order %i to data of power %i...\n", order, xpower); + Serial.print(buf); + + double x[26]; + double t[26]; + for (int i = 0; i < sizeof(x)/sizeof(double); i++){ + t[i] = i; + x[i] = pow(i, xpower); + } + + double coeffs[order+1]; + int ret = fitCurve(order, sizeof(x)/sizeof(double), t, x, sizeof(coeffs)/sizeof(double), coeffs); + + if (ret == 0){ //Returned value is 0 if no error + uint8_t c = 'a'; + Serial.println("Coefficients are"); + for (int i = 0; i < sizeof(coeffs)/sizeof(double); i++){ + snprintf(buf, 100, "%c=",c++); + Serial.print(buf); + Serial.print(coeffs[i]); + Serial.print('\t'); + } + } +} + +void loop(){ +} diff --git a/Arduino/libraries/arduinoCurveFitting-master/keywords.txt b/Arduino/libraries/arduinoCurveFitting-master/keywords.txt new file mode 100644 index 0000000..e155106 --- /dev/null +++ b/Arduino/libraries/arduinoCurveFitting-master/keywords.txt @@ -0,0 +1 @@ +fitCurve KEYWORD2 diff --git a/Arduino/libraries/arduinoCurveFitting-master/library.properties b/Arduino/libraries/arduinoCurveFitting-master/library.properties new file mode 100644 index 0000000..469dba8 --- /dev/null +++ b/Arduino/libraries/arduinoCurveFitting-master/library.properties @@ -0,0 +1,10 @@ +name=CurveFitting +version=1.0.6 +author=Rotario +maintainer=Rotario +sentence=Fits polynomial curves to given datapoints +paragraph=Fit polynomial curves to given points using least squares regression. The max order of polynomial fitting is 20, this should be more than enough to fit most practical problems. All values are kept as double for precision, this works well on a Teensy due to its floating point unit and large (64 bit) double precision. the numbers required increase exponentially as the number of points or order increases. +url=https://github.com/Rotario/arduinoCurveFitting +includes=curveFitting.h +category=Data Processing +architectures=* diff --git a/Arduino/libraries/arduinoCurveFitting-master/src/curveFitting.cpp b/Arduino/libraries/arduinoCurveFitting-master/src/curveFitting.cpp new file mode 100644 index 0000000..5e526c0 --- /dev/null +++ b/Arduino/libraries/arduinoCurveFitting-master/src/curveFitting.cpp @@ -0,0 +1,195 @@ +/* + curveFitting.h - Library for fitting curves to given + points using Least Squares method, with Cramer's rule + used to solve the linear equation. Max polynomial order 20. + Created by Rowan Easter-Robinson, August 23, 2018. + Released into the public domain. +*/ + +#include +#include "curveFitting.h" + +void printMat(const char *s, double*m, int n){ + Serial.println(s); + char buf[40]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + snprintf(buf, 40, "%30.4f\t", m[i*n+j]); + Serial.print(buf); + } + Serial.println(); + } +} + +void showmat(const char *s, double **m, int n){ + Serial.println(s); + char buf[40]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++){ + snprintf(buf, 40, "%30.4f\t", m[i][j]); + Serial.print(buf); + } + Serial.println(); + } +} + +void cpyArray(double *src, double*dest, int n){ + for (int i = 0; i < n*n; i++){ + dest[i] = src[i]; + } +} + +void subCol(double *mat, double* sub, uint8_t coln, uint8_t n){ + if (coln >= n) return; + for (int i = 0; i < n; i++){ + mat[(i*n)+coln] = sub[i]; + } +} + +/*Determinant algorithm taken from https://codeforwin.org/2015/08/c-program-to-find-determinant-of-matrix.html */ +int trianglize(double **m, int n) +{ + int sign = 1; + for (int i = 0; i < n; i++) { + int max = 0; + for (int row = i; row < n; row++) + if (fabs(m[row][i]) > fabs(m[max][i])) + max = row; + if (max) { + sign = -sign; + double *tmp = m[i]; + m[i] = m[max], m[max] = tmp; + } + if (!m[i][i]) return 0; + for (int row = i + 1; row < n; row++) { + double r = m[row][i] / m[i][i]; + if (!r) continue; + for (int col = i; col < n; col ++) + m[row][col] -= m[i][col] * r; + } + } + return sign; +} + +double det(double *in, int n, uint8_t prnt) +{ + double *m[n]; + m[0] = in; + + for (int i = 1; i < n; i++) + m[i] = m[i - 1] + n; + if(prnt) showmat("Matrix", m, n); + int sign = trianglize(m, n); + if (!sign) + return 0; + if(prnt) showmat("Upper triangle", m, n); + double p = 1; + for (int i = 0; i < n; i++) + p *= m[i][i]; + return p * sign; +} +/*End of Determinant algorithm*/ + +//Raise x to power +double curveFitPower(double base, int exponent){ + if (exponent == 0){ + return 1; + } else { + double val = base; + for (int i = 1; i < exponent; i++){ + val = val * base; + } + return val; + } +} + +int fitCurve (int order, int nPoints, double py[], int nCoeffs, double *coeffs) { + uint8_t maxOrder = MAX_ORDER; + if (nCoeffs != order + 1) return ORDER_AND_NCOEFFS_DO_NOT_MATCH; // no of coefficients is one larger than the order of the equation + if (nCoeffs > maxOrder || nCoeffs < 2) return ORDER_INCORRECT; //matrix memory hard coded for max of 20 order, which is huge + if (nPoints < 1) return NPOINTS_INCORRECT; //Npoints needs to be positive and nonzero + int i, j; + double T[MAX_ORDER] = {0}; //Values to generate RHS of linear equation + double S[MAX_ORDER*2+1] = {0}; //Values for LHS and RHS of linear equation + double denom; //denominator for Cramer's rule, determinant of LHS linear equation + double x, y; + + double px[nPoints]; //Generate X values, from 0 to n + for (i=0; i maxOrder || nCoeffs < 2) return ORDER_INCORRECT; //Matrix memory hard coded for max of 20 order, which is huge + if (nPoints < 1) return NPOINTS_INCORRECT; //Npoints needs to be positive and nonzero + int i, j; + double T[MAX_ORDER] = {0}; //Values to generate RHS of linear equation + double S[MAX_ORDER*2+1] = {0}; //Values for LHS and RHS of linear equation + double denom; //denominator for Cramer's rule, determinant of LHS linear equation + double x, y; + + for (i=0; i +#define MAX_ORDER 20 + +#ifndef CURVE_FIT_DEBUG +#define CURVE_FIT_DEBUG 0 +#endif + +/* Enum for error messages */ +enum curveFitERROR{ + ORDER_AND_NCOEFFS_DO_NOT_MATCH = -1, + ORDER_INCORRECT = -2, + NPOINTS_INCORRECT = -3 +}; + +/* Matrix Helper Functions */ +void printMat(const char *s, double*m, int n); +void showmat(const char *s, double **m, int n); +void cpyArray(double *src, double*dest, int n); +void subCol(double *mat, double* sub, uint8_t coln, uint8_t n); +double curveFitPower(double base, int exponent); + +/* Determinant matrix functions */ +int trianglize(double **m, int n); +double det(double *in, int n, uint8_t prnt); + +/* Curve fitting functions */ +int fitCurve (int order, int nPoints, double py[], int nCoeffs, double *coeffs); +int fitCurve (int order, int nPoints, double px[], double py[], int nCoeffs, double *coeffs); +#endif diff --git a/Arduino/libraries/detectorBuilding/detectorBuilding.cpp b/Arduino/libraries/detectorBuilding/detectorBuilding.cpp new file mode 100644 index 0000000..8c38009 --- /dev/null +++ b/Arduino/libraries/detectorBuilding/detectorBuilding.cpp @@ -0,0 +1,61 @@ +/* + detectorBuilding.cpp - Library for Detector Building utility functions + Created by Anthony Wang, February 21, 2020. + Released into the public domain. +*/ + +#include "Arduino.h" +#include "detectorBuilding.h" + +const int LED_R = 4, LED_G = 3, LED_B = 2, THERM = 0; // Device component pins +const double R_k = 10000, V_in = 5, analog_max = 1023; // Device constants + +// Temperature conversions +inline double f2c(double f) { return (f - 32) * 5 / 9; } // Fahrenheit to Celsius +inline double c2f(double c) { return c * 9 / 5 + 32; } // Celsius to Fahrenheit +inline double k2c(double k) { return k - 273.15; } // Kelvin to Celsius +inline double c2k(double c) { return c + 273.15; } // Celsius to Kelvin +inline double f2k(double f) { return c2k(f2c(f)); } // Fahrenheit to Kelvin +inline double k2f(double k) { return c2f(k2c(k)); } // Kelvin to Fahrenheit + +// Analog to digital conversion +inline double a2d(int a) { return V_in * a / analog_max; } +inline int d2a(double d) { return d * analog_max / V_in; } + +// Utility functions +// No C++ standard library :( +void sort(double a[], int n) { + // Bubble sort + // Slow but n < 30 so OK + // Too lazy to implement a fast sort + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - 1; j++) { + if (a[j] > a[j + 1]) { + double tmp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = tmp; + } + } + } +} + +// Calculations: Steinhart-hart stuff +// Probably unused but can include anyways +void calculate() { + sort(V, n); + sort(T, n); + double R[n], L[n], Y[n], G[n]; + for (int i = 0; i < n; i++) R[i] = R_k * (V_in / V[i] - 1); + for (int i = 0; i < n; i++) L[i] = log(R[i]); + for (int i = 0; i < n; i++) Y[i] = 1 / c2k(T[i]); + for (int i = 0; i < n; i += 3) { + G[i + 1] = (Y[i + 1] - Y[i]) / (L[i + 1] - L[i]); + G[i + 2] = (Y[i + 2] - Y[i]) / (L[i + 2] - L[i]); + } + for (int i = 0; i < n; i += 3) { // Don't ask how this works + C[i / 3] = (G[i + 2] - G[i + 1]) / (L[i + 2] - L[i + 1]) / (L[i] + L[i + 1] + L[i + 2]); + B[i / 3] = G[i + 1] - C[i / 3] * (L[i] * L[i] + L[i] * L[i + 1] + L[i + 1] * L[i + 1]); + A[i / 3] = Y[i] - L[i] * (B[i / 3] + L[i] * L[i] * C[i / 3]); + } + for (int i = 0; i < n; i += 3) V_mid[i / 3] = (i ? (V[i - 1] + V[i]) / 2 : V[i]); +} \ No newline at end of file diff --git a/Arduino/libraries/detectorBuilding/detectorBuilding.h b/Arduino/libraries/detectorBuilding/detectorBuilding.h new file mode 100644 index 0000000..6bae13c --- /dev/null +++ b/Arduino/libraries/detectorBuilding/detectorBuilding.h @@ -0,0 +1,14 @@ +/* + detectorBuilding.h - Library for Detector Building utility functions + Created by Anthony Wang, February 21, 2020. + Released into the public domain. +*/ + +#ifndef detectorBuilding_h +#define detectorBuilding_h + +#include "Arduino.h" + + + +#endif \ No newline at end of file diff --git a/Arduino/libraries/readme.txt b/Arduino/libraries/readme.txt new file mode 100644 index 0000000..96ce674 --- /dev/null +++ b/Arduino/libraries/readme.txt @@ -0,0 +1 @@ +For information on installing libraries, see: http://www.arduino.cc/en/Guide/Libraries -- cgit v1.2.3-70-g09d2