Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Diff: measurement/Potentiometer.cpp
- Revision:
- 112:3872569be2af
- Parent:
- 100:dbcd3bc51758
- Child:
- 113:80610be056e2
--- a/measurement/Potentiometer.cpp Fri May 15 10:14:05 2015 +0000 +++ b/measurement/Potentiometer.cpp Fri May 15 14:07:51 2015 +0000 @@ -1,4 +1,5 @@ #include <stdio.h> +#include "rtos.h" #include "Potentiometer.h" #include "SmartRestConf.h" #include "logging.h" @@ -14,7 +15,24 @@ size_t Potentiometer::read(char *buf, size_t maxLen, char *status, size_t num) { static const char *fmt = "107,%ld,%f,%f\r\n"; - float data[2] = {(float)analog1*100, (float)analog2*100}; + const unsigned short N = 10; + float data[2] = {0, 0}; + float min[2] = {100, 100}; + float max[2] = {0, 0}; + // multiple sampling for data smoothing + for (unsigned short i = 0; i < N; ++i) { + float d0 = (float)analog1*100; + float d1 = (float)analog2*100; + data[0] += d0; + data[1] += d1; + min[0] = min[0] <= d0 ? min[0] : d0; + min[1] = min[1] <= d1 ? min[1] : d1; + max[0] = max[0] >= d0 ? max[0] : d0; + max[1] = max[1] >= d1 ? max[1] : d1; + Thread::wait(10); + } + data[0] = (data[0]-min[0]-max[0]) / (N-2); + data[1] = (data[1]-min[1]-max[1]) / (N-2); if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ANA && abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ANA) {