-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInputProcessor.cpp
More file actions
289 lines (220 loc) · 5.54 KB
/
Copy pathInputProcessor.cpp
File metadata and controls
289 lines (220 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
/*
InputProcessor.h - Library for dealing with sensors in arduino in easier ways
by Comingle / A Quitmeyer Public Domain
get calibration, buffer smoothing, derivatives, and scaling easily
calibrating sensors hooked to analog inputs.
InputProcessor is released under the
hacked off examples by
Copyright 2013 Julian Vidal
https://github.com/poisa/Calibrator
and Rob Tillaart
arduino rolling average / smoothing example
*/
#include "Arduino.h"
#include "InputProcessor.h"
#include <stdlib.h>
//Setup buffer and analog pin (if supplied)
InputProcessor::InputProcessor(int buffersize, int thepin) // if no pin
{
_size = buffersize;
_ar = (int*) malloc(_size * sizeof(int));
if (_ar == NULL) _size = 0;
clearBuffer();
pin = thepin;
}
InputProcessor::~InputProcessor()
{
if (_ar != NULL) free(_ar);
}
/*
* Takes input if already calibrated to just use a sensor pin
*/
int InputProcessor::update()
{
prevRawValue = rawValue;
rawValue = analogRead(pin);
addValue(rawValue); //add the value to our buffer
scaledValue = scaleValue(rawValue);
return scaledValue;
}
/*
* Takes input from the output of a different sensor function
*/
int InputProcessor::update(int funcval) {
prevRawValue = rawValue;
rawValue = funcval;
addValue(rawValue); //add the value to our buffer
scaledValue = scaleValue(rawValue);
return scaledValue;
}
//Takes in a value from an arbitrary function you setup and feed to it
int InputProcessor::update(int (*function)()) {
prevRawValue = rawValue;
rawValue = (int)function();
addValue(rawValue); //add the value to our buffer
scaledValue = scaleValue(rawValue);
return scaledValue;
}
//calibrate for a certain duration in millis
//Used on basic analog input where we already setup the pin
void InputProcessor::calibrate(int duration)
{
long ctime = millis();
resetCal();
while (millis() - ctime < duration) {
Serial.println(millis() - ctime);
setValue(analogRead(pin));
}
lastCal = millis();
calmode = false;
}
//calibrate for a certain duration in millis
//Used on input with some outside function providing the value
void InputProcessor::calibrate(int duration, int funcval)
{
long ctime = millis();
resetCal();
while (millis() < duration + ctime) {
Serial.println(millis() - ctime);
setValue(funcval);
}
lastCal = millis();
calmode = false;
}
//Takes in a value from an arbitrary function you setup and feed to it
void InputProcessor::calibrate(int duration, int (*function)()) {
long ctime = millis();
resetCal();
while (millis() < duration + ctime) {
Serial.println(millis() - ctime);
setValue((int)function());
}
lastCal = millis();
calmode = false;
}
void InputProcessor::setValue(int currentValue)
{
if (currentValue > max) {
max = currentValue;
}
if (currentValue < min) {
min = currentValue;
}
};
/*
*Takes in a value and scales it between 0-255 for use with motors and leds and other PWM
*/
int InputProcessor::scaleValue(int currentValue)
{
currentValue = map(currentValue, min, max, 0, 255);
currentValue = constrain(currentValue, 0, 255);
return currentValue;
}
/*
*Takes in a value and scales it between 0-1023 for full resolution use
*/
int InputProcessor::scaleValue10bit(int currentValue)
{
currentValue = map(currentValue, min, max, 0, 1023);
currentValue = constrain(currentValue, 0, 1023);
return currentValue;
}
int InputProcessor::getMin()
{
return min;
}
int InputProcessor::getMax()
{
return max;
}
void InputProcessor::resetCal()
{
min = 32000;
max = -32000;
}
// resets all counters
void InputProcessor::clearBuffer()
{
_cnt = 0;
_idx = 0;
_sum = 0.0;
for (int i = 0; i < _size; i++) _ar[i] = 0.0; // needed to keep addValue simple
}
// adds a new value to the data-set
void InputProcessor::addValue(int f)
{
if (_ar == NULL) return;
_sum -= _ar[_idx];
_ar[_idx] = f;
_sum += _ar[_idx];
_idx++;
if (_idx == _size) _idx = 0; // faster than %
if (_cnt < _size) _cnt++;
}
// returns the average of the data-set added sofar
float InputProcessor::getAverage()
{
if (_cnt == 0) return NAN;
return _sum / _cnt;
}
// returns the average of the data-set added sofar scaled between max and min
float InputProcessor::getscaledAverage()
{
if (_cnt == 0) return NAN;
return scaleValue(_sum / _cnt);
}
float InputProcessor::getSTDEV(){
float square;
float sum;
float mu;
float theta;
int i;
if (_cnt == 0) {
return 0;
}
mu = getAverage();
sum = 0;
for(uint32_t i = 0; i < _cnt; i++) {
theta = mu - getElement(i);
square = theta * theta;
sum += square;
}
return sqrt(sum/(float)_cnt);
}
// returns the value of an element if exist, 0 otherwise
float InputProcessor::getElement(uint8_t idx)
{
if (idx >= _cnt ) return NAN;
return _ar[idx];
}
int InputProcessor::getDerivative() {
//Instant derivative
//Subtract the previous value from the current
return rawValue - prevRawValue;
// int deriv=-1;
// if(_idx == 0){
// deriv = getElement(_idx) - getElement(_size);
//
// }
// else{
// deriv = getElement(_idx) - getElement(_idx-1);
// }
// return deriv;
}
int InputProcessor::getscaledDerivative() {
//Instant derivative
//Subtract the previous value from the current
return scaleValue(rawValue - prevRawValue);
}
// fill the average with a value
// the param number determines how often value is added (weight)
// number should preferably be between 1 and size
void InputProcessor::fillValue(int value, int number)
{
clearBuffer();
for (int i = 0; i < number; i++)
{
addValue(value);
}
}