This program tests and verifies the funcitonality of the Accelerometer as a means of calculating incline angle.

Dependencies:   FXOS8700Q mbed

Accel.cpp

Committer:
roberthill04
Date:
2016-02-24
Revision:
0:101b684deb00

File content as of revision 0:101b684deb00:

/* FXOS8700Q Example Program
 * Copyright (c) 2014-2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "FXOS8700Q.h"




#define PI 3.14159265

Serial pc(USBTX, USBRX);
I2C i2c(PTE25, PTE24);
//FXOS8700Q fxos(i2c, FXOS8700CQ_SLAVE_ADDR1);
FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);    // Configured for the FRDM-K64F with onboard sensors
//FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1);

//FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR0);    // Configured for use with the FRDM-MULTI shield
//FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR0);

DigitalOut led_red(LED_RED);
DigitalOut led_green(LED_GREEN);
DigitalOut led_blue(LED_BLUE);
DigitalIn sw2(SW2);
DigitalIn sw3(SW3);
double angleX, denomX_T, denomX_A, denomX_B, denomX_C;           //intializing variable to hold the angle calculation 
double angleY, denomY_T, denomY_A, denomY_B, denomY_C;                                                                         //and denominator pieces of algorithm
bool Button_Pressed = true;                                                                         //see algorithm in main for what denom_A & _B are defined as



int main(void)
{
    motion_data_units_t acc_data;
 //   motion_data_counts_t acc_raw;             //commented out because we don't need this data
    float faX, faY, faZ, tmp_float;
  //  int16_t raX, raY, raZ, tmp_int;           //commented out because we don't need this data

    acc.enable();
    
    printf("FXOS8700QAccelerometer Who Am I= %X\r\n", acc.whoAmI());
    //printf("FXOS8700QMagnetometer Who Am I= %X\r\n", acc.whoAmI());
    while (1) {
        // counts based results
        /*
        acc.getAxis(acc_raw);
        
        printf("ACC: X=%06dd Y=%06dd Z=%06dd \t \r\n", acc_raw.x, acc_raw.y, acc_raw.z);
        acc.getX(raX);
        acc.getY(raY);
        acc.getZ(raZ);
        
        printf("ACC: X=%06dd Y=%06dd Z=%06dd \t \r\n", raX, raY, raZ);
        printf("ACC: X=%06dd Y=%06dd Z=%06dd \t \r\n", acc.getX(tmp_int), acc.getY(tmp_int), acc.getZ(tmp_int));
        */
        
        // unit based results
        acc.getAxis(acc_data);
        
        printf("ACC: X=%1.4ff Y=%1.4ff Z=%1.4ff \t \r\n", acc_data.x, acc_data.y, acc_data.z);
        acc.getX(faX);
        acc.getY(faY);
        acc.getZ(faZ);
        
        printf("ACC: X=%1.4ff Y=%1.4ff Z=%1.4ff \t \r\n", faX, faY, faZ);
        printf("ACC: X=%1.4ff Y=%1.4ff Z=%1.4ff \t \r\n", acc.getX(tmp_float), acc.getY(tmp_float), acc.getZ(tmp_float));
        
        
        /*
        In order to calculate angle from accel data use the following algorithm:
        Ax = arctan( rawX/sqrt(rawY^2 + rawZ^2))
        Ay = arctan( rawY/sqrt(rawX^2 + rawZ^2))
        using Ax as an example:
        we have denom_T = the total denominator = (sqrt(rawY^2 + rawZ^2 )
        denom_A = rawY^2 && denom_B = rawZ^2 && denom_C = denom_A + denom_B
        we may only be concerned with one of these angles for our application
        also note value is output in radians will need to convert to degrees using: 180/PI
        
        example of how to use inverse tangent function :    
        
        int main ()
        {
            double param, result;
            param = 1.0;
            result = atan (param) * 180 / PI;
            printf ("The arc tangent of %f is %f degrees\n", param, result );
            return 0;
        }
        */
        
        /*
            Example of pow: 7 ^ 3 would be written as pow(7.0, 3.0);
            we can use this find squareroots by making the power exponent = 0.5
        */
        
        //X-AXIS
        denomX_A = pow(faY, 2);
        denomX_B = pow(faZ, 2);
        denomX_C = denomX_A + denomX_B;
        denomX_T = pow(denomX_C, .5);                   //pow returns base raised to the power exponent 
        
        angleX = atan(faX/denomX_T) * 180/PI;           //should calculate the angle on the X axis in degrees based on raw data 
                                
        //Y-AXIS
        denomY_A = pow(faX, 2);
        denomY_B = pow(faZ, 2);
        denomY_C = denomY_A + denomY_B;
        denomY_T = pow(denomY_C, .5);                   //pow returns base raised to the power exponent 
        
        angleY = atan(faY/denomY_T) * 180/PI;           //should calculate the angle on the Y axis in degrees based on raw data                        
    
        printf("Approximate angle in the X-Axis =%f degrees \t \r\n", angleX);     //should print out angle in degrees
        printf("Approximate angle in the Y-Axis =%f degrees \t \r\n", angleY);
        puts("");
        wait(5.0f);
        
         if(sw3 == 0 && Button_Pressed == true)
        {
            acc.disable();   
            printf("Program is paused, press button again to resume.\t \r\n");
            Button_Pressed =false;
        }
        wait(2);
        if(sw3 == 0 && Button_Pressed == false)
        {
            acc.enable();
            Button_Pressed = true;   
        } 
        
        
    }
}