tortuga DataLogging Library: battery, speed, time, average...

Dependents:   TORTUGA_BLE

Fork of DataLogging by aapje monkey

Committer:
ptuytsch
Date:
Fri Jul 15 12:48:06 2016 +0000
Revision:
4:f91f45d52f9b
Parent:
3:610ace66d7af
Child:
5:fad416fb6979
working datalogging on prototype itself

Who changed what in which revision?

UserRevisionLine numberNew 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 4:f91f45d52f9b 4 #include "_24LCXXX.h"
ptuytsch 2:f3c2bf5521e5 5
ptuytsch 4:f91f45d52f9b 6 #define Period 1 //1000ms period of sampling
ptuytsch 4:f91f45d52f9b 7 #define PPR 51 // Pulses Per Rotation
ptuytsch 4:f91f45d52f9b 8 #define periphery 1.595928736 //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 4:f91f45d52f9b 14
ptuytsch 4:f91f45d52f9b 15 I2C i2c(PB_9, PB_8);
ptuytsch 4:f91f45d52f9b 16 Serial pc(SERIAL_TX, SERIAL_RX);
ptuytsch 4:f91f45d52f9b 17 _24LCXXX mem(&i2c);
ptuytsch 4:f91f45d52f9b 18
ptuytsch 2:f3c2bf5521e5 19 /*
ptuytsch 2:f3c2bf5521e5 20 EEPROM ADRESSES:
ptuytsch 2:f3c2bf5521e5 21
ptuytsch 2:f3c2bf5521e5 22 0->3: overall distance
ptuytsch 2:f3c2bf5521e5 23 4->7: overall time
ptuytsch 3:610ace66d7af 24 8: name size
ptuytsch 3:610ace66d7af 25 9->...: bike name
ptuytsch 2:f3c2bf5521e5 26 */
ptuytsch 2:f3c2bf5521e5 27
ptuytsch 2:f3c2bf5521e5 28 //CONSTRUCTOR
ptuytsch 2:f3c2bf5521e5 29 BikeData::BikeData(PinName pin) :
ptuytsch 2:f3c2bf5521e5 30 button(pin),
ptuytsch 3:610ace66d7af 31 logging(false),
ptuytsch 3:610ace66d7af 32 count(0),
ptuytsch 3:610ace66d7af 33 lastDistance(0)
ptuytsch 2:f3c2bf5521e5 34 {
ptuytsch 4:f91f45d52f9b 35
ptuytsch 3:610ace66d7af 36 //setting things up for logging the raw data.
ptuytsch 2:f3c2bf5521e5 37 float readDistance;
ptuytsch 4:f91f45d52f9b 38 printf("reading...\n");
ptuytsch 4:f91f45d52f9b 39 if(mem.nbyte_read(0,&readDistance,sizeof(float))){
ptuytsch 4:f91f45d52f9b 40 printf("eeprom reading error");
ptuytsch 4:f91f45d52f9b 41 }
ptuytsch 4:f91f45d52f9b 42 printf("data read: %f\n",readDistance);
ptuytsch 4:f91f45d52f9b 43 uint32_t readTime;
ptuytsch 4:f91f45d52f9b 44 if(mem.nbyte_read(4,&readTime,sizeof(uint32_t))){
ptuytsch 4:f91f45d52f9b 45 printf("eeprom reading error");
ptuytsch 4:f91f45d52f9b 46 }
ptuytsch 4:f91f45d52f9b 47 printf("data read: %#x\n",readTime);
ptuytsch 4:f91f45d52f9b 48
ptuytsch 3:610ace66d7af 49 overallData = new Data(readDistance,readTime,PPR,periphery);
ptuytsch 3:610ace66d7af 50 dataSet[OVERALL] = overallData;
ptuytsch 3:610ace66d7af 51 batChangeData = new Data(0.0,0,PPR,periphery);
ptuytsch 3:610ace66d7af 52 dataSet[BATCHANGE] = batChangeData;
ptuytsch 4:f91f45d52f9b 53
ptuytsch 2:f3c2bf5521e5 54 tick.attach(this,&BikeData::interval,Period);
ptuytsch 2:f3c2bf5521e5 55 button.fall(this,&BikeData::pressed);
ptuytsch 2:f3c2bf5521e5 56 }
ptuytsch 2:f3c2bf5521e5 57
ptuytsch 3:610ace66d7af 58 int8_t BikeData::getBikeNameSize(){
ptuytsch 4:f91f45d52f9b 59 //printf("size retrieving\n");
ptuytsch 3:610ace66d7af 60 int8_t size = 0;
ptuytsch 4:f91f45d52f9b 61 //mem.read(8,size);
ptuytsch 4:f91f45d52f9b 62 if(mem.nbyte_read(8,&size,sizeof(int8_t))){
ptuytsch 4:f91f45d52f9b 63 printf("eeprom reading error");
ptuytsch 4:f91f45d52f9b 64 }
ptuytsch 4:f91f45d52f9b 65 printf("size retrieved: %i\n", size);
ptuytsch 3:610ace66d7af 66 return size;
ptuytsch 3:610ace66d7af 67 }
ptuytsch 3:610ace66d7af 68
ptuytsch 3:610ace66d7af 69 void BikeData::getBikeName(char *name){
ptuytsch 4:f91f45d52f9b 70 printf("name retrieving\n");
ptuytsch 3:610ace66d7af 71 uint8_t size = getBikeNameSize();
ptuytsch 3:610ace66d7af 72 //uint8_t* name;
ptuytsch 4:f91f45d52f9b 73 //mem.read(9,name, size);
ptuytsch 4:f91f45d52f9b 74 if(mem.nbyte_read(9,name,size)){
ptuytsch 4:f91f45d52f9b 75 printf("eeprom reading error");
ptuytsch 4:f91f45d52f9b 76 }
ptuytsch 4:f91f45d52f9b 77 //printf("name retrieved\n");
ptuytsch 3:610ace66d7af 78 //return name;
ptuytsch 3:610ace66d7af 79 }
ptuytsch 3:610ace66d7af 80
ptuytsch 3:610ace66d7af 81 void BikeData::setBikeName(char *name, uint8_t length){
ptuytsch 4:f91f45d52f9b 82 printf("setting bike name size to eeprom\n");
ptuytsch 4:f91f45d52f9b 83 //mem.write(8,(int8_t)length);
ptuytsch 4:f91f45d52f9b 84 printf("length: %i\n", length);
ptuytsch 4:f91f45d52f9b 85 if(mem.nbyte_write(8,&length,1)){
ptuytsch 4:f91f45d52f9b 86 printf("eeprom writing error");
ptuytsch 4:f91f45d52f9b 87 }
ptuytsch 4:f91f45d52f9b 88 //printf("setting bike name to eeprom\n");
ptuytsch 4:f91f45d52f9b 89 //mem.write(9,name, length);
ptuytsch 4:f91f45d52f9b 90 if(mem.nbyte_write(9,name,length)){
ptuytsch 4:f91f45d52f9b 91 printf("eeprom writing error");
ptuytsch 4:f91f45d52f9b 92 }
ptuytsch 4:f91f45d52f9b 93 //printf("name set\n");
ptuytsch 3:610ace66d7af 94
ptuytsch 3:610ace66d7af 95 }
ptuytsch 3:610ace66d7af 96
ptuytsch 2:f3c2bf5521e5 97 void BikeData::startTrip(){
ptuytsch 3:610ace66d7af 98 //start trip function, creating new tripdata object
ptuytsch 3:610ace66d7af 99 tripData = new Data(0.0,0,PPR,periphery);
ptuytsch 3:610ace66d7af 100 dataSet[TRIP] = tripData; // setting the pointer position to the tripdata object
ptuytsch 3:610ace66d7af 101 logging = true; // start logging
ptuytsch 2:f3c2bf5521e5 102 }
ptuytsch 2:f3c2bf5521e5 103
ptuytsch 2:f3c2bf5521e5 104 void BikeData::pauzeTrip(){
ptuytsch 3:610ace66d7af 105 logging = !logging;
ptuytsch 2:f3c2bf5521e5 106 }
ptuytsch 2:f3c2bf5521e5 107
ptuytsch 2:f3c2bf5521e5 108 void BikeData::stopTrip(){
ptuytsch 3:610ace66d7af 109 logging = false;
ptuytsch 2:f3c2bf5521e5 110 }
ptuytsch 2:f3c2bf5521e5 111
ptuytsch 2:f3c2bf5521e5 112 void BikeData::pressed(void){
ptuytsch 3:610ace66d7af 113 //count each pulse of the sensor
ptuytsch 2:f3c2bf5521e5 114 count++;
ptuytsch 2:f3c2bf5521e5 115 }
ptuytsch 2:f3c2bf5521e5 116
ptuytsch 2:f3c2bf5521e5 117 Data* BikeData::getDataSet(uint8_t type){
ptuytsch 2:f3c2bf5521e5 118 return dataSet[type];
ptuytsch 2:f3c2bf5521e5 119 }
ptuytsch 2:f3c2bf5521e5 120
ptuytsch 3:610ace66d7af 121 float BikeData::getSpeed(void){
ptuytsch 3:610ace66d7af 122 return lastDistance/Period * 3.6;
ptuytsch 3:610ace66d7af 123 }
ptuytsch 3:610ace66d7af 124
ptuytsch 3:610ace66d7af 125 uint8_t BikeData::getLastCount(void){
ptuytsch 3:610ace66d7af 126 return lastCount;
ptuytsch 3:610ace66d7af 127 }
ptuytsch 3:610ace66d7af 128
ptuytsch 3:610ace66d7af 129 bool BikeData::isLogging(void){
ptuytsch 3:610ace66d7af 130 return logging;
ptuytsch 3:610ace66d7af 131 }
ptuytsch 3:610ace66d7af 132
ptuytsch 3:610ace66d7af 133 //interval function is called each second to monitor the progress by the datasets
ptuytsch 2:f3c2bf5521e5 134 void BikeData::interval(void){
ptuytsch 3:610ace66d7af 135 //calculate the distances
ptuytsch 3:610ace66d7af 136 lastDistance = count * periphery / PPR;
ptuytsch 3:610ace66d7af 137 lastCount = count;
ptuytsch 3:610ace66d7af 138
ptuytsch 3:610ace66d7af 139 //printf("\n==================\n\n"); // debugging
ptuytsch 3:610ace66d7af 140 if(logging){ // only interval when needed
ptuytsch 3:610ace66d7af 141 //printf("Trip:\n------------------\n");
ptuytsch 3:610ace66d7af 142 tripData->interval(count); // send interval to the tripdata object
ptuytsch 2:f3c2bf5521e5 143 }
ptuytsch 3:610ace66d7af 144 //printf("Overall:\n------------------\n");
ptuytsch 2:f3c2bf5521e5 145 overallData->interval(count);
ptuytsch 4:f91f45d52f9b 146
ptuytsch 4:f91f45d52f9b 147 // update the eeprom values
ptuytsch 4:f91f45d52f9b 148 float overallDistance = overallData->getDistance();
ptuytsch 4:f91f45d52f9b 149 if(mem.nbyte_write(0,&overallDistance,4)){
ptuytsch 4:f91f45d52f9b 150 printf("eeprom writing error distance\n");
ptuytsch 4:f91f45d52f9b 151 }
ptuytsch 4:f91f45d52f9b 152 uint32_t overallTime = overallData->getTime();
ptuytsch 4:f91f45d52f9b 153 if(mem.nbyte_write(4,&overallTime,4)){
ptuytsch 4:f91f45d52f9b 154 printf("eeprom writing error time\n");
ptuytsch 4:f91f45d52f9b 155 }
ptuytsch 3:610ace66d7af 156
ptuytsch 3:610ace66d7af 157 //printf("batChange:\n------------------\n");
ptuytsch 2:f3c2bf5521e5 158 batChangeData->interval(count);
ptuytsch 3:610ace66d7af 159 //printf("speed: %f\ncount: %i\n",lastDistance/Period * 3.6, lastCount); // debugging
ptuytsch 2:f3c2bf5521e5 160 count = 0;
ptuytsch 3:610ace66d7af 161 }
ptuytsch 3:610ace66d7af 162