Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of programmaACC by
main.cpp@2:61afd3fd7d6e, 2017-12-05 (annotated)
- Committer:
 - giuseppe_guida
 - Date:
 - Tue Dec 05 19:37:35 2017 +0000
 - Revision:
 - 2:61afd3fd7d6e
 - Parent:
 - 1:c7794bb235e9
 - Child:
 - 3:c9fbf54ed265
 
output aggiustato
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 "rtos.h" | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 3 | #include "header.h" | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 4 | #include <time.h> | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 5 | #include "SDFileSystem.h" | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 6 | #include "MPU6050.h" | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 7 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 8 | //SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd"); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 9 | SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd"); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 10 | Serial pc(USBTX,USBRX,9600); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 11 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 12 | Mutex mutex_ac; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 13 | Mutex mutex_cs; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 14 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 15 | /**********Funzioni***********/ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 16 | int ok_produzione_ac = 1; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 17 | int ok_consumo_ac = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 18 | int ok_produzione_cs = 1; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 19 | int ok_consumo_cs = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 20 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 21 | /*********Inizia Thread Acquisizione********/ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 22 | void funzione_acquisisci(){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 23 | float sum = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 24 | uint32_t sumCount = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 25 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 26 | MPU6050 mpu6050; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 27 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 28 | Timer t; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 29 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 30 | t.start(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 31 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 32 | // Read the WHO_AM_I register, this is a good test of communication | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 33 | uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050 | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 34 | //pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r"); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 35 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 36 | if (whoami == 0x68) // WHO_AM_I should always be 0x68 | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 37 | { | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 38 | //pc.printf("MPU6050 is online..."); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 39 | //wait(1); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 40 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 41 | mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 42 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 43 | 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 | 44 | mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 45 | mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 46 | 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 | 47 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 48 | else | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 49 | pc.printf("Device did not the pass self-test!\n\r"); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 50 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 51 | else{ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 52 | pc.printf("Could not connect to MPU6050: \n\r"); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 53 | pc.printf("%#x \n", whoami); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 54 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 55 | while(1) ; // Loop forever if communication doesn't happen | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 56 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 57 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 58 | srand(time(NULL)); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 59 | int i; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 60 | static const int off_set_a=400; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 61 | time_t rawtime; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 62 | struct tm* timeinfo; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 63 | Timer temp3; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 64 | temp3.start(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 65 | for(i = 0; i < BLOCCO; i++){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 66 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 67 | // If data ready bit set, all data registers have new data | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 68 | if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 69 | mpu6050.readAccelData(accelCount); // Read the x/y/z adc values | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 70 | mpu6050.getAres(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 71 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 72 | // Now we'll calculate the accleration value into actual g's | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 73 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 74 | ay = (float)accelCount[1]*aRes - accelBias[1]; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 75 | az = (float)accelCount[2]*aRes - accelBias[2]; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 76 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 77 | time ( &rawtime ); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 78 | timeinfo = localtime ( &rawtime ); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 79 | vettore[i].x = ax; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 80 | vettore[i].y = ay; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 81 | vettore[i].z = az; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 82 | vettore[i].t = asctime(timeinfo); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 83 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 84 | mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 85 | mpu6050.getGres(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 86 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 87 | // Calculate the gyro value into actual degrees per second | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 88 | gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 89 | gy = (float)gyroCount[1]*gRes; // - gyroBias[1]; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 90 | gz = (float)gyroCount[2]*gRes; // - gyroBias[2]; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 91 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 92 | tempCount = mpu6050.readTempData(); // Read the x/y/z adc values | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 93 | temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 94 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 95 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 96 | Now = t.read_us(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 97 | deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 98 | lastUpdate = Now; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 99 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 100 | sum += deltat; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 101 | sumCount++; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 102 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 103 | if(lastUpdate - firstUpdate > 10000000.0f) { | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 104 | beta = 0.04; // decrease filter gain after stabilized | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 105 | zeta = 0.015; // increasey bias drift gain after stabilized | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 106 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 107 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 108 | // Pass gyro rate as rad/s | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 109 | mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 110 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 111 | // Serial print and/or display at 0.5 s rate independent of data rates | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 112 | delt_t = t.read_ms() - count; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 113 | if (delt_t > 100) { // update LCD once per half-second independent of read rate | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 114 | yaw = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 115 | pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 116 | roll = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 117 | pitch *= 180.0f / PI; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 118 | yaw *= 180.0f / PI; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 119 | roll *= 180.0f / PI; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 120 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 121 | myled= !myled; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 122 | count = t.read_ms(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 123 | sum = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 124 | sumCount = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 125 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 126 | pc.printf("A%03.0f%03.0f%03.0f",100*vettore[i].x+off_set_a,100*vettore[i].y+off_set_a,100*vettore[i].z+off_set_a); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 127 | //pc.printf("X: %f | Y: %f | Z: %f | Time: %s\n\r",vettore[i].x,vettore[i].y,vettore[i].z,vettore[i].t); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 128 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 129 | temp3.stop(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 130 | pc.printf("Tempo impiegato per l'acquisizione di 10 elementi: %f\n\r",temp3.read()); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 131 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 132 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 133 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 134 | void acquisisci(){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 135 | while (true) { | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 136 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 137 | mutex_ac.lock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 138 | while(ok_produzione_ac == 0){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 139 | mutex_ac.unlock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 140 | //Thread::wait(100); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 141 | mutex_ac.lock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 142 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 143 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 144 | funzione_acquisisci(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 145 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 146 | ok_produzione_ac = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 147 | ok_consumo_ac = 1; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 148 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 149 | mutex_ac.unlock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 150 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 151 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 152 | /*******Fine Thread Acquisizione********/ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 153 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 154 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 155 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 156 | /**********Inizio thread copia*******/ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 157 | void funzione_copia(){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 158 | int i; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 159 | Timer temp2; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 160 | temp2.start(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 161 | for(i = 0; i < BLOCCO; i++){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 162 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 163 | appoggio[i].x = vettore[i].x; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 164 | appoggio[i].y = vettore[i].y; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 165 | appoggio[i].z = vettore[i].z; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 166 | appoggio[i].t = vettore[i].t; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 167 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 168 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 169 | temp2.stop(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 170 | pc.printf("Tempo impiegato per la copia di 10 elementi: %f\n\r",temp2.read()); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 171 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 172 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 173 | void copia() | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 174 | { | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 175 | while (true) { | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 176 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 177 | mutex_ac.lock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 178 | while(ok_consumo_ac==0){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 179 | mutex_ac.unlock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 180 | //Thread::wait(100); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 181 | mutex_ac.lock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 182 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 183 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 184 | mutex_cs.lock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 185 | while(ok_produzione_cs==0){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 186 | mutex_cs.unlock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 187 | //Thread::wait(500); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 188 | mutex_cs.lock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 189 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 190 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 191 | funzione_copia(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 192 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 193 | ok_produzione_cs = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 194 | ok_consumo_cs = 1; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 195 | mutex_cs.unlock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 196 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 197 | ok_produzione_ac = 1; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 198 | ok_consumo_ac = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 199 | mutex_ac.unlock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 200 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 201 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 202 | /**********Fine thread copia*********/ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 203 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 204 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 205 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 206 | /********Inizio thread salvataggio***********/ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 207 | void funzione_salva(){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 208 | int i; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 209 | static const int off_set_a=400; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 210 | Timer temp1; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 211 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 212 | mkdir("/sd/mydir",0777); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 213 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 214 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "a"); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 215 | if(fp == NULL) | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 216 | pc.printf("Could not open file for write\n\r"); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 217 | else{ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 218 | temp1.start(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 219 | for(i = 0; i < BLOCCO; i++){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 220 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 221 | //fprintf(fp,"X: %3.3f | Y: %3.3f | Z: %3.3f | Time: %s\r\n",appoggio[i].x,appoggio[i].y,appoggio[i].z,appoggio[i].t); | 
| giuseppe_guida | 2:61afd3fd7d6e | 222 | fprintf(fp,"\n\rA%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); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 223 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 224 | temp1.stop(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 225 | pc.printf("Tempo impiegato per la scrittura di 10 elementi su file: %f\n\r",temp1.read()); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 226 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 227 | fclose(fp); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 228 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 229 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 230 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 231 | void salva() | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 232 | { | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 233 | while (true) { | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 234 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 235 | mutex_cs.lock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 236 | while(ok_consumo_cs==0){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 237 | mutex_cs.unlock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 238 | //Thread::wait(100); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 239 | mutex_cs.lock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 240 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 241 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 242 | funzione_salva(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 243 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 244 | ok_produzione_cs = 1; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 245 | ok_consumo_cs = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 246 | mutex_cs.unlock(); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 247 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 248 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 249 | /************Fine thread salvataggio*************/ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 250 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 251 | /*********Fine Funzioni**********/ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 252 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 253 | Thread Acquisizione; //(acquisisci, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 254 | Thread Copia; //(copia, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 255 | Thread Salvataggio; //(salva, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 256 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 257 | int main() | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 258 | { | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 259 | i2c.frequency(400000); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 260 | int i; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 261 | for(i = 0; i < BLOCCO; i++){ | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 262 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 263 | vettore[i].x = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 264 | vettore[i].y = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 265 | vettore[i].z = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 266 | //vettore[i][j].t = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 267 | appoggio[i].x = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 268 | appoggio[i].y = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 269 | appoggio[i].z = 0; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 270 | //appoggio[i][j].t = ''; | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 271 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 272 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 273 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 274 | while (true) { | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 275 | Acquisizione.start(callback(acquisisci)); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 276 | Copia.start(callback(copia)); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 277 | Salvataggio.start(callback(salva)); | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 278 | |
| giuseppe_guida | 0:7d3cc2de8dd2 | 279 | } | 
| giuseppe_guida | 0:7d3cc2de8dd2 | 280 | } | 
