Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Fri May 15 14:07:51 2015 +0000
Revision:
112:3872569be2af
Parent:
100:dbcd3bc51758
Child:
113:80610be056e2
data smoothing for pots readings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xinlei 100:dbcd3bc51758 1 #include <stdio.h>
xinlei 112:3872569be2af 2 #include "rtos.h"
xinlei 100:dbcd3bc51758 3 #include "Potentiometer.h"
xinlei 100:dbcd3bc51758 4 #include "SmartRestConf.h"
xinlei 100:dbcd3bc51758 5 #include "logging.h"
xinlei 100:dbcd3bc51758 6
xinlei 100:dbcd3bc51758 7 // Cutoff for avoiding sending similar sensor data.
xinlei 100:dbcd3bc51758 8 #define THRESHOLD_PERCENT_ANA 0.02
xinlei 100:dbcd3bc51758 9 // Timeout for forcing a sending even if readings are similar [seconds]
xinlei 100:dbcd3bc51758 10 #define TIME_LIMIT_ANA 900
xinlei 100:dbcd3bc51758 11
xinlei 100:dbcd3bc51758 12 AnalogIn analog1(A0);
xinlei 100:dbcd3bc51758 13 AnalogIn analog2(A1);
xinlei 100:dbcd3bc51758 14
xinlei 100:dbcd3bc51758 15 size_t Potentiometer::read(char *buf, size_t maxLen, char *status, size_t num)
xinlei 100:dbcd3bc51758 16 {
xinlei 100:dbcd3bc51758 17 static const char *fmt = "107,%ld,%f,%f\r\n";
xinlei 112:3872569be2af 18 const unsigned short N = 10;
xinlei 112:3872569be2af 19 float data[2] = {0, 0};
xinlei 112:3872569be2af 20 float min[2] = {100, 100};
xinlei 112:3872569be2af 21 float max[2] = {0, 0};
xinlei 112:3872569be2af 22 // multiple sampling for data smoothing
xinlei 112:3872569be2af 23 for (unsigned short i = 0; i < N; ++i) {
xinlei 112:3872569be2af 24 float d0 = (float)analog1*100;
xinlei 112:3872569be2af 25 float d1 = (float)analog2*100;
xinlei 112:3872569be2af 26 data[0] += d0;
xinlei 112:3872569be2af 27 data[1] += d1;
xinlei 112:3872569be2af 28 min[0] = min[0] <= d0 ? min[0] : d0;
xinlei 112:3872569be2af 29 min[1] = min[1] <= d1 ? min[1] : d1;
xinlei 112:3872569be2af 30 max[0] = max[0] >= d0 ? max[0] : d0;
xinlei 112:3872569be2af 31 max[1] = max[1] >= d1 ? max[1] : d1;
xinlei 112:3872569be2af 32 Thread::wait(10);
xinlei 112:3872569be2af 33 }
xinlei 112:3872569be2af 34 data[0] = (data[0]-min[0]-max[0]) / (N-2);
xinlei 112:3872569be2af 35 data[1] = (data[1]-min[1]-max[1]) / (N-2);
xinlei 100:dbcd3bc51758 36
xinlei 100:dbcd3bc51758 37 if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ANA &&
xinlei 100:dbcd3bc51758 38 abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ANA) {
xinlei 100:dbcd3bc51758 39 time_t t_interval = time(NULL) - t_start;
xinlei 100:dbcd3bc51758 40 if (t_interval < TIME_LIMIT_ANA) {
xinlei 100:dbcd3bc51758 41 return 0;
xinlei 100:dbcd3bc51758 42 } else {
xinlei 100:dbcd3bc51758 43 aDebug("Poti: Timeout at %d s.\n", t_interval);
xinlei 100:dbcd3bc51758 44 }
xinlei 100:dbcd3bc51758 45 }
xinlei 100:dbcd3bc51758 46
xinlei 100:dbcd3bc51758 47 size_t l = snprintf(buf, maxLen, fmt, deviceID, data[0], data[1]);
xinlei 100:dbcd3bc51758 48 if (status) {
xinlei 100:dbcd3bc51758 49 snprintf(status, num, "Send Poti %.1f,%.1f", data[0], data[1]);
xinlei 100:dbcd3bc51758 50 }
xinlei 100:dbcd3bc51758 51 oldValues[0] = data[0];
xinlei 100:dbcd3bc51758 52 oldValues[1] = data[1];
xinlei 100:dbcd3bc51758 53 t_start = time(NULL);
xinlei 100:dbcd3bc51758 54 return l;
xinlei 100:dbcd3bc51758 55 }