fft+analohADXL100x

Dependencies:   COG4050_ADT7420

Fork of COG4050_blink by valeria toffoli

Committer:
vtoffoli
Date:
Mon Nov 05 08:39:17 2018 +0000
Revision:
2:dbe2cc9e3b23
fft+analogADXL;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vtoffoli 2:dbe2cc9e3b23 1 // --------------------------------------------------------------------------------------------------------
vtoffoli 2:dbe2cc9e3b23 2 //
vtoffoli 2:dbe2cc9e3b23 3 // September 2018
vtoffoli 2:dbe2cc9e3b23 4 // Author: Valeria Toffoli, Rohan Gurav
vtoffoli 2:dbe2cc9e3b23 5 //
vtoffoli 2:dbe2cc9e3b23 6 // --------------------------------------------------------------------------------------------------------
vtoffoli 2:dbe2cc9e3b23 7 //
vtoffoli 2:dbe2cc9e3b23 8 // ADXL100x.h
vtoffoli 2:dbe2cc9e3b23 9 //
vtoffoli 2:dbe2cc9e3b23 10 // --------------------------------------------------------------------------------------------------------
vtoffoli 2:dbe2cc9e3b23 11 //
vtoffoli 2:dbe2cc9e3b23 12 // This library provides all the functions necessary to interface the ADXL355 with EV-COG-AD3029 or
vtoffoli 2:dbe2cc9e3b23 13 // EV-COG-AD4050 Board. Functions for reads and writes,and scaling are included.
vtoffoli 2:dbe2cc9e3b23 14 // This library may be used for the entire ADXL100x family of devices
vtoffoli 2:dbe2cc9e3b23 15 // with some modification.
vtoffoli 2:dbe2cc9e3b23 16 //
vtoffoli 2:dbe2cc9e3b23 17 // Permission is hereby granted, free of charge, to any person obtaining
vtoffoli 2:dbe2cc9e3b23 18 // a copy of this software and associated documentation files (the
vtoffoli 2:dbe2cc9e3b23 19 // "Software"), to deal in the Software without restriction, including
vtoffoli 2:dbe2cc9e3b23 20 // without limitation the rights to use, copy, modify, merge, publish,
vtoffoli 2:dbe2cc9e3b23 21 // distribute, sublicense, and/or sell copies of the Software, and to
vtoffoli 2:dbe2cc9e3b23 22 // permit persons to whom the Software is furnished to do so, subject to
vtoffoli 2:dbe2cc9e3b23 23 // the following conditions:
vtoffoli 2:dbe2cc9e3b23 24 //
vtoffoli 2:dbe2cc9e3b23 25 // The above copyright notice and this permission notice shall be
vtoffoli 2:dbe2cc9e3b23 26 // included in all copies or substantial portions of the Software.
vtoffoli 2:dbe2cc9e3b23 27 //
vtoffoli 2:dbe2cc9e3b23 28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
vtoffoli 2:dbe2cc9e3b23 29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
vtoffoli 2:dbe2cc9e3b23 30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
vtoffoli 2:dbe2cc9e3b23 31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
vtoffoli 2:dbe2cc9e3b23 32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
vtoffoli 2:dbe2cc9e3b23 33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
vtoffoli 2:dbe2cc9e3b23 34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
vtoffoli 2:dbe2cc9e3b23 35 //
vtoffoli 2:dbe2cc9e3b23 36 // --------------------------------------------------------------------------------------------------------
vtoffoli 2:dbe2cc9e3b23 37
vtoffoli 2:dbe2cc9e3b23 38 #include <stdint.h>
vtoffoli 2:dbe2cc9e3b23 39 #include <math.h>
vtoffoli 2:dbe2cc9e3b23 40 #include "mbed.h"
vtoffoli 2:dbe2cc9e3b23 41 #include "ADXL100x.h"
vtoffoli 2:dbe2cc9e3b23 42
vtoffoli 2:dbe2cc9e3b23 43
vtoffoli 2:dbe2cc9e3b23 44
vtoffoli 2:dbe2cc9e3b23 45 /* Constructor with configurable ADXL1005 under study */
vtoffoli 2:dbe2cc9e3b23 46 ADXL100x::ADXL100x(PinName ST_pin , PinName STB_pin, PinName OR_pin , PinName Vout_pin, int device, double power):
vtoffoli 2:dbe2cc9e3b23 47 st(ST_pin), stb(STB_pin), over(OR_pin), vout(Vout_pin) { // pins declarations
vtoffoli 2:dbe2cc9e3b23 48 st = 0; stb = 0;
vtoffoli 2:dbe2cc9e3b23 49 offset = power/2;
vtoffoli 2:dbe2cc9e3b23 50 fact = 13e-3; // V/g
vtoffoli 2:dbe2cc9e3b23 51 sens = 1/fact; // g/V
vtoffoli 2:dbe2cc9e3b23 52 dyn = 200; // g
vtoffoli 2:dbe2cc9e3b23 53 }
vtoffoli 2:dbe2cc9e3b23 54 /* Low power mode - standby */
vtoffoli 2:dbe2cc9e3b23 55 void ADXL100x::standby(){
vtoffoli 2:dbe2cc9e3b23 56 stb = 1;
vtoffoli 2:dbe2cc9e3b23 57 wait(0.001);
vtoffoli 2:dbe2cc9e3b23 58 }
vtoffoli 2:dbe2cc9e3b23 59 /* Wake up rutine: turn On Time <500us */
vtoffoli 2:dbe2cc9e3b23 60 void ADXL100x::wakeup(){
vtoffoli 2:dbe2cc9e3b23 61 stb = 0;
vtoffoli 2:dbe2cc9e3b23 62 wait(0.001);
vtoffoli 2:dbe2cc9e3b23 63 }
vtoffoli 2:dbe2cc9e3b23 64 float ADXL100x::standard_dev(int bit){
vtoffoli 2:dbe2cc9e3b23 65 float sum, sd, mean;
vtoffoli 2:dbe2cc9e3b23 66 float data[128];
vtoffoli 2:dbe2cc9e3b23 67 for (int j = 0; j < 128; j++){
vtoffoli 2:dbe2cc9e3b23 68 data[j] = accelScale(scanx(), bit);
vtoffoli 2:dbe2cc9e3b23 69 sum += data[j];
vtoffoli 2:dbe2cc9e3b23 70 }
vtoffoli 2:dbe2cc9e3b23 71 mean = sum / 128;
vtoffoli 2:dbe2cc9e3b23 72 for (int j = 0; j < 128; j++){
vtoffoli 2:dbe2cc9e3b23 73 sd += pow(data[j] - mean, 2);;
vtoffoli 2:dbe2cc9e3b23 74 }
vtoffoli 2:dbe2cc9e3b23 75 return sqrt(sd/128);
vtoffoli 2:dbe2cc9e3b23 76 }
vtoffoli 2:dbe2cc9e3b23 77 /* Perform calibration over 1g and -1g*/
vtoffoli 2:dbe2cc9e3b23 78 void ADXL100x::calibrate1g(float value_p1g, float value_n1g){
vtoffoli 2:dbe2cc9e3b23 79 float m, b;
vtoffoli 2:dbe2cc9e3b23 80 m = 2/(value_p1g-value_n1g);
vtoffoli 2:dbe2cc9e3b23 81 b = -(value_p1g+value_n1g)/(value_p1g-value_n1g);
vtoffoli 2:dbe2cc9e3b23 82 fact /= m;
vtoffoli 2:dbe2cc9e3b23 83 sens = 1/fact;
vtoffoli 2:dbe2cc9e3b23 84 offset -= b;
vtoffoli 2:dbe2cc9e3b23 85 }
vtoffoli 2:dbe2cc9e3b23 86 /* Perform the self test rutine: output takes approximately 300 μs to assenst*/
vtoffoli 2:dbe2cc9e3b23 87 bool ADXL100x::selftest(int bit, double power){
vtoffoli 2:dbe2cc9e3b23 88 double data0, data1, range;
vtoffoli 2:dbe2cc9e3b23 89 st = 0; wait(0.3);
vtoffoli 2:dbe2cc9e3b23 90 data0 = accelScale(scanx(), bit);
vtoffoli 2:dbe2cc9e3b23 91 st = 1; wait(0.3);
vtoffoli 2:dbe2cc9e3b23 92 data1 = accelScale(scanx(), bit);
vtoffoli 2:dbe2cc9e3b23 93 st = 0; wait(0.3);
vtoffoli 2:dbe2cc9e3b23 94 range = 0.1e-3+0.4e-3*(power-3)/2;
vtoffoli 2:dbe2cc9e3b23 95 if ( abs(data1-data0-range) < 0.05e-3 ){return true;}
vtoffoli 2:dbe2cc9e3b23 96 else {return false;}
vtoffoli 2:dbe2cc9e3b23 97 }
vtoffoli 2:dbe2cc9e3b23 98 /* Read data as 16bit from the ADUCM converter */
vtoffoli 2:dbe2cc9e3b23 99 uint16_t ADXL100x::scanx(){
vtoffoli 2:dbe2cc9e3b23 100 return vout.read_u16();
vtoffoli 2:dbe2cc9e3b23 101 }
vtoffoli 2:dbe2cc9e3b23 102 /* Data conversion from 16bit to g. Need to specify the converter bit num */
vtoffoli 2:dbe2cc9e3b23 103 float ADXL100x::accelScale(uint16_t data, int bit){
vtoffoli 2:dbe2cc9e3b23 104 float result;
vtoffoli 2:dbe2cc9e3b23 105 result = float(scanx())*(dyn*fact)/pow(2.0,double(bit));
vtoffoli 2:dbe2cc9e3b23 106 return (result -offset)*sens;
vtoffoli 2:dbe2cc9e3b23 107 }
vtoffoli 2:dbe2cc9e3b23 108