Waël Hazami / Mbed 2 deprecated Lib_PIXY2

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Wael_H
Date:
Mon Feb 03 09:33:58 2020 +0000
Parent:
27:bee63ac9b70b
Commit message:
library Pixy2

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
pixy2.cpp Show annotated file Show diff for this revision Revisions of this file
pixy2.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Jan 21 12:59:37 2020 +0000
+++ b/main.cpp	Mon Feb 03 09:33:58 2020 +0000
@@ -1,148 +1,189 @@
 #include "pixy2.h"
 
-/*      _DEBUG_     */
-int sommeDeControle,sommeRecue;
-char *ptr;
-
-void automate_debug(T_Pixy2State &etat);
+#define NB_FONCTIONNALITES 8
 
 Serial pc(USBTX, USBRX, 9600);
-/*                  */
 
-typedef enum {IDLE, SND, SND_2, CHECKSUM_ERROR, TIMEOUT, OK, OK_2} T_state;
+// debug en cas d'erreur de checksum
+int sommeDeControle,sommeRecue;
 
-// FLAGS
+// Gestion appui sur BP
 bool FLAG_BP = false;
-int nb_appui = 0;
+int nb_appui = -1;
 
-// Prototypes
-void appui_BP(void);
+void appui_BP(void)
+{
+    if(!FLAG_BP) // pour éviter les rebonds
+        nb_appui = (nb_appui+1)%NB_FONCTIONNALITES;
+    FLAG_BP = true;
+}
 
+/* PROGRAMME DE TEST DES FEATURES FONCTIONNEL */
 
 int main(void)
 {
     PIXY2               maPixy(PC_12, PD_2);     // PC_12 : UART5_TX --- PD_2 : UART5_RX
     
-    T_pixy2Version* version;
-    T_pixy2Resolution* resolution;
-    T_pixy2ErrorCode    rep;
-    
+    T_pixy2Version *version;
+    T_pixy2Resolution *resolution;
+    T_pixy2ReturnCode *framerate;
+    T_pixy2Pixel *pixel;
+    T_pixy2ErrorCode    cr;
+
     // Initialisations
     InterruptIn BP(USER_BUTTON);
     BP.rise(&appui_BP);
-    
-    Timer temps;
-    temps.start();
-    
+
+    Timer timeOut;
+    timeOut.start();
+
+    typedef enum {IDLE, SND, CHECKSUM_ERROR, TIMEOUT, OK} T_state;
     T_state etat = IDLE;
-    
+
     while(1) {
 
         switch (etat) {
             case IDLE :
-                if (FLAG_BP && nb_appui == 0) {
+                if (FLAG_BP) {
                     pc.printf("Envoi...\n\r");
-                    etat = SND;
-                    FLAG_BP = false;
-                    temps.reset();
-                    nb_appui += 1;
-                }
-                if (FLAG_BP && nb_appui == 1) {
-                    pc.printf("Envoi...\n\r");
-                    etat = SND_2;
                     FLAG_BP = false;
-                    temps.reset();
-                    nb_appui = 0;
-                }
-                break;
-
-            case SND :
-                rep = maPixy.pixy2_getVersion(&version);
-                
-                if (rep == PIXY2_BAD_CHECKSUM) {
-                    etat = CHECKSUM_ERROR;   
-                }
-                if (temps.read()>0.5f) {
-                    etat = TIMEOUT;
-                }
-                if (rep == PIXY2_OK) {
-                    etat = OK;
+                    timeOut.reset();
+                    etat = SND;
                 }
                 break;
                 
-            case SND_2 :
-                rep = maPixy.pixy2_getResolution(&resolution);
-                
-                if (rep == PIXY2_BAD_CHECKSUM) {
-                    etat = CHECKSUM_ERROR;   
+            case SND :
+                switch(nb_appui) {
+                    case 0:
+                        cr = maPixy.pixy2_getVersion(&version);
+                        break;
+    
+                    case 1:
+                        cr = maPixy.pixy2_getResolution(&resolution);
+                        break;
+    
+                    case 2:
+                        cr = maPixy.pixy2_getFPS(&framerate);
+                        break;
+                        
+                    case 3:
+                        cr = maPixy.pixy2_getRGB(resolution->pixFrameWidth / 2, resolution->pixFrameHeight / 2, 0, &pixel);
+                        break;
+                        
+                    case 4:
+                        cr = maPixy.pixy2_setLED(0,0,255);
+                        break;
+    
+                    case 5:
+                        cr = maPixy.pixy2_setLamp(1,1);
+                        break;
+    
+                    case 6:
+                        cr = maPixy.pixy2_setCameraBrightness(255);
+                        break;
+    
+                    case 7:
+                        cr = maPixy.pixy2_getAllFeature(PIXY2_VECTOR | PIXY2_INTERSECTION | PIXY2_BARCODE);
+                        if(cr != PIXY2_BUSY)
+                        {
+                            if(cr <= 7)
+                            {
+                                if((cr & PIXY2_VECTOR) == PIXY2_VECTOR)
+                                {
+                                    pc.printf("\n\r%d vecteurs detect\202s\n\r", maPixy.Pixy2_numVectors);
+                                    etat = OK;
+                                }
+                                if((cr & PIXY2_INTERSECTION) == PIXY2_INTERSECTION)
+                                {
+                                    pc.printf("\n\r%d intersections detect\202es\n\r", maPixy.Pixy2_numIntersections);
+                                    etat = OK;
+                                }
+                                if((cr & PIXY2_BARCODE) == PIXY2_BARCODE)
+                                {
+                                    pc.printf("\n\r%d codes barre detect\202s\n\r", maPixy.Pixy2_numBarcodes);
+                                    etat = OK;
+                                }
+                            }
+                            else if(cr > 7)
+                                pc.printf("Aucune feature recue !\n\r");
+                        }
+                        break;
                 }
-                if (temps.read()>0.5f) {
+    
+                // Test des possibles erreurs
+                if (cr == PIXY2_BAD_CHECKSUM)
+                    etat = CHECKSUM_ERROR;
+                else if (timeOut.read()>2.f)
                     etat = TIMEOUT;
+                else if (cr == PIXY2_OK)
+                    etat = OK;
+                else if (cr != PIXY2_BUSY)
+                    pc.printf("Valeur de retour : %d\n\r", cr);
+                break;
+
+            case OK :
+                switch(nb_appui) {
+                    case 0:
+                        pc.printf("Version de la camera : %s\n\r", version->pixHFString);
+                        break;
+
+                    case 1:
+                        pc.printf("Resolution de la camera : %d - %d\n\r", resolution->pixFrameWidth, resolution->pixFrameHeight);
+                        break;
+                        
+                    case 2:
+                        pc.printf("Framerate : %d\n\r", framerate->pixReturn);
+                        break;
+                        
+                    case 3:
+                        pc.printf("Pixel au centre de l'\202cran: R : %d, G : %d, B : %d\n\r", pixel->pixRed, pixel->pixGreen, pixel->pixBlue);
+                        break;
+                        
+                    case 4:
+                        pc.printf("Led bleue allum\202e !\n\r");
+                        break;
+
+                    case 5:
+                        pc.printf("Leds blanches allum\202es !\n\r");
+                        break;
+                        
+                    case 6:
+                        pc.printf("Luminosit\202 de la camera a fond !\n\r");
+                        break;
+
+                    case 7:
+                        for (int i=0; i<maPixy.Pixy2_numVectors; i++)
+                        {
+                            pc.printf("\n1er pt du %de vecteur : x: %d, y: %d\n\r", i+1, maPixy.Pixy2_vectors[i].pixX0, maPixy.Pixy2_vectors[i].pixY0);
+                            pc.printf("2e  pt du %de vecteur : x: %d, y: %d\n\r", i+1, maPixy.Pixy2_vectors[i].pixX1, maPixy.Pixy2_vectors[i].pixY1);
+                        }
+                        for (int i=0; i<maPixy.Pixy2_numIntersections; i++)
+                        {
+                            pc.printf("\npt de l'intersection %d : x: %d, y: %d\n\r", i+1, maPixy.Pixy2_intersections[i].pixX, maPixy.Pixy2_intersections[i].pixY);
+                            pc.printf("Nombre de droites sur cette intersection : %d\n\r", maPixy.Pixy2_intersections[i].pixN);
+                        }
+                        for (int i=0; i<maPixy.Pixy2_numBarcodes; i++)
+                        {
+                            pc.printf("\nCoordonn\202es du code barre %d : x: %d, y: %d\n\r", i+1, maPixy.Pixy2_barcodes[i].pixX, maPixy.Pixy2_barcodes[i].pixY);
+                            pc.printf("Nombre associ\202 a ce code barre : %d\n\r", maPixy.Pixy2_barcodes[i].pixCode);
+                        }
+                        break;
                 }
-                if (rep == PIXY2_OK) {
-                    etat = OK_2;
-                }
+                etat = IDLE;
                 break;
-            
+
             case CHECKSUM_ERROR :
                 pc.printf("Erreur de CheckSum...\n\r");
                 pc.printf("CheckSum Recu    : %d\n\r", sommeRecue);
-                pc.printf("CheckSum Calcule : %d\n\r", sommeDeControle); 
-                etat = IDLE;
-                break;
-            
-            case TIMEOUT :
-                pc.printf("Erreur Timeout...\n\rRep : %d\n\r", rep);
-                for(int i=0; i<2; ++i)
-                    pc.printf("%d\n\r", *(ptr + i));
-                etat = IDLE;
-                break;
-
-            case OK :
-                pc.printf("Version de la camera : %s\n\r", version->pixHFString);
-                etat = IDLE;
-                break;
-                
-            case OK_2 :
-                pc.printf("Resolution de la camera : %d - %d\n\r", resolution->pixFrameWidth, resolution->pixFrameHeight);
+                pc.printf("CheckSum Calcule : %d\n\r", sommeDeControle);
                 etat = IDLE;
                 break;
 
-        }
-        
-        //automate_debug(maPixy.etat);
-    }
-}
-
-// FONCTIONS
-void appui_BP(void)
-{
-    FLAG_BP = true;
-}
-
-void automate_debug(T_Pixy2State &etat) {
-    
-    static int etat_debug_passe = -1;
-    
-    if(etat != etat_debug_passe) {
-        switch(etat) {
-            case idle :
-                pc.printf("IDLE\n\r");
+            case TIMEOUT :
+                pc.printf("Erreur Timeout...\n\rError %d\n\r", cr);
+                maPixy.affDataSize();
+                etat = IDLE;
                 break;
-            case messageSent :
-                pc.printf("DATA_SENT\n\r");
-                break;
-            case receivingHeader :
-                pc.printf("RECEIVING_HEADER\n\r");
-                break;
-            case receivingData :
-                pc.printf("RECEIVING_DATA\n\r");
-                break;
-            case dataReceived :
-                pc.printf("DATA_RECEIVED\n\r");
-                break;    
         }
     }
-    
-    etat_debug_passe = etat;   
 }
\ No newline at end of file
--- a/mbed.bld	Tue Jan 21 12:59:37 2020 +0000
+++ b/mbed.bld	Mon Feb 03 09:33:58 2020 +0000
@@ -1,1 +1,1 @@
-https://mbed.org/users/mbed_official/code/mbed/builds/a330f0fddbec
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file
--- a/pixy2.cpp	Tue Jan 21 12:59:37 2020 +0000
+++ b/pixy2.cpp	Mon Feb 03 09:33:58 2020 +0000
@@ -1,9 +1,9 @@
 #include "pixy2.h"
 
 extern  int sommeDeControle,sommeRecue;
-extern char *ptr;
+extern Serial pc;
 
-PIXY2::PIXY2(PinName tx, PinName rx, int debit)
+PIXY2::PIXY2(PinName tx, PinName rx, int debit) : Pixy2_numBlocks(0), Pixy2_numVectors(0), Pixy2_numIntersections(0), Pixy2_numBarcodes(0)
 {   
     _Pixy2 = new Serial (tx, rx, debit);
     _Pixy2->attach (callback(this,&PIXY2::pixy2_getByte));
@@ -16,6 +16,19 @@
     free (Pixy2_buffer);
 }
 
+// POUR DEBUG //
+T_Pixy2State PIXY2::getEtat()
+{
+    return this->etat;
+}
+
+void PIXY2::affDataSize()
+{
+    pc.printf("dataSize : %d\n\r", this->dataSize);
+}
+// Pour détail d'une trame (a mettre dans la fct correspondante) :
+//pc.printf("pixSync : %d, pixType : %d, pixLength : %d, pixChecksum : %d\n\r", msg->pixSync, msg->pixType, msg->pixLength, msg->pixChecksum);
+
 /* Le programme utilise l'interruption de la liaison série pour recevoir le message et avancer la machine d'état pour permettre au programme de ne pas être bloquant en réception.
    Lorsqu'on appelle une fonction, elle retourne le code -2 pour signifier que le message de réponse de la caméra n'est pas completement reçu...
    Les étapes de fonctionnement sont les suivantes :
@@ -66,7 +79,7 @@
                 buffer = (T_Word*) &Pixy2_buffer[wPointer-1];                       // On pointe la structure sur les 2 derniers octets reçus
                 if ((buffer->mot == PIXY2_CSSYNC) || (buffer->mot == PIXY2_SYNC)) { // Si c'est un mot d'entête
                     etat = receivingHeader;                                         // On passe à l'état réception de l'entête
-                    hPointer = wPointer - 1;                                        // On initialise le pointeur de l'entête
+                    hPointer = wPointer - 1;                                       // On initialise le pointeur de l'entête
                     if (buffer->mot == PIXY2_SYNC) {
                         frameContainChecksum = 0;                                   // Si c'est un entête sans checksum, on mémorise qu'il n'y a pas de checksum à vérifier
                         dPointer = hPointer + PIXY2_NCSHEADERSIZE;
@@ -83,12 +96,14 @@
                                                                                     // Si on a reçu 6 octets pour une trame avec checksum ou 4 pour une trame sans checksum, c'est à dire un entête complet
                 etat = receivingData;                                               // On dit que l'on va de recevoir des données
                 dataSize = Pixy2_buffer[hPointer + 3];                              // On enregistre la taille de la payload
-            }
+                if (dataSize == 0)                                                  // Si on reçoit rien, on reset le processus
+                    etat = idle;
+            }    
             break;
 
-        case receivingData :                                                        // Si on est en train de recevoir des données.
+        case receivingData :                                                 // Si on est en train de recevoir des données.
             if (wPointer == ((dataSize - 1) + dPointer)) { 
-                etat = dataReceived;                                                // Quand on a reçu toutes les données promises on dit que c'est OK         
+                etat = dataReceived;                                          // Quand on a reçu toutes les données promises on dit que c'est OK         
             }
             break;
 
@@ -185,7 +200,7 @@
     T_pixy2SendBuffer   msg;
     int                 i = 0, dataSize = 2;
     msg.frame.header.pixSync = PIXY2_SYNC;
-    msg.frame.header.pixType = PIXY2_SET_LED;
+    msg.frame.header.pixType = PIXY2_SET_LAMP;
     msg.frame.header.pixLength = dataSize;
     msg.frame.data[0] = upper;
     msg.frame.data[1] = lower;
@@ -327,7 +342,7 @@
 
 T_pixy2ErrorCode PIXY2::pixy2_sndGetRGB (Word x, Word y, Byte saturate){
     T_pixy2SendBuffer   msg;
-    int                 i = 0, dataSize = 3;
+    int                 i = 0, dataSize = 5;
     msg.frame.header.pixSync = PIXY2_SYNC;
     msg.frame.header.pixType = PIXY2_ASK_VIDEO;
     msg.frame.header.pixLength = dataSize;
@@ -555,7 +570,7 @@
     return cr;
 }
 
-T_pixy2ErrorCode PIXY2::pixy2_getFPS (T_pixy2ReturnCode *framerate){
+T_pixy2ErrorCode PIXY2::pixy2_getFPS (T_pixy2ReturnCode **framerate){
 
     T_pixy2RcvHeader    *msg = (T_pixy2RcvHeader*) &Pixy2_buffer[hPointer];
     T_pixy2ErrorCode    cr = PIXY2_OK;
@@ -576,7 +591,7 @@
                 }
             }
             if (msg->pixType == PIXY2_REP_FPS) {                                    // On vérifie que la trame est du type convenable (REPONSE FPS)
-                framerate = (T_pixy2ReturnCode*) &Pixy2_buffer[dPointer];           // On mappe le pointeur de structure sur le buffer de réception.
+                *framerate = (T_pixy2ReturnCode*) &Pixy2_buffer[dPointer];           // On mappe le pointeur de structure sur le buffer de réception.
             } else {                                                                // Si ce n'est pas le bon type
                 if (msg->pixType == PIXY2_REP_ERROR) {                              // Cela pourrait être une trame d'erreur
                     cr = *(T_pixy2ErrorCode*) &Pixy2_buffer[dPointer];              // Si c'est le cas, on copie le code d'erreur reçu dans la variable de retour
@@ -643,11 +658,10 @@
             return PIXY2_BAD_CHECKSUM;                                              // Si le checksum est faux on retourne une erreur
         }
     }
-    if (msg->pixType == PIXY2_REP_LINE) {                                           // On vérifie que la trame est du type convenable (REPONSE LIGNE)
+    if (msg->pixType == PIXY2_REP_LINE) {                                       // On vérifie que la trame est du type convenable (REPONSE LIGNE)
         fPointer = dPointer;                                                        // On pointe sur la premiere feature
         do {
             lineFeature = (T_pixy2LineFeature*) &Pixy2_buffer[fPointer];            // On mappe le pointeur de structure sur le buffer de réception des features.
-            ptr = (char*) &Pixy2_buffer[fPointer];
             if (lineFeature->fType == PIXY2_VECTOR) {                               // On regarde si le type est vecteur
                 Pixy2_numVectors = lineFeature->fLength / sizeof(T_pixy2Vector);    // Si oui, on compte combien il y a de vecteurs
                 fdPointer = fPointer + 2;                                           // On pointe sur le premier élément de la feature
@@ -674,7 +688,7 @@
             }
         } while(fPointer < ((dataSize - 1) + dPointer));                            // Tant qu'il y a des données à traiter
     } else {                                                                        // Si ce n'est pas le bon type
-        if (msg->pixType == PIXY2_REP_ERROR) {                                      // Cela pourrait être une trame d'erreur
+        if (msg->pixType == PIXY2_REP_ERROR) {                                      // Cela pourrait être une trame d'erreur ou quand on ne reçoit rien
             cr = *(T_pixy2ErrorCode*) &Pixy2_buffer[dPointer];                      // Si c'est le cas, on copie le code d'erreur reçu dans la variable de retour
         } else cr = PIXY2_TYPE_ERROR;                                               // Si le type ne correspond à rien de normal on signale une erreur de type.
     }
@@ -690,10 +704,10 @@
     switch (etat) {
         case idle :                                                                 // Si la caméra est inactive
             wPointer = 0;                                                           // On remonte en haut du buffer
-            cr = PIXY2::pixy2_sndGetLineFeature(0, features);                       // On envoie la trame de demande de suivi de ligne
-            if (cr!= PIXY2_OK) return cr;                                           // S'il y a une erreur lors de l'envoi on ejecte !
+            cr = PIXY2::pixy2_sndGetLineFeature(0, features);                      // On envoie la trame de demande de suivi de ligne
+            if (cr!= PIXY2_OK) return cr;                                          // S'il y a une erreur lors de l'envoi on ejecte !
             etat = messageSent;                                                     // On passe à l'attente du message de réponse
-            cr = PIXY2_BUSY;                                                        // On signale à l'utilisateur que la caméra est maintenant occupée
+            cr = PIXY2_BUSY;                                                    // On signale à l'utilisateur que la caméra est maintenant occupée
             break;
             
         case dataReceived :                                                         // Quand on a reçu l'intégralité du message
@@ -901,7 +915,7 @@
     return cr;
 }
 
-T_pixy2ErrorCode PIXY2::pixy2_getRGB (Word x, Word y, Byte saturate, T_pixy2Pixel *pixel){
+T_pixy2ErrorCode PIXY2::pixy2_getRGB (Word x, Word y, Byte saturate, T_pixy2Pixel **pixel){
 
     T_pixy2RcvHeader    *msg = (T_pixy2RcvHeader*) &Pixy2_buffer[hPointer];
     T_pixy2ErrorCode    cr = PIXY2_OK;
@@ -915,14 +929,14 @@
             cr = PIXY2_BUSY;                                                        // On signale à l'utilisateur que la caméra est maintenant occupée
             break;
             
-        case dataReceived :                                                         // Quand on a reçu l'intégralité du message
+        case dataReceived :                                                      // Quand on a reçu l'intégralité du message
             if (frameContainChecksum) {                                             // Si la trame contient un checksum
                 if (pixy2_validateChecksum (&Pixy2_buffer[hPointer]) != 0) {        // On lance la validation du checksum
                     return PIXY2_BAD_CHECKSUM;                                      // Si le checksum est faux on retourne une erreur
                 }
             }
             if (msg->pixType == PIXY2_REP_ACK) {                                    // On vérifie que la trame est du type convenable (REPONSE ACK)
-                pixel = (T_pixy2Pixel*) &Pixy2_buffer[dPointer];                    // On mappe le pointeur de structure sur le buffer de réception.
+                *pixel = (T_pixy2Pixel*) &Pixy2_buffer[dPointer];                    // On mappe le pointeur de structure sur le buffer de réception.
             } else {                                                                // Si ce n'est pas le bon type
                 if (msg->pixType == PIXY2_REP_ERROR) {                              // Cela pourrait être une trame d'erreur
                     cr = *(T_pixy2ErrorCode*) &Pixy2_buffer[dPointer];              // Si c'est le cas, on copie le code d'erreur reçu dans la variable de retour
--- a/pixy2.h	Tue Jan 21 12:59:37 2020 +0000
+++ b/pixy2.h	Mon Feb 03 09:33:58 2020 +0000
@@ -73,6 +73,11 @@
 #define PIXY2_BARCODE       4
 #define PIXY2_MAX_INT_LINE  6
 
+// setMode
+#define LINE_MODE_TURN_DELAYED                   0x01
+#define LINE_MODE_MANUAL_SELECT_VECTOR           0x02
+#define LINE_MODE_WHITE_LINE                     0x80
+
 /**************** ERRORS ****************/
 
 /**
@@ -408,6 +413,10 @@
  */
 ~PIXY2();
 
+// POUR DEBUG //
+T_Pixy2State getEtat();
+void affDataSize();
+
 // Fonctions publiques
 
 /**
@@ -447,7 +456,7 @@
  * @param s1 (Word - passed by value) : value between 0 and 511 
  * @return T_pixy2ErrorCode : error code.
  */
-T_pixy2ErrorCode pixy2_setServos (Word s0, Word s1); 
+T_pixy2ErrorCode pixy2_setServos (Word s0, Word s1);
 
 /**
  * Sets Pixy2's RGB LED value. The three arguments sets the brightness of the red, green and blue sections of the LED.
@@ -482,7 +491,7 @@
  * @param framerate (T_pixy2ReturnCode - passed by reference) : number of frame per second (between 2 and 62) 
  * @return T_pixy2ErrorCode : error code.
  */
-T_pixy2ErrorCode pixy2_getFPS (T_pixy2ReturnCode *framerate);
+T_pixy2ErrorCode pixy2_getFPS (T_pixy2ReturnCode **framerate);
 
 /**
  * Gets all detected color blocks in the most recent frame.
@@ -602,7 +611,7 @@
  * @param pixel (T_pixy2Pixel - passed by reference) : RGB pixel. 
  * @return T_pixy2ErrorCode : error code.
  */
-T_pixy2ErrorCode pixy2_getRGB (Word x, Word y, Byte saturate, T_pixy2Pixel *pixel);
+T_pixy2ErrorCode pixy2_getRGB (Word x, Word y, Byte saturate, T_pixy2Pixel **pixel);
 
 // Variables globales Publiques
 /**
@@ -625,7 +634,7 @@
 Byte                Pixy2_numBarcodes;
 T_pixy2BarCode      *Pixy2_barcodes;
 
-//private :
+private :
 // Variables globales Privées
 /**
  * @var etat (T_Pixy2State) state of the pixy2 cam (idle = No action, messageSent = Query or Set message sent, receivingHeader = Camera is respondig to the query/set, receivingData = Header received, dataReceived = All data has been recovered)