three axis accelerometer shown as degrees for arccosine

Dependencies:   MMA8451Q SLCD_degrees mbed

Fork of ACC_LCD_341_trig by Stanley Cohen

Committer:
scohennm
Date:
Tue Dec 13 02:24:37 2016 +0000
Revision:
5:75c5e67ef9f1
Child:
6:238277171159
Show angle of orientation modify SLCD for showing degrees

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 5:75c5e67ef9f1 1 #include "mbed.h"
scohennm 5:75c5e67ef9f1 2 #include <math.h>
scohennm 5:75c5e67ef9f1 3 #include "MMA8451Q.h"
scohennm 5:75c5e67ef9f1 4 #include "SLCD.h"
scohennm 5:75c5e67ef9f1 5
scohennm 5:75c5e67ef9f1 6 #define NUMAXES 3
scohennm 5:75c5e67ef9f1 7 #define XAXIS 0
scohennm 5:75c5e67ef9f1 8 #define YAXIS 1
scohennm 5:75c5e67ef9f1 9 #define ZAXIS 2
scohennm 5:75c5e67ef9f1 10 #define NUMBUTS 2
scohennm 5:75c5e67ef9f1 11 #define LBUT PTC12 // port addresses for buttons
scohennm 5:75c5e67ef9f1 12 #define RBUT PTC3
scohennm 5:75c5e67ef9f1 13 #define BUTTONTIME 0.200
scohennm 5:75c5e67ef9f1 14 #define DATAINTERVAL 0.200
scohennm 5:75c5e67ef9f1 15 #define LCDWAIT 1.5
scohennm 5:75c5e67ef9f1 16 #define LCDDATALEN 10
scohennm 5:75c5e67ef9f1 17 #define PI 3.14159265
scohennm 5:75c5e67ef9f1 18 #define PITODEG 180.0
scohennm 5:75c5e67ef9f1 19
scohennm 5:75c5e67ef9f1 20 #define PROGNAME "ACC_LCD_all_axes_v1\r\n"
scohennm 5:75c5e67ef9f1 21
scohennm 5:75c5e67ef9f1 22 #define PRINTDBUG
scohennm 5:75c5e67ef9f1 23 //
scohennm 5:75c5e67ef9f1 24 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
scohennm 5:75c5e67ef9f1 25 PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer.
scohennm 5:75c5e67ef9f1 26 PinName const SCL = PTE24; // DO NOT CHANGE
scohennm 5:75c5e67ef9f1 27 #elif defined (TARGET_KL05Z)
scohennm 5:75c5e67ef9f1 28 PinName const SDA = PTB4;
scohennm 5:75c5e67ef9f1 29 PinName const SCL = PTB3;
scohennm 5:75c5e67ef9f1 30 #else
scohennm 5:75c5e67ef9f1 31 #error TARGET NOT DEFINED
scohennm 5:75c5e67ef9f1 32 #endif
scohennm 5:75c5e67ef9f1 33
scohennm 5:75c5e67ef9f1 34 #define MMA8451_I2C_ADDRESS (0x1d<<1)
scohennm 5:75c5e67ef9f1 35
scohennm 5:75c5e67ef9f1 36 SLCD slcd; //define LCD display
scohennm 5:75c5e67ef9f1 37 char lcdData[LCDDATALEN]; //buffer needs places dor decimal pt and colon
scohennm 5:75c5e67ef9f1 38 int currentAxis = XAXIS; // xaxis
scohennm 5:75c5e67ef9f1 39
scohennm 5:75c5e67ef9f1 40 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
scohennm 5:75c5e67ef9f1 41 Serial pc(USBTX, USBRX);
scohennm 5:75c5e67ef9f1 42 Timer dataTimer;
scohennm 5:75c5e67ef9f1 43 Timer ButtonTimer; // for reading button states
scohennm 5:75c5e67ef9f1 44 DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; // set up buttons
scohennm 5:75c5e67ef9f1 45
scohennm 5:75c5e67ef9f1 46 char axisName[NUMAXES][LCDDATALEN] = {"<X<","<Y<", "<Z<"};
scohennm 5:75c5e67ef9f1 47
scohennm 5:75c5e67ef9f1 48 void LCDMess(char *lMess){
scohennm 5:75c5e67ef9f1 49 slcd.Home();
scohennm 5:75c5e67ef9f1 50 slcd.clear();
scohennm 5:75c5e67ef9f1 51 slcd.printf(lMess);
scohennm 5:75c5e67ef9f1 52 }
scohennm 5:75c5e67ef9f1 53
scohennm 5:75c5e67ef9f1 54 void LCDsignedFloat(float theNumber){
scohennm 5:75c5e67ef9f1 55 sprintf (lcdData," %3.2f",theNumber);
scohennm 5:75c5e67ef9f1 56 // changed SLCD.cpp to interpret < as -
scohennm 5:75c5e67ef9f1 57 if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber));
scohennm 5:75c5e67ef9f1 58 LCDMess(lcdData);
scohennm 5:75c5e67ef9f1 59 }
scohennm 5:75c5e67ef9f1 60 void LCDsignedAngle(float theAngle){
scohennm 5:75c5e67ef9f1 61 sprintf (lcdData," %2.0f@",theAngle);
scohennm 5:75c5e67ef9f1 62 // changed SLCD.cpp to interpret < as -
scohennm 5:75c5e67ef9f1 63 if (theAngle < 0.0) sprintf (lcdData,"<%2.0f@",fabs(theAngle));
scohennm 5:75c5e67ef9f1 64 LCDMess(lcdData);
scohennm 5:75c5e67ef9f1 65 }
scohennm 5:75c5e67ef9f1 66
scohennm 5:75c5e67ef9f1 67 void initialize_global_vars(){
scohennm 5:75c5e67ef9f1 68 pc.printf(PROGNAME);
scohennm 5:75c5e67ef9f1 69 // set up DAQ timers
scohennm 5:75c5e67ef9f1 70 ButtonTimer.start();
scohennm 5:75c5e67ef9f1 71 ButtonTimer.reset();
scohennm 5:75c5e67ef9f1 72 dataTimer.start();
scohennm 5:75c5e67ef9f1 73 dataTimer.reset();
scohennm 5:75c5e67ef9f1 74 LCDMess(axisName[currentAxis]);
scohennm 5:75c5e67ef9f1 75 wait(LCDWAIT);
scohennm 5:75c5e67ef9f1 76
scohennm 5:75c5e67ef9f1 77 }
scohennm 5:75c5e67ef9f1 78
scohennm 5:75c5e67ef9f1 79 int main() {
scohennm 5:75c5e67ef9f1 80 int i; // loop index
scohennm 5:75c5e67ef9f1 81 float axisValue[NUMAXES];
scohennm 5:75c5e67ef9f1 82 float angleValue[NUMAXES];
scohennm 5:75c5e67ef9f1 83 int buttonSum = 0;
scohennm 5:75c5e67ef9f1 84
scohennm 5:75c5e67ef9f1 85 initialize_global_vars();
scohennm 5:75c5e67ef9f1 86 // main loop forever
scohennm 5:75c5e67ef9f1 87 while(true) {
scohennm 5:75c5e67ef9f1 88 while (ButtonTimer > BUTTONTIME){
scohennm 5:75c5e67ef9f1 89 buttonSum = 0;
scohennm 5:75c5e67ef9f1 90 buttonSum = !buttons[0] + !buttons[1];
scohennm 5:75c5e67ef9f1 91 if (buttonSum != 0) {
scohennm 5:75c5e67ef9f1 92 currentAxis = (currentAxis + 1 ) % NUMAXES;
scohennm 5:75c5e67ef9f1 93 LCDMess(axisName[currentAxis]);
scohennm 5:75c5e67ef9f1 94 wait(LCDWAIT);
scohennm 5:75c5e67ef9f1 95 }// for loop to look at buttons
scohennm 5:75c5e67ef9f1 96 ButtonTimer.reset();
scohennm 5:75c5e67ef9f1 97 }
scohennm 5:75c5e67ef9f1 98 while (dataTimer.read() > DATAINTERVAL){
scohennm 5:75c5e67ef9f1 99 dataTimer.reset();
scohennm 5:75c5e67ef9f1 100 axisValue[XAXIS]= acc.getAccX();
scohennm 5:75c5e67ef9f1 101 axisValue[YAXIS] = acc.getAccY();
scohennm 5:75c5e67ef9f1 102 axisValue[ZAXIS] = acc.getAccZ();
scohennm 5:75c5e67ef9f1 103 #ifdef PRINTDBUG
scohennm 5:75c5e67ef9f1 104 for (i=0; i< NUMAXES;i++){
scohennm 5:75c5e67ef9f1 105 pc.printf("Acc %d = %f\r\n",i, axisValue[i]);
scohennm 5:75c5e67ef9f1 106 }
scohennm 5:75c5e67ef9f1 107 #endif
scohennm 5:75c5e67ef9f1 108 if(fabs(axisValue[currentAxis]) > 1.0) axisValue[currentAxis] = 1.0;
scohennm 5:75c5e67ef9f1 109 angleValue[currentAxis] = asin(axisValue[currentAxis]) * PITODEG / PI;
scohennm 5:75c5e67ef9f1 110 LCDsignedAngle(angleValue[currentAxis]);
scohennm 5:75c5e67ef9f1 111 //LCDsignedFloat(axisValue[currentAxis]);
scohennm 5:75c5e67ef9f1 112 }
scohennm 5:75c5e67ef9f1 113 }
scohennm 5:75c5e67ef9f1 114 }