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

Dependencies:   MPU6050 mbed-rtos mbed

Committer:
pommzorz
Date:
Mon Feb 18 13:32:59 2013 +0000
Revision:
2:141571165c57
Parent:
1:d2011078309d
memory issues

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pommzorz 0:b1322cbc592f 1 #include "mbed.h"
pommzorz 0:b1322cbc592f 2 #include "MPU6050.h"
pommzorz 1:d2011078309d 3 #include "rtos.h"
pommzorz 0:b1322cbc592f 4
pommzorz 0:b1322cbc592f 5 DigitalOut myled(LED1);
pommzorz 1:d2011078309d 6 Serial pc1(USBTX, USBRX);
pommzorz 0:b1322cbc592f 7 MPU6050 mpu(0x69);
pommzorz 2:141571165c57 8 int16_t ax, ay, az;
pommzorz 2:141571165c57 9 int16_t gx, gy, gz;
pommzorz 2:141571165c57 10 int moyZ=0; //global Z value (mean)
pommzorz 2:141571165c57 11 int16_t moy[64]; //array of different measurements
pommzorz 1:d2011078309d 12
pommzorz 0:b1322cbc592f 13
pommzorz 2:141571165c57 14 void moyennage_Z() //calculates the mean value by going through the whole array, sum and divide by the sample size (64)
pommzorz 0:b1322cbc592f 15 {
pommzorz 0:b1322cbc592f 16 for (int n=0; n<64; n++) {
pommzorz 0:b1322cbc592f 17 moyZ=moyZ+moy[n];
pommzorz 0:b1322cbc592f 18 }
pommzorz 1:d2011078309d 19 moyZ=(int)moyZ/64;
pommzorz 0:b1322cbc592f 20 }
pommzorz 0:b1322cbc592f 21
pommzorz 2:141571165c57 22 void mon_thr(void const *args) //blinking thread to test mbed's state
pommzorz 1:d2011078309d 23 {
pommzorz 1:d2011078309d 24 while (true) {
pommzorz 1:d2011078309d 25 myled=!myled;
pommzorz 2:141571165c57 26 wait(0.5);
pommzorz 1:d2011078309d 27 }
pommzorz 1:d2011078309d 28 }
pommzorz 0:b1322cbc592f 29
pommzorz 0:b1322cbc592f 30 int main()
pommzorz 1:d2011078309d 31
pommzorz 0:b1322cbc592f 32 {
pommzorz 2:141571165c57 33 Thread thread(mon_thr);
pommzorz 2:141571165c57 34 pc1.printf("MPU6050 test\n\n\r"); //procedure to test the connection to the mpu6050, if valid
pommzorz 2:141571165c57 35 pc1.printf("MPU6050 initialize \n\r"); //the code to execute is embedded in a if{}
pommzorz 0:b1322cbc592f 36 mpu.initialize();
pommzorz 1:d2011078309d 37 pc1.printf("MPU6050 testConnection \n\r");
pommzorz 0:b1322cbc592f 38 bool mpu6050TestResult = mpu.testConnection();
pommzorz 0:b1322cbc592f 39 if(mpu6050TestResult) {
pommzorz 1:d2011078309d 40 pc1.printf("MPU6050 test passed \n\r");
pommzorz 0:b1322cbc592f 41
pommzorz 2:141571165c57 42 while(1) {
pommzorz 2:141571165c57 43 for (int n=0; n<64; n++) { //refreshing the 64-int16 array
pommzorz 1:d2011078309d 44 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
pommzorz 2:141571165c57 45 moy[n]=az;
pommzorz 1:d2011078309d 46 wait(0.01);
pommzorz 1:d2011078309d 47 }
pommzorz 2:141571165c57 48 moyennage_Z(); //calculating the mean value by a simple sum and divide
pommzorz 0:b1322cbc592f 49 printf("%i\n\r",moyZ+17000);
pommzorz 2:141571165c57 50 wait(0.1);
pommzorz 0:b1322cbc592f 51 }
pommzorz 0:b1322cbc592f 52 } else {
pommzorz 1:d2011078309d 53 pc1.printf("MPU6050 test failed \n\r");
pommzorz 0:b1322cbc592f 54 }
pommzorz 0:b1322cbc592f 55 }