whatever

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

Committer:
sgmcb
Date:
Wed Jan 20 21:51:25 2016 +0000
Revision:
54:6ce53a145fa0
Parent:
52:ba2017c3ef8d
fuck you

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgmcb 41:f603d76dc6fe 1 /*
sgmcb 41:f603d76dc6fe 2
sgmcb 41:f603d76dc6fe 3 PONY_sense.cpp
sgmcb 41:f603d76dc6fe 4 (C) 2015 Igor Institute
sgmcb 41:f603d76dc6fe 5
sgmcb 41:f603d76dc6fe 6 */
sgmcb 41:f603d76dc6fe 7
sgmcb 41:f603d76dc6fe 8 #include "PONY_sense.h"
sgmcb 41:f603d76dc6fe 9
sgmcb 41:f603d76dc6fe 10 #define THROWAWAY
sgmcb 41:f603d76dc6fe 11
sgmcb 41:f603d76dc6fe 12 #ifdef THROWAWAY
sgmcb 41:f603d76dc6fe 13 // Codes for a throwaway M2X device
sgmcb 41:f603d76dc6fe 14 #define M2XAPIKEY "54c6bbf11012f34830bd23cc091ca250"
sgmcb 41:f603d76dc6fe 15 #define DEVID "b9c57667ee0495dc0b3cddd890f8d2df"
sgmcb 41:f603d76dc6fe 16 #endif
sgmcb 41:f603d76dc6fe 17
sgmcb 41:f603d76dc6fe 18 #ifndef THROWAWAY
sgmcb 41:f603d76dc6fe 19 // v1.2 codes
sgmcb 41:f603d76dc6fe 20 #define M2XAPIKEY "bbc483492238dc76f7d12f0cd6e13a4b"
sgmcb 41:f603d76dc6fe 21 #define DEVID "3764db38b6c9ec4045a38e0125b14b4c"
sgmcb 41:f603d76dc6fe 22 #endif
sgmcb 41:f603d76dc6fe 23
sgmcb 41:f603d76dc6fe 24
sgmcb 41:f603d76dc6fe 25
sgmcb 41:f603d76dc6fe 26
sgmcb 41:f603d76dc6fe 27
sgmcb 41:f603d76dc6fe 28
sgmcb 41:f603d76dc6fe 29
sgmcb 41:f603d76dc6fe 30
sgmcb 41:f603d76dc6fe 31
sgmcb 41:f603d76dc6fe 32
sgmcb 41:f603d76dc6fe 33
sgmcb 41:f603d76dc6fe 34
sgmcb 41:f603d76dc6fe 35 // Convert voltage across thermistor to temperature
sgmcb 41:f603d76dc6fe 36 // Right now, this math assumes we're using a Vishay NTCLG100E2 thermistor
sgmcb 41:f603d76dc6fe 37
sgmcb 41:f603d76dc6fe 38 // Thermistor values vs. temperature (-20C to 100C x 5C)
sgmcb 41:f603d76dc6fe 39 // NOTE: This setup assumes that we're measuring a thermistor where one end is connected directly to ground
sgmcb 41:f603d76dc6fe 40 float thermistorToTemp(float thermVoltage) {
sgmcb 41:f603d76dc6fe 41 float nomCurrent = 0.0000825; // This is the set current of the current configuration (on breadboard, 2015-12-17)
sgmcb 41:f603d76dc6fe 42 float nomResistance = thermVoltage / nomCurrent;
sgmcb 41:f603d76dc6fe 43
sgmcb 51:61a1ec3c56fc 44 //printf("nomResistance = %.2f\r\n", nomResistance);
sgmcb 51:61a1ec3c56fc 45
sgmcb 51:61a1ec3c56fc 46 //float nomTemp = -25.87 * log(nomResistance) + 266.53; // Best fit for Vishay NTCLG100E2 between -20C and 100C; R = 0.99228
sgmcb 51:61a1ec3c56fc 47 float nomTemp = -26.68 * log(nomResistance) + 272.96; // Best fit for Cantherm CWF3AA103G3380 between -20C and 100C; R = 0.99041
sgmcb 41:f603d76dc6fe 48
sgmcb 41:f603d76dc6fe 49 return nomTemp;
sgmcb 41:f603d76dc6fe 50
sgmcb 41:f603d76dc6fe 51 };
sgmcb 41:f603d76dc6fe 52
sgmcb 41:f603d76dc6fe 53 float getTemp(AnalogIn* pTempPin) {
sgmcb 41:f603d76dc6fe 54 float tempV = *pTempPin;
sgmcb 41:f603d76dc6fe 55 float temp = thermistorToTemp(AINTOV(tempV));
sgmcb 41:f603d76dc6fe 56
sgmcb 41:f603d76dc6fe 57 return temp;
sgmcb 41:f603d76dc6fe 58 }
sgmcb 41:f603d76dc6fe 59
sgmcb 41:f603d76dc6fe 60 // Records and sends the temperature up to the M2X cloud
sgmcb 41:f603d76dc6fe 61 int logTemp(AnalogIn* pTempPin, float* pTemp, M2XStreamClient* m2x) {
sgmcb 41:f603d76dc6fe 62 *pTemp = getTemp(pTempPin);
sgmcb 41:f603d76dc6fe 63 printf("Temp=%f\r\n", *pTemp);
sgmcb 41:f603d76dc6fe 64
sgmcb 41:f603d76dc6fe 65 int ret = m2x->updateStreamValue(DEVID, "kegtemp", *pTemp);
sgmcb 41:f603d76dc6fe 66 printf("m2x ret=%i\r\n",ret);
sgmcb 41:f603d76dc6fe 67
sgmcb 41:f603d76dc6fe 68 return ret;
sgmcb 41:f603d76dc6fe 69
sgmcb 41:f603d76dc6fe 70 }
sgmcb 41:f603d76dc6fe 71
sgmcb 41:f603d76dc6fe 72
sgmcb 41:f603d76dc6fe 73
sgmcb 41:f603d76dc6fe 74
sgmcb 41:f603d76dc6fe 75
sgmcb 41:f603d76dc6fe 76 //-----------------------------------------------
sgmcb 41:f603d76dc6fe 77 //-----------------------------------------------
sgmcb 41:f603d76dc6fe 78
sgmcb 41:f603d76dc6fe 79 #define AX_WRITE_REG 0x3A
sgmcb 41:f603d76dc6fe 80 #define AX_READ_REG 0x3B
sgmcb 41:f603d76dc6fe 81
sgmcb 41:f603d76dc6fe 82 #define AX_WHOAMI_REG 0x0F
sgmcb 41:f603d76dc6fe 83
sgmcb 41:f603d76dc6fe 84 #define AX_CONFIG1_REG 0x20
sgmcb 41:f603d76dc6fe 85 #define AX_CONFIG1_VAL 0xC7
sgmcb 41:f603d76dc6fe 86
sgmcb 41:f603d76dc6fe 87
sgmcb 41:f603d76dc6fe 88 #define AX_X_REG 0x29
sgmcb 41:f603d76dc6fe 89 #define AX_Y_REG 0x2B
sgmcb 41:f603d76dc6fe 90 #define AX_Z_REG 0x2D
sgmcb 41:f603d76dc6fe 91
sgmcb 41:f603d76dc6fe 92 #define AX_NEWFILTER 0b00000111
sgmcb 41:f603d76dc6fe 93
sgmcb 41:f603d76dc6fe 94
sgmcb 41:f603d76dc6fe 95
sgmcb 41:f603d76dc6fe 96
sgmcb 41:f603d76dc6fe 97
sgmcb 41:f603d76dc6fe 98 void LIS331write(I2C* axis, int targetReg, int targetVal) {
sgmcb 41:f603d76dc6fe 99 axis->start();
sgmcb 41:f603d76dc6fe 100
sgmcb 41:f603d76dc6fe 101 axis->write(AX_WRITE_REG);
sgmcb 41:f603d76dc6fe 102 axis->write(targetReg);
sgmcb 41:f603d76dc6fe 103 axis->write(targetVal);
sgmcb 41:f603d76dc6fe 104
sgmcb 41:f603d76dc6fe 105 axis->stop();
sgmcb 41:f603d76dc6fe 106
sgmcb 41:f603d76dc6fe 107 return;
sgmcb 41:f603d76dc6fe 108 }
sgmcb 41:f603d76dc6fe 109
sgmcb 41:f603d76dc6fe 110 int LIS331read(I2C* axis, const int readReg, int* readVal) {
sgmcb 41:f603d76dc6fe 111 axis->start();
sgmcb 41:f603d76dc6fe 112
sgmcb 41:f603d76dc6fe 113 axis->write(AX_WRITE_REG);
sgmcb 41:f603d76dc6fe 114 axis->write(readReg);
sgmcb 41:f603d76dc6fe 115
sgmcb 41:f603d76dc6fe 116 axis->start();
sgmcb 41:f603d76dc6fe 117 axis->write(AX_READ_REG);
sgmcb 41:f603d76dc6fe 118 *readVal = axis->read(1);
sgmcb 41:f603d76dc6fe 119
sgmcb 41:f603d76dc6fe 120 axis->stop();
sgmcb 41:f603d76dc6fe 121 return *readVal;
sgmcb 41:f603d76dc6fe 122 }
sgmcb 41:f603d76dc6fe 123
sgmcb 41:f603d76dc6fe 124 int LIS331read(I2C* axis, const int readReg) {
sgmcb 41:f603d76dc6fe 125 axis->start();
sgmcb 41:f603d76dc6fe 126
sgmcb 41:f603d76dc6fe 127 axis->write(AX_WRITE_REG);
sgmcb 41:f603d76dc6fe 128 axis->write(readReg);
sgmcb 41:f603d76dc6fe 129
sgmcb 41:f603d76dc6fe 130 axis->start();
sgmcb 41:f603d76dc6fe 131 axis->write(AX_READ_REG);
sgmcb 41:f603d76dc6fe 132 int readVal = axis->read(1);
sgmcb 41:f603d76dc6fe 133
sgmcb 41:f603d76dc6fe 134 axis->stop();
sgmcb 41:f603d76dc6fe 135 return readVal;
sgmcb 41:f603d76dc6fe 136 }
sgmcb 41:f603d76dc6fe 137
sgmcb 41:f603d76dc6fe 138
sgmcb 41:f603d76dc6fe 139 int configureAccel(I2C* axis) {
sgmcb 41:f603d76dc6fe 140 axis->frequency(400);
sgmcb 41:f603d76dc6fe 141
sgmcb 41:f603d76dc6fe 142 LIS331write(axis, AX_CONFIG1_REG, AX_CONFIG1_VAL);
sgmcb 41:f603d76dc6fe 143 int configVal = 0;
sgmcb 41:f603d76dc6fe 144 LIS331read(axis, AX_CONFIG1_REG, &configVal);
sgmcb 41:f603d76dc6fe 145
sgmcb 41:f603d76dc6fe 146 return configVal;
sgmcb 41:f603d76dc6fe 147 }
sgmcb 41:f603d76dc6fe 148
sgmcb 52:ba2017c3ef8d 149 // Query the WHOAMI register; should return 0x3B
sgmcb 41:f603d76dc6fe 150 int accelWhoAmI (I2C* axis) {
sgmcb 41:f603d76dc6fe 151 int axID = 0;
sgmcb 41:f603d76dc6fe 152
sgmcb 41:f603d76dc6fe 153 LIS331read(axis, AX_WHOAMI_REG, &axID);
sgmcb 41:f603d76dc6fe 154
sgmcb 41:f603d76dc6fe 155 return axID;
sgmcb 41:f603d76dc6fe 156 }
sgmcb 41:f603d76dc6fe 157
sgmcb 41:f603d76dc6fe 158 int accelX (I2C* axis) {
sgmcb 41:f603d76dc6fe 159 int xAccel = 0;
sgmcb 41:f603d76dc6fe 160
sgmcb 41:f603d76dc6fe 161 LIS331read(axis,AX_X_REG,&xAccel);
sgmcb 41:f603d76dc6fe 162
sgmcb 41:f603d76dc6fe 163 return xAccel;
sgmcb 41:f603d76dc6fe 164 }
sgmcb 41:f603d76dc6fe 165
sgmcb 41:f603d76dc6fe 166 int accelY (I2C* axis) {
sgmcb 41:f603d76dc6fe 167 int yAccel = 0;
sgmcb 41:f603d76dc6fe 168
sgmcb 41:f603d76dc6fe 169 LIS331read(axis,AX_Y_REG,&yAccel);
sgmcb 41:f603d76dc6fe 170
sgmcb 41:f603d76dc6fe 171 return yAccel;
sgmcb 41:f603d76dc6fe 172 }
sgmcb 41:f603d76dc6fe 173
sgmcb 41:f603d76dc6fe 174
sgmcb 41:f603d76dc6fe 175 int accelZ (I2C* axis) {
sgmcb 41:f603d76dc6fe 176 int zAccel = 0;
sgmcb 41:f603d76dc6fe 177
sgmcb 41:f603d76dc6fe 178 LIS331read(axis,AX_Z_REG,&zAccel);
sgmcb 41:f603d76dc6fe 179
sgmcb 41:f603d76dc6fe 180 return zAccel;
sgmcb 41:f603d76dc6fe 181 }
sgmcb 41:f603d76dc6fe 182
sgmcb 41:f603d76dc6fe 183
sgmcb 51:61a1ec3c56fc 184
sgmcb 51:61a1ec3c56fc 185
sgmcb 51:61a1ec3c56fc 186 // FSR interpretation
sgmcb 51:61a1ec3c56fc 187
sgmcb 51:61a1ec3c56fc 188
sgmcb 51:61a1ec3c56fc 189 #define FSR_THRESHOLD 0.860
sgmcb 51:61a1ec3c56fc 190
sgmcb 51:61a1ec3c56fc 191 // TODO: Calibrations for different keg sizes
sgmcb 51:61a1ec3c56fc 192
sgmcb 51:61a1ec3c56fc 193 float getWeight(AnalogIn* pFSRvolt) {
sgmcb 51:61a1ec3c56fc 194
sgmcb 51:61a1ec3c56fc 195 float fsrVa = *pFSRvolt;
sgmcb 51:61a1ec3c56fc 196 float fsrV = AINTOV(fsrVa);
sgmcb 51:61a1ec3c56fc 197
sgmcb 51:61a1ec3c56fc 198 //printf("fsrV = %.3f \r\n",fsrV);
sgmcb 51:61a1ec3c56fc 199
sgmcb 51:61a1ec3c56fc 200 if(fsrV <= FSR_THRESHOLD)
sgmcb 51:61a1ec3c56fc 201 return 0;
sgmcb 51:61a1ec3c56fc 202
sgmcb 51:61a1ec3c56fc 203
sgmcb 51:61a1ec3c56fc 204 float kegW = 23839 * (fsrV * fsrV) - 41348 * fsrV + 17936; // Based on fit to empty weights and 50L when full
sgmcb 51:61a1ec3c56fc 205
sgmcb 51:61a1ec3c56fc 206 return kegW;
sgmcb 51:61a1ec3c56fc 207
sgmcb 51:61a1ec3c56fc 208 }
sgmcb 51:61a1ec3c56fc 209
sgmcb 51:61a1ec3c56fc 210
sgmcb 51:61a1ec3c56fc 211
sgmcb 51:61a1ec3c56fc 212
sgmcb 51:61a1ec3c56fc 213
sgmcb 51:61a1ec3c56fc 214
sgmcb 51:61a1ec3c56fc 215
sgmcb 51:61a1ec3c56fc 216
sgmcb 51:61a1ec3c56fc 217 // Returns 0-255 for proportional fill level
sgmcb 51:61a1ec3c56fc 218 int fillLevel(float kegW, float emptyW) {
sgmcb 51:61a1ec3c56fc 219
sgmcb 51:61a1ec3c56fc 220
sgmcb 51:61a1ec3c56fc 221
sgmcb 51:61a1ec3c56fc 222
sgmcb 51:61a1ec3c56fc 223
sgmcb 51:61a1ec3c56fc 224 }
sgmcb 51:61a1ec3c56fc 225
sgmcb 51:61a1ec3c56fc 226
sgmcb 51:61a1ec3c56fc 227
sgmcb 51:61a1ec3c56fc 228
sgmcb 51:61a1ec3c56fc 229
sgmcb 51:61a1ec3c56fc 230
sgmcb 51:61a1ec3c56fc 231
sgmcb 51:61a1ec3c56fc 232
sgmcb 43:80aa0c933e1a 233 // Torn-out accelerometer code
sgmcb 43:80aa0c933e1a 234 /*
sgmcb 43:80aa0c933e1a 235
sgmcb 43:80aa0c933e1a 236 // ACCELEROMETER
sgmcb 43:80aa0c933e1a 237
sgmcb 43:80aa0c933e1a 238 //printf("Set up accelerometer\r\n");
sgmcb 43:80aa0c933e1a 239
sgmcb 43:80aa0c933e1a 240 printf("I2C write address ID: %01x\r\n",(LIS331_I2C_ADDRESS << 1) & 0xFE );
sgmcb 43:80aa0c933e1a 241 printf("I2C read address ID: %01x\r\n",(LIS331_I2C_ADDRESS << 1) | 0x01 );
sgmcb 43:80aa0c933e1a 242
sgmcb 43:80aa0c933e1a 243 printf("axleID=%01x\r\n", axle.getWhoAmI());
sgmcb 43:80aa0c933e1a 244
sgmcb 43:80aa0c933e1a 245 // Set and read power mode
sgmcb 43:80aa0c933e1a 246 axle.setPowerMode(NORMAL_400HZ);
sgmcb 43:80aa0c933e1a 247 printf("axle Power Mode = %01x\r\n",axle.getPowerMode());
sgmcb 43:80aa0c933e1a 248
sgmcb 43:80aa0c933e1a 249 printf("axle AccelStatus = %01x\r\n", axle.getAccelStatus());
sgmcb 43:80aa0c933e1a 250
sgmcb 43:80aa0c933e1a 251 //float axleAccelZ = ();
sgmcb 43:80aa0c933e1a 252
sgmcb 43:80aa0c933e1a 253
sgmcb 43:80aa0c933e1a 254
sgmcb 43:80aa0c933e1a 255 printf("Begin accel loop\r\n\n");
sgmcb 43:80aa0c933e1a 256 while(true){
sgmcb 43:80aa0c933e1a 257 printf("Z accel = %02f\r\n", axle.getAccelZ());
sgmcb 43:80aa0c933e1a 258 printf("X accel = %02f\r\n", axle.getAccelX());
sgmcb 43:80aa0c933e1a 259 printf("Y accel = %02f\r\n", axle.getAccelY());
sgmcb 43:80aa0c933e1a 260
sgmcb 43:80aa0c933e1a 261
sgmcb 43:80aa0c933e1a 262 delay(5000);
sgmcb 43:80aa0c933e1a 263 };
sgmcb 41:f603d76dc6fe 264
sgmcb 41:f603d76dc6fe 265
sgmcb 43:80aa0c933e1a 266 */