Skip to content

Commit

Permalink
add better smoothing
Browse files Browse the repository at this point in the history
add calibration reset
  • Loading branch information
vospascal committed Apr 25, 2021
1 parent 9c63af4 commit e528407
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 122 deletions.
85 changes: 0 additions & 85 deletions pedaal/AnalogSmooth.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions pedaal/AnalogSmooth.h

This file was deleted.

18 changes: 12 additions & 6 deletions pedaal/Pedal.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef UtilLib
#include "UtilLibrary.h"

#include "AnalogSmooth.h"
#include "Smoothed.h"

#include <HX711.h>

Expand All @@ -19,8 +19,6 @@
// init util library
UtilLib utilLib;

// set to window 10
AnalogSmooth as = AnalogSmooth(10);

class Pedal
{
Expand All @@ -29,6 +27,8 @@ class Pedal
//initialise pedal
Pedal(String prefix) {
_prefix = prefix;
_mySensor.begin(SMOOTHED_EXPONENTIAL, 10);
_mySensor.clear();
}

void Pedal::ConfigAnalog ( byte analogInput) {
Expand Down Expand Up @@ -84,7 +84,6 @@ class Pedal
Pedal::updatePedal(rawValue);
}


void Pedal::setSmoothValues(int smoothValues) {
_smooth = smoothValues;
}
Expand All @@ -104,7 +103,12 @@ class Pedal
////////////////////
void Pedal::resetCalibrationValues(int EEPROMSpace) {
int resetMap[4] = {0, SENSOR_RANGE, 0, SENSOR_RANGE};
_calibration[0] = resetMap[0];
_calibration[1] = resetMap[1];
_calibration[2] = resetMap[2];
_calibration[3] = resetMap[3];
utilLib.writeStringToEEPROM(EEPROMSpace, utilLib.generateStringMapCali(resetMap));

}

void Pedal::getEEPROMCalibrationValues(int EEPROMSpace) {
Expand Down Expand Up @@ -161,11 +165,12 @@ class Pedal
String _pedalString;
int _afterHID;
int _signal = 0;
Smoothed <int> _mySensor;
HX711 _loadCell;
ADS1115 _ads1015;
int _analogInput = 0;
int _inverted = 0; //0 = false / 1 - true
int _smooth = 0;
int _smooth = 0; //0 = false / 1 - true
int _inputMap[6] = { 0, 20, 40, 60, 80, 100 };
int _outputMap[6] = { 0, 20, 40, 60, 80, 100 };
int _calibration[4] = {0, SENSOR_RANGE, 0, SENSOR_RANGE}; // calibration low, calibration high, deadzone low, deadzone high
Expand All @@ -181,7 +186,8 @@ class Pedal
////////////////////////////////////////////////////////////////////////////////

if (_smooth == 1) {
rawValue = as.smooth(rawValue);
_mySensor.add(rawValue);
rawValue = _mySensor.get();
}

if (_inverted == 1) {
Expand Down
7 changes: 7 additions & 0 deletions pedaal/Smoothed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Smoothed.cpp
* Store and calculate smoothed values from sensors.
* Created by Matt Fryer on 2017-11-17.
* Licensed under LGPL (free to modify and use as you wish)
*/

199 changes: 199 additions & 0 deletions pedaal/Smoothed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Smoothed.h
* Store and calculate smoothed values from sensors.
* Created by Matt Fryer on 2017-11-17.
* Licensed under LGPL (free to modify and use as you wish)
*/

#pragma once

#define SMOOTHED_AVERAGE 1
#define SMOOTHED_EXPONENTIAL 2

// A class used to store and calculate the values to be smoothed.
template <typename T>
class Smoothed {
private:
byte smoothMode;
uint16_t smoothReadingsFactor = 10; // The smoothing factor. In average mode, this is the number of readings to average.
uint16_t smoothReadingsPosition = 0; // Current position in the array
uint16_t smoothReadingsNum = 0; // Number of readings currently being averaged
T *smoothReading; // Array of readings
public:
Smoothed();
~Smoothed(); // Destructor to clean up when class instance killed
bool begin (byte smoothMode, uint16_t smoothFactor = 10);
bool add (T newReading);
T get ();
T getLast ();
bool clear ();
};

// Constructor
template <typename T>
Smoothed<T>::Smoothed () { // Constructor

}

// Destructor
template <typename T>
Smoothed<T>::~Smoothed () { // Destructor
delete[] smoothReading;
}

// Inintialise the array for storing sensor values
template <typename T>
bool Smoothed<T>::begin (byte mode, uint16_t smoothFactor) {
smoothMode = mode;
smoothReadingsFactor = smoothFactor;

switch (smoothMode) {
case SMOOTHED_AVERAGE : // SMOOTHED_AVERAGE

smoothReading = new T[smoothReadingsFactor]; // Create the actual array of the required size

// Initialise all the values in the array to zero
for (int thisReading = 0; thisReading < smoothReadingsNum; thisReading++) {
smoothReading[thisReading] = 0;
}

return true;
break;

case SMOOTHED_EXPONENTIAL : // SMOOTHED_EXPONENTIAL

smoothReading = new T[2];
smoothReading[0] = 0;
smoothReading[1] = 0; // Second value in array used for storing last value added

return true;
break;

default :
return false;
break;
}

}

// Add a value to the array
template <typename T>
bool Smoothed<T>::add (T newReading) {
switch (smoothMode) {
case SMOOTHED_AVERAGE : // SMOOTHED_AVERAGE

if(smoothReadingsNum < smoothReadingsFactor) { smoothReadingsNum++; } // Keep record of the number of readings being averaged. This will count up to the arrany saize then stay at that number

smoothReading[smoothReadingsPosition] = newReading; // Add the new value

if (smoothReadingsPosition == (smoothReadingsFactor - 1)) { // If at the end of the array
smoothReadingsPosition = 0; // Increment to the beginning of the array
} else {
smoothReadingsPosition++; // Increment to next array position position
}

return true;
break;

case SMOOTHED_EXPONENTIAL : // SMOOTHED_EXPONENTIAL

if( smoothReadingsNum == 0 ) {
smoothReadingsNum++;
smoothReading[0] = newReading;
} else {
smoothReading[0] = (T)(((long double)smoothReadingsFactor/100) * newReading + (1 - ((long double)smoothReadingsFactor/100)) * smoothReading[0]);
}

smoothReading[1] = newReading; // Update the last value added

return true;
break;

default :
return false;
break;
}
}

// Get the smoothed result
template <typename T>
T Smoothed<T>::get () {
switch (smoothMode) {
case SMOOTHED_AVERAGE : { // SMOOTHED_AVERAGE
T runningTotal = 0;
// calculating a `SUM(smoothReadings) / smoothReadingsNum` can lead to overflows.
T tmpRes = 0;
T remainder = 0;
for (int x = 0; x < smoothReadingsNum; x++) {
tmpRes = smoothReading[x] / smoothReadingsNum;
remainder += smoothReading[x] - tmpRes * smoothReadingsNum;
runningTotal += tmpRes;
if (remainder > smoothReadingsNum) {
tmpRes = remainder / smoothReadingsNum;
remainder -= tmpRes * smoothReadingsNum;
runningTotal += tmpRes;
}
}
return runningTotal;
}
break;

case SMOOTHED_EXPONENTIAL : // SMOOTHED_EXPONENTIAL
return smoothReading[0];
break;

default :
return false;
break;
}
}

// Gets the last result stored
template <typename T>
T Smoothed<T>::getLast () {
switch (smoothMode) {
case SMOOTHED_AVERAGE : // SMOOTHED_AVERAGE
// Just return the last reading
if (smoothReadingsPosition == 0) {
return smoothReading[smoothReadingsFactor-1];
} else {
return smoothReading[smoothReadingsPosition-1];
}
break;

case SMOOTHED_EXPONENTIAL : // SMOOTHED_EXPONENTIAL
return smoothReading[1];
break;

default :
return false;
break;
}
}

// Clears all stored values
template <typename T>
bool Smoothed<T>::clear () {
switch (smoothMode) {
case SMOOTHED_AVERAGE : // SMOOTHED_AVERAGE
// Reset the counters
smoothReadingsPosition = 0;
smoothReadingsNum = 0;

// Set all the values in the array to zero. Not really needed
for (int thisReading = 0; thisReading < smoothReadingsNum; thisReading++) {
smoothReading[thisReading] = 0;
}
break;

case SMOOTHED_EXPONENTIAL : // SMOOTHED_EXPONENTIAL
smoothReadingsNum = 0;
smoothReading[0] = 0;
smoothReading[1] = 0;
break;

default :
return false;
break;
}
}
Loading

0 comments on commit e528407

Please sign in to comment.