-
Notifications
You must be signed in to change notification settings - Fork 2
/
LoadcellSensor.cpp
384 lines (336 loc) · 12.5 KB
/
LoadcellSensor.cpp
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**************************************************************************/
/*!
@file LoadcellSensor.cpp
@mainpage LoadcellSensor data processing class
@section intro Introduction
This is a library for processing raw values from a load cell or similar sensor
for application in HCI applications (e.g. for mouse cursor control).
The values are noise-filtered and compared to a baseline which adapts to slow drifting.
Automatic calibration is supported (either by resetting the baseline or by apating movement thresholds).
Note: the values should be fed into the LoadcellSensor::process() method periodically
Thanks to Jim Peters for the marvellous fiview filter tool and the fidlib filter library:
http://uazu.net/fiview/
The AsTeRICS Foundation promotes Open Source Assistive Technologies, visit:
https://www.asterics-foundation.org
@section author Author
Chris Veigl (AsTeRICS Foundation)
@section license License
GPL (see license.txt)
*/
/**************************************************************************/
#include <Arduino.h>
#include "LoadcellSensor.h"
/**************************************************************************/
/*!
@brief Instantiates a new LoadcellSensor class, sets default values
*/
/**************************************************************************/
LoadcellSensor::LoadcellSensor () {
offset=activity=thresholdCorrection=0;
lastFilteredValue=lastActivityValue=0;
autoCalibrationMode=AUTOCALIBRATION_RESET_BASELINE;
activityTimestamp=0;
moving=false;baselineLocked=false;
movementThreshold=MOVEMENT_THRESHOLD;
idleDetectionThreshold=IDLE_DETECTION_THRESHOLD;
idleDetectionPeriod=IDLE_DETECTION_PERIOD;
thresholdDecay=THRESHOLD_DECAY;
gain=GAIN;
sampleRate=SAMPLE_RATE;
lpBaseline=LP_BASELINE;
lpNoise=LP_NOISE;
lpActivity=LP_ACTIVITY;
initFilters(FILTERS_ALL);
calib();
}
LoadcellSensor::~LoadcellSensor () {
freeFilters(FILTERS_ALL);
}
/**************************************************************************/
/*!
@brief processes the next raw data value, must be called periodically
@param value Raw data value.
@return processed movement estimation value.
*/
/**************************************************************************/
int32_t LoadcellSensor::process (int32_t value) {
int32_t result=0;
value*=gain;
raw=value;
int32_t a=func_activity(fbuf_activity,raw);
activity += abs(a-lastActivityValue);
lastActivityValue=a;
// remove offset from adjust raw values
raw-=offset;
// calculate filtered channel value
filtered= func_noise(fbuf_noise,raw);
lastFilteredValue=filtered;
// autocalibration / idle detection
if (autoCalibrationMode && moving) {
if (millis()-activityTimestamp > idleDetectionPeriod) {
// check if threshold or baseline should be adapted
if (activity < idleDetectionThreshold) {
switch (autoCalibrationMode) {
case AUTOCALIBRATION_RESET_BASELINE:
calib();
break;
case AUTOCALIBRATION_ADAPT_THRESHOLD:
thresholdCorrection += movementThreshold/2; // increase movement threshold
break;
}
}
clearActivity();
activityTimestamp=millis();
}
}
// handle baseline and movement
int actThreshold = movementThreshold + abs(thresholdCorrection);
if ((abs(filtered-baseline) <= actThreshold )) {
moving=false;
if (!baselineLocked)
baseline= func_baseline(fbuf_baseline,raw);
if (thresholdCorrection>0)
thresholdCorrection*=thresholdDecay;
}
if (abs(filtered-baseline) > actThreshold ) { // moving! leave baseline as it is!
if (!moving) {
activityTimestamp=millis();
activity+=idleDetectionThreshold;
moving=true;
}
result=filtered-baseline;
if (result < 0) result += actThreshold;
else result -= actThreshold;
}
return(result);
}
/**************************************************************************/
/*!
@brief calculates the sign (signum) of an integer variable
@param x the integer value
@return the sign value of x (-1/0/1)
*/
/**************************************************************************/
int LoadcellSensor::sgn(int x) {
return (x > 0) - (x < 0);
}
/**************************************************************************/
/*!
@brief initiates a calibration (offset is set to current filtered value)
*/
/**************************************************************************/
void LoadcellSensor::calib(void) { // perform offset calibration
fid_run_zapbuf(fbuf_baseline);
fid_run_zapbuf(fbuf_noise);
fid_run_zapbuf(fbuf_activity);
baseline= func_baseline(fbuf_baseline, 0);
offset+=filtered;
}
/**************************************************************************/
/*!
@brief returns the current accumulated activity value
@return activity value
*/
/**************************************************************************/
int32_t LoadcellSensor::getActivity(void) {
return (activity);
}
/**************************************************************************/
/*!
@brief sets the current accumulated activity value to 0
*/
/**************************************************************************/
void LoadcellSensor::clearActivity(void) {
activity=0;
}
/**************************************************************************/
/*!
@brief sets movement threshold value
@param movementThreshold the threshold value
*/
/**************************************************************************/
void LoadcellSensor::setMovementThreshold(int32_t movementThreshold) {
this->movementThreshold=movementThreshold;
}
/**************************************************************************/
/*!
@brief sets idle detection threshold value
@param idleDetectionThreshold the threshold value
*/
/**************************************************************************/
void LoadcellSensor::setIdleDetectionThreshold(int32_t idleDetectionThreshold) {
this->idleDetectionThreshold=idleDetectionThreshold;
}
/**************************************************************************/
/*!
@brief sets idle detection period
@param idleDetectionPeriod the measurement period (in milliseconds)
*/
/**************************************************************************/
void LoadcellSensor::setIdleDetectionPeriod(int32_t idleDetectionPeriod) {
this->idleDetectionPeriod=idleDetectionPeriod;
}
/**************************************************************************/
/*!
@brief sets overshoot threshold decay
@param thresholdDecay the threshold decay (0-1, close to 1 -> long decay)
*/
/**************************************************************************/
void LoadcellSensor::setThresholdDecay(double thresholdDecay) {
this->thresholdDecay=thresholdDecay;
}
/**************************************************************************/
/*!
@brief sets gain for incoming values
@param gain factor
*/
/**************************************************************************/
void LoadcellSensor::setGain(double gain) {
this->gain=gain;
}
/**************************************************************************/
/*!
@brief sets sampling rate for signal processing / filtering
@param sampleRate: sampling rate (Hz)
*/
/**************************************************************************/
void LoadcellSensor::setSampleRate(double sampleRate) {
this->sampleRate=sampleRate;
freeFilters(FILTERS_ALL);
initFilters(FILTERS_ALL);
}
/**************************************************************************/
/*!
@brief sets low pass filter for baseline adjustment
@param lpBaseline: low pass cutoff frequency
*/
/**************************************************************************/
void LoadcellSensor::setBaselineLowpass(double lpBaseline) {
if (this->lpBaseline!=lpBaseline) {
this->lpBaseline=lpBaseline;
freeFilters(FILTER_BASELINE);
initFilters(FILTER_BASELINE);
}
}
/**************************************************************************/
/*!
@brief sets low pass filter for noise removal from signal
@param lpNoise: low pass cutoff frequency
*/
/**************************************************************************/
void LoadcellSensor::setNoiseLowpass(double lpNoise) {
if (this->lpNoise!=lpNoise) {
this->lpNoise=lpNoise;
freeFilters(FILTER_NOISE);
initFilters(FILTER_NOISE);
}
}
/**************************************************************************/
/*!
@brief sets low pass filter for activity measurement
@param lpActivity: low pass cutoff frequency
*/
/**************************************************************************/
void LoadcellSensor::setActivityLowpass(double lpActivity) {
if (this->lpActivity!=lpActivity) {
this->lpActivity=lpActivity;
freeFilters(FILTER_ACTIVITY);
initFilters(FILTER_ACTIVITY);
}
}
/**************************************************************************/
/*!
@brief set autocalibration mode
@param m: mode
*/
/**************************************************************************/
void LoadcellSensor::setAutoCalibrationMode(uint8_t m) {
autoCalibrationMode=m;
}
/**************************************************************************/
/*!
@brief returns if movement is currently active
@return true if movement active, false otherwise
*/
/**************************************************************************/
bool LoadcellSensor::isMoving(void) {
return (moving);
}
/**************************************************************************/
/*!
@brief sets the lockmode for baseline updates
@return none
*/
/**************************************************************************/
void LoadcellSensor::lockBaseline(bool b) {
baselineLocked=b;
}
/**************************************************************************/
/*!
@brief prints current signal values to Serial window (plotter)
@param mask mask for signal selection, bit0: raw signal, bit1:filtered signal
bit2: baseline signal
@param limit constrain for signal plotter (e.g 1000 limits signal to -1000/1000)
*/
/**************************************************************************/
void LoadcellSensor::printValues(uint8_t mask, int32_t limit) {
if (mask&1) Serial.print(constrain(raw,-limit,limit)); else Serial.print("0");
Serial.print(" ");
if (mask&2) Serial.print(constrain(filtered,-limit,limit)); else Serial.print("0");
Serial.print(" ");
if (mask&4) {
Serial.print(constrain(baseline,-limit,limit));
Serial.print(" ");
Serial.print(constrain(baseline-movementThreshold-thresholdCorrection,-limit,limit));
Serial.print(" ");
Serial.print(constrain(baseline+movementThreshold+thresholdCorrection,-limit,limit));
} else Serial.print("0 0 0");
Serial.print(" ");
// if (autoCalibrationEnabled) Serial.print(constrain(activity,-limit,limit)); else Serial.print("0");
// Serial.print(" ");
}
/**************************************************************************/
/*!
@brief initialise filters for actual sampling rate and cutoff frequencies
@param filterMask: a binary mask byte, identifying the filter(s)
*/
/**************************************************************************/
void LoadcellSensor::initFilters(uint8_t filterMask)
{
if (filterMask & FILTER_BASELINE) {
filt_baseline=fid_design((char *)"LpBe2", sampleRate, lpBaseline, 0, 0, 0);
run_baseline= fid_run_new(filt_baseline, &func_baseline);
fbuf_baseline= fid_run_newbuf(run_baseline);
}
if (filterMask & FILTER_NOISE) {
filt_noise=fid_design((char *)"LpBe2", sampleRate, lpNoise, 0, 0, 0);
run_noise= fid_run_new(filt_noise, &func_noise);
fbuf_noise= fid_run_newbuf(run_noise);
}
if (filterMask & FILTER_ACTIVITY) {
filt_activity=fid_design((char *)"LpBe2", sampleRate, lpActivity, 0, 0, 0);
run_activity= fid_run_new(filt_activity, &func_activity);
fbuf_activity= fid_run_newbuf(run_activity);
}
}
/**************************************************************************/
/*!
@brief free filters and allocated buffers
@param filterMask: a binary mask byte, identifying the filter(s)
*/
/**************************************************************************/
void LoadcellSensor::freeFilters(uint8_t filterMask)
{
if (filterMask & FILTER_BASELINE) {
fid_run_freebuf(run_baseline);
fid_run_free(filt_baseline);
}
if (filterMask & FILTER_NOISE) {
fid_run_freebuf(run_noise);
fid_run_free(filt_noise);
}
if (filterMask & FILTER_ACTIVITY) {
fid_run_freebuf(run_activity);
fid_run_free(filt_activity);
}
}