plop
Accelerometre.cpp@0:807955c8969c, 2017-03-14 (annotated)
- Committer:
- Risord
- Date:
- Tue Mar 14 13:30:11 2017 +0000
- Revision:
- 0:807955c8969c
plop
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Risord | 0:807955c8969c | 1 | #include "Accelerometre.h" |
Risord | 0:807955c8969c | 2 | |
Risord | 0:807955c8969c | 3 | Accelerometre::Accelerometre(void) |
Risord | 0:807955c8969c | 4 | { |
Risord | 0:807955c8969c | 5 | position =0; |
Risord | 0:807955c8969c | 6 | _acc =NULL; |
Risord | 0:807955c8969c | 7 | for (i=0; i<3;i++){ |
Risord | 0:807955c8969c | 8 | readings[i] = 0 ; |
Risord | 0:807955c8969c | 9 | } |
Risord | 0:807955c8969c | 10 | } |
Risord | 0:807955c8969c | 11 | |
Risord | 0:807955c8969c | 12 | |
Risord | 0:807955c8969c | 13 | Accelerometre::Accelerometre(ADXL345_I2C * acc) |
Risord | 0:807955c8969c | 14 | { |
Risord | 0:807955c8969c | 15 | position =0; |
Risord | 0:807955c8969c | 16 | for (i=0; i<3;i++){ |
Risord | 0:807955c8969c | 17 | readings[i] = 0 ; |
Risord | 0:807955c8969c | 18 | } |
Risord | 0:807955c8969c | 19 | if (_acc) delete _acc; // destruction de l'objet accelerometre |
Risord | 0:807955c8969c | 20 | _acc =acc; |
Risord | 0:807955c8969c | 21 | |
Risord | 0:807955c8969c | 22 | } |
Risord | 0:807955c8969c | 23 | |
Risord | 0:807955c8969c | 24 | |
Risord | 0:807955c8969c | 25 | |
Risord | 0:807955c8969c | 26 | |
Risord | 0:807955c8969c | 27 | Accelerometre ::~Accelerometre(void){ |
Risord | 0:807955c8969c | 28 | if (_acc) delete _acc; // destruction de l'objet accelerometre |
Risord | 0:807955c8969c | 29 | if (readings) delete readings; |
Risord | 0:807955c8969c | 30 | |
Risord | 0:807955c8969c | 31 | } |
Risord | 0:807955c8969c | 32 | |
Risord | 0:807955c8969c | 33 | |
Risord | 0:807955c8969c | 34 | int Accelerometre::initAcc(void){ |
Risord | 0:807955c8969c | 35 | if (_acc) { |
Risord | 0:807955c8969c | 36 | |
Risord | 0:807955c8969c | 37 | // These are here to test whether any of the initialization fails. It will print the failure |
Risord | 0:807955c8969c | 38 | if (_acc->setPowerControl(0x00)){ |
Risord | 0:807955c8969c | 39 | //pc.printf("didn't intitialize power control\n\r"); |
Risord | 0:807955c8969c | 40 | return 1; } |
Risord | 0:807955c8969c | 41 | //Full resolution, +/-16g, 4mg/LSB. |
Risord | 0:807955c8969c | 42 | wait(.001); |
Risord | 0:807955c8969c | 43 | |
Risord | 0:807955c8969c | 44 | if(_acc->setDataFormatControl(0x0B)){ |
Risord | 0:807955c8969c | 45 | //pc.printf("didn't set data format\n\r"); |
Risord | 0:807955c8969c | 46 | return 2; } |
Risord | 0:807955c8969c | 47 | wait(.001); |
Risord | 0:807955c8969c | 48 | |
Risord | 0:807955c8969c | 49 | //3.2kHz data rate. |
Risord | 0:807955c8969c | 50 | if(_acc->setDataRate(ADXL345_3200HZ)){ |
Risord | 0:807955c8969c | 51 | //pc.printf("didn't set data rate\n\r"); |
Risord | 0:807955c8969c | 52 | return 3; } |
Risord | 0:807955c8969c | 53 | wait(.001); |
Risord | 0:807955c8969c | 54 | |
Risord | 0:807955c8969c | 55 | //Measurement mode. |
Risord | 0:807955c8969c | 56 | if(_acc->setPowerControl(MeasurementMode)) { |
Risord | 0:807955c8969c | 57 | //pc.printf("didn't set the power control to measurement\n\r"); |
Risord | 0:807955c8969c | 58 | return 4; } |
Risord | 0:807955c8969c | 59 | |
Risord | 0:807955c8969c | 60 | _acc->getOutput(readingPrec); |
Risord | 0:807955c8969c | 61 | |
Risord | 0:807955c8969c | 62 | for(i=0;i<3;i++){ |
Risord | 0:807955c8969c | 63 | if ((int16_t)readingPrec[i]<0){ |
Risord | 0:807955c8969c | 64 | readingPrecABS[i] = readingPrec[i]*(-1); |
Risord | 0:807955c8969c | 65 | } |
Risord | 0:807955c8969c | 66 | else{ |
Risord | 0:807955c8969c | 67 | |
Risord | 0:807955c8969c | 68 | readingPrecABS[i] = readingPrec[i]; |
Risord | 0:807955c8969c | 69 | } |
Risord | 0:807955c8969c | 70 | |
Risord | 0:807955c8969c | 71 | } |
Risord | 0:807955c8969c | 72 | return 10; |
Risord | 0:807955c8969c | 73 | } |
Risord | 0:807955c8969c | 74 | return 0; |
Risord | 0:807955c8969c | 75 | } |
Risord | 0:807955c8969c | 76 | |
Risord | 0:807955c8969c | 77 | void Accelerometre::calculeABS(){ |
Risord | 0:807955c8969c | 78 | for(i=0;i<3;i++){ |
Risord | 0:807955c8969c | 79 | if ((int16_t)readings[i]<0){ |
Risord | 0:807955c8969c | 80 | readingsABS[i] = readings[i]*(-1); |
Risord | 0:807955c8969c | 81 | } |
Risord | 0:807955c8969c | 82 | else{ |
Risord | 0:807955c8969c | 83 | readingsABS[i] = readings[i]; |
Risord | 0:807955c8969c | 84 | } |
Risord | 0:807955c8969c | 85 | |
Risord | 0:807955c8969c | 86 | } |
Risord | 0:807955c8969c | 87 | } |
Risord | 0:807955c8969c | 88 | |
Risord | 0:807955c8969c | 89 | |
Risord | 0:807955c8969c | 90 | |
Risord | 0:807955c8969c | 91 | |
Risord | 0:807955c8969c | 92 | |
Risord | 0:807955c8969c | 93 | char Accelerometre::getPosition(void){ |
Risord | 0:807955c8969c | 94 | _acc->getOutput(readings); |
Risord | 0:807955c8969c | 95 | |
Risord | 0:807955c8969c | 96 | calculeABS(); |
Risord | 0:807955c8969c | 97 | |
Risord | 0:807955c8969c | 98 | |
Risord | 0:807955c8969c | 99 | |
Risord | 0:807955c8969c | 100 | if(((int16_t)readings[0]>150)&&((int16_t)readings[1]<150)&&((int16_t)readings[1]>-150)){ //bas |
Risord | 0:807955c8969c | 101 | position = 1; |
Risord | 0:807955c8969c | 102 | } |
Risord | 0:807955c8969c | 103 | else {if(((int16_t)readings[0]<-100)&&((int16_t)readings[1]<150)&&((int16_t)readings[1]>-150)){//haut |
Risord | 0:807955c8969c | 104 | position = 2; |
Risord | 0:807955c8969c | 105 | } |
Risord | 0:807955c8969c | 106 | else {if(((int16_t)readings[1]>150)&&((int16_t)readings[0]<150)&&((int16_t)readings[0]>-100)){//gauche |
Risord | 0:807955c8969c | 107 | position = 3; |
Risord | 0:807955c8969c | 108 | } |
Risord | 0:807955c8969c | 109 | else {if(((int16_t)readings[1]<-150)&&((int16_t)readings[0]<150)&&((int16_t)readings[0]>-100)){//droite |
Risord | 0:807955c8969c | 110 | position = 4; |
Risord | 0:807955c8969c | 111 | } |
Risord | 0:807955c8969c | 112 | else{position=0;} // attente |
Risord | 0:807955c8969c | 113 | } |
Risord | 0:807955c8969c | 114 | } |
Risord | 0:807955c8969c | 115 | } |
Risord | 0:807955c8969c | 116 | |
Risord | 0:807955c8969c | 117 | |
Risord | 0:807955c8969c | 118 | for (i=0;i<3;i++){ |
Risord | 0:807955c8969c | 119 | readingPrecABS[i] = readingsABS[i]; |
Risord | 0:807955c8969c | 120 | readingPrec[i] = readings[i]; |
Risord | 0:807955c8969c | 121 | } |
Risord | 0:807955c8969c | 122 | |
Risord | 0:807955c8969c | 123 | |
Risord | 0:807955c8969c | 124 | return position; |
Risord | 0:807955c8969c | 125 | |
Risord | 0:807955c8969c | 126 | } |
Risord | 0:807955c8969c | 127 | |
Risord | 0:807955c8969c | 128 | void Accelerometre::getXYZ(int* posXYZ){ |
Risord | 0:807955c8969c | 129 | _acc->getOutput(readings); |
Risord | 0:807955c8969c | 130 | posXYZ[0] = readings[0]; |
Risord | 0:807955c8969c | 131 | posXYZ[1] = readings[1]; |
Risord | 0:807955c8969c | 132 | posXYZ[2] = readings[2]; |
Risord | 0:807955c8969c | 133 | } |
Risord | 0:807955c8969c | 134 |