a simple code with a not-so-simple mbed freeze

Dependencies:   MPU6050 mbed-rtos mbed

main.cpp

Committer:
pommzorz
Date:
2013-02-18
Revision:
1:d2011078309d
Parent:
0:b1322cbc592f
Child:
2:141571165c57

File content as of revision 1:d2011078309d:

#include "mbed.h"
#include "MPU6050.h"
#include "rtos.h"

DigitalOut myled(LED1);
Serial pc1(USBTX, USBRX);
MPU6050 mpu(0x69);

int moyZ=0;
int16_t moy[64];


void moyennage_Z()
{
    for (int n=0; n<64; n++) {
        moyZ=moyZ+moy[n];


    }
    moyZ=(int)moyZ/64;
}


void mon_thr(void const *args)
{
    while (true) {
        myled=!myled;
    }
}


int main()

{

    Thread thread(mon_thr); 
    pc1.printf("MPU6050 test\n\n\r");
    pc1.printf("MPU6050 initialize \n\r");

    mpu.initialize();
    pc1.printf("MPU6050 testConnection \n\r");

    int16_t ax, ay, az;
    int16_t gx, gy, gz;
    

int comptFin=0;
int comptDeb=0;


    bool mpu6050TestResult = mpu.testConnection();
    if(mpu6050TestResult) {
        pc1.printf("MPU6050 test passed \n\r");

        while(comptFin<64) {
            mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
            moy[comptFin]=az;
            comptFin++;

        }
        moyennage_Z();
        while(1) {
            while(comptFin<64) {
                mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
                moy[comptFin]=az;
                comptFin++;
                wait(0.01);
            }


            comptFin%=64;
            moyZ-=(int)moy[comptDeb]/64;
            comptDeb++;
            comptDeb%=64;
            moyZ+=(int)moy[comptFin]/64;
            //moyennage_Z();
            printf("%i\n\r",moyZ+17000);
            wait(0.01);




        }


    } else {
        pc1.printf("MPU6050 test failed \n\r");
    }


}