FRC - Hackathon / Pixy

Dependents:   TestPixy FRC_2018 FRC2018_Bis 0hackton_08_06_18 ... more

Fork of Pixy by FRC - Hackathon

Files at this revision

API Documentation at this revision

Comitter:
haarkon
Date:
Thu May 31 17:36:49 2018 +0000
Parent:
2:90355c600404
Child:
4:5982d904f7aa
Commit message:
Puxy HelloWorld program

Changed in this revision

Pixy.cpp Show annotated file Show diff for this revision Revisions of this file
Pixy.h Show annotated file Show diff for this revision Revisions of this file
--- a/Pixy.cpp	Tue May 22 17:15:30 2018 +0000
+++ b/Pixy.cpp	Thu May 31 17:36:49 2018 +0000
@@ -8,65 +8,66 @@
     FlagPixyOverflow = 0;
     Pixy_check = -1;
 
-    _cmucam = new Serial (tx, rx, debit);
-    _cmucam->attach (callback(this,&PIXY::getPixyByte));
+    _Pixy = new Serial (tx, rx, debit);
+    _Pixy->attach (callback(this,&PIXY::getPixyByte));
 
 }
 
 
 void PIXY::getPixyByte ()
 {
-    static T_tmpBuffer      tmpBuffer;
-    static T_structBuffer   msgBuffer;
+    static T_msgBuffer      msgBuffer;
+    static T_wordBuffer     wordBuffer;
     static T_pixyState      PIXY_state = none;
     static Byte             byteCount = 0;
-    static int              PIXY_synced = 0, dummy;
-    int                     i, somme;
+    static int              PIXY_synced = 0;
+    Word                    i, somme;
+    Byte                    dummy;
     static Byte             PIXY_nbCCObjet = 0, PIXY_wCCObjet = 0;
     static Byte             PIXY_nbNMObjet = 0, PIXY_wNMObjet = 0;
 
     Pixy_check = 0;
 
-    if (!PIXY_synced) {                                                         // On n'a pas trouvé le START (0x55aa0000)
-        tmpBuffer.tab[byteCount] = _cmucam->getc();                             // On stocke l'octet reçu dans la première case dispo du tableau temporaire
+    dummy = _Pixy->getc();                                                                // On stocke l'octet reçu dans une variable temporaire
+
+    if (!PIXY_synced) {                                                                 // Si la camera et la carte ne sont pas synchronisées
+        msgBuffer.octet[byteCount] = dummy;                                             // On stocke l'octet reçu dans la première case dispo du tableau temporaire
 
-        if (byteCount < 3) {                                                    // Si on n'a pas encore reçu les 4 premier octets
-            byteCount++;                                                        // On passe à la case suivante du tableau temporaire
-        } else {                                                                // Lorsqu'on a 4 octets
-            if (tmpBuffer.mot != 0xaa550000) {                                  // Si le code n'est pas le code de START
-                for (i=1; i<4; i++) tmpBuffer.tab[i-1] = tmpBuffer.tab[i];      // On décalle les cases du tableau
-                byteCount = 3;                                                  // Et on attend le caractère suivant
-            } else {                                                            // Si on a trouvé le code de START
-                PIXY_synced = 1;                                                // On passe en mode synchronisé
-                PIXY_state = begin;
-                byteCount = 0;
+        if (byteCount < 3) {                                                            // Tant qu'on n'a pas encore reçu les 4 premier octets
+            byteCount++;                                                                    // On passe à la case suivante du tableau temporaire
+        } else {                                                                        // Lorsqu'on a 4 octets
+            if ((msgBuffer.motlong == 0xaa550000)||(msgBuffer.motlong == 0xaa560000)) { // On teste pour voir si on a reçu un entête
+                PIXY_synced = 1;                                                            // Si oui : On passe en mode synchronisé
+                PIXY_state = begin;                                                         // Et on va à l'aiguillage
+                byteCount = 0;                                                              // Et on remet le comptage d'octet à 0
+            } else {                                                                    // Si on n'a pas trouvé d'entête
+                for (i=1; i<4; i++) msgBuffer.octet[i-1] = msgBuffer.octet[i];              // On décalle les cases du tableau
+                byteCount = 3;                                                              // Et on attend le caractère suivant
             }
         }
-    }
-
-    if (PIXY_synced) {
-
+    } else {
         switch (PIXY_state) {
             case begin :                                                        // l'aiguillage est là !
-
-                msgBuffer.tab[byteCount%2] = _cmucam->getc();                   // on stocke les octets reçus
+                wordBuffer.octet[byteCount%2] = dummy;                          // on stocke les octets reçus
                 byteCount++;
                 if (byteCount == 2) {                                           // Quand on a 2 octets
+                    switch (wordBuffer.mot) {
+                        case 0xaa55 :                                           // Si c'est un bloc normal (code = 0xAA55)
+                            PIXY_state = normal;                                // On part vers le traitement spécifique
+                            break;
 
-                    if (msgBuffer.mot == 0xaa55) {                              // Si c'est un bloc normal (code 0xAA55)
-                        PIXY_state = normal;                                    // On part vers le traitement spécifique
-                    }
+                        case 0xaa56 :                                           // Si c'est un bloc color Code (code = 0xAA56)
+                            PIXY_state = colorCode;                             // On part vers le traitement spécifique
+                            break;
 
-                    if (msgBuffer.mot == 0xaa56) {                              // Si c'est un bloc Color Code (code 0xAA56)
-                        PIXY_state = colorCode;                                 // On part vers le traitement spécifique
-                    }
+                        case 0 :                                                // Si c'est un debut de trame (code = 0x0000)
+                            PIXY_state = doubleZero;                            // On part vers le traitement spécifique
+                            break;
 
-                    if (msgBuffer.mot == 0) {                                   // Si on a un debut de trame (code 0000)
-                        PIXY_state = doubleZero;                                // On part vers le traitement spécifique
-                    }
-                    if ((PIXY_state == begin) || (PIXY_state == none)) {        // Si c'est autre chose
-                        PIXY_synced = 0;                                        // C'est qu'on est perdu donc plus synchronisé.
-                        PIXY_state = none;                                      // Ceinture et bretelle
+                        default :                                               // Si c'est autre chose
+                            PIXY_synced = 0;                                    // C'est qu'on est perdu donc plus synchronisé.
+                            PIXY_state = none;                                  // Ceinture et bretelle
+                            break;
                     }
                     byteCount = 0;
                 }
@@ -74,7 +75,7 @@
 
             case normal :                                                       // Si on a un bloc normal
 
-                Pixy_NMFIFO[PIXY_wNMObjet].tab[byteCount] = _cmucam->getc();    // On stocke les octets un à un dans la structure Bloc
+                Pixy_NMFIFO[PIXY_wNMObjet].tab[byteCount] = dummy;              // On stocke les octets un à un dans la structure Bloc
                 if (byteCount < 11) {                                           // Tant que la structure n'est pas pleine
                     byteCount++;                                                // On passe à l'octet suivant
                 } else {                                                        // Quand elle est pleine
@@ -113,27 +114,37 @@
                 }
                 break;
 
-            case doubleZero :                                                   // Si on a reçu le code de début d'une nouvelle trame.
+            case doubleZero :                                                   // Si on a reçu le code de début d'une nouvelle trame (0000).
+
+                Pixy_NMObjet = PIXY_nbNMObjet;                                  // On met à jour les variables pour le traitement extérieur
+                Pixy_CCObjet = PIXY_nbCCObjet;
+                Pixy_CCFrameIsNew = 1;
+                Pixy_FirstCCObjet = PIXY_wCCObjet;
+                Pixy_NMFrameIsNew = 1;
+                Pixy_FirstNMObjet = PIXY_wNMObjet;
+                FlagPixy = 1;                                                   // On valide le traitement de la trame précédente.
 
-                msgBuffer.tab[byteCount%2] = _cmucam->getc();                   // on stocke les octets reçus
+                PIXY_nbCCObjet = 0;                                             // On remet à 0 les variables internes
+                PIXY_nbNMObjet = 0;
+                byteCount = 0;
+
+                wordBuffer.octet[byteCount%2] = dummy;                          // on stocke les octets reçus
+                byteCount++;
+                PIXY_state = waitForStart;                                      // On passe en attente de la deuxième partie du code
+                break;
+
+            case waitForStart :                                                 // Si on a reçu le code de début d'une nouvelle trame.
+
+                wordBuffer.octet[byteCount%2] = dummy;                          // on stocke les octets reçus
                 byteCount++;
                 if (byteCount == 2) {                                           // Quand on a 2 octets
-                    if (msgBuffer.mot == 0xaa55) {                              // On doit impérativement trouver le code 0xAA55
+                    byteCount = 0;
+                    if (wordBuffer.mot == 0xaa55) {                             // On vérifie qu'on a bien reçu l'entête de depart
                         PIXY_state = begin;                                     // Si c'est le cas, alors tout va bien et on va à l'aiguillage
-                        Pixy_NMObjet = PIXY_nbNMObjet;                          // On met à jour les variables pour le traitement
-                        Pixy_CCObjet = PIXY_nbCCObjet;
-                        PIXY_nbCCObjet = 0;
-                        PIXY_nbNMObjet = 0;
-                        Pixy_CCFrameIsNew = 1;
-                        Pixy_FirstCCObjet = PIXY_wCCObjet;
-                        Pixy_NMFrameIsNew = 1;
-                        Pixy_FirstNMObjet = PIXY_wNMObjet;
-                        FlagPixy = 1;                                           // On valide le traitement de la trame précédente.
-                    } else {                                                    // Si on trouve autre chose
-                        PIXY_synced = 0;                                        // C'est qu'on est perdu donc plus synchronisé.
-                        PIXY_state = none;                                      // Ceinture et bretelle
+                    } else {                                                    // Si ce n'est pas le cas
+                        PIXY_synced = 0;                                        // On n'est plus synchronisés
+                        PIXY_state = none;                                      // Ceinture et Bretelles
                     }
-                    byteCount = 0;
                 }
                 break;
         }
@@ -191,14 +202,24 @@
     return dummy;
 }
 
-int PIXY::setBrightness (Byte brightness)
+void PIXY::setBrightness (Byte brightness)
 {
-
-    Byte PixyMsgBuf[3];
+    _Pixy->putc(0); //Start
+    _Pixy->putc(0xFE); //Brightness Level
+    _Pixy->putc(brightness);
+}
 
-    PixyMsgBuf[0] = 0x00; //Start
-    PixyMsgBuf[1] = 0xFE; //Brightness Level
-    PixyMsgBuf[2] = brightness;
+int PIXY::checkNewImage (void)
+{
+    if (FlagPixy) {
+        FlagPixy = 0;
+        return 1;
+    } else {
+        return 0;
+    }
+}
 
-    return _cmucam->write(PixyMsgBuf,3,NULL,0);
+int PIXY::checkPixy (void)
+{
+    return Pixy_check;
 }
\ No newline at end of file
--- a/Pixy.h	Tue May 22 17:15:30 2018 +0000
+++ b/Pixy.h	Thu May 31 17:36:49 2018 +0000
@@ -97,7 +97,7 @@
     
     /**
      *  \union T_pixyCCData
-     *  \brief Structured type to match pixy Color Code bloc organisation
+     *  \brief Overlaped type to access a structured pixy Color Code bloc type with both byte and structured 
      *  \param CCBloc (T_pixyCCBloc) : Color Code bloc structured element
      *  \param tab[14] (Byte) : Byte access to the Color Code bloc 
      */
@@ -108,8 +108,8 @@
     
     /**
      *  \union T_pixyNMData
-     *  \brief Structured type to match pixy normal bloc organisation
-     *  \param CCBloc (T_pixyCCBloc) : Normal bloc structured element
+     *  \brief Overlaped type to access a structured pixy Normal bloc type with both byte and structured
+     *  \param NMBloc (T_pixyNMBloc) : Normal bloc structured element
      *  \param tab[12] (Byte) : Byte access to the normal bloc 
      */
     typedef union {
@@ -117,20 +117,19 @@
         T_pixyNMBloc    NMbloc;
     } T_pixyNMData;
 
-    typedef enum {none, begin, normal, colorCode, doubleZero} T_pixyState;
+    typedef enum {none, begin, normal, colorCode, doubleZero, waitForStart} T_pixyState;
     
     typedef union {
-        lWord   mot;
-        Byte    tab[4];
-    } T_tmpBuffer;
+        lWord   motlong;
+        Word    mot[2];
+        Byte    octet[4];
+    } T_msgBuffer;
     
     typedef union {
         Word    mot;
-        Byte    tab[2];
-    } T_structBuffer;
+        Byte    octet[2];
+    } T_wordBuffer;
     
-
-
 /**
  * Pixy : CMU CAM 5 - Smart camera
  * More informations at http://cmucam.org/projects/cmucam5
@@ -139,7 +138,7 @@
 
 protected :
 
-    Serial*  _cmucam;
+    Serial*  _Pixy;
 
 public :
 
@@ -201,13 +200,13 @@
 
     /**
      * Return the number of objects (normal and ColorCode) that have been
-     * received from the PIXY during the last frame
+     * received from the PIXY and stored in the FIFO during the last frame
      *
-     * @param nbNM (passed by reference) : number of normal object detected
-     * @param nbCC (passed by reference) : number of color code object detected
+     * @param nbNM (int - passed by reference) : number of normal object detected
+     * @param nbCC (int - passed by reference) : number of color code object detected
      * @return   0 if sucessfull,
      *          -1 if no PIXY is talking,
-     *          -2 if object(s) have been lost (from previous frame or from actual) 
+     *          -2 sucessfull but object(s) have been lost from previous frames (missed frame) or from actual (too many objects) 
      */
     int detectedObject (int* nbNM, int* nbCC);
 
@@ -233,9 +232,20 @@
      * Set the Brightness of the Pixy
      *
      * @param   brightness level (between 0 and 255)
-     * @return  0 if successfull, else -1
+     */
+    void setBrightness (Byte brightness); 
+
+    /**
+     * Gives information about a new image taken by Pixy.
+     * @return  0 if no new image or  1 if there is a new image
      */
-    int setBrightness (Byte brightness); 
+    int checkNewImage (void);
+
+    /**
+     * Gives information about a detected Pixy.
+     * @return  -1 if no Pixy detected or  0 if Pixy detected
+     */
+    int checkPixy (void);
 
 private :