![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
programma di prova
Dependencies: SDFileSystemFunzionante mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "header.h" 00003 #include <time.h> 00004 #include "MPU6050.h" 00005 00006 int main() 00007 { 00008 i2c.frequency(400000); 00009 MPU6050 mpu6050; 00010 uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050 00011 //pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r"); 00012 00013 if (whoami == 0x68) // WHO_AM_I should always be 0x68 00014 { 00015 //pc.printf("MPU6050 is online..."); 00016 //wait(1); 00017 00018 mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values 00019 00020 if(SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f){ 00021 mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration 00022 mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers 00023 mpu6050.initMPU6050(); pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature 00024 } 00025 else 00026 pc.printf("Device did not the pass self-test!\n\r"); 00027 } 00028 else{ 00029 pc.printf("Could not connect to MPU6050: \n\r"); 00030 pc.printf("%#x \n", whoami); 00031 00032 while(1) ; // Loop forever if communication doesn't happen 00033 } 00034 00035 int i; 00036 for(i = 0; i < BLOCCO; i++){ 00037 00038 vettore[i].x = 0; 00039 vettore[i].y = 0; 00040 vettore[i].z = 0; 00041 vettore[i].xx = 0; 00042 vettore[i].yy = 0; 00043 vettore[i].zz = 0; 00044 00045 } 00046 00047 while (true) { 00048 // Read the WHO_AM_I register, this is a good test of communication 00049 srand(time(NULL)); 00050 int i; 00051 static const int off_set_a=400; 00052 //time_t rawtime; 00053 //struct tm* timeinfo; 00054 //Timer temp3; 00055 //temp3.start(); 00056 for(i = 0; i < BLOCCO; i++){ 00057 00058 // If data ready bit set, all data registers have new data 00059 if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt 00060 mpu6050.readAccelData(accelCount); // Read the x/y/z adc values 00061 mpu6050.getAres(); 00062 00063 // Now we'll calculate the accleration value into actual g's 00064 vettore[i].x = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set 00065 vettore[i].y = (float)accelCount[1]*aRes - accelBias[1]; 00066 vettore[i].z = (float)accelCount[2]*aRes - accelBias[2]; 00067 00068 mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values 00069 mpu6050.getGres(); 00070 00071 // Calculate the gyro value into actual degrees per second 00072 vettore[i].xx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set 00073 vettore[i].yy = (float)gyroCount[1]*gRes; // - gyroBias[1]; 00074 vettore[i].zz = (float)gyroCount[2]*gRes; // - gyroBias[2]; 00075 00076 } 00077 00078 pc.printf("%03.0f %03.0f %03.0f %03.0f %03.0f %03.0f\n\r",100*vettore[i].x+off_set_a,100*vettore[i].y+off_set_a,100*vettore[i].z+off_set_a,100*vettore[i].xx+off_set_a,100*vettore[i].yy+off_set_a,100*vettore[i].zz+off_set_a); 00079 00080 } 00081 //temp3.stop(); 00082 //pc.printf("Tempo impiegato per l'acquisizione di 1000 elementi: %f\n\r",temp3.read()); 00083 } 00084 }
Generated on Thu Jul 14 2022 08:03:03 by
![doxygen](doxygen.png)