Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
measurement/Potentiometer.cpp@138:f8ab852e83e7, 2016-08-08 (annotated)
- Committer:
- xinlei
- Date:
- Mon Aug 08 11:05:57 2016 +0000
- Revision:
- 138:f8ab852e83e7
- Parent:
- 136:a52821cdb108
Etisalat and Teleena APN.
Who changed what in which revision?
User | Revision | Line number | New 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 | 114:c54e9731b9de | 15 | static inline bool valueChanged(float *V0, float *V1) |
xinlei | 114:c54e9731b9de | 16 | { |
xinlei | 114:c54e9731b9de | 17 | if (abs(V0[0]-V1[0]) <= abs(V0[0])*THRESHOLD_PERCENT_ANA && |
xinlei | 114:c54e9731b9de | 18 | abs(V0[1]-V1[1]) <= abs(V0[1])*THRESHOLD_PERCENT_ANA) |
xinlei | 114:c54e9731b9de | 19 | return false; |
xinlei | 114:c54e9731b9de | 20 | else |
xinlei | 114:c54e9731b9de | 21 | return true; |
xinlei | 114:c54e9731b9de | 22 | } |
xinlei | 114:c54e9731b9de | 23 | |
xinlei | 136:a52821cdb108 | 24 | int Potentiometer::read(char *buf, size_t maxLen, char *status, size_t num) |
xinlei | 100:dbcd3bc51758 | 25 | { |
xinlei | 100:dbcd3bc51758 | 26 | static const char *fmt = "107,%ld,%f,%f\r\n"; |
xinlei | 116:5de54f09f754 | 27 | float data[2] = {0, 0}; |
xinlei | 116:5de54f09f754 | 28 | float min[2] = {100, 100}; |
xinlei | 116:5de54f09f754 | 29 | float max[2] = {0, 0}; |
xinlei | 116:5de54f09f754 | 30 | // multiple sampling for data smoothing |
xinlei | 123:311fa85af2b3 | 31 | const unsigned short N = 20; |
xinlei | 116:5de54f09f754 | 32 | for (unsigned short i = 0; i < N; ++i) { |
xinlei | 116:5de54f09f754 | 33 | float d0 = (float)analog1*100; |
xinlei | 116:5de54f09f754 | 34 | float d1 = (float)analog2*100; |
xinlei | 116:5de54f09f754 | 35 | data[0] += d0; |
xinlei | 116:5de54f09f754 | 36 | data[1] += d1; |
xinlei | 116:5de54f09f754 | 37 | min[0] = min[0] <= d0 ? min[0] : d0; |
xinlei | 116:5de54f09f754 | 38 | min[1] = min[1] <= d1 ? min[1] : d1; |
xinlei | 116:5de54f09f754 | 39 | max[0] = max[0] >= d0 ? max[0] : d0; |
xinlei | 116:5de54f09f754 | 40 | max[1] = max[1] >= d1 ? max[1] : d1; |
xinlei | 124:979ab0d075de | 41 | Thread::wait(10); |
xinlei | 116:5de54f09f754 | 42 | } |
xinlei | 116:5de54f09f754 | 43 | data[0] = (data[0]-min[0]-max[0]) / (N-2); |
xinlei | 116:5de54f09f754 | 44 | data[1] = (data[1]-min[1]-max[1]) / (N-2); |
xinlei | 116:5de54f09f754 | 45 | |
xinlei | 123:311fa85af2b3 | 46 | float t_interval = timer.read(); |
xinlei | 123:311fa85af2b3 | 47 | if (!valueChanged(oldValues, data) && t_interval < TIME_LIMIT_ANA) { |
xinlei | 123:311fa85af2b3 | 48 | return 0; |
xinlei | 100:dbcd3bc51758 | 49 | } |
xinlei | 100:dbcd3bc51758 | 50 | |
xinlei | 100:dbcd3bc51758 | 51 | size_t l = snprintf(buf, maxLen, fmt, deviceID, data[0], data[1]); |
xinlei | 100:dbcd3bc51758 | 52 | if (status) { |
xinlei | 100:dbcd3bc51758 | 53 | snprintf(status, num, "Send Poti %.1f,%.1f", data[0], data[1]); |
xinlei | 100:dbcd3bc51758 | 54 | } |
xinlei | 100:dbcd3bc51758 | 55 | oldValues[0] = data[0]; |
xinlei | 100:dbcd3bc51758 | 56 | oldValues[1] = data[1]; |
xinlei | 123:311fa85af2b3 | 57 | timer.reset(); |
xinlei | 100:dbcd3bc51758 | 58 | return l; |
xinlei | 100:dbcd3bc51758 | 59 | } |