tortuga DataLogging Library: battery, speed, time, average...
Fork of DataLogging by
Data.cpp@0:ec76b22829d5, 2016-04-16 (annotated)
- Committer:
- ptuytsch
- Date:
- Sat Apr 16 14:55:37 2016 +0000
- Revision:
- 0:ec76b22829d5
- Child:
- 1:6e25c2b5c20c
basic logging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptuytsch | 0:ec76b22829d5 | 1 | #include "mbed.h" |
ptuytsch | 0:ec76b22829d5 | 2 | #include "Data.h" |
ptuytsch | 0:ec76b22829d5 | 3 | #define Period 1 //5000ms |
ptuytsch | 0:ec76b22829d5 | 4 | #define PPR 1 |
ptuytsch | 0:ec76b22829d5 | 5 | #define periphery 2.233168 //periphery of the wheel, in this example a 28" wheel. |
ptuytsch | 0:ec76b22829d5 | 6 | |
ptuytsch | 0:ec76b22829d5 | 7 | |
ptuytsch | 0:ec76b22829d5 | 8 | |
ptuytsch | 0:ec76b22829d5 | 9 | int count; |
ptuytsch | 0:ec76b22829d5 | 10 | time_t StartTime; |
ptuytsch | 0:ec76b22829d5 | 11 | double distance; |
ptuytsch | 0:ec76b22829d5 | 12 | double lastDistance; |
ptuytsch | 0:ec76b22829d5 | 13 | int lastCount; |
ptuytsch | 0:ec76b22829d5 | 14 | |
ptuytsch | 0:ec76b22829d5 | 15 | void interval(void){ |
ptuytsch | 0:ec76b22829d5 | 16 | lastDistance = count * periphery / PPR; |
ptuytsch | 0:ec76b22829d5 | 17 | distance += lastDistance; |
ptuytsch | 0:ec76b22829d5 | 18 | lastCount = count; |
ptuytsch | 0:ec76b22829d5 | 19 | count = 0; |
ptuytsch | 0:ec76b22829d5 | 20 | } |
ptuytsch | 0:ec76b22829d5 | 21 | |
ptuytsch | 0:ec76b22829d5 | 22 | void pressed(void){ |
ptuytsch | 0:ec76b22829d5 | 23 | count++; |
ptuytsch | 0:ec76b22829d5 | 24 | } |
ptuytsch | 0:ec76b22829d5 | 25 | |
ptuytsch | 0:ec76b22829d5 | 26 | Data::Data(PinName pin) : button(pin){ |
ptuytsch | 0:ec76b22829d5 | 27 | StartTime = time(NULL); |
ptuytsch | 0:ec76b22829d5 | 28 | count = 0; |
ptuytsch | 0:ec76b22829d5 | 29 | distance = 0; |
ptuytsch | 0:ec76b22829d5 | 30 | tick.attach(interval,Period); |
ptuytsch | 0:ec76b22829d5 | 31 | button.fall(pressed); |
ptuytsch | 0:ec76b22829d5 | 32 | } |
ptuytsch | 0:ec76b22829d5 | 33 | |
ptuytsch | 0:ec76b22829d5 | 34 | double Data::getDistance(void){ |
ptuytsch | 0:ec76b22829d5 | 35 | return distance; |
ptuytsch | 0:ec76b22829d5 | 36 | } |
ptuytsch | 0:ec76b22829d5 | 37 | |
ptuytsch | 0:ec76b22829d5 | 38 | int Data::getTime(void){ |
ptuytsch | 0:ec76b22829d5 | 39 | return time(NULL) - StartTime; |
ptuytsch | 0:ec76b22829d5 | 40 | } |
ptuytsch | 0:ec76b22829d5 | 41 | |
ptuytsch | 0:ec76b22829d5 | 42 | double Data::getSpeed(void){ |
ptuytsch | 0:ec76b22829d5 | 43 | return lastDistance/Period * 3.6; |
ptuytsch | 0:ec76b22829d5 | 44 | } |
ptuytsch | 0:ec76b22829d5 | 45 | |
ptuytsch | 0:ec76b22829d5 | 46 | double Data::getAverage(void){ |
ptuytsch | 0:ec76b22829d5 | 47 | return distance/(time(NULL) - StartTime) * 3.6; |
ptuytsch | 0:ec76b22829d5 | 48 | } |
ptuytsch | 0:ec76b22829d5 | 49 | |
ptuytsch | 0:ec76b22829d5 | 50 | int Data::getLastCount(void){ |
ptuytsch | 0:ec76b22829d5 | 51 | return lastCount; |
ptuytsch | 0:ec76b22829d5 | 52 | } |