ADXL345 Evaluation

Dependencies:   mbed

Committer:
Frederick_H
Date:
Fri Sep 01 10:10:46 2017 +0000
Revision:
5:88787a48e96e
Parent:
4:f5a78245f2d0
Child:
6:592373200317
integration will minus mean before add a value. ; correct mapout option

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Frederick_H 3:e4783c57bcc0 1 #include "ADXL345_I2C.h"
screamer 0:51be560629b8 2
Frederick_H 3:e4783c57bcc0 3 ADXL345_I2C accelerometer(I2C_SDA, I2C_SCL);
Frederick_H 3:e4783c57bcc0 4 Serial pc(USBTX, USBRX);
Frederick_H 4:f5a78245f2d0 5
Frederick_H 4:f5a78245f2d0 6 class RangeMapper
Frederick_H 4:f5a78245f2d0 7 {
Frederick_H 4:f5a78245f2d0 8 /*
Frederick_H 4:f5a78245f2d0 9 RangeMapper convert data from input range to output range
Frederick_H 4:f5a78245f2d0 10 Math formular is y_output = ratio * x_input + offset;
Frederick_H 4:f5a78245f2d0 11 */
Frederick_H 4:f5a78245f2d0 12 protected:
Frederick_H 4:f5a78245f2d0 13 double _ratio;
Frederick_H 4:f5a78245f2d0 14 double _offset;
Frederick_H 4:f5a78245f2d0 15 public:
Frederick_H 4:f5a78245f2d0 16 RangeMapper(double x_min, double x_max, double y_min, double y_max)
Frederick_H 4:f5a78245f2d0 17 {
Frederick_H 4:f5a78245f2d0 18 _ratio = (y_max - y_min) / (x_max - x_min);
Frederick_H 4:f5a78245f2d0 19 _offset = y_min - x_min * _ratio;
Frederick_H 4:f5a78245f2d0 20 };
Frederick_H 4:f5a78245f2d0 21 ~RangeMapper(){};
Frederick_H 4:f5a78245f2d0 22 double getOutput(double x_input)
Frederick_H 4:f5a78245f2d0 23 {return x_input * _ratio + _offset;};
Frederick_H 4:f5a78245f2d0 24 };
screamer 0:51be560629b8 25
Frederick_H 4:f5a78245f2d0 26
Frederick_H 4:f5a78245f2d0 27 class DataProcess
Frederick_H 4:f5a78245f2d0 28 {
Frederick_H 4:f5a78245f2d0 29 protected:
Frederick_H 4:f5a78245f2d0 30 // Statistics
Frederick_H 4:f5a78245f2d0 31 int64_t _summary;
Frederick_H 4:f5a78245f2d0 32 double _squardsum; // summary (xi^2)
Frederick_H 4:f5a78245f2d0 33 int64_t _numbers;
Frederick_H 4:f5a78245f2d0 34 int64_t _max;
Frederick_H 4:f5a78245f2d0 35 int64_t _min;
Frederick_H 4:f5a78245f2d0 36 // integration, assume time interval is same
Frederick_H 4:f5a78245f2d0 37 int64_t _itg1; //1 order integration
Frederick_H 4:f5a78245f2d0 38 int64_t _itg2; //2 order integration
Frederick_H 4:f5a78245f2d0 39 int64_t _itg3; //3 order integration
Frederick_H 4:f5a78245f2d0 40 // differentiation, assume time interval is same
Frederick_H 4:f5a78245f2d0 41 int64_t _preData;
Frederick_H 4:f5a78245f2d0 42 int64_t _dif1; //1 order differentiation
Frederick_H 4:f5a78245f2d0 43 int64_t _dif2; //2 order differentiation
Frederick_H 4:f5a78245f2d0 44 public:
Frederick_H 4:f5a78245f2d0 45 DataProcess():
Frederick_H 4:f5a78245f2d0 46 _summary(0), _squardsum(0), _numbers(0), _max(0), _min(0),
Frederick_H 4:f5a78245f2d0 47 _itg1(0), _itg2(0), _itg3(0), _preData(0), _dif1(0), _dif2(0) {};
Frederick_H 4:f5a78245f2d0 48 ~DataProcess() {};
Frederick_H 4:f5a78245f2d0 49 void putData(int64_t x) {
Frederick_H 4:f5a78245f2d0 50 int64_t temp;
Frederick_H 4:f5a78245f2d0 51 if (x > _max) _max = x;
Frederick_H 4:f5a78245f2d0 52 if (x < _min) _min = x;
Frederick_H 4:f5a78245f2d0 53 _summary += x;
Frederick_H 4:f5a78245f2d0 54 _squardsum += x*x;
Frederick_H 4:f5a78245f2d0 55 _numbers ++;
Frederick_H 5:88787a48e96e 56 _itg1 += x - _summary / _numbers ;
Frederick_H 4:f5a78245f2d0 57 _itg2 += _itg1;
Frederick_H 4:f5a78245f2d0 58 _itg3 += _itg2;
Frederick_H 4:f5a78245f2d0 59 temp = _dif1;
Frederick_H 4:f5a78245f2d0 60 _dif1 = x - _preData;
Frederick_H 4:f5a78245f2d0 61 _preData = x;
Frederick_H 4:f5a78245f2d0 62 _dif2 = _dif1 - temp;
Frederick_H 4:f5a78245f2d0 63 };
Frederick_H 4:f5a78245f2d0 64 int64_t getSum(void) {return _summary;};
Frederick_H 4:f5a78245f2d0 65 double getMean(void) {return (double)_summary / (double)_numbers ;};
Frederick_H 4:f5a78245f2d0 66 double getStdDiv(void) {return sqrt((_squardsum - (double)_summary*_summary / (double)_numbers ) / (double)_numbers ); };
Frederick_H 4:f5a78245f2d0 67 int64_t getMax(void) {return _max; };
Frederick_H 4:f5a78245f2d0 68 int64_t getMin(void) {return _min; };
Frederick_H 4:f5a78245f2d0 69 int64_t getCount(void) {return _numbers;} ;
Frederick_H 4:f5a78245f2d0 70 int64_t getO1integration(void) {return _itg1;};
Frederick_H 4:f5a78245f2d0 71 int64_t getO2integration(void) {return _itg2;};
Frederick_H 4:f5a78245f2d0 72 int64_t getO3integration(void) {return _itg3;};
Frederick_H 4:f5a78245f2d0 73 int64_t GetO1differ(void) {return _dif1;};
Frederick_H 4:f5a78245f2d0 74 int64_t GetO2differ(void) {return _dif2;};
Frederick_H 4:f5a78245f2d0 75 };
Frederick_H 4:f5a78245f2d0 76
Frederick_H 3:e4783c57bcc0 77 int main() {
Frederick_H 4:f5a78245f2d0 78 char getc =0;
Frederick_H 4:f5a78245f2d0 79 int channel = 0, datamode =0;
Frederick_H 5:88787a48e96e 80 bool output = true, mapout = true;
Frederick_H 3:e4783c57bcc0 81 pc.baud(115200);
Frederick_H 3:e4783c57bcc0 82 int readings[3] = {0, 0, 0};
Frederick_H 4:f5a78245f2d0 83 RangeMapper DAC_Mapper( 0 - 1<<13, 1<<13, 0, 1);
Frederick_H 4:f5a78245f2d0 84 DataProcess dp1, dp2, dp3;
Frederick_H 4:f5a78245f2d0 85 DataProcess dp[3] = {dp1, dp2, dp3};
Frederick_H 4:f5a78245f2d0 86
Frederick_H 4:f5a78245f2d0 87
Frederick_H 4:f5a78245f2d0 88
Frederick_H 3:e4783c57bcc0 89 pc.printf("Starting ADXL345 test...\r\n");
Frederick_H 3:e4783c57bcc0 90 wait(.001);
Frederick_H 3:e4783c57bcc0 91 pc.printf("Device ID is: 0x%02x\r\n", accelerometer.getDeviceID());
Frederick_H 3:e4783c57bcc0 92 wait(.001);
Frederick_H 3:e4783c57bcc0 93
Frederick_H 3:e4783c57bcc0 94 restart:
Frederick_H 3:e4783c57bcc0 95
Frederick_H 3:e4783c57bcc0 96 // These are here to test whether any of the initialization fails. It will print the failure
Frederick_H 3:e4783c57bcc0 97 if (accelerometer.setPowerControl(0x00)){
Frederick_H 3:e4783c57bcc0 98 pc.printf("didn't intitialize power control\r\n");
Frederick_H 3:e4783c57bcc0 99 return 0; }
Frederick_H 3:e4783c57bcc0 100 wait(.001);
Frederick_H 3:e4783c57bcc0 101
Frederick_H 3:e4783c57bcc0 102 //Full resolution, +/-16g, 4mg/LSB.
Frederick_H 3:e4783c57bcc0 103 if(accelerometer.setDataFormatControl(0x0B)){
Frederick_H 3:e4783c57bcc0 104 //Full resolution, +/-2g, 4mg/LSB.
Frederick_H 3:e4783c57bcc0 105 //if(accelerometer.setDataFormatControl(0x08)){
Frederick_H 3:e4783c57bcc0 106 pc.printf("didn't set data format\r\n");
Frederick_H 3:e4783c57bcc0 107 return 0; }
Frederick_H 3:e4783c57bcc0 108 wait(.001);
Frederick_H 3:e4783c57bcc0 109
Frederick_H 3:e4783c57bcc0 110 //3.2kHz data rate.
Frederick_H 3:e4783c57bcc0 111 if(accelerometer.setDataRate(ADXL345_3200HZ)){
Frederick_H 3:e4783c57bcc0 112 pc.printf("didn't set data rate\r\n");
Frederick_H 3:e4783c57bcc0 113 return 0; }
Frederick_H 3:e4783c57bcc0 114 wait(.001);
Frederick_H 3:e4783c57bcc0 115
Frederick_H 3:e4783c57bcc0 116 //Measurement mode.
Frederick_H 3:e4783c57bcc0 117
Frederick_H 3:e4783c57bcc0 118 if(accelerometer.setPowerControl(MeasurementMode)) {
Frederick_H 3:e4783c57bcc0 119 pc.printf("didn't set the power control to measurement\r\n");
Frederick_H 3:e4783c57bcc0 120 return 0; }
Frederick_H 3:e4783c57bcc0 121
Frederick_H 4:f5a78245f2d0 122 // pc.printf("x-axis, y-axis, z-axis\r\n");
Frederick_H 4:f5a78245f2d0 123 pc.printf("Data Commands: \r\n");
Frederick_H 4:f5a78245f2d0 124 pc.printf(" x -> x channdel \r\n");
Frederick_H 4:f5a78245f2d0 125 pc.printf(" y -> y channdel \r\n");
Frederick_H 4:f5a78245f2d0 126 pc.printf(" z -> z channdel \r\n");
Frederick_H 4:f5a78245f2d0 127 pc.printf(" o,0 -> data output on/off \r\n");
Frederick_H 4:f5a78245f2d0 128 pc.printf(" i,1 -> mapper on/off \r\n");
Frederick_H 4:f5a78245f2d0 129 pc.printf(" a -> raw data \r\n");
Frederick_H 4:f5a78245f2d0 130 pc.printf(" b -> mean \r\n");
Frederick_H 4:f5a78245f2d0 131 pc.printf(" c -> summary \r\n");
Frederick_H 4:f5a78245f2d0 132 pc.printf(" d -> variance \r\n");
Frederick_H 4:f5a78245f2d0 133 pc.printf(" e -> 1st order Integration \r\n");
Frederick_H 4:f5a78245f2d0 134 pc.printf(" f -> 2nd order Integration \r\n");
Frederick_H 4:f5a78245f2d0 135 pc.printf(" g -> 3rd order Integration \r\n");
Frederick_H 4:f5a78245f2d0 136 pc.printf(" h -> 1st order Differentiation \r\n");
Frederick_H 4:f5a78245f2d0 137 pc.printf(" k -> 2nd order Differentiation \r\n");
Frederick_H 4:f5a78245f2d0 138 pc.printf("Press any key to start. \r\n");
Frederick_H 4:f5a78245f2d0 139 getc = pc.getc();
Frederick_H 4:f5a78245f2d0 140
Frederick_H 4:f5a78245f2d0 141
Frederick_H 3:e4783c57bcc0 142
Frederick_H 3:e4783c57bcc0 143 while (1) {
Frederick_H 3:e4783c57bcc0 144 int error_count=0;
Frederick_H 4:f5a78245f2d0 145 if (pc.readable())
Frederick_H 4:f5a78245f2d0 146 {
Frederick_H 4:f5a78245f2d0 147 getc = pc.getc();
Frederick_H 4:f5a78245f2d0 148 switch( getc)
Frederick_H 4:f5a78245f2d0 149 {
Frederick_H 4:f5a78245f2d0 150 case 'x' : channel = 0 ; pc.printf("Data procesing output switch to X channdel \r\n"); break;
Frederick_H 4:f5a78245f2d0 151 case 'y' : channel = 1 ; pc.printf("Data procesing output switch to Y channdel \r\n"); break;
Frederick_H 4:f5a78245f2d0 152 case 'z' : channel = 2 ; pc.printf("Data procesing output switch to Z channdel \r\n"); break;
Frederick_H 4:f5a78245f2d0 153 case '0' :
Frederick_H 4:f5a78245f2d0 154 case 'o' : output = !output ; pc.printf("Turn %s data output.\r\n", (output ? "On" : "Off") ); break;
Frederick_H 4:f5a78245f2d0 155 case '1' :
Frederick_H 4:f5a78245f2d0 156 case 'i' : mapout = !mapout ; pc.printf("Turn %s mapping output.\r\n", (mapout ? "On" : "Off") ); break;
Frederick_H 4:f5a78245f2d0 157 case 'a' : datamode = 0 ; pc.printf("Set to raw data output.\r\n"); break;
Frederick_H 4:f5a78245f2d0 158 case 'b' : datamode = 1 ; pc.printf("Set to mean output.\r\n"); break;
Frederick_H 4:f5a78245f2d0 159 case 'c' : datamode = 2 ; pc.printf("Set to summary output.\r\n"); break;
Frederick_H 4:f5a78245f2d0 160 case 'd' : datamode = 3 ; pc.printf("Set to variance output.\r\n"); break;
Frederick_H 4:f5a78245f2d0 161 case 'e' : datamode = 4 ; pc.printf("Set to 1st order Integration output.\r\n"); break;
Frederick_H 4:f5a78245f2d0 162 case 'f' : datamode = 5 ; pc.printf("Set to 2nd order Integration output.\r\n"); break;
Frederick_H 4:f5a78245f2d0 163 case 'g' : datamode = 6 ; pc.printf("Set to 3rd order Integration output.\r\n"); break;
Frederick_H 4:f5a78245f2d0 164 case 'h' : datamode = 7 ; pc.printf("Set to 1st order Differentiation output.\r\n"); break;
Frederick_H 4:f5a78245f2d0 165 case 'k' : datamode = 8 ; pc.printf("Set to 2nd order Differentiation output.\r\n"); break;
Frederick_H 4:f5a78245f2d0 166 default: break;
Frederick_H 4:f5a78245f2d0 167 }
Frederick_H 4:f5a78245f2d0 168 }
Frederick_H 3:e4783c57bcc0 169 accelerometer.getOutput(readings);
Frederick_H 3:e4783c57bcc0 170 if (( 17601 == readings[0] ) || ( 17601 == readings[1] ) || ( 17601 == readings[2] ))
Frederick_H 3:e4783c57bcc0 171 {
Frederick_H 3:e4783c57bcc0 172 error_count++;
Frederick_H 3:e4783c57bcc0 173 if (error_count>10)
Frederick_H 3:e4783c57bcc0 174 {
Frederick_H 3:e4783c57bcc0 175 accelerometer.setPowerControl(0);
Frederick_H 3:e4783c57bcc0 176 pc.printf("Sensor Halt!\r\n");
Frederick_H 3:e4783c57bcc0 177 goto restart;
Frederick_H 3:e4783c57bcc0 178 }
Frederick_H 3:e4783c57bcc0 179 }
Frederick_H 3:e4783c57bcc0 180 else
Frederick_H 3:e4783c57bcc0 181 {
Frederick_H 4:f5a78245f2d0 182 error_count = 0;
Frederick_H 5:88787a48e96e 183 int i;
Frederick_H 5:88787a48e96e 184 for (i = 0; i<3; i++)
Frederick_H 5:88787a48e96e 185 dp[i].putData(readings[i] = (int16_t) readings[i] );
Frederick_H 5:88787a48e96e 186
Frederick_H 4:f5a78245f2d0 187 if (output)
Frederick_H 4:f5a78245f2d0 188 {
Frederick_H 4:f5a78245f2d0 189 switch (datamode)
Frederick_H 4:f5a78245f2d0 190 {
Frederick_H 5:88787a48e96e 191 case 0: pc.printf("RAW: %i, %i, %i\r\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); break;
Frederick_H 5:88787a48e96e 192 //case 0: pc.printf("RAW: %i, %i, %i\r\n", readings[0], readings[1], readings[2]); break;
Frederick_H 5:88787a48e96e 193 case 1: pc.printf("MEAN: %f, %f, %f\r\n", dp[0].getMean(), dp[1].getMean(), dp[2].getMean() ); break;
Frederick_H 5:88787a48e96e 194 case 2: pc.printf("SUM: %jd, %jd, %jd\r\n", dp[0].getSum(), dp[1].getSum(), dp[2].getSum() ); break;
Frederick_H 5:88787a48e96e 195 case 3: pc.printf("STD: %f, %f, %f\r\n", dp[0].getStdDiv(), dp[1].getStdDiv(), dp[2].getStdDiv() ); break;
Frederick_H 5:88787a48e96e 196 case 4: pc.printf("1ITG: %jd, %jd, %jd\r\n", dp[0].getO1integration(), dp[1].getO1integration(), dp[2].getO1integration() ); break;
Frederick_H 5:88787a48e96e 197 case 5: pc.printf("2ITG: %jd, %jd, %jd\r\n", dp[0].getO2integration(), dp[1].getO2integration(), dp[2].getO2integration() ); break;
Frederick_H 5:88787a48e96e 198 case 6: pc.printf("3ITG: %jd, %jd, %jd\r\n", dp[0].getO3integration(), dp[1].getO3integration(), dp[2].getO3integration() ); break;
Frederick_H 5:88787a48e96e 199 case 7: pc.printf("1DIF: %jd, %jd, %jd\r\n", dp[0].GetO1differ(), dp[1].GetO1differ(), dp[2].GetO1differ() ); break;
Frederick_H 5:88787a48e96e 200 case 8: pc.printf("2DIF: %jd, %jd, %jd\r\n", dp[0].GetO2differ(), dp[1].GetO2differ(), dp[2].GetO2differ() ); break;
Frederick_H 4:f5a78245f2d0 201 default: break;
Frederick_H 4:f5a78245f2d0 202 }
Frederick_H 4:f5a78245f2d0 203 }
Frederick_H 4:f5a78245f2d0 204 if (mapout)
Frederick_H 4:f5a78245f2d0 205 {
Frederick_H 5:88787a48e96e 206 double mapdata;
Frederick_H 5:88787a48e96e 207 switch (datamode)
Frederick_H 5:88787a48e96e 208 {
Frederick_H 5:88787a48e96e 209 case 0: mapdata = readings[channel]; break;
Frederick_H 5:88787a48e96e 210 case 1: mapdata = dp[channel].getMean(); break;
Frederick_H 5:88787a48e96e 211 case 2: mapdata = dp[channel].getSum(); break;
Frederick_H 5:88787a48e96e 212 case 3: mapdata = dp[channel].getStdDiv(); break;
Frederick_H 5:88787a48e96e 213 case 4: mapdata = dp[channel].getO1integration(); break;
Frederick_H 5:88787a48e96e 214 case 5: mapdata = dp[channel].getO2integration(); break;
Frederick_H 5:88787a48e96e 215 case 6: mapdata = dp[channel].getO3integration(); break;
Frederick_H 5:88787a48e96e 216 case 7: mapdata = dp[channel].GetO1differ(); break;
Frederick_H 5:88787a48e96e 217 case 8: mapdata = dp[channel].GetO2differ(); break;
Frederick_H 5:88787a48e96e 218 default: mapdata = 0; break;
Frederick_H 5:88787a48e96e 219 }
Frederick_H 5:88787a48e96e 220 // Set data the DAC
Frederick_H 5:88787a48e96e 221 DAC_Mapper.getOutput(mapdata);
Frederick_H 4:f5a78245f2d0 222 }
Frederick_H 4:f5a78245f2d0 223 wait(1);
Frederick_H 3:e4783c57bcc0 224 }
Frederick_H 4:f5a78245f2d0 225
Frederick_H 3:e4783c57bcc0 226 }
Frederick_H 3:e4783c57bcc0 227
Frederick_H 3:e4783c57bcc0 228 }