main_imu, MPU6050 , racolta_dati sono per il funzionamento dell' accelerometro. my_img_sd è una libreria per gestire i dati su un sd sulla quale vengono forniti solamente le funzioni di lettura e scrittura a blocchi i file trasmetti sono la definizione e implementazione delle funzioni del protoccolo per la gestione dell' invio dei dati con il relativo formato

Dependencies:   mbed

Committer:
rattokiller
Date:
Sun Nov 05 14:20:26 2017 +0000
Revision:
0:a9753886e1e0
librerie utili

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rattokiller 0:a9753886e1e0 1 /* MPU6050 Basic Example Code
rattokiller 0:a9753886e1e0 2 by: Kris Winer
rattokiller 0:a9753886e1e0 3 date: May 1, 2014
rattokiller 0:a9753886e1e0 4 license: Beerware - Use this code however you'd like. If you
rattokiller 0:a9753886e1e0 5 find it useful you can buy me a beer some time.
rattokiller 0:a9753886e1e0 6
rattokiller 0:a9753886e1e0 7 Demonstrate MPU-6050 basic functionality including initialization, accelerometer trimming, sleep mode functionality as well as
rattokiller 0:a9753886e1e0 8 parameterizing the register addresses. Added display functions to allow display to on breadboard monitor.
rattokiller 0:a9753886e1e0 9 No DMP use. We just want to get out the accelerations, temperature, and gyro readings.
rattokiller 0:a9753886e1e0 10
rattokiller 0:a9753886e1e0 11 SDA and SCL should have external pull-up resistors (to 3.3V).
rattokiller 0:a9753886e1e0 12 10k resistors worked for me. They should be on the breakout
rattokiller 0:a9753886e1e0 13 board.
rattokiller 0:a9753886e1e0 14
rattokiller 0:a9753886e1e0 15 Hardware setup:
rattokiller 0:a9753886e1e0 16 MPU6050 Breakout --------- Arduino
rattokiller 0:a9753886e1e0 17 3.3V --------------------- 3.3V
rattokiller 0:a9753886e1e0 18 SDA ----------------------- A4
rattokiller 0:a9753886e1e0 19 SCL ----------------------- A5
rattokiller 0:a9753886e1e0 20 GND ---------------------- GND
rattokiller 0:a9753886e1e0 21
rattokiller 0:a9753886e1e0 22 Note: The MPU6050 is an I2C sensor and uses the Arduino Wire library.
rattokiller 0:a9753886e1e0 23 Because the sensor is not 5V tolerant, we are using a 3.3 V 8 MHz Pro Mini or a 3.3 V Teensy 3.1.
rattokiller 0:a9753886e1e0 24 We have disabled the internal pull-ups used by the Wire library in the Wire.h/twi.c utility file.
rattokiller 0:a9753886e1e0 25 We are also using the 400 kHz fast I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file.
rattokiller 0:a9753886e1e0 26 */
rattokiller 0:a9753886e1e0 27 #include "MPU6050.h"
rattokiller 0:a9753886e1e0 28 void calcola_dati();
rattokiller 0:a9753886e1e0 29 float sum = 0;
rattokiller 0:a9753886e1e0 30 uint32_t sumCount = 0;
rattokiller 0:a9753886e1e0 31
rattokiller 0:a9753886e1e0 32 MPU6050 mpu6050;
rattokiller 0:a9753886e1e0 33 Timer t;
rattokiller 0:a9753886e1e0 34
rattokiller 0:a9753886e1e0 35 Thread calcolo_q;
rattokiller 0:a9753886e1e0 36
rattokiller 0:a9753886e1e0 37 //void pc_trasmisione(int n,char* s);
rattokiller 0:a9753886e1e0 38 bool inPosition=true;
rattokiller 0:a9753886e1e0 39
rattokiller 0:a9753886e1e0 40
rattokiller 0:a9753886e1e0 41 #include "racolta_dati.h"
rattokiller 0:a9753886e1e0 42 char buffer[100];
rattokiller 0:a9753886e1e0 43
rattokiller 0:a9753886e1e0 44
rattokiller 0:a9753886e1e0 45 void main_imu() // prendere tutto questo main e meterno in main_imu, rinominarlo e aviorlo da qui.
rattokiller 0:a9753886e1e0 46 {
rattokiller 0:a9753886e1e0 47 // char n; pacco posta;
rattokiller 0:a9753886e1e0 48
rattokiller 0:a9753886e1e0 49 using namespace mydati;
rattokiller 0:a9753886e1e0 50
rattokiller 0:a9753886e1e0 51 dati_imu myimu;
rattokiller 0:a9753886e1e0 52
rattokiller 0:a9753886e1e0 53
rattokiller 0:a9753886e1e0 54
rattokiller 0:a9753886e1e0 55
rattokiller 0:a9753886e1e0 56
rattokiller 0:a9753886e1e0 57
rattokiller 0:a9753886e1e0 58 //Set up I2C
rattokiller 0:a9753886e1e0 59 i2c.frequency(100000); // use fast (400 kHz) I2C
rattokiller 0:a9753886e1e0 60 t.start();
rattokiller 0:a9753886e1e0 61
rattokiller 0:a9753886e1e0 62
rattokiller 0:a9753886e1e0 63
rattokiller 0:a9753886e1e0 64
rattokiller 0:a9753886e1e0 65 wait_ms(350);
rattokiller 0:a9753886e1e0 66
rattokiller 0:a9753886e1e0 67 inPosition=pul;//vero se non è premuto
rattokiller 0:a9753886e1e0 68 if(inPosition)pc.printf("pulsante premuto!");
rattokiller 0:a9753886e1e0 69
rattokiller 0:a9753886e1e0 70 // Read the WHO_AM_I register, this is a good test of communication
rattokiller 0:a9753886e1e0 71 uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050
rattokiller 0:a9753886e1e0 72 pc.printf("\t\tI AM 0x%x\n\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r");
rattokiller 0:a9753886e1e0 73
rattokiller 0:a9753886e1e0 74
rattokiller 0:a9753886e1e0 75 if (whoami == 0x68) // WHO_AM_I should always be 0x68
rattokiller 0:a9753886e1e0 76 {
rattokiller 0:a9753886e1e0 77 pc.printf("MPU6050 is online...");
rattokiller 0:a9753886e1e0 78 wait_ms(50);
rattokiller 0:a9753886e1e0 79
rattokiller 0:a9753886e1e0 80
rattokiller 0:a9753886e1e0 81
rattokiller 0:a9753886e1e0 82 mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values
rattokiller 0:a9753886e1e0 83 pc.printf("x-axis self test: acceleration trim within : "); pc.printf("%d", SelfTest[0]); pc.printf("% of factory value \n\r");
rattokiller 0:a9753886e1e0 84 pc.printf("y-axis self test: acceleration trim within : "); pc.printf("%d", SelfTest[1]); pc.printf("% of factory value \n\r");
rattokiller 0:a9753886e1e0 85 pc.printf("z-axis self test: acceleration trim within : "); pc.printf("%d", SelfTest[2]); pc.printf("% of factory value \n\r");
rattokiller 0:a9753886e1e0 86 pc.printf("x-axis self test: gyration trim within : "); pc.printf("%d", SelfTest[3]); pc.printf("% of factory value \n\r");
rattokiller 0:a9753886e1e0 87 pc.printf("y-axis self test: gyration trim within : "); pc.printf("%d", SelfTest[4]); pc.printf("% of factory value \n\r");
rattokiller 0:a9753886e1e0 88 pc.printf("z-axis self test: gyration trim within : "); pc.printf("%d", SelfTest[5]); pc.printf("% of factory value \n\r");
rattokiller 0:a9753886e1e0 89 wait(1);
rattokiller 0:a9753886e1e0 90
rattokiller 0:a9753886e1e0 91 if(inPosition && SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f)
rattokiller 0:a9753886e1e0 92 {
rattokiller 0:a9753886e1e0 93 mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration
rattokiller 0:a9753886e1e0 94 mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
rattokiller 0:a9753886e1e0 95 mpu6050.initMPU6050(); pc.printf("MPU6050 initialized for active data mode....\n\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
rattokiller 0:a9753886e1e0 96
rattokiller 0:a9753886e1e0 97
rattokiller 0:a9753886e1e0 98 wait(2);
rattokiller 0:a9753886e1e0 99 pc.printf("set acc : x= %f\t,y= %f\tz= %f\r\n;",accelBias[0],accelBias[1],accelBias[2]);
rattokiller 0:a9753886e1e0 100 }
rattokiller 0:a9753886e1e0 101 else
rattokiller 0:a9753886e1e0 102 {
rattokiller 0:a9753886e1e0 103 pc.printf("Device did not the pass self-test!\n\r");
rattokiller 0:a9753886e1e0 104
rattokiller 0:a9753886e1e0 105
rattokiller 0:a9753886e1e0 106 }
rattokiller 0:a9753886e1e0 107 }
rattokiller 0:a9753886e1e0 108 else
rattokiller 0:a9753886e1e0 109 {
rattokiller 0:a9753886e1e0 110 pc.printf("Could not connect to MPU6050: \n\n\r");
rattokiller 0:a9753886e1e0 111 pc.printf("%#x \n", whoami);
rattokiller 0:a9753886e1e0 112
rattokiller 0:a9753886e1e0 113
rattokiller 0:a9753886e1e0 114
rattokiller 0:a9753886e1e0 115 while(1) ; // Loop forever if communication doesn't happen
rattokiller 0:a9753886e1e0 116 }
rattokiller 0:a9753886e1e0 117
rattokiller 0:a9753886e1e0 118
rattokiller 0:a9753886e1e0 119
rattokiller 0:a9753886e1e0 120 calcolo_q.start(calcola_dati);
rattokiller 0:a9753886e1e0 121
rattokiller 0:a9753886e1e0 122 while(1) {
rattokiller 0:a9753886e1e0 123
rattokiller 0:a9753886e1e0 124 wait_ms(100);
rattokiller 0:a9753886e1e0 125
rattokiller 0:a9753886e1e0 126 sprintf(buffer,"\tax = %6.1f\tay = %6.1f\taz = %6.1f\t\t", 1000*ax,1000*ay,1000*az);
rattokiller 0:a9753886e1e0 127 //sprintf(buffer,"ciao");
rattokiller 0:a9753886e1e0 128 wait_ms(10);
rattokiller 0:a9753886e1e0 129
rattokiller 0:a9753886e1e0 130 #if test
rattokiller 0:a9753886e1e0 131
rattokiller 0:a9753886e1e0 132
rattokiller 0:a9753886e1e0 133 //sprintf(buffer,"\tax = %6.1f\tay = %6.1f\taz = %6.1f mg\t\t", 1000*ax,100*ay,100*az);
rattokiller 0:a9753886e1e0 134
rattokiller 0:a9753886e1e0 135
rattokiller 0:a9753886e1e0 136 pc.printf("\tax = %6.1f", 1000*ax);
rattokiller 0:a9753886e1e0 137 pc.printf(" ay = %6.1f", 1000*ay);
rattokiller 0:a9753886e1e0 138 pc.printf(" az = %6.1f mg\t\t", 1000*az);
rattokiller 0:a9753886e1e0 139
rattokiller 0:a9753886e1e0 140 pc.printf("gx = %6.1f", gx);
rattokiller 0:a9753886e1e0 141 pc.printf(" gy = %6.1f", gy);
rattokiller 0:a9753886e1e0 142 pc.printf(" gz = %6.1f deg/s\t", gz);
rattokiller 0:a9753886e1e0 143 // pc.printf("Yaw: %.2f , Pitch: %.2f, Roll: %.2f", yaw, pitch, roll);
rattokiller 0:a9753886e1e0 144 pc.printf("\t temperature = %.2f C\n\r", temperature);
rattokiller 0:a9753886e1e0 145 // pc.printf("q0 = %f\tq1 = %f\tq2 = %f\tq3 = %f\n\r", q[0],q[1],q[2],q[3]);
rattokiller 0:a9753886e1e0 146
rattokiller 0:a9753886e1e0 147 n=strlen(buffer);
rattokiller 0:a9753886e1e0 148 posta.n=n+1;
rattokiller 0:a9753886e1e0 149
rattokiller 0:a9753886e1e0 150 posta.txt=buffer;
rattokiller 0:a9753886e1e0 151
rattokiller 0:a9753886e1e0 152 // telemetria.ins_in_coda(&posta);
rattokiller 0:a9753886e1e0 153
rattokiller 0:a9753886e1e0 154 wait_ms(1);
rattokiller 0:a9753886e1e0 155 // telemetria.invio();
rattokiller 0:a9753886e1e0 156 wait_ms(1);
rattokiller 0:a9753886e1e0 157
rattokiller 0:a9753886e1e0 158 // pc.printf("q0 = %f\tq1 = %f\tq2 = %f\tq3 = %f\n\r", q[0],q[1],q[2],q[3]);
rattokiller 0:a9753886e1e0 159
rattokiller 0:a9753886e1e0 160 #endif
rattokiller 0:a9753886e1e0 161
rattokiller 0:a9753886e1e0 162
rattokiller 0:a9753886e1e0 163 myimu.set_all(ax,ay,az,gx,gy,gz,0,0,0,temperature);
rattokiller 0:a9753886e1e0 164 wait_ms(2);
rattokiller 0:a9753886e1e0 165 myimu.invia();
rattokiller 0:a9753886e1e0 166 wait_ms(2);
rattokiller 0:a9753886e1e0 167 //da sostituire con la funzione della classe sensore imu
rattokiller 0:a9753886e1e0 168
rattokiller 0:a9753886e1e0 169
rattokiller 0:a9753886e1e0 170
rattokiller 0:a9753886e1e0 171 //myled= !myled;
rattokiller 0:a9753886e1e0 172 }
rattokiller 0:a9753886e1e0 173
rattokiller 0:a9753886e1e0 174 }
rattokiller 0:a9753886e1e0 175