Projet S5 Lecture de l'accelerometre avec interruption

Dependencies:   mbed PowerControl

Fork of Projet_S5 by Jonathan Tousignant

Files at this revision

API Documentation at this revision

Comitter:
joGenie
Date:
Sat Apr 05 22:40:09 2014 +0000
Parent:
7:89be89aeed5a
Child:
9:40197c24ce2c
Commit message:
D?tection de mouvement fonctionnel

Changed in this revision

analyzer.cpp Show annotated file Show diff for this revision Revisions of this file
analyzer.h Show annotated file Show diff for this revision Revisions of this file
mouvement.cpp Show annotated file Show diff for this revision Revisions of this file
mouvement.h Show annotated file Show diff for this revision Revisions of this file
--- a/analyzer.cpp	Sat Apr 05 18:07:45 2014 +0000
+++ b/analyzer.cpp	Sat Apr 05 22:40:09 2014 +0000
@@ -1,59 +1,158 @@
 #include "analyzer.h"
-
+ 
 Analyzer::Analyzer()
-{}
+{
+    allMouvement[0x0002] = new Mouvement(0x0002, 0x0002, "Z vertical, main face au ciel");
+    allMouvement[0x0003] = new Mouvement(0x0003, 0x0003, "Z vertical, main face au ciel INVERSE");
+    allMouvement[0x0004] = new Mouvement(0x0004, 0x0004, "Z vertical, main face au sol");
+    allMouvement[0x0005] = new Mouvement(0x0005, 0x0005, "Z vertical, main face au sol INVERSE");
+    allMouvement[0x0006] = new Mouvement(0x0006, 0x0006, "Z horizontal");
+    allMouvement[0x0007] = new Mouvement(0x0007, 0x0007, "Z horizontal INVERSE");
 
+    allMouvement[0x0020] = new Mouvement(0x0020, 0x0020, "X vertical, main face au ciel");
+    allMouvement[0x0030] = new Mouvement(0x0030, 0x0030, "X vertical, main face au ciel INVERSE");
+    allMouvement[0x0040] = new Mouvement(0x0040, 0x0040, "X vertical, main face au sol");
+    allMouvement[0x0050] = new Mouvement(0x0050, 0x0050, "X vertical, main face au sol INVERSE");
+    allMouvement[0x0060] = new Mouvement(0x0060, 0x0060, "X horizontal");
+    allMouvement[0x0070] = new Mouvement(0x0070, 0x0070, "X horizontal INVERSE");
+
+    allMouvement[0x0200] = new Mouvement(0x0200, 0x0200, "Y vertical, main face au ciel");
+    allMouvement[0x0300] = new Mouvement(0x0300, 0x0300, "Y vertical, main face au ciel INVERSE");
+    allMouvement[0x0400] = new Mouvement(0x0400, 0x0400, "Y vertical, main face au sol");
+    allMouvement[0x0500] = new Mouvement(0x0500, 0x0500, "Y vertical, main face au sol INVERSE");
+    allMouvement[0x0600] = new Mouvement(0x0600, 0x0600, "Y horizontal");
+    allMouvement[0x0700] = new Mouvement(0x0700, 0x0700, "Y horizontal INVERSE");
+}
+ 
 Analyzer::~Analyzer()
-{}
-
+{
+    allMouvement.clear();
+}
+ 
 void Analyzer::setMinMax(signed char* values)
 {
     x.setMinMax(values[0]);
     y.setMinMax(values[1]);
     z.setMinMax(values[2]);
 }
-
+ 
 void Analyzer::checkMouvement()
 {
-    Serial pc(USBTX, USBRX);
+    bool deplacement[3] = {0};
+    int code_mouvement = 0;
+    
+    deplacement[0] = x.difference();
+    deplacement[1] = y.difference();          
+    deplacement[2] = z.difference();  
+    
+    Serial pc(USBTX, USBRX); 
+    
+    // Mouvement en x
+    if(deplacement[0])
+    {
+        if(x.initial > SEUIL_DETECTION)
+        {
+            code_mouvement = 0x1 << 9; // 0010
+        }
+        else if(x.initial < -SEUIL_DETECTION)
+        {       
+            code_mouvement = 0x2 << 9; // 0100
+        }
+        else
+        {
+            code_mouvement = 0x3 << 9; // 0110
+        }
+        
+        if(!x.sens)
+        {
+            code_mouvement = 0x1 << 8; // 0xx1 
+        }
+    }
     
-    pc.printf("Init: %i, min: %i, max: %i\n", x.initial, x.min, x.max);
-    pc.printf("Init: %i, min: %i, max: %i\n", y.initial, y.min, y.max);
-    pc.printf("Init: %i, min: %i, max: %i\n", z.initial, z.min, z.max);
+    // Mouvement en y  
+    if(deplacement[1])
+    {
+        if(y.initial > SEUIL_DETECTION)
+        {
+            code_mouvement = 0x1 << 5; // 0xxx 0010
+        }
+        else if(y.initial < -SEUIL_DETECTION)
+        {
+            code_mouvement = 0x2 << 5; // 0xxx 0100
+        }
+        else
+        {
+            code_mouvement = 0x3 << 5; // 0xxx 0110
+        }
+        
+        if(!y.sens)
+        {
+            code_mouvement = 0x1 << 4; // 0xxx 0yy1 
+        }
+    }
+    
+    // Mouvement en z
+    if(deplacement[2])
+    {
+        if(z.initial > SEUIL_DETECTION)
+        {
+            code_mouvement = 1 << 1; // 0xxx 0yyy 0010
+        }
+        else if(z.initial < -SEUIL_DETECTION)
+        {
+            code_mouvement = 2 << 1; // 0xxx 0yyy 0100
+        }
+        else
+        {
+            code_mouvement = 3 << 1; // 0xxx 0yyy 0110
+        }
+        
+        if(!z.sens)
+        {
+            code_mouvement = 1 << 0; // 0xxx 0yyy 0zz1
+        }
+    }
+
+    pc.printf("\n\r mouvement: %s, %s, %s", deplacement[0] ? "vrai" : "faux",
+                                            deplacement[1] ? "vrai" : "faux",
+                                            deplacement[2] ? "vrai" : "faux");
+    // pc.printf("\n\rInit: %i, min: %i, max: %i, sens: %s", x.initial, x.min, x.max, x.sens ? "acceleration":"deceleration");
+    // pc.printf("\n\rInit: %i, min: %i, max: %i, sens: %s", y.initial, y.min, y.max, y.sens ? "acceleration":"deceleration");
+    // pc.printf("\n\rInit: %i, min: %i, max: %i, sens: %s", z.initial, z.min, z.max, z.sens ? "acceleration":"deceleration");
 }
-
+ 
 void Analyzer::setInitial(signed char* init)
 {
     x.setInitial(init[0]);
     y.setInitial(init[1]);
     z.setInitial(init[2]);
 }
-
+ 
 extern "C" void *Analyzer_C_new()
 {
     return new Analyzer();
 }
-
+ 
 extern "C" void Analyzer_C_delete(void *analyzer)
 {
     Analyzer *an = (Analyzer*)analyzer;
     delete an;    
 }
-
+ 
 extern "C" void Analyzer_C_setMinMax(signed char* values, void *analyzer)
 {
     Analyzer *an = (Analyzer*)analyzer;
     an->setMinMax(values);    
 }
-
+ 
 extern "C" void Analyzer_C_setInitial(signed char* init, void *analyzer)
 {
     Analyzer *an = (Analyzer*)analyzer;
     an->setInitial(init);    
 }
-
+ 
 extern "C" void Analyzer_C_checkMouvement(void *analyzer)
 {
     Analyzer *an = (Analyzer*)analyzer;
     an->checkMouvement();
-}
\ No newline at end of file
+}
--- a/analyzer.h	Sat Apr 05 18:07:45 2014 +0000
+++ b/analyzer.h	Sat Apr 05 22:40:09 2014 +0000
@@ -3,7 +3,13 @@
 
 #include <mbed.h>
 #include <climits>
+#include <map>
+#include "mouvement.h"
 
+using namespace std;
+ 
+#define SEUIL_DETECTION 20
+ 
 class Analyzer
 {
 public:
@@ -20,16 +26,24 @@
         signed char min;
         signed char max;
         signed char initial;
+        bool sens;  //true =  acceleration, false = deceleration
+                
         
         Data(): min(SCHAR_MAX), max(SCHAR_MIN){}
         
         void setMinMax(signed char value)
         {
             if (value < min)
+            {
                 min = value;
+                sens = true;
+            }
                 
             if (value > max)
+            {
                 max = value;
+                sens = false;
+            }
         }
         
         void setInitial(signed char init)
@@ -38,24 +52,34 @@
             min = initial;
             max = initial;
         }
+                
+        bool difference()
+        {
+            if(sens)
+                return (max-initial >= SEUIL_DETECTION);
+            
+            else
+                return (initial-min >= SEUIL_DETECTION);
+        }
     };
     
     Data x, y, z;
+    map<int, Mouvement*> allMouvement;
 };
-
+ 
 // Define function in C for interruption
 #ifdef __cplusplus
 extern "C" {
 #endif
-
+ 
 extern void *Analyzer_C_new();
 extern void Analyzer_C_delete(void *analyzer);
 extern void Analyzer_C_setMinMax(signed char* values, void *analyzer);
 extern void Analyzer_C_setInitial(signed char* value, void *analyzer);
 extern void Analyzer_C_checkMouvement(void *analyzer);
-
+ 
 #ifdef __cplusplus
 }
 #endif
-
-#endif
\ No newline at end of file
+ 
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mouvement.cpp	Sat Apr 05 22:40:09 2014 +0000
@@ -0,0 +1,8 @@
+#include "mouvement.h"
+
+Mouvement::Mouvement(int code, int code_envoie, string definition) :
+    _code(code),
+    _code_envoie(code_envoie),
+    _definition(definition)
+{
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mouvement.h	Sat Apr 05 22:40:09 2014 +0000
@@ -0,0 +1,19 @@
+#ifndef MOUVEMENT_H
+#define MOUVEMENT_H
+
+#include <string>
+
+using namespace std;
+
+class Mouvement
+{
+public:
+    Mouvement(int code, int code_envoie, string definition);
+    
+private:
+    int _code;
+    int _code_envoie;
+    string _definition;
+};
+
+#endif