Skip to content

Commit e528407

Browse files
committed
add better smoothing
add calibration reset
1 parent 9c63af4 commit e528407

File tree

6 files changed

+227
-122
lines changed

6 files changed

+227
-122
lines changed

pedaal/AnalogSmooth.cpp

Lines changed: 0 additions & 85 deletions
This file was deleted.

pedaal/AnalogSmooth.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

pedaal/Pedal.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef UtilLib
1111
#include "UtilLibrary.h"
1212

13-
#include "AnalogSmooth.h"
13+
#include "Smoothed.h"
1414

1515
#include <HX711.h>
1616

@@ -19,8 +19,6 @@
1919
// init util library
2020
UtilLib utilLib;
2121

22-
// set to window 10
23-
AnalogSmooth as = AnalogSmooth(10);
2422

2523
class Pedal
2624
{
@@ -29,6 +27,8 @@ class Pedal
2927
//initialise pedal
3028
Pedal(String prefix) {
3129
_prefix = prefix;
30+
_mySensor.begin(SMOOTHED_EXPONENTIAL, 10);
31+
_mySensor.clear();
3232
}
3333

3434
void Pedal::ConfigAnalog ( byte analogInput) {
@@ -84,7 +84,6 @@ class Pedal
8484
Pedal::updatePedal(rawValue);
8585
}
8686

87-
8887
void Pedal::setSmoothValues(int smoothValues) {
8988
_smooth = smoothValues;
9089
}
@@ -104,7 +103,12 @@ class Pedal
104103
////////////////////
105104
void Pedal::resetCalibrationValues(int EEPROMSpace) {
106105
int resetMap[4] = {0, SENSOR_RANGE, 0, SENSOR_RANGE};
106+
_calibration[0] = resetMap[0];
107+
_calibration[1] = resetMap[1];
108+
_calibration[2] = resetMap[2];
109+
_calibration[3] = resetMap[3];
107110
utilLib.writeStringToEEPROM(EEPROMSpace, utilLib.generateStringMapCali(resetMap));
111+
108112
}
109113

110114
void Pedal::getEEPROMCalibrationValues(int EEPROMSpace) {
@@ -161,11 +165,12 @@ class Pedal
161165
String _pedalString;
162166
int _afterHID;
163167
int _signal = 0;
168+
Smoothed <int> _mySensor;
164169
HX711 _loadCell;
165170
ADS1115 _ads1015;
166171
int _analogInput = 0;
167172
int _inverted = 0; //0 = false / 1 - true
168-
int _smooth = 0;
173+
int _smooth = 0; //0 = false / 1 - true
169174
int _inputMap[6] = { 0, 20, 40, 60, 80, 100 };
170175
int _outputMap[6] = { 0, 20, 40, 60, 80, 100 };
171176
int _calibration[4] = {0, SENSOR_RANGE, 0, SENSOR_RANGE}; // calibration low, calibration high, deadzone low, deadzone high
@@ -181,7 +186,8 @@ class Pedal
181186
////////////////////////////////////////////////////////////////////////////////
182187

183188
if (_smooth == 1) {
184-
rawValue = as.smooth(rawValue);
189+
_mySensor.add(rawValue);
190+
rawValue = _mySensor.get();
185191
}
186192

187193
if (_inverted == 1) {

pedaal/Smoothed.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Smoothed.cpp
3+
* Store and calculate smoothed values from sensors.
4+
* Created by Matt Fryer on 2017-11-17.
5+
* Licensed under LGPL (free to modify and use as you wish)
6+
*/
7+

pedaal/Smoothed.h

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* Smoothed.h
3+
* Store and calculate smoothed values from sensors.
4+
* Created by Matt Fryer on 2017-11-17.
5+
* Licensed under LGPL (free to modify and use as you wish)
6+
*/
7+
8+
#pragma once
9+
10+
#define SMOOTHED_AVERAGE 1
11+
#define SMOOTHED_EXPONENTIAL 2
12+
13+
// A class used to store and calculate the values to be smoothed.
14+
template <typename T>
15+
class Smoothed {
16+
private:
17+
byte smoothMode;
18+
uint16_t smoothReadingsFactor = 10; // The smoothing factor. In average mode, this is the number of readings to average.
19+
uint16_t smoothReadingsPosition = 0; // Current position in the array
20+
uint16_t smoothReadingsNum = 0; // Number of readings currently being averaged
21+
T *smoothReading; // Array of readings
22+
public:
23+
Smoothed();
24+
~Smoothed(); // Destructor to clean up when class instance killed
25+
bool begin (byte smoothMode, uint16_t smoothFactor = 10);
26+
bool add (T newReading);
27+
T get ();
28+
T getLast ();
29+
bool clear ();
30+
};
31+
32+
// Constructor
33+
template <typename T>
34+
Smoothed<T>::Smoothed () { // Constructor
35+
36+
}
37+
38+
// Destructor
39+
template <typename T>
40+
Smoothed<T>::~Smoothed () { // Destructor
41+
delete[] smoothReading;
42+
}
43+
44+
// Inintialise the array for storing sensor values
45+
template <typename T>
46+
bool Smoothed<T>::begin (byte mode, uint16_t smoothFactor) {
47+
smoothMode = mode;
48+
smoothReadingsFactor = smoothFactor;
49+
50+
switch (smoothMode) {
51+
case SMOOTHED_AVERAGE : // SMOOTHED_AVERAGE
52+
53+
smoothReading = new T[smoothReadingsFactor]; // Create the actual array of the required size
54+
55+
// Initialise all the values in the array to zero
56+
for (int thisReading = 0; thisReading < smoothReadingsNum; thisReading++) {
57+
smoothReading[thisReading] = 0;
58+
}
59+
60+
return true;
61+
break;
62+
63+
case SMOOTHED_EXPONENTIAL : // SMOOTHED_EXPONENTIAL
64+
65+
smoothReading = new T[2];
66+
smoothReading[0] = 0;
67+
smoothReading[1] = 0; // Second value in array used for storing last value added
68+
69+
return true;
70+
break;
71+
72+
default :
73+
return false;
74+
break;
75+
}
76+
77+
}
78+
79+
// Add a value to the array
80+
template <typename T>
81+
bool Smoothed<T>::add (T newReading) {
82+
switch (smoothMode) {
83+
case SMOOTHED_AVERAGE : // SMOOTHED_AVERAGE
84+
85+
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
86+
87+
smoothReading[smoothReadingsPosition] = newReading; // Add the new value
88+
89+
if (smoothReadingsPosition == (smoothReadingsFactor - 1)) { // If at the end of the array
90+
smoothReadingsPosition = 0; // Increment to the beginning of the array
91+
} else {
92+
smoothReadingsPosition++; // Increment to next array position position
93+
}
94+
95+
return true;
96+
break;
97+
98+
case SMOOTHED_EXPONENTIAL : // SMOOTHED_EXPONENTIAL
99+
100+
if( smoothReadingsNum == 0 ) {
101+
smoothReadingsNum++;
102+
smoothReading[0] = newReading;
103+
} else {
104+
smoothReading[0] = (T)(((long double)smoothReadingsFactor/100) * newReading + (1 - ((long double)smoothReadingsFactor/100)) * smoothReading[0]);
105+
}
106+
107+
smoothReading[1] = newReading; // Update the last value added
108+
109+
return true;
110+
break;
111+
112+
default :
113+
return false;
114+
break;
115+
}
116+
}
117+
118+
// Get the smoothed result
119+
template <typename T>
120+
T Smoothed<T>::get () {
121+
switch (smoothMode) {
122+
case SMOOTHED_AVERAGE : { // SMOOTHED_AVERAGE
123+
T runningTotal = 0;
124+
// calculating a `SUM(smoothReadings) / smoothReadingsNum` can lead to overflows.
125+
T tmpRes = 0;
126+
T remainder = 0;
127+
for (int x = 0; x < smoothReadingsNum; x++) {
128+
tmpRes = smoothReading[x] / smoothReadingsNum;
129+
remainder += smoothReading[x] - tmpRes * smoothReadingsNum;
130+
runningTotal += tmpRes;
131+
if (remainder > smoothReadingsNum) {
132+
tmpRes = remainder / smoothReadingsNum;
133+
remainder -= tmpRes * smoothReadingsNum;
134+
runningTotal += tmpRes;
135+
}
136+
}
137+
return runningTotal;
138+
}
139+
break;
140+
141+
case SMOOTHED_EXPONENTIAL : // SMOOTHED_EXPONENTIAL
142+
return smoothReading[0];
143+
break;
144+
145+
default :
146+
return false;
147+
break;
148+
}
149+
}
150+
151+
// Gets the last result stored
152+
template <typename T>
153+
T Smoothed<T>::getLast () {
154+
switch (smoothMode) {
155+
case SMOOTHED_AVERAGE : // SMOOTHED_AVERAGE
156+
// Just return the last reading
157+
if (smoothReadingsPosition == 0) {
158+
return smoothReading[smoothReadingsFactor-1];
159+
} else {
160+
return smoothReading[smoothReadingsPosition-1];
161+
}
162+
break;
163+
164+
case SMOOTHED_EXPONENTIAL : // SMOOTHED_EXPONENTIAL
165+
return smoothReading[1];
166+
break;
167+
168+
default :
169+
return false;
170+
break;
171+
}
172+
}
173+
174+
// Clears all stored values
175+
template <typename T>
176+
bool Smoothed<T>::clear () {
177+
switch (smoothMode) {
178+
case SMOOTHED_AVERAGE : // SMOOTHED_AVERAGE
179+
// Reset the counters
180+
smoothReadingsPosition = 0;
181+
smoothReadingsNum = 0;
182+
183+
// Set all the values in the array to zero. Not really needed
184+
for (int thisReading = 0; thisReading < smoothReadingsNum; thisReading++) {
185+
smoothReading[thisReading] = 0;
186+
}
187+
break;
188+
189+
case SMOOTHED_EXPONENTIAL : // SMOOTHED_EXPONENTIAL
190+
smoothReadingsNum = 0;
191+
smoothReading[0] = 0;
192+
smoothReading[1] = 0;
193+
break;
194+
195+
default :
196+
return false;
197+
break;
198+
}
199+
}

0 commit comments

Comments
 (0)