Accelerator test for force measurement all axis. v1
Dependencies: MMA8451Q SLCD mbed
lcd_acc_46_v3.cpp@0:88ddbd76b455, 2014-11-30 (annotated)
- Committer:
- scohennm
- Date:
- Sun Nov 30 00:08:36 2014 +0000
- Revision:
- 0:88ddbd76b455
- Child:
- 1:6c2ec7b0e1c9
Accelerator test for force measurement all axis. v1
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 | 0:88ddbd76b455 | 12 | #define BLINKTIME 0.5 |
scohennm | 0:88ddbd76b455 | 13 | #define DATATIME 0.1 |
scohennm | 0:88ddbd76b455 | 14 | #define NUMLEDS 2 |
scohennm | 0:88ddbd76b455 | 15 | #define LEDON 0 |
scohennm | 0:88ddbd76b455 | 16 | #define LEDOFF 1 |
scohennm | 0:88ddbd76b455 | 17 | #define SCALING 13 |
scohennm | 0:88ddbd76b455 | 18 | #define RSTARTMESS "RSET" |
scohennm | 0:88ddbd76b455 | 19 | #define MAXVECT "MAXV" |
scohennm | 0:88ddbd76b455 | 20 | #define YCOMP "YCMP" |
scohennm | 0:88ddbd76b455 | 21 | #define ANALTOVOLTS 3.3 |
scohennm | 0:88ddbd76b455 | 22 | #define LEDDELAY 0.400 |
scohennm | 0:88ddbd76b455 | 23 | |
scohennm | 0:88ddbd76b455 | 24 | #define PROGNAME "LCD_ACC_LCDv3 46\r/n" |
scohennm | 0:88ddbd76b455 | 25 | |
scohennm | 0:88ddbd76b455 | 26 | //#define PRINTDBUG |
scohennm | 0:88ddbd76b455 | 27 | // Accelerometer SPI pins |
scohennm | 0:88ddbd76b455 | 28 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 0:88ddbd76b455 | 29 | PinName const SDA = PTE25; |
scohennm | 0:88ddbd76b455 | 30 | PinName const SCL = PTE24; |
scohennm | 0:88ddbd76b455 | 31 | #elif defined (TARGET_KL05Z) |
scohennm | 0:88ddbd76b455 | 32 | PinName const SDA = PTB4; |
scohennm | 0:88ddbd76b455 | 33 | PinName const SCL = PTB3; |
scohennm | 0:88ddbd76b455 | 34 | #else |
scohennm | 0:88ddbd76b455 | 35 | #error TARGET NOT DEFINED |
scohennm | 0:88ddbd76b455 | 36 | #endif |
scohennm | 0:88ddbd76b455 | 37 | |
scohennm | 0:88ddbd76b455 | 38 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:88ddbd76b455 | 39 | |
scohennm | 0:88ddbd76b455 | 40 | SLCD slcd; //define LCD display |
scohennm | 0:88ddbd76b455 | 41 | Timer blinkTimer; |
scohennm | 0:88ddbd76b455 | 42 | Timer dataTimer; |
scohennm | 0:88ddbd76b455 | 43 | Timer displayTimer; |
scohennm | 0:88ddbd76b455 | 44 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:88ddbd76b455 | 45 | AnalogIn TMP01(PTB1); |
scohennm | 0:88ddbd76b455 | 46 | DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs |
scohennm | 0:88ddbd76b455 | 47 | Serial pc(USBTX, USBRX); |
scohennm | 0:88ddbd76b455 | 48 | |
scohennm | 0:88ddbd76b455 | 49 | |
scohennm | 0:88ddbd76b455 | 50 | void LCDMess(char *lMess, float dWait){ |
scohennm | 0:88ddbd76b455 | 51 | slcd.Home(); |
scohennm | 0:88ddbd76b455 | 52 | slcd.clear(); |
scohennm | 0:88ddbd76b455 | 53 | slcd.printf(lMess); |
scohennm | 0:88ddbd76b455 | 54 | wait(dWait); |
scohennm | 0:88ddbd76b455 | 55 | } |
scohennm | 0:88ddbd76b455 | 56 | void LCDMessNoDwell(char *lMess){ |
scohennm | 0:88ddbd76b455 | 57 | slcd.Home(); |
scohennm | 0:88ddbd76b455 | 58 | slcd.clear(); |
scohennm | 0:88ddbd76b455 | 59 | slcd.printf(lMess); |
scohennm | 0:88ddbd76b455 | 60 | } |
scohennm | 0:88ddbd76b455 | 61 | void GetOffsets(float *xo, float *yo){ |
scohennm | 0:88ddbd76b455 | 62 | LCDMess(RSTARTMESS, BLINKTIME); |
scohennm | 0:88ddbd76b455 | 63 | *xo = acc.getAccX(); |
scohennm | 0:88ddbd76b455 | 64 | *yo = acc.getAccY(); |
scohennm | 0:88ddbd76b455 | 65 | return; |
scohennm | 0:88ddbd76b455 | 66 | } |
scohennm | 0:88ddbd76b455 | 67 | |
scohennm | 0:88ddbd76b455 | 68 | |
scohennm | 0:88ddbd76b455 | 69 | void allLEDsOFF(int numberOfLEDS) { |
scohennm | 0:88ddbd76b455 | 70 | int i; |
scohennm | 0:88ddbd76b455 | 71 | for (i=0;i<numberOfLEDS; i++){ |
scohennm | 0:88ddbd76b455 | 72 | LEDs[i] = LEDOFF; |
scohennm | 0:88ddbd76b455 | 73 | } |
scohennm | 0:88ddbd76b455 | 74 | |
scohennm | 0:88ddbd76b455 | 75 | } |
scohennm | 0:88ddbd76b455 | 76 | |
scohennm | 0:88ddbd76b455 | 77 | void runLEDs(int numberOfLEDS) { |
scohennm | 0:88ddbd76b455 | 78 | int i; |
scohennm | 0:88ddbd76b455 | 79 | for (i=0;i<numberOfLEDS; i++){ |
scohennm | 0:88ddbd76b455 | 80 | LEDs[i] = LEDON; |
scohennm | 0:88ddbd76b455 | 81 | wait(0.2); |
scohennm | 0:88ddbd76b455 | 82 | } |
scohennm | 0:88ddbd76b455 | 83 | allLEDsOFF(numberOfLEDS); |
scohennm | 0:88ddbd76b455 | 84 | } |
scohennm | 0:88ddbd76b455 | 85 | |
scohennm | 0:88ddbd76b455 | 86 | |
scohennm | 0:88ddbd76b455 | 87 | |
scohennm | 0:88ddbd76b455 | 88 | int main() { |
scohennm | 0:88ddbd76b455 | 89 | |
scohennm | 0:88ddbd76b455 | 90 | int RButtonState; |
scohennm | 0:88ddbd76b455 | 91 | int LButtonState; |
scohennm | 0:88ddbd76b455 | 92 | DigitalIn RtButton(PTC12); |
scohennm | 0:88ddbd76b455 | 93 | DigitalIn LftButton(PTC3); |
scohennm | 0:88ddbd76b455 | 94 | float xOffset; |
scohennm | 0:88ddbd76b455 | 95 | float yOffset; |
scohennm | 0:88ddbd76b455 | 96 | int displayState = true; |
scohennm | 0:88ddbd76b455 | 97 | float xAcc; |
scohennm | 0:88ddbd76b455 | 98 | float yAcc; |
scohennm | 0:88ddbd76b455 | 99 | float zAcc; |
scohennm | 0:88ddbd76b455 | 100 | float vector; |
scohennm | 0:88ddbd76b455 | 101 | float vMax = 0.0; |
scohennm | 0:88ddbd76b455 | 102 | int DisplayTime = DATATIME; |
scohennm | 0:88ddbd76b455 | 103 | int outState = false; |
scohennm | 0:88ddbd76b455 | 104 | char lcdData[10]; //buffer needs places dor decimal pt and colon |
scohennm | 0:88ddbd76b455 | 105 | |
scohennm | 0:88ddbd76b455 | 106 | |
scohennm | 0:88ddbd76b455 | 107 | #ifdef PRINTDBUG |
scohennm | 0:88ddbd76b455 | 108 | pc.printf(PROGNAME); |
scohennm | 0:88ddbd76b455 | 109 | #endif |
scohennm | 0:88ddbd76b455 | 110 | // Initialze readings and sequence the LED's for dramtic effect. |
scohennm | 0:88ddbd76b455 | 111 | allLEDsOFF(NUMLEDS); |
scohennm | 0:88ddbd76b455 | 112 | GetOffsets(&xOffset, &yOffset); |
scohennm | 0:88ddbd76b455 | 113 | // runLEDs(NUMLEDS); |
scohennm | 0:88ddbd76b455 | 114 | blinkTimer.start(); |
scohennm | 0:88ddbd76b455 | 115 | blinkTimer.reset(); |
scohennm | 0:88ddbd76b455 | 116 | |
scohennm | 0:88ddbd76b455 | 117 | // main loop forever |
scohennm | 0:88ddbd76b455 | 118 | while(true) { |
scohennm | 0:88ddbd76b455 | 119 | // Alive LEDs |
scohennm | 0:88ddbd76b455 | 120 | while(blinkTimer.read()> LEDDELAY) { |
scohennm | 0:88ddbd76b455 | 121 | LEDs[0].write(outState); |
scohennm | 0:88ddbd76b455 | 122 | LEDs[1].write(!outState); |
scohennm | 0:88ddbd76b455 | 123 | outState = !outState; |
scohennm | 0:88ddbd76b455 | 124 | blinkTimer.reset(); |
scohennm | 0:88ddbd76b455 | 125 | } |
scohennm | 0:88ddbd76b455 | 126 | // Handle user input for display selections |
scohennm | 0:88ddbd76b455 | 127 | RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream |
scohennm | 0:88ddbd76b455 | 128 | if (RButtonState){ |
scohennm | 0:88ddbd76b455 | 129 | vMax = 0.0; // Clear vMax |
scohennm | 0:88ddbd76b455 | 130 | GetOffsets(&xOffset, &yOffset); // set zero baseling for position |
scohennm | 0:88ddbd76b455 | 131 | #ifdef PRINTDBUG |
scohennm | 0:88ddbd76b455 | 132 | pc.printf("xOffset = %f\r\n", xOffset); |
scohennm | 0:88ddbd76b455 | 133 | pc.printf("yOffset = %f\r\n", yOffset); |
scohennm | 0:88ddbd76b455 | 134 | #endif |
scohennm | 0:88ddbd76b455 | 135 | } |
scohennm | 0:88ddbd76b455 | 136 | LButtonState = !LftButton; |
scohennm | 0:88ddbd76b455 | 137 | if (LButtonState) { //Change data that is displayed |
scohennm | 0:88ddbd76b455 | 138 | displayState = !displayState; |
scohennm | 0:88ddbd76b455 | 139 | // Change to switch/case soon. |
scohennm | 0:88ddbd76b455 | 140 | if (displayState) { |
scohennm | 0:88ddbd76b455 | 141 | LCDMess(YCOMP,BLINKTIME); |
scohennm | 0:88ddbd76b455 | 142 | }else { |
scohennm | 0:88ddbd76b455 | 143 | LCDMess(MAXVECT,BLINKTIME); |
scohennm | 0:88ddbd76b455 | 144 | } |
scohennm | 0:88ddbd76b455 | 145 | } |
scohennm | 0:88ddbd76b455 | 146 | //while (dataTimer.read() > DATATIME){ |
scohennm | 0:88ddbd76b455 | 147 | //Get accelerometer data - tilt angles minus offset for zero mark. |
scohennm | 0:88ddbd76b455 | 148 | // No offset |
scohennm | 0:88ddbd76b455 | 149 | xAcc = abs(acc.getAccX()); |
scohennm | 0:88ddbd76b455 | 150 | yAcc = abs(acc.getAccY()); |
scohennm | 0:88ddbd76b455 | 151 | zAcc = abs(acc.getAccZ()); |
scohennm | 0:88ddbd76b455 | 152 | // Calulate vector sum of x,y and z reading. |
scohennm | 0:88ddbd76b455 | 153 | vector = sqrt(pow(xAcc,2) + pow(yAcc,2)+ pow(zAcc,2)); |
scohennm | 0:88ddbd76b455 | 154 | if (vector > vMax) { |
scohennm | 0:88ddbd76b455 | 155 | vMax = vector; |
scohennm | 0:88ddbd76b455 | 156 | } |
scohennm | 0:88ddbd76b455 | 157 | wait(DATATIME); |
scohennm | 0:88ddbd76b455 | 158 | #ifdef PRINTDBUG |
scohennm | 0:88ddbd76b455 | 159 | pc.printf("vector = %f\r\n", yAcc); |
scohennm | 0:88ddbd76b455 | 160 | pc.printf("scaling = %f\r\n", scaleExpansion); |
scohennm | 0:88ddbd76b455 | 161 | pc.printf("RawTemp = %f\r\n", FDeg); |
scohennm | 0:88ddbd76b455 | 162 | #endif |
scohennm | 0:88ddbd76b455 | 163 | // Display the appropriate data on the LCD based upon what mode was chosen |
scohennm | 0:88ddbd76b455 | 164 | |
scohennm | 0:88ddbd76b455 | 165 | if (displayState) { |
scohennm | 0:88ddbd76b455 | 166 | sprintf (lcdData,"%4.3f",yAcc); |
scohennm | 0:88ddbd76b455 | 167 | }else { |
scohennm | 0:88ddbd76b455 | 168 | sprintf (lcdData,"%4.3f",vMax); |
scohennm | 0:88ddbd76b455 | 169 | } |
scohennm | 0:88ddbd76b455 | 170 | DisplayTime = BLINKTIME; |
scohennm | 0:88ddbd76b455 | 171 | LCDMess(lcdData,DisplayTime); |
scohennm | 0:88ddbd76b455 | 172 | // Wait then do the whole thing again. |
scohennm | 0:88ddbd76b455 | 173 | }//forever loop |
scohennm | 0:88ddbd76b455 | 174 | }// main |