tortuga DataLogging Library: battery, speed, time, average...
Fork of DataLogging by
BikeData.cpp@3:610ace66d7af, 2016-07-12 (annotated)
- Committer:
- ptuytsch
- Date:
- Tue Jul 12 11:54:41 2016 +0000
- Revision:
- 3:610ace66d7af
- Parent:
- 2:f3c2bf5521e5
- Child:
- 4:f91f45d52f9b
same as before
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptuytsch | 2:f3c2bf5521e5 | 1 | #include "mbed.h" |
ptuytsch | 2:f3c2bf5521e5 | 2 | #include "BikeData.h" |
ptuytsch | 2:f3c2bf5521e5 | 3 | #include "Data.h" |
ptuytsch | 2:f3c2bf5521e5 | 4 | #include "eeprom.h" |
ptuytsch | 2:f3c2bf5521e5 | 5 | |
ptuytsch | 3:610ace66d7af | 6 | #define Period 1 //1000ms period of sampling |
ptuytsch | 3:610ace66d7af | 7 | #define PPR 3 // Pulses Per Rotation |
ptuytsch | 3:610ace66d7af | 8 | #define periphery 1.3 //periphery of the wheel in meters |
ptuytsch | 2:f3c2bf5521e5 | 9 | |
ptuytsch | 2:f3c2bf5521e5 | 10 | #define EEPROM_ADDR 0x0 // I2c EEPROM address is 0x00 |
ptuytsch | 2:f3c2bf5521e5 | 11 | #define SDA PB_9 // I2C SDA pin |
ptuytsch | 2:f3c2bf5521e5 | 12 | #define SCL PB_8 // I2C SCL pin |
ptuytsch | 2:f3c2bf5521e5 | 13 | |
ptuytsch | 2:f3c2bf5521e5 | 14 | EEPROM mem(SDA,SCL,EEPROM_ADDR,EEPROM::T24C32); |
ptuytsch | 2:f3c2bf5521e5 | 15 | /* |
ptuytsch | 2:f3c2bf5521e5 | 16 | EEPROM ADRESSES: |
ptuytsch | 2:f3c2bf5521e5 | 17 | |
ptuytsch | 2:f3c2bf5521e5 | 18 | 0->3: overall distance |
ptuytsch | 2:f3c2bf5521e5 | 19 | 4->7: overall time |
ptuytsch | 3:610ace66d7af | 20 | 8: name size |
ptuytsch | 3:610ace66d7af | 21 | 9->...: bike name |
ptuytsch | 2:f3c2bf5521e5 | 22 | */ |
ptuytsch | 2:f3c2bf5521e5 | 23 | |
ptuytsch | 2:f3c2bf5521e5 | 24 | //CONSTRUCTOR |
ptuytsch | 2:f3c2bf5521e5 | 25 | BikeData::BikeData(PinName pin) : |
ptuytsch | 2:f3c2bf5521e5 | 26 | button(pin), |
ptuytsch | 3:610ace66d7af | 27 | logging(false), |
ptuytsch | 3:610ace66d7af | 28 | count(0), |
ptuytsch | 3:610ace66d7af | 29 | lastDistance(0) |
ptuytsch | 2:f3c2bf5521e5 | 30 | { |
ptuytsch | 3:610ace66d7af | 31 | //setting things up for logging the raw data. |
ptuytsch | 2:f3c2bf5521e5 | 32 | float readDistance; |
ptuytsch | 2:f3c2bf5521e5 | 33 | mem.read(0,readDistance); |
ptuytsch | 2:f3c2bf5521e5 | 34 | int32_t readTime; |
ptuytsch | 2:f3c2bf5521e5 | 35 | mem.read(4,readTime); |
ptuytsch | 3:610ace66d7af | 36 | overallData = new Data(readDistance,readTime,PPR,periphery); |
ptuytsch | 3:610ace66d7af | 37 | dataSet[OVERALL] = overallData; |
ptuytsch | 3:610ace66d7af | 38 | batChangeData = new Data(0.0,0,PPR,periphery); |
ptuytsch | 3:610ace66d7af | 39 | dataSet[BATCHANGE] = batChangeData; |
ptuytsch | 2:f3c2bf5521e5 | 40 | tick.attach(this,&BikeData::interval,Period); |
ptuytsch | 2:f3c2bf5521e5 | 41 | button.fall(this,&BikeData::pressed); |
ptuytsch | 2:f3c2bf5521e5 | 42 | } |
ptuytsch | 2:f3c2bf5521e5 | 43 | |
ptuytsch | 3:610ace66d7af | 44 | int8_t BikeData::getBikeNameSize(){ |
ptuytsch | 3:610ace66d7af | 45 | int8_t size = 0; |
ptuytsch | 3:610ace66d7af | 46 | mem.read(8,size); |
ptuytsch | 3:610ace66d7af | 47 | return size; |
ptuytsch | 3:610ace66d7af | 48 | } |
ptuytsch | 3:610ace66d7af | 49 | |
ptuytsch | 3:610ace66d7af | 50 | void BikeData::getBikeName(char *name){ |
ptuytsch | 3:610ace66d7af | 51 | uint8_t size = getBikeNameSize(); |
ptuytsch | 3:610ace66d7af | 52 | //uint8_t* name; |
ptuytsch | 3:610ace66d7af | 53 | mem.read(9,name,size); |
ptuytsch | 3:610ace66d7af | 54 | printf("name retrieved\n"); |
ptuytsch | 3:610ace66d7af | 55 | //return name; |
ptuytsch | 3:610ace66d7af | 56 | } |
ptuytsch | 3:610ace66d7af | 57 | |
ptuytsch | 3:610ace66d7af | 58 | void BikeData::setBikeName(char *name, uint8_t length){ |
ptuytsch | 3:610ace66d7af | 59 | mem.write(8,length); |
ptuytsch | 3:610ace66d7af | 60 | mem.write(9,name,length); |
ptuytsch | 3:610ace66d7af | 61 | printf("name set\n"); |
ptuytsch | 3:610ace66d7af | 62 | |
ptuytsch | 3:610ace66d7af | 63 | } |
ptuytsch | 3:610ace66d7af | 64 | |
ptuytsch | 2:f3c2bf5521e5 | 65 | void BikeData::startTrip(){ |
ptuytsch | 3:610ace66d7af | 66 | //start trip function, creating new tripdata object |
ptuytsch | 3:610ace66d7af | 67 | tripData = new Data(0.0,0,PPR,periphery); |
ptuytsch | 3:610ace66d7af | 68 | dataSet[TRIP] = tripData; // setting the pointer position to the tripdata object |
ptuytsch | 3:610ace66d7af | 69 | logging = true; // start logging |
ptuytsch | 2:f3c2bf5521e5 | 70 | } |
ptuytsch | 2:f3c2bf5521e5 | 71 | |
ptuytsch | 2:f3c2bf5521e5 | 72 | void BikeData::pauzeTrip(){ |
ptuytsch | 3:610ace66d7af | 73 | logging = !logging; |
ptuytsch | 2:f3c2bf5521e5 | 74 | } |
ptuytsch | 2:f3c2bf5521e5 | 75 | |
ptuytsch | 2:f3c2bf5521e5 | 76 | void BikeData::stopTrip(){ |
ptuytsch | 3:610ace66d7af | 77 | logging = false; |
ptuytsch | 2:f3c2bf5521e5 | 78 | } |
ptuytsch | 2:f3c2bf5521e5 | 79 | |
ptuytsch | 2:f3c2bf5521e5 | 80 | void BikeData::pressed(void){ |
ptuytsch | 3:610ace66d7af | 81 | //count each pulse of the sensor |
ptuytsch | 2:f3c2bf5521e5 | 82 | count++; |
ptuytsch | 2:f3c2bf5521e5 | 83 | } |
ptuytsch | 2:f3c2bf5521e5 | 84 | |
ptuytsch | 2:f3c2bf5521e5 | 85 | Data* BikeData::getDataSet(uint8_t type){ |
ptuytsch | 2:f3c2bf5521e5 | 86 | return dataSet[type]; |
ptuytsch | 2:f3c2bf5521e5 | 87 | } |
ptuytsch | 2:f3c2bf5521e5 | 88 | |
ptuytsch | 3:610ace66d7af | 89 | float BikeData::getSpeed(void){ |
ptuytsch | 3:610ace66d7af | 90 | return lastDistance/Period * 3.6; |
ptuytsch | 3:610ace66d7af | 91 | } |
ptuytsch | 3:610ace66d7af | 92 | |
ptuytsch | 3:610ace66d7af | 93 | uint8_t BikeData::getLastCount(void){ |
ptuytsch | 3:610ace66d7af | 94 | return lastCount; |
ptuytsch | 3:610ace66d7af | 95 | } |
ptuytsch | 3:610ace66d7af | 96 | |
ptuytsch | 3:610ace66d7af | 97 | bool BikeData::isLogging(void){ |
ptuytsch | 3:610ace66d7af | 98 | return logging; |
ptuytsch | 3:610ace66d7af | 99 | } |
ptuytsch | 3:610ace66d7af | 100 | |
ptuytsch | 3:610ace66d7af | 101 | //interval function is called each second to monitor the progress by the datasets |
ptuytsch | 2:f3c2bf5521e5 | 102 | void BikeData::interval(void){ |
ptuytsch | 3:610ace66d7af | 103 | //keep the pointers well placed |
ptuytsch | 3:610ace66d7af | 104 | dataSet[TRIP] = tripData; |
ptuytsch | 3:610ace66d7af | 105 | dataSet[OVERALL] = overallData; |
ptuytsch | 3:610ace66d7af | 106 | dataSet[BATCHANGE] = batChangeData; |
ptuytsch | 3:610ace66d7af | 107 | //calculate the distances |
ptuytsch | 3:610ace66d7af | 108 | lastDistance = count * periphery / PPR; |
ptuytsch | 3:610ace66d7af | 109 | lastCount = count; |
ptuytsch | 3:610ace66d7af | 110 | |
ptuytsch | 3:610ace66d7af | 111 | //printf("\n==================\n\n"); // debugging |
ptuytsch | 3:610ace66d7af | 112 | if(logging){ // only interval when needed |
ptuytsch | 3:610ace66d7af | 113 | //printf("Trip:\n------------------\n"); |
ptuytsch | 3:610ace66d7af | 114 | tripData->interval(count); // send interval to the tripdata object |
ptuytsch | 2:f3c2bf5521e5 | 115 | } |
ptuytsch | 3:610ace66d7af | 116 | //printf("Overall:\n------------------\n"); |
ptuytsch | 2:f3c2bf5521e5 | 117 | overallData->interval(count); |
ptuytsch | 3:610ace66d7af | 118 | mem.write(0,overallData->getDistance()); // update the eeprom values |
ptuytsch | 2:f3c2bf5521e5 | 119 | mem.write(4,(int32_t)overallData->getTime()); |
ptuytsch | 3:610ace66d7af | 120 | |
ptuytsch | 3:610ace66d7af | 121 | //printf("batChange:\n------------------\n"); |
ptuytsch | 2:f3c2bf5521e5 | 122 | batChangeData->interval(count); |
ptuytsch | 3:610ace66d7af | 123 | |
ptuytsch | 3:610ace66d7af | 124 | //printf("speed: %f\ncount: %i\n",lastDistance/Period * 3.6, lastCount); // debugging |
ptuytsch | 2:f3c2bf5521e5 | 125 | count = 0; |
ptuytsch | 3:610ace66d7af | 126 | } |
ptuytsch | 3:610ace66d7af | 127 |