acceleration et etallonage du zero

Dependencies:   mbed mbed

Fork of ADXL335_3axis_Accel by SHIVAM TRIPATHI

Files at this revision

API Documentation at this revision

Comitter:
Zlatan10
Date:
Wed Apr 11 19:28:43 2018 +0000
Parent:
3:7cef8e5abfe5
Commit message:
Acceleration et etallonage du zero

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.h Show annotated file Show diff for this revision Revisions of this file
mbed.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 7cef8e5abfe5 -r ee0a25a1c3dc main.cpp
--- a/main.cpp	Sun Sep 04 08:04:35 2016 +0000
+++ b/main.cpp	Wed Apr 11 19:28:43 2018 +0000
@@ -1,28 +1,210 @@
-/****************************************************************************
- ****** 3-axis Accelerometer ADXL335 or GY-61 interfacing to FRDM-KL25Z *****
- ******                  Author :  SHIVAM TRIPATHI                     ******  
- ***************************************************************************/
+/*****************************************
+ ****** 3-axis Accelerometer ADXL335 *****  
+ *****************************************/
 
 #include "mbed.h"
 
-Serial pc(USBTX, USBRX);
-AnalogIn analog_value1(A0);  //Output of X-axis at analog pin A0  ........ (Refer pinout)
-AnalogIn analog_value2(A1);  //Output of y-axis at analog pin A1
-AnalogIn analog_value3(A2);  //Output of z-axis at analog pin A1
+#define ETALONNAGE 100
+#define VERIFICATION 100
+#define MESURE 500
+//#define LIMITE 65535 * 1 / 1000
+#define LIMITE 4095 * 1 / 1000
+#define LSB 14.365
+
+Timer timer;
+
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+AnalogIn acc_x(A0);     // Output of X-axis
+AnalogIn acc_y(A1);     // Output of y-axis
+AnalogIn acc_z(A2);     // Output of z-axis
+DigitalOut led(LED1);   // output of LED
+
+void mesure_zero_acceleration(int *zero_x, int *zero_y, int *zero_z)
+{
+    float fx, fy, fz;
+    float f_etalonnage = (float)ETALONNAGE;
+    int x, y, z;
+    int c, k;
+    
+    for (c = 0; c < ETALONNAGE; c++)
+    {
+           x = 0;
+           y = 0;
+           z = 0;
+           for (k = 0; k < MESURE; k++)
+           {
+              x += acc_x.read_u16() >> 4;
+              y += acc_y.read_u16() >> 4;
+              z += acc_z.read_u16() >> 4;
+           }
+           *zero_x += x;
+           *zero_y += y;
+           *zero_z += z;
+    }
+    fx = *zero_x / f_etalonnage;
+    fy = *zero_y / f_etalonnage;
+    fz = *zero_z / f_etalonnage;
+    *zero_x /= ETALONNAGE;
+    *zero_y /= ETALONNAGE;
+    *zero_z /= ETALONNAGE;
+    
+    if ((fx - *zero_x) >= 0.5) *zero_x ++;
+    if ((fy - *zero_y) >= 0.5) *zero_y ++;
+    if ((fz - *zero_z) >= 0.5) *zero_z ++;
+    
+    pc.printf("AX_0;AY_0;AZ_0\n");
+    pc.printf("%d;%d;%d\n", *zero_x, *zero_y, *zero_z);
+}
 
-DigitalOut led(LED1);
+void verif_zero_acceleration(int zero_x, int zero_y, int zero_z)
+{
+    int x, y, z;
+    float fx, fy, fz;
+    float f_mesure = (float)MESURE;
+    int c, k;
+    
+    for (c = 0; c < VERIFICATION; c++)
+    {
+        x = 0; y = 0; z = 0;
+        
+        for (k = 0; k < MESURE; k++)
+        {
+            x += ( acc_x.read_u16() >> 4 );
+            y += ( acc_y.read_u16() >> 4 );
+            z += ( acc_z.read_u16() >> 4 );
+        }
+        
+        x -= zero_x;
+        y -= zero_y;
+        z -= zero_z;
+        
+        fx = x / f_mesure;
+        fy = y / f_mesure;
+        fz = z / f_mesure;
+        x /= MESURE;
+        y /= MESURE;
+        z /= MESURE;
+        
+        if (fx < 0)
+        {
+            if ( (fx - x) <= - 0.5 ) x--;
+        }else
+            if ( (fx - x) >= 0.5 ) x++;
+        if (fy < 0)
+        {
+            if ( (fy - y) <= - 0.5 ) y--;
+        }else
+            if ( (fy - y) >= 0.5 ) y++;
+        if (fz < 0)
+        {
+            if ( (fz - z) <= - 0.5 ) z--;
+        }else
+            if ( (fz - z) >= 0.5 ) z++;
+        
+        if( abs (x) > LIMITE )
+        {
+             pc.printf("Etalonnage en X manque! Ecart = %d\n", abs(x) - LIMITE);
+             exit(EXIT_FAILURE);
+        }
+        if( abs (y) > LIMITE > LIMITE )
+        {
+             pc.printf("Etalonnage en Y manque! Ecart = %d\n", abs(y) - LIMITE);
+             exit(EXIT_FAILURE);
+        }
+        if( abs (z) > LIMITE )
+        {
+            pc.printf("Etalonnage en Z manque! Ecart = %d\n", abs(z) - LIMITE);
+            exit(EXIT_FAILURE);
+        }
+    }
+}
 
 int main() {
-    int x,y,z;
+    int ax, ay, az;                            // Store the acceleration
+    /*
+    float vx = 0, vy = 0, vz = 0;              // Store the speed
+    float x = 0, y = 0, z = 0;                 // Store the position
+    */
+    float fx, fy, fz;
+    float f_mesure = (float)MESURE;
+    int zero_x = 0, zero_y = 0, zero_z = 0; // 0 acceleration
+    float t;                                // Measurement Time
+    int i, cpt = 0;                         // Counters
     
-    printf("\nAnalogIn example\n");
+    wait(2);
+    
+    timer.start();
+    
+    pc.baud(9600);
+    
+    mesure_zero_acceleration(&zero_x, &zero_y, &zero_z);
+    //verif_zero_acceleration(zero_x, zero_y, zero_z);
+    
+    led = 1;
+    
+    pc.printf("Temps d'acquisition;AX;AY;AZ;Numero d'echantillon\n");
+    //pc.printf("AX;AY;AZ;VX;VY;VZ;X;Y;Z\n");
     
     while(1) {
-        x = analog_value1.read_u16();           // Reads X-axis value and then converts in 16 bit format (3.3V --> 65535).........Analog values are read
-        y = analog_value2.read_u16();           // Reads Y-axis value and then converts in 16 bit format (3.3V --> 65535)
-        z = analog_value3.read_u16();           // Reads Z-axis value and then converts in 16 bit format (3.3V --> 65535)
-        printf("\r x = %d  y = %d  z = %d  \n", x,y,z);    //Prints output on pc Serial terminal (serial USB com driver is a must)
-        wait(0.5); // 500 ms
+        cpt ++;
+        ax = 0; ay = 0; az = 0;
+        //timer.start();
+        for (i = 0; i < MESURE; i++)
+        {
+            ax += ( acc_x.read_u16() >> 4 );           // Reads X-axis value (on 16 bits converted to 12)
+            ay += ( acc_y.read_u16() >> 4 );           // Reads Y-axis value
+            az += ( acc_z.read_u16() >> 4 );           // Reads Z-axis value
+        }
+        //timer.stop();
+        t = timer.read();
+        //timer.reset();
+        
+        ax -= zero_x;
+        ay -= zero_y;
+        az -= zero_z;
+    
+        fx = ax / f_mesure;
+        fy = ay / f_mesure;
+        fz = az / f_mesure;
+        ax /= MESURE;
+        ay /= MESURE;
+        az /= MESURE;
         
+        if (fx < 0)
+        {
+            if ( (fx - ax) <= - 0.5 ) ax--;
+        }else
+            if ( (fx - ax) >= 0.5 ) ax++;
+        if (fy < 0)
+        {
+            if ( (fy - ay) <= - 0.5 ) ay--;
+        }else
+            if ( (fy - ay) >= 0.5 ) ay++;
+        if (fz < 0)
+        {
+            if ( (fz - az) <= - 0.5 ) az--;
+        }else
+            if ( (fz - az) >= 0.5 ) az++;
+    
+        if(ax == -1 || ax == 1) ax = 0;
+        if(ay == -1 || ay == 1) ay = 0;
+        if(az == -1 || az == 1) az = 0;
+    
+        pc.printf("%f;%d;%d;%d;%d\n", t, ax, ay, az, cpt);
+        
+        /*
+        
+        vx += (ax * t * LSB);
+        vy += (ay * t * LSB);
+        vz += (az * t * LSB);
+        
+        x += (vx * t * LSB);
+        y += (vy * t * LSB);
+        z += (vz * t * LSB);
+        
+        if (cpt % 16 == 0) pc.printf("%d;%d;%d;%d;%d;%d;%d;%d;%d\n", ax, ay, az, vx, vy, vz, x, y, z);   
+        
+        */    
     }
-}
+}
\ No newline at end of file
diff -r 7cef8e5abfe5 -r ee0a25a1c3dc mbed.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.h	Wed Apr 11 19:28:43 2018 +0000
@@ -0,0 +1,59 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2011 ARM Limited. All rights reserved.
+ */ 
+ 
+#ifndef MBED_H
+#define MBED_H
+ 
+#define MBED_LIBRARY_VERSION 29
+ 
+// Useful C libraries
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <time.h>
+ 
+// mbed Debug libraries
+ 
+#include "error.h"
+#include "mbed_interface.h"
+ 
+// mbed Peripheral components
+ 
+#include "DigitalIn.h"
+#include "DigitalOut.h"
+#include "DigitalInOut.h"
+#include "BusIn.h"
+#include "BusOut.h"
+#include "BusInOut.h"
+#include "PortIn.h"
+#include "PortInOut.h"
+#include "PortOut.h"
+#include "AnalogIn.h"
+#include "AnalogOut.h"
+#include "PwmOut.h"
+#include "Serial.h"
+#include "SerialHalfDuplex.h"
+#include "SPI.h"
+#include "SPISlave.h"
+#include "SPIHalfDuplex.h"
+#include "I2C.h"
+#include "I2CSlave.h"
+#include "Ethernet.h"
+#include "CAN.h"
+ 
+// mbed Internal components
+#include "Timer.h"
+#include "Ticker.h"
+#include "Timeout.h"
+#include "LocalFileSystem.h"
+#include "InterruptIn.h"
+#include "wait_api.h"
+#include "rtc_time.h"
+ 
+using namespace mbed; 
+using namespace std; 
+ 
+#endif 
+ 
\ No newline at end of file
diff -r 7cef8e5abfe5 -r ee0a25a1c3dc mbed.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.lib	Wed Apr 11 19:28:43 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/gokmenascioglu/code/mbed/#a8fa94490a0a