IGOR / Mbed 2 deprecated PONY_Ph0-uAXIS

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

Committer:
sgmcb
Date:
Tue Jan 19 03:23:28 2016 +0000
Revision:
51:61a1ec3c56fc
Parent:
43:80aa0c933e1a
Child:
52:ba2017c3ef8d
Include FSR-reading code

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 41:f603d76dc6fe 149 int accelWhoAmI (I2C* axis) {
sgmcb 41:f603d76dc6fe 150 int axID = 0;
sgmcb 41:f603d76dc6fe 151
sgmcb 41:f603d76dc6fe 152 LIS331read(axis, AX_WHOAMI_REG, &axID);
sgmcb 41:f603d76dc6fe 153
sgmcb 41:f603d76dc6fe 154 return axID;
sgmcb 41:f603d76dc6fe 155 }
sgmcb 41:f603d76dc6fe 156
sgmcb 41:f603d76dc6fe 157 int accelX (I2C* axis) {
sgmcb 41:f603d76dc6fe 158 int xAccel = 0;
sgmcb 41:f603d76dc6fe 159
sgmcb 41:f603d76dc6fe 160 LIS331read(axis,AX_X_REG,&xAccel);
sgmcb 41:f603d76dc6fe 161
sgmcb 41:f603d76dc6fe 162 return xAccel;
sgmcb 41:f603d76dc6fe 163 }
sgmcb 41:f603d76dc6fe 164
sgmcb 41:f603d76dc6fe 165 int accelY (I2C* axis) {
sgmcb 41:f603d76dc6fe 166 int yAccel = 0;
sgmcb 41:f603d76dc6fe 167
sgmcb 41:f603d76dc6fe 168 LIS331read(axis,AX_Y_REG,&yAccel);
sgmcb 41:f603d76dc6fe 169
sgmcb 41:f603d76dc6fe 170 return yAccel;
sgmcb 41:f603d76dc6fe 171 }
sgmcb 41:f603d76dc6fe 172
sgmcb 41:f603d76dc6fe 173
sgmcb 41:f603d76dc6fe 174 int accelZ (I2C* axis) {
sgmcb 41:f603d76dc6fe 175 int zAccel = 0;
sgmcb 41:f603d76dc6fe 176
sgmcb 41:f603d76dc6fe 177 LIS331read(axis,AX_Z_REG,&zAccel);
sgmcb 41:f603d76dc6fe 178
sgmcb 41:f603d76dc6fe 179 return zAccel;
sgmcb 41:f603d76dc6fe 180 }
sgmcb 41:f603d76dc6fe 181
sgmcb 41:f603d76dc6fe 182
sgmcb 51:61a1ec3c56fc 183
sgmcb 51:61a1ec3c56fc 184
sgmcb 51:61a1ec3c56fc 185 // FSR interpretation
sgmcb 51:61a1ec3c56fc 186
sgmcb 51:61a1ec3c56fc 187
sgmcb 51:61a1ec3c56fc 188 #define FSR_THRESHOLD 0.860
sgmcb 51:61a1ec3c56fc 189
sgmcb 51:61a1ec3c56fc 190 // TODO: Calibrations for different keg sizes
sgmcb 51:61a1ec3c56fc 191
sgmcb 51:61a1ec3c56fc 192 float getWeight(AnalogIn* pFSRvolt) {
sgmcb 51:61a1ec3c56fc 193
sgmcb 51:61a1ec3c56fc 194 float fsrVa = *pFSRvolt;
sgmcb 51:61a1ec3c56fc 195 float fsrV = AINTOV(fsrVa);
sgmcb 51:61a1ec3c56fc 196
sgmcb 51:61a1ec3c56fc 197 //printf("fsrV = %.3f \r\n",fsrV);
sgmcb 51:61a1ec3c56fc 198
sgmcb 51:61a1ec3c56fc 199 if(fsrV <= FSR_THRESHOLD)
sgmcb 51:61a1ec3c56fc 200 return 0;
sgmcb 51:61a1ec3c56fc 201
sgmcb 51:61a1ec3c56fc 202
sgmcb 51:61a1ec3c56fc 203 float kegW = 23839 * (fsrV * fsrV) - 41348 * fsrV + 17936; // Based on fit to empty weights and 50L when full
sgmcb 51:61a1ec3c56fc 204
sgmcb 51:61a1ec3c56fc 205 return kegW;
sgmcb 51:61a1ec3c56fc 206
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 // Returns 0-255 for proportional fill level
sgmcb 51:61a1ec3c56fc 217 int fillLevel(float kegW, float emptyW) {
sgmcb 51:61a1ec3c56fc 218
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 43:80aa0c933e1a 232 // Torn-out accelerometer code
sgmcb 43:80aa0c933e1a 233 /*
sgmcb 43:80aa0c933e1a 234
sgmcb 43:80aa0c933e1a 235 // ACCELEROMETER
sgmcb 43:80aa0c933e1a 236
sgmcb 43:80aa0c933e1a 237 //printf("Set up accelerometer\r\n");
sgmcb 43:80aa0c933e1a 238
sgmcb 43:80aa0c933e1a 239 printf("I2C write address ID: %01x\r\n",(LIS331_I2C_ADDRESS << 1) & 0xFE );
sgmcb 43:80aa0c933e1a 240 printf("I2C read address ID: %01x\r\n",(LIS331_I2C_ADDRESS << 1) | 0x01 );
sgmcb 43:80aa0c933e1a 241
sgmcb 43:80aa0c933e1a 242 printf("axleID=%01x\r\n", axle.getWhoAmI());
sgmcb 43:80aa0c933e1a 243
sgmcb 43:80aa0c933e1a 244 // Set and read power mode
sgmcb 43:80aa0c933e1a 245 axle.setPowerMode(NORMAL_400HZ);
sgmcb 43:80aa0c933e1a 246 printf("axle Power Mode = %01x\r\n",axle.getPowerMode());
sgmcb 43:80aa0c933e1a 247
sgmcb 43:80aa0c933e1a 248 printf("axle AccelStatus = %01x\r\n", axle.getAccelStatus());
sgmcb 43:80aa0c933e1a 249
sgmcb 43:80aa0c933e1a 250 //float axleAccelZ = ();
sgmcb 43:80aa0c933e1a 251
sgmcb 43:80aa0c933e1a 252
sgmcb 43:80aa0c933e1a 253
sgmcb 43:80aa0c933e1a 254 printf("Begin accel loop\r\n\n");
sgmcb 43:80aa0c933e1a 255 while(true){
sgmcb 43:80aa0c933e1a 256 printf("Z accel = %02f\r\n", axle.getAccelZ());
sgmcb 43:80aa0c933e1a 257 printf("X accel = %02f\r\n", axle.getAccelX());
sgmcb 43:80aa0c933e1a 258 printf("Y accel = %02f\r\n", axle.getAccelY());
sgmcb 43:80aa0c933e1a 259
sgmcb 43:80aa0c933e1a 260
sgmcb 43:80aa0c933e1a 261 delay(5000);
sgmcb 43:80aa0c933e1a 262 };
sgmcb 41:f603d76dc6fe 263
sgmcb 41:f603d76dc6fe 264
sgmcb 43:80aa0c933e1a 265 */