Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
measurement/Acceleration.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 | 114:c54e9731b9de | 2 | #include "rtos.h" |
xinlei | 100:dbcd3bc51758 | 3 | #include "SmartRestConf.h" |
xinlei | 100:dbcd3bc51758 | 4 | #include "Acceleration.h" |
xinlei | 100:dbcd3bc51758 | 5 | #include "logging.h" |
xinlei | 100:dbcd3bc51758 | 6 | |
xinlei | 100:dbcd3bc51758 | 7 | // Percentage cut-off for avoiding sending similar sensor reading. |
xinlei | 100:dbcd3bc51758 | 8 | #define THRESHOLD_PERCENT_ACCE 0.1 |
xinlei | 100:dbcd3bc51758 | 9 | // Timeout for forcing a sending even if readings are similar [seconds]. |
xinlei | 100:dbcd3bc51758 | 10 | #define TIME_LIMIT_ACCE 900 |
xinlei | 100:dbcd3bc51758 | 11 | |
xinlei | 123:311fa85af2b3 | 12 | static inline bool valueChanged(float *V0, float *V1) |
xinlei | 123:311fa85af2b3 | 13 | { |
xinlei | 123:311fa85af2b3 | 14 | // if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ACCE || |
xinlei | 123:311fa85af2b3 | 15 | // abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ACCE || |
xinlei | 123:311fa85af2b3 | 16 | // abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_ACCE) { |
xinlei | 123:311fa85af2b3 | 17 | if ((0.15 > abs(V1[0]) || abs(V0[0]-V1[0]) <= abs(V0[0])*THRESHOLD_PERCENT_ACCE) && |
xinlei | 123:311fa85af2b3 | 18 | (0.10 > abs(V1[1]) || abs(V0[1]-V1[1]) <= abs(V0[1])*THRESHOLD_PERCENT_ACCE)) |
xinlei | 123:311fa85af2b3 | 19 | return false; |
xinlei | 123:311fa85af2b3 | 20 | else |
xinlei | 123:311fa85af2b3 | 21 | return true; |
xinlei | 123:311fa85af2b3 | 22 | } |
xinlei | 123:311fa85af2b3 | 23 | |
xinlei | 136:a52821cdb108 | 24 | int Acceleration::read(char *buf, size_t maxLen, char *status, size_t num) |
xinlei | 100:dbcd3bc51758 | 25 | { |
xinlei | 100:dbcd3bc51758 | 26 | static const char *fmt = "106,%ld,%f,%f,%f\r\n"; |
xinlei | 100:dbcd3bc51758 | 27 | if (!deviceReady) |
xinlei | 100:dbcd3bc51758 | 28 | return 0; |
xinlei | 114:c54e9731b9de | 29 | float data[3] = {0.0, 0.0, 0.0}; |
xinlei | 114:c54e9731b9de | 30 | // float min[3] = {100, 100, 100}; |
xinlei | 114:c54e9731b9de | 31 | // float max[3] = {0, 0, 0}; |
xinlei | 114:c54e9731b9de | 32 | // const unsigned short N = 10; |
xinlei | 114:c54e9731b9de | 33 | // for (unsigned short i = 0; i < N; ++i) { |
xinlei | 114:c54e9731b9de | 34 | // float d[3] = {0, 0, 0}; |
xinlei | 114:c54e9731b9de | 35 | // sensor.readData(d); |
xinlei | 114:c54e9731b9de | 36 | // for (unsigned short j = 0; j < 3; ++j) { |
xinlei | 114:c54e9731b9de | 37 | // data[j] += d[j]; |
xinlei | 114:c54e9731b9de | 38 | // if (d[j] < min[j]) min[j] = d[j]; |
xinlei | 114:c54e9731b9de | 39 | // if (d[j] > max[j]) max[j] = d[j]; |
xinlei | 114:c54e9731b9de | 40 | // } |
xinlei | 114:c54e9731b9de | 41 | // Thread::wait(5); |
xinlei | 114:c54e9731b9de | 42 | // } |
xinlei | 114:c54e9731b9de | 43 | // data[0] = (data[0]-min[0]-max[0]) / (N-2); |
xinlei | 114:c54e9731b9de | 44 | // data[1] = (data[1]-min[1]-max[1]) / (N-2); |
xinlei | 114:c54e9731b9de | 45 | // data[2] = (data[2]-min[2]-max[2]) / (N-2); |
xinlei | 100:dbcd3bc51758 | 46 | sensor.readData(data); |
xinlei | 123:311fa85af2b3 | 47 | float t_interval = timer.read(); |
xinlei | 123:311fa85af2b3 | 48 | if (!valueChanged(oldValues, data) && t_interval < TIME_LIMIT_ACCE) { |
xinlei | 123:311fa85af2b3 | 49 | return 0; |
xinlei | 100:dbcd3bc51758 | 50 | } |
xinlei | 100:dbcd3bc51758 | 51 | |
xinlei | 100:dbcd3bc51758 | 52 | size_t l = snprintf(buf, maxLen, fmt, deviceID, data[0], data[1], data[2]); |
xinlei | 100:dbcd3bc51758 | 53 | if (status) { |
xinlei | 100:dbcd3bc51758 | 54 | snprintf(status, num, "Send Acc %.1f,%.1f,%.1f", |
xinlei | 100:dbcd3bc51758 | 55 | data[0], data[1], data[2]); |
xinlei | 100:dbcd3bc51758 | 56 | } |
xinlei | 100:dbcd3bc51758 | 57 | oldValues[0] = data[0]; |
xinlei | 100:dbcd3bc51758 | 58 | oldValues[1] = data[1]; |
xinlei | 100:dbcd3bc51758 | 59 | oldValues[2] = data[2]; |
xinlei | 123:311fa85af2b3 | 60 | timer.reset(); |
xinlei | 100:dbcd3bc51758 | 61 | return l; |
xinlei | 100:dbcd3bc51758 | 62 | } |