Accelerator test for force measurement all axis. v1
Dependencies: MMA8451Q SLCD mbed
lcd_acc_46_v3.cpp@2:8cdbe8a96b59, 2014-12-01 (annotated)
- Committer:
- scohennm
- Date:
- Mon Dec 01 00:29:40 2014 +0000
- Revision:
- 2:8cdbe8a96b59
- Parent:
- 1:6c2ec7b0e1c9
Added all acc vectors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:88ddbd76b455 | 1 | #include "mbed.h" |
scohennm | 0:88ddbd76b455 | 2 | #include "MMA8451Q.h" |
scohennm | 0:88ddbd76b455 | 3 | #include "SLCD.h" |
scohennm | 0:88ddbd76b455 | 4 | /* |
scohennm | 0:88ddbd76b455 | 5 | Test of the accelerometer, digital I/O, on-board LCD screen, and 16-bit ADC. |
scohennm | 0:88ddbd76b455 | 6 | Looing at vector product of the x-y components of the accelerometer. |
scohennm | 0:88ddbd76b455 | 7 | Works pretty well. Still rough, program wise - sc 140710 |
scohennm | 0:88ddbd76b455 | 8 | Addiing touch sensor to replace potentiometer. |
scohennm | 0:88ddbd76b455 | 9 | Show x and y components |
scohennm | 0:88ddbd76b455 | 10 | */ |
scohennm | 0:88ddbd76b455 | 11 | |
scohennm | 1:6c2ec7b0e1c9 | 12 | #define BLINKTIME 0.7 |
scohennm | 0:88ddbd76b455 | 13 | #define DATATIME 0.1 |
scohennm | 1:6c2ec7b0e1c9 | 14 | #define DATADISPDWELL 0.2 |
scohennm | 0:88ddbd76b455 | 15 | #define NUMLEDS 2 |
scohennm | 0:88ddbd76b455 | 16 | #define LEDON 0 |
scohennm | 0:88ddbd76b455 | 17 | #define LEDOFF 1 |
scohennm | 0:88ddbd76b455 | 18 | #define SCALING 13 |
scohennm | 0:88ddbd76b455 | 19 | #define RSTARTMESS "RSET" |
scohennm | 0:88ddbd76b455 | 20 | #define MAXVECT "MAXV" |
scohennm | 2:8cdbe8a96b59 | 21 | #define XCOMP "XCMP" |
scohennm | 0:88ddbd76b455 | 22 | #define YCOMP "YCMP" |
scohennm | 2:8cdbe8a96b59 | 23 | #define ZCOMP "ZCMP" |
scohennm | 0:88ddbd76b455 | 24 | #define ANALTOVOLTS 3.3 |
scohennm | 0:88ddbd76b455 | 25 | #define LEDDELAY 0.400 |
scohennm | 2:8cdbe8a96b59 | 26 | //define states |
scohennm | 2:8cdbe8a96b59 | 27 | #define NUMSTATES 4 |
scohennm | 2:8cdbe8a96b59 | 28 | #define XCOMPD 0 |
scohennm | 2:8cdbe8a96b59 | 29 | #define YCOMPD 1 |
scohennm | 2:8cdbe8a96b59 | 30 | #define ZCOMPD 2 |
scohennm | 2:8cdbe8a96b59 | 31 | #define VMAXD 3 |
scohennm | 0:88ddbd76b455 | 32 | |
scohennm | 0:88ddbd76b455 | 33 | #define PROGNAME "LCD_ACC_LCDv3 46\r/n" |
scohennm | 0:88ddbd76b455 | 34 | |
scohennm | 0:88ddbd76b455 | 35 | //#define PRINTDBUG |
scohennm | 0:88ddbd76b455 | 36 | // Accelerometer SPI pins |
scohennm | 0:88ddbd76b455 | 37 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 0:88ddbd76b455 | 38 | PinName const SDA = PTE25; |
scohennm | 0:88ddbd76b455 | 39 | PinName const SCL = PTE24; |
scohennm | 0:88ddbd76b455 | 40 | #elif defined (TARGET_KL05Z) |
scohennm | 0:88ddbd76b455 | 41 | PinName const SDA = PTB4; |
scohennm | 0:88ddbd76b455 | 42 | PinName const SCL = PTB3; |
scohennm | 0:88ddbd76b455 | 43 | #else |
scohennm | 0:88ddbd76b455 | 44 | #error TARGET NOT DEFINED |
scohennm | 0:88ddbd76b455 | 45 | #endif |
scohennm | 0:88ddbd76b455 | 46 | |
scohennm | 0:88ddbd76b455 | 47 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:88ddbd76b455 | 48 | |
scohennm | 0:88ddbd76b455 | 49 | SLCD slcd; //define LCD display |
scohennm | 0:88ddbd76b455 | 50 | Timer blinkTimer; |
scohennm | 0:88ddbd76b455 | 51 | Timer dataTimer; |
scohennm | 0:88ddbd76b455 | 52 | Timer displayTimer; |
scohennm | 0:88ddbd76b455 | 53 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:88ddbd76b455 | 54 | AnalogIn TMP01(PTB1); |
scohennm | 0:88ddbd76b455 | 55 | DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs |
scohennm | 0:88ddbd76b455 | 56 | Serial pc(USBTX, USBRX); |
scohennm | 0:88ddbd76b455 | 57 | |
scohennm | 0:88ddbd76b455 | 58 | |
scohennm | 0:88ddbd76b455 | 59 | void LCDMess(char *lMess, float dWait){ |
scohennm | 0:88ddbd76b455 | 60 | slcd.Home(); |
scohennm | 0:88ddbd76b455 | 61 | slcd.clear(); |
scohennm | 0:88ddbd76b455 | 62 | slcd.printf(lMess); |
scohennm | 0:88ddbd76b455 | 63 | wait(dWait); |
scohennm | 0:88ddbd76b455 | 64 | } |
scohennm | 0:88ddbd76b455 | 65 | void LCDMessNoDwell(char *lMess){ |
scohennm | 0:88ddbd76b455 | 66 | slcd.Home(); |
scohennm | 0:88ddbd76b455 | 67 | slcd.clear(); |
scohennm | 0:88ddbd76b455 | 68 | slcd.printf(lMess); |
scohennm | 0:88ddbd76b455 | 69 | } |
scohennm | 0:88ddbd76b455 | 70 | |
scohennm | 2:8cdbe8a96b59 | 71 | |
scohennm | 0:88ddbd76b455 | 72 | void allLEDsOFF(int numberOfLEDS) { |
scohennm | 0:88ddbd76b455 | 73 | int i; |
scohennm | 0:88ddbd76b455 | 74 | for (i=0;i<numberOfLEDS; i++){ |
scohennm | 0:88ddbd76b455 | 75 | LEDs[i] = LEDOFF; |
scohennm | 0:88ddbd76b455 | 76 | } |
scohennm | 0:88ddbd76b455 | 77 | |
scohennm | 0:88ddbd76b455 | 78 | } |
scohennm | 0:88ddbd76b455 | 79 | |
scohennm | 0:88ddbd76b455 | 80 | void runLEDs(int numberOfLEDS) { |
scohennm | 0:88ddbd76b455 | 81 | int i; |
scohennm | 0:88ddbd76b455 | 82 | for (i=0;i<numberOfLEDS; i++){ |
scohennm | 0:88ddbd76b455 | 83 | LEDs[i] = LEDON; |
scohennm | 0:88ddbd76b455 | 84 | wait(0.2); |
scohennm | 0:88ddbd76b455 | 85 | } |
scohennm | 0:88ddbd76b455 | 86 | allLEDsOFF(numberOfLEDS); |
scohennm | 0:88ddbd76b455 | 87 | } |
scohennm | 0:88ddbd76b455 | 88 | |
scohennm | 0:88ddbd76b455 | 89 | |
scohennm | 0:88ddbd76b455 | 90 | |
scohennm | 0:88ddbd76b455 | 91 | int main() { |
scohennm | 0:88ddbd76b455 | 92 | |
scohennm | 0:88ddbd76b455 | 93 | int RButtonState; |
scohennm | 0:88ddbd76b455 | 94 | int LButtonState; |
scohennm | 0:88ddbd76b455 | 95 | DigitalIn RtButton(PTC12); |
scohennm | 0:88ddbd76b455 | 96 | DigitalIn LftButton(PTC3); |
scohennm | 2:8cdbe8a96b59 | 97 | int displayState = XCOMPD; |
scohennm | 1:6c2ec7b0e1c9 | 98 | float xAcc = 0.0; |
scohennm | 1:6c2ec7b0e1c9 | 99 | float yAcc = 0.0; |
scohennm | 2:8cdbe8a96b59 | 100 | float zAcc = 0.0; |
scohennm | 0:88ddbd76b455 | 101 | float vector; |
scohennm | 0:88ddbd76b455 | 102 | float vMax = 0.0; |
scohennm | 1:6c2ec7b0e1c9 | 103 | float DisplayTime = DATADISPDWELL; |
scohennm | 0:88ddbd76b455 | 104 | int outState = false; |
scohennm | 0:88ddbd76b455 | 105 | char lcdData[10]; //buffer needs places dor decimal pt and colon |
scohennm | 0:88ddbd76b455 | 106 | |
scohennm | 0:88ddbd76b455 | 107 | |
scohennm | 0:88ddbd76b455 | 108 | #ifdef PRINTDBUG |
scohennm | 0:88ddbd76b455 | 109 | pc.printf(PROGNAME); |
scohennm | 0:88ddbd76b455 | 110 | #endif |
scohennm | 2:8cdbe8a96b59 | 111 | LCDMess(RSTARTMESS, BLINKTIME); |
scohennm | 0:88ddbd76b455 | 112 | // Initialze readings and sequence the LED's for dramtic effect. |
scohennm | 0:88ddbd76b455 | 113 | allLEDsOFF(NUMLEDS); |
scohennm | 0:88ddbd76b455 | 114 | blinkTimer.start(); |
scohennm | 0:88ddbd76b455 | 115 | blinkTimer.reset(); |
scohennm | 1:6c2ec7b0e1c9 | 116 | displayTimer.start(); |
scohennm | 1:6c2ec7b0e1c9 | 117 | displayTimer.reset(); |
scohennm | 1:6c2ec7b0e1c9 | 118 | dataTimer.start(); |
scohennm | 1:6c2ec7b0e1c9 | 119 | dataTimer.reset(); |
scohennm | 0:88ddbd76b455 | 120 | |
scohennm | 0:88ddbd76b455 | 121 | // main loop forever |
scohennm | 0:88ddbd76b455 | 122 | while(true) { |
scohennm | 2:8cdbe8a96b59 | 123 | |
scohennm | 0:88ddbd76b455 | 124 | // Handle user input for display selections |
scohennm | 0:88ddbd76b455 | 125 | RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream |
scohennm | 0:88ddbd76b455 | 126 | if (RButtonState){ |
scohennm | 0:88ddbd76b455 | 127 | vMax = 0.0; // Clear vMax |
scohennm | 2:8cdbe8a96b59 | 128 | LCDMess(RSTARTMESS, BLINKTIME); |
scohennm | 0:88ddbd76b455 | 129 | } |
scohennm | 0:88ddbd76b455 | 130 | LButtonState = !LftButton; |
scohennm | 0:88ddbd76b455 | 131 | if (LButtonState) { //Change data that is displayed |
scohennm | 2:8cdbe8a96b59 | 132 | displayState = (displayState + 1) % NUMSTATES; |
scohennm | 0:88ddbd76b455 | 133 | // Change to switch/case soon. |
scohennm | 2:8cdbe8a96b59 | 134 | switch (displayState){ |
scohennm | 2:8cdbe8a96b59 | 135 | case XCOMPD: { |
scohennm | 2:8cdbe8a96b59 | 136 | LCDMess(XCOMP,BLINKTIME); |
scohennm | 2:8cdbe8a96b59 | 137 | break; |
scohennm | 2:8cdbe8a96b59 | 138 | } |
scohennm | 2:8cdbe8a96b59 | 139 | case YCOMPD: { |
scohennm | 2:8cdbe8a96b59 | 140 | LCDMess(YCOMP,BLINKTIME); |
scohennm | 2:8cdbe8a96b59 | 141 | break; |
scohennm | 2:8cdbe8a96b59 | 142 | } |
scohennm | 2:8cdbe8a96b59 | 143 | case ZCOMPD: { |
scohennm | 2:8cdbe8a96b59 | 144 | LCDMess(ZCOMP,BLINKTIME); |
scohennm | 2:8cdbe8a96b59 | 145 | break; |
scohennm | 2:8cdbe8a96b59 | 146 | } |
scohennm | 2:8cdbe8a96b59 | 147 | case VMAXD:{ |
scohennm | 2:8cdbe8a96b59 | 148 | LCDMess(MAXVECT,BLINKTIME); |
scohennm | 2:8cdbe8a96b59 | 149 | break; |
scohennm | 2:8cdbe8a96b59 | 150 | } |
scohennm | 2:8cdbe8a96b59 | 151 | }// switch |
scohennm | 0:88ddbd76b455 | 152 | } |
scohennm | 1:6c2ec7b0e1c9 | 153 | |
scohennm | 1:6c2ec7b0e1c9 | 154 | // -------------------------------------------- |
scohennm | 1:6c2ec7b0e1c9 | 155 | while (dataTimer.read() > DATATIME){ |
scohennm | 0:88ddbd76b455 | 156 | //Get accelerometer data - tilt angles minus offset for zero mark. |
scohennm | 0:88ddbd76b455 | 157 | // No offset |
scohennm | 0:88ddbd76b455 | 158 | xAcc = abs(acc.getAccX()); |
scohennm | 0:88ddbd76b455 | 159 | yAcc = abs(acc.getAccY()); |
scohennm | 2:8cdbe8a96b59 | 160 | zAcc = abs(acc.getAccZ()); |
scohennm | 0:88ddbd76b455 | 161 | // Calulate vector sum of x,y and z reading. |
scohennm | 2:8cdbe8a96b59 | 162 | vector = sqrt(pow(xAcc,2) + pow(zAcc,2)); |
scohennm | 0:88ddbd76b455 | 163 | if (vector > vMax) { |
scohennm | 0:88ddbd76b455 | 164 | vMax = vector; |
scohennm | 0:88ddbd76b455 | 165 | } |
scohennm | 1:6c2ec7b0e1c9 | 166 | dataTimer.reset(); |
scohennm | 1:6c2ec7b0e1c9 | 167 | } |
scohennm | 0:88ddbd76b455 | 168 | #ifdef PRINTDBUG |
scohennm | 2:8cdbe8a96b59 | 169 | pc.printf("vector = %f\r\n", xAcc); |
scohennm | 0:88ddbd76b455 | 170 | pc.printf("scaling = %f\r\n", scaleExpansion); |
scohennm | 0:88ddbd76b455 | 171 | pc.printf("RawTemp = %f\r\n", FDeg); |
scohennm | 0:88ddbd76b455 | 172 | #endif |
scohennm | 0:88ddbd76b455 | 173 | // Display the appropriate data on the LCD based upon what mode was chosen |
scohennm | 1:6c2ec7b0e1c9 | 174 | while (displayTimer.read() > DisplayTime){ |
scohennm | 2:8cdbe8a96b59 | 175 | switch (displayState) { |
scohennm | 2:8cdbe8a96b59 | 176 | case XCOMPD: { |
scohennm | 2:8cdbe8a96b59 | 177 | sprintf (lcdData,"%4.3f",xAcc); |
scohennm | 2:8cdbe8a96b59 | 178 | break; |
scohennm | 2:8cdbe8a96b59 | 179 | } |
scohennm | 2:8cdbe8a96b59 | 180 | case YCOMPD: { |
scohennm | 2:8cdbe8a96b59 | 181 | sprintf (lcdData,"%4.3f",yAcc); |
scohennm | 2:8cdbe8a96b59 | 182 | break; |
scohennm | 2:8cdbe8a96b59 | 183 | } |
scohennm | 2:8cdbe8a96b59 | 184 | case ZCOMPD: { |
scohennm | 2:8cdbe8a96b59 | 185 | sprintf (lcdData,"%4.3f",zAcc); |
scohennm | 2:8cdbe8a96b59 | 186 | break; |
scohennm | 2:8cdbe8a96b59 | 187 | } |
scohennm | 2:8cdbe8a96b59 | 188 | case VMAXD:{ |
scohennm | 2:8cdbe8a96b59 | 189 | sprintf (lcdData,"%4.3f",vMax); |
scohennm | 2:8cdbe8a96b59 | 190 | break; |
scohennm | 2:8cdbe8a96b59 | 191 | } |
scohennm | 0:88ddbd76b455 | 192 | } |
scohennm | 1:6c2ec7b0e1c9 | 193 | LCDMessNoDwell(lcdData); |
scohennm | 1:6c2ec7b0e1c9 | 194 | displayTimer.reset(); |
scohennm | 1:6c2ec7b0e1c9 | 195 | } // displaytimer |
scohennm | 2:8cdbe8a96b59 | 196 | // Wait then do the whole thing again. |
scohennm | 2:8cdbe8a96b59 | 197 | // Alive LEDs |
scohennm | 2:8cdbe8a96b59 | 198 | while(blinkTimer.read()> LEDDELAY) { |
scohennm | 2:8cdbe8a96b59 | 199 | LEDs[0].write(outState); |
scohennm | 2:8cdbe8a96b59 | 200 | LEDs[1].write(!outState); |
scohennm | 2:8cdbe8a96b59 | 201 | outState = !outState; |
scohennm | 2:8cdbe8a96b59 | 202 | blinkTimer.reset(); |
scohennm | 2:8cdbe8a96b59 | 203 | } |
scohennm | 0:88ddbd76b455 | 204 | }//forever loop |
scohennm | 0:88ddbd76b455 | 205 | }// main |