Simple step tracking

Dependencies:   MPU9250 mbed-os

Fork of ST by alonso palomino

main.cpp

Committer:
alonsopg
Date:
2017-10-12
Revision:
1:3eec9883598a
Parent:
0:30a995e45e2a
Child:
2:02175845b24c

File content as of revision 1:3eec9883598a:

#include "mbed.h"
#include "MPU9250.h"

// Serial comms
Serial pc(USBTX, USBRX);

// Sensor board library
MPU9250 mpu = MPU9250(p26, p27);

// Configuration
bool testMPUConnection = true;
bool doSensorInitialization = false;
bool printAccelerometer = true;
bool printGyroscope = true;

int main () {
    int16_t accelerometer[3] = {0,0,0};
    int16_t gyroscope[3] = {0,0,0};    
        
    if (testMPUConnection) {
        uint8_t whoami = mpu.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);  // Read WHO_AM_I register for MPU-9250
        pc.printf("I AM 0x%x\n\r", whoami); 
        pc.printf("I SHOULD BE 0x71\n\r");
        wait(1);   
    }  
    
    if (doSensorInitialization) {
        // Initialise sensor board    
        pc.printf("Initializing sensor\n\r");
        mpu.initMPU9250();
        pc.printf("Initialization finished\n\r");
        wait(1);
    }
        
    while(1) {
        pc.printf("Starting to stream data");                    
        if(printAccelerometer){
            mpu.readAccelData(accelerometer);
            float ax = accelerometer[0] * 2.0 / 32768.0;
            float ay = accelerometer[1] * 2.0 / 32768.0;
            float az = accelerometer[2] * 2.0 / 32768.0;
            
            pc.printf("Acelerometer information, AX, AY, AZ \n");
            pc.printf("(%f, %f, %f)\n", ax,ay,az);
            
            float roll = float(atan2(ay, az) * 180/3.1416f);
            float pitch = float(atan2(-ax, sqrt(ay*ay + az*az)) * 180/3.1416f);
            float yaw = atan(ax/-ay);
            
            pc.printf("Roll/Pitch/Yaw: (%f, %f, %f)\n", roll, pitch, yaw);
        }
        
        if(printGyroscope){
            mpu.readGyroData(gyroscope);
            float gx = gyroscope[0] * 250.0 / 32768.0;
            float gy = gyroscope[1] * 250.0 / 32768.0;
            float gz = gyroscope[2] * 250.0 / 32768.0;
            pc.printf("Gyroscope information, GX, GY, GZ \n");
            pc.printf("(:%f, %f, %f)\n", gx,gy,gz);
        }
        
        wait(0.3);
    } 
}