Midterm test program for publishing practice SSD 341 AY2014-2015 NMHU
Dependencies: MMA8451Q SLCD mbed
Fork of ACC_LCD_341_MID by
acc_341.cpp@3:7fe87e8f3563, 2014-10-06 (annotated)
- Committer:
- scohennm
- Date:
- Mon Oct 06 19:44:14 2014 +0000
- Revision:
- 3:7fe87e8f3563
- Parent:
- 2:6003ed409def
MT 8.1 accelerometer code no-hysteresis
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:203b4129a213 | 1 | #include "mbed.h" |
scohennm | 0:203b4129a213 | 2 | #include "MMA8451Q.h" |
scohennm | 0:203b4129a213 | 3 | #include "SLCD.h" |
scohennm | 0:203b4129a213 | 4 | |
scohennm | 0:203b4129a213 | 5 | /* |
scohennm | 0:203b4129a213 | 6 | Test of the accelerometer, digital I/O, on-board LCD screen. |
scohennm | 0:203b4129a213 | 7 | Looing at vector product of the x-y components of the accelerometer. |
scohennm | 0:203b4129a213 | 8 | Works pretty well. Still rough, program wise - sc 140710 |
scohennm | 0:203b4129a213 | 9 | */ |
scohennm | 3:7fe87e8f3563 | 10 | // solution UG 1 |
scohennm | 3:7fe87e8f3563 | 11 | #define NUMLEDS 2 |
scohennm | 3:7fe87e8f3563 | 12 | #define PORTRAIT 0 |
scohennm | 3:7fe87e8f3563 | 13 | #define PORTRAIT1 1 // Add bandwidth |
scohennm | 3:7fe87e8f3563 | 14 | #define LANDSCAPE 10 |
scohennm | 3:7fe87e8f3563 | 15 | #define LANDSCAPE1 9 |
scohennm | 3:7fe87e8f3563 | 16 | #define LEDON 0 |
scohennm | 3:7fe87e8f3563 | 17 | #define LEDOFF 1 |
scohennm | 3:7fe87e8f3563 | 18 | #define RED 0 |
scohennm | 3:7fe87e8f3563 | 19 | #define GREEN 1 |
scohennm | 3:7fe87e8f3563 | 20 | #define ACCSCALING 10.0 |
scohennm | 3:7fe87e8f3563 | 21 | DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; // plan for scalability |
scohennm | 3:7fe87e8f3563 | 22 | // end UG 1 |
scohennm | 0:203b4129a213 | 23 | #define DATATIME 0.200 |
scohennm | 0:203b4129a213 | 24 | |
scohennm | 0:203b4129a213 | 25 | #define PROGNAME "ACCLCD341\r/n" |
scohennm | 0:203b4129a213 | 26 | |
scohennm | 0:203b4129a213 | 27 | #define PRINTDBUG |
scohennm | 2:6003ed409def | 28 | // |
scohennm | 0:203b4129a213 | 29 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 2:6003ed409def | 30 | PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer. |
scohennm | 2:6003ed409def | 31 | PinName const SCL = PTE24; // DO NOT CHANGE |
scohennm | 0:203b4129a213 | 32 | #elif defined (TARGET_KL05Z) |
scohennm | 0:203b4129a213 | 33 | PinName const SDA = PTB4; |
scohennm | 0:203b4129a213 | 34 | PinName const SCL = PTB3; |
scohennm | 0:203b4129a213 | 35 | #else |
scohennm | 0:203b4129a213 | 36 | #error TARGET NOT DEFINED |
scohennm | 0:203b4129a213 | 37 | #endif |
scohennm | 0:203b4129a213 | 38 | |
scohennm | 0:203b4129a213 | 39 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:203b4129a213 | 40 | |
scohennm | 0:203b4129a213 | 41 | SLCD slcd; //define LCD display |
scohennm | 0:203b4129a213 | 42 | |
scohennm | 0:203b4129a213 | 43 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:203b4129a213 | 44 | Serial pc(USBTX, USBRX); |
scohennm | 0:203b4129a213 | 45 | |
scohennm | 2:6003ed409def | 46 | float sqrt_newt(float argument) { |
scohennm | 3:7fe87e8f3563 | 47 | int i = 0; |
scohennm | 3:7fe87e8f3563 | 48 | float xnew = 0.0; |
scohennm | 3:7fe87e8f3563 | 49 | int itermax = 20; |
scohennm | 3:7fe87e8f3563 | 50 | float epsilon = 1e-7; |
scohennm | 3:7fe87e8f3563 | 51 | float xold = argument/2.0; |
scohennm | 3:7fe87e8f3563 | 52 | float delta = 1; |
scohennm | 3:7fe87e8f3563 | 53 | while ((delta > epsilon) && (i < itermax)){ |
scohennm | 3:7fe87e8f3563 | 54 | xnew = 0.5*(xold + (argument/xold)); |
scohennm | 3:7fe87e8f3563 | 55 | delta = abs(xnew-xold); |
scohennm | 3:7fe87e8f3563 | 56 | xold = xnew; |
scohennm | 3:7fe87e8f3563 | 57 | i++; |
scohennm | 3:7fe87e8f3563 | 58 | #ifdef PRINTDBUG |
scohennm | 3:7fe87e8f3563 | 59 | // wait(0.1); |
scohennm | 3:7fe87e8f3563 | 60 | pc.printf("%5.4f %5.4e\r\n",xold, delta); |
scohennm | 3:7fe87e8f3563 | 61 | #endif |
scohennm | 3:7fe87e8f3563 | 62 | } // end while |
scohennm | 3:7fe87e8f3563 | 63 | return (xold); |
scohennm | 3:7fe87e8f3563 | 64 | |
scohennm | 2:6003ed409def | 65 | } |
scohennm | 2:6003ed409def | 66 | |
scohennm | 0:203b4129a213 | 67 | |
scohennm | 0:203b4129a213 | 68 | void LCDMess(char *lMess, float dWait){ |
scohennm | 0:203b4129a213 | 69 | slcd.Home(); |
scohennm | 0:203b4129a213 | 70 | slcd.clear(); |
scohennm | 0:203b4129a213 | 71 | slcd.printf(lMess); |
scohennm | 0:203b4129a213 | 72 | wait(dWait); |
scohennm | 0:203b4129a213 | 73 | } |
scohennm | 0:203b4129a213 | 74 | |
scohennm | 0:203b4129a213 | 75 | |
scohennm | 0:203b4129a213 | 76 | int main() { |
scohennm | 3:7fe87e8f3563 | 77 | int i; |
scohennm | 0:203b4129a213 | 78 | float xAcc; |
scohennm | 0:203b4129a213 | 79 | float yAcc; |
scohennm | 3:7fe87e8f3563 | 80 | float zAcc; |
scohennm | 0:203b4129a213 | 81 | float vector; |
scohennm | 3:7fe87e8f3563 | 82 | int positionState; |
scohennm | 0:203b4129a213 | 83 | char lcdData[10]; //buffer needs places dor decimal pt and colon |
scohennm | 3:7fe87e8f3563 | 84 | Timer DATATimer; |
scohennm | 3:7fe87e8f3563 | 85 | |
scohennm | 3:7fe87e8f3563 | 86 | |
scohennm | 0:203b4129a213 | 87 | |
scohennm | 0:203b4129a213 | 88 | #ifdef PRINTDBUG |
scohennm | 0:203b4129a213 | 89 | pc.printf(PROGNAME); |
scohennm | 0:203b4129a213 | 90 | #endif |
scohennm | 3:7fe87e8f3563 | 91 | DATATimer.start(); |
scohennm | 3:7fe87e8f3563 | 92 | DATATimer.reset(); |
scohennm | 0:203b4129a213 | 93 | // main loop forever |
scohennm | 0:203b4129a213 | 94 | while(true) { |
scohennm | 3:7fe87e8f3563 | 95 | while (DATATimer.read_ms() > DATATIME) { |
scohennm | 3:7fe87e8f3563 | 96 | |
scohennm | 0:203b4129a213 | 97 | //Get accelerometer data - tilt angles minus offset for zero mark. |
scohennm | 3:7fe87e8f3563 | 98 | xAcc = abs(acc.getAccX()); |
scohennm | 3:7fe87e8f3563 | 99 | yAcc = abs(acc.getAccY()); |
scohennm | 3:7fe87e8f3563 | 100 | zAcc = abs(acc.getAccX()); |
scohennm | 0:203b4129a213 | 101 | // Calulate vector sum of x and y reading. |
scohennm | 3:7fe87e8f3563 | 102 | vector = sqrt_newt(pow(xAcc,2) + pow(yAcc,2)); |
scohennm | 3:7fe87e8f3563 | 103 | positionState = int(ACCSCALING * xAcc); |
scohennm | 3:7fe87e8f3563 | 104 | // State Machine |
scohennm | 3:7fe87e8f3563 | 105 | switch (positionState){ |
scohennm | 3:7fe87e8f3563 | 106 | case LANDSCAPE: |
scohennm | 3:7fe87e8f3563 | 107 | case LANDSCAPE1: { |
scohennm | 3:7fe87e8f3563 | 108 | LEDs[RED].write(LEDON); |
scohennm | 3:7fe87e8f3563 | 109 | LEDs[GREEN].write (LEDOFF); |
scohennm | 3:7fe87e8f3563 | 110 | break; |
scohennm | 3:7fe87e8f3563 | 111 | } |
scohennm | 3:7fe87e8f3563 | 112 | case PORTRAIT: |
scohennm | 3:7fe87e8f3563 | 113 | case PORTRAIT1: { |
scohennm | 3:7fe87e8f3563 | 114 | LEDs[RED].write(LEDOFF); |
scohennm | 3:7fe87e8f3563 | 115 | LEDs[GREEN].write (LEDON); |
scohennm | 3:7fe87e8f3563 | 116 | break; |
scohennm | 3:7fe87e8f3563 | 117 | } |
scohennm | 3:7fe87e8f3563 | 118 | default: { |
scohennm | 3:7fe87e8f3563 | 119 | for (i = 0; i< NUMLEDS; i++){ |
scohennm | 3:7fe87e8f3563 | 120 | LEDs[i].write(LEDON); |
scohennm | 3:7fe87e8f3563 | 121 | } |
scohennm | 3:7fe87e8f3563 | 122 | break; |
scohennm | 3:7fe87e8f3563 | 123 | } |
scohennm | 3:7fe87e8f3563 | 124 | } //switch |
scohennm | 0:203b4129a213 | 125 | |
scohennm | 0:203b4129a213 | 126 | #ifdef PRINTDBUG |
scohennm | 1:9340a340e588 | 127 | pc.printf("xAcc = %f\r\n", xAcc); |
scohennm | 1:9340a340e588 | 128 | pc.printf("yAcc = %f\r\n", yAcc); |
scohennm | 0:203b4129a213 | 129 | pc.printf("vector = %f\r\n", vector); |
scohennm | 0:203b4129a213 | 130 | #endif |
scohennm | 0:203b4129a213 | 131 | |
scohennm | 3:7fe87e8f3563 | 132 | sprintf (lcdData,"%4.3f",zAcc); |
scohennm | 3:7fe87e8f3563 | 133 | LCDMess(lcdData, DATATIME); |
scohennm | 3:7fe87e8f3563 | 134 | DATATimer.reset(); |
scohennm | 0:203b4129a213 | 135 | // Wait then do the whole thing again. |
scohennm | 3:7fe87e8f3563 | 136 | } // end whle for timer |
scohennm | 3:7fe87e8f3563 | 137 | }// end forever while |
scohennm | 0:203b4129a213 | 138 | } |