da testare
Dependencies: mbed
Fork of programmaACC by
main.cpp@4:fa71806deb67, 2018-04-25 (annotated)
- Committer:
- NdA994
- Date:
- Wed Apr 25 11:59:37 2018 +0000
- Revision:
- 4:fa71806deb67
- Parent:
- 3:c9fbf54ed265
ProgrammaACC_daTestare;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giuseppe_guida | 0:7d3cc2de8dd2 | 1 | #include "mbed.h" |
giuseppe_guida | 0:7d3cc2de8dd2 | 2 | #include "header.h" |
giuseppe_guida | 0:7d3cc2de8dd2 | 3 | #include <time.h> |
giuseppe_guida | 0:7d3cc2de8dd2 | 4 | #include "MPU6050.h" |
giuseppe_guida | 0:7d3cc2de8dd2 | 5 | |
NdA994 | 4:fa71806deb67 | 6 | int main(){ |
giuseppe_guida | 3:c9fbf54ed265 | 7 | i2c.frequency(400000); |
giuseppe_guida | 0:7d3cc2de8dd2 | 8 | MPU6050 mpu6050; |
giuseppe_guida | 3:c9fbf54ed265 | 9 | uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050 |
giuseppe_guida | 0:7d3cc2de8dd2 | 10 | |
NdA994 | 4:fa71806deb67 | 11 | if (whoami == 0x68){ |
NdA994 | 4:fa71806deb67 | 12 | |
giuseppe_guida | 0:7d3cc2de8dd2 | 13 | |
giuseppe_guida | 0:7d3cc2de8dd2 | 14 | mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values |
giuseppe_guida | 0:7d3cc2de8dd2 | 15 | |
giuseppe_guida | 0:7d3cc2de8dd2 | 16 | 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){ |
giuseppe_guida | 0:7d3cc2de8dd2 | 17 | mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration |
giuseppe_guida | 0:7d3cc2de8dd2 | 18 | mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers |
giuseppe_guida | 0:7d3cc2de8dd2 | 19 | mpu6050.initMPU6050(); pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature |
giuseppe_guida | 0:7d3cc2de8dd2 | 20 | } |
giuseppe_guida | 0:7d3cc2de8dd2 | 21 | else |
giuseppe_guida | 0:7d3cc2de8dd2 | 22 | pc.printf("Device did not the pass self-test!\n\r"); |
giuseppe_guida | 0:7d3cc2de8dd2 | 23 | } |
giuseppe_guida | 0:7d3cc2de8dd2 | 24 | else{ |
giuseppe_guida | 0:7d3cc2de8dd2 | 25 | pc.printf("Could not connect to MPU6050: \n\r"); |
giuseppe_guida | 0:7d3cc2de8dd2 | 26 | pc.printf("%#x \n", whoami); |
giuseppe_guida | 0:7d3cc2de8dd2 | 27 | |
giuseppe_guida | 0:7d3cc2de8dd2 | 28 | while(1) ; // Loop forever if communication doesn't happen |
giuseppe_guida | 0:7d3cc2de8dd2 | 29 | } |
giuseppe_guida | 0:7d3cc2de8dd2 | 30 | |
giuseppe_guida | 0:7d3cc2de8dd2 | 31 | int i; |
giuseppe_guida | 0:7d3cc2de8dd2 | 32 | for(i = 0; i < BLOCCO; i++){ |
giuseppe_guida | 0:7d3cc2de8dd2 | 33 | |
giuseppe_guida | 0:7d3cc2de8dd2 | 34 | vettore[i].x = 0; |
giuseppe_guida | 0:7d3cc2de8dd2 | 35 | vettore[i].y = 0; |
giuseppe_guida | 0:7d3cc2de8dd2 | 36 | vettore[i].z = 0; |
giuseppe_guida | 3:c9fbf54ed265 | 37 | vettore[i].xx = 0; |
giuseppe_guida | 3:c9fbf54ed265 | 38 | vettore[i].yy = 0; |
giuseppe_guida | 3:c9fbf54ed265 | 39 | vettore[i].zz = 0; |
giuseppe_guida | 3:c9fbf54ed265 | 40 | |
giuseppe_guida | 0:7d3cc2de8dd2 | 41 | } |
giuseppe_guida | 0:7d3cc2de8dd2 | 42 | |
giuseppe_guida | 0:7d3cc2de8dd2 | 43 | while (true) { |
NdA994 | 4:fa71806deb67 | 44 | // Read the WHO_AM_I register, this is a good test of communication |
giuseppe_guida | 3:c9fbf54ed265 | 45 | srand(time(NULL)); |
giuseppe_guida | 3:c9fbf54ed265 | 46 | int i; |
giuseppe_guida | 3:c9fbf54ed265 | 47 | static const int off_set_a=400; |
giuseppe_guida | 3:c9fbf54ed265 | 48 | for(i = 0; i < BLOCCO; i++){ |
giuseppe_guida | 3:c9fbf54ed265 | 49 | |
giuseppe_guida | 3:c9fbf54ed265 | 50 | // If data ready bit set, all data registers have new data |
giuseppe_guida | 3:c9fbf54ed265 | 51 | if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt |
giuseppe_guida | 3:c9fbf54ed265 | 52 | mpu6050.readAccelData(accelCount); // Read the x/y/z adc values |
giuseppe_guida | 3:c9fbf54ed265 | 53 | mpu6050.getAres(); |
giuseppe_guida | 3:c9fbf54ed265 | 54 | |
NdA994 | 4:fa71806deb67 | 55 | //Now we'll calculate the accleration value into actual g's |
NdA994 | 4:fa71806deb67 | 56 | vettore[i].x = (float)accelCount[0]*aRes; // get actual g value, this depends on scale being set |
NdA994 | 4:fa71806deb67 | 57 | vettore[i].y = (float)accelCount[1]*aRes; |
NdA994 | 4:fa71806deb67 | 58 | vettore[i].z = (float)accelCount[2]*aRes; |
giuseppe_guida | 3:c9fbf54ed265 | 59 | |
giuseppe_guida | 3:c9fbf54ed265 | 60 | mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values |
giuseppe_guida | 3:c9fbf54ed265 | 61 | mpu6050.getGres(); |
giuseppe_guida | 3:c9fbf54ed265 | 62 | |
NdA994 | 4:fa71806deb67 | 63 | //Calculate the gyro value into actual degrees per second |
NdA994 | 4:fa71806deb67 | 64 | vettore[i].xx = (float)gyroCount[0]*gRes; // get actual gyro value, this depends on scale being set |
NdA994 | 4:fa71806deb67 | 65 | vettore[i].yy = (float)gyroCount[1]*gRes; |
NdA994 | 4:fa71806deb67 | 66 | vettore[i].zz = (float)gyroCount[2]*gRes; |
giuseppe_guida | 3:c9fbf54ed265 | 67 | |
giuseppe_guida | 3:c9fbf54ed265 | 68 | } |
giuseppe_guida | 3:c9fbf54ed265 | 69 | |
giuseppe_guida | 3:c9fbf54ed265 | 70 | 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); |
giuseppe_guida | 3:c9fbf54ed265 | 71 | |
NdA994 | 4:fa71806deb67 | 72 | } |
giuseppe_guida | 0:7d3cc2de8dd2 | 73 | } |
giuseppe_guida | 0:7d3cc2de8dd2 | 74 | } |