Library for Pixy - CMUCAM5 smart camera

Dependents:   TestPixy FRC_2018 FRC2018_Bis 0hackton_08_06_18 ... more

Fork of Pixy by FRC - Hackathon

Committer:
haarkon
Date:
Wed Apr 17 12:02:15 2019 +0000
Revision:
10:b7c012987c5d
Parent:
9:a298df20a2dd
Stupid error correction

Who changed what in which revision?

UserRevisionLine numberNew contents of line
haarkon 0:6f78c735f07c 1 /**
haarkon 0:6f78c735f07c 2 * @author Hugues Angelis
haarkon 0:6f78c735f07c 3 *
haarkon 0:6f78c735f07c 4 * @section LICENSE
haarkon 0:6f78c735f07c 5 *
haarkon 0:6f78c735f07c 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
haarkon 0:6f78c735f07c 7 * of this software and associated documentation files (the "Software"), to deal
haarkon 0:6f78c735f07c 8 * in the Software without restriction, including without limitation the rights
haarkon 0:6f78c735f07c 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
haarkon 0:6f78c735f07c 10 * copies of the Software, and to permit persons to whom the Software is
haarkon 0:6f78c735f07c 11 * furnished to do so, subject to the following conditions:
haarkon 0:6f78c735f07c 12 *
haarkon 0:6f78c735f07c 13 * The above copyright notice and this permission notice shall be included in
haarkon 0:6f78c735f07c 14 * all copies or substantial portions of the Software.
haarkon 0:6f78c735f07c 15 *
haarkon 0:6f78c735f07c 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
haarkon 0:6f78c735f07c 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
haarkon 0:6f78c735f07c 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
haarkon 0:6f78c735f07c 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
haarkon 0:6f78c735f07c 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
haarkon 0:6f78c735f07c 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
haarkon 0:6f78c735f07c 22 * THE SOFTWARE.
haarkon 0:6f78c735f07c 23 *
haarkon 0:6f78c735f07c 24 * @section DESCRIPTION
haarkon 0:6f78c735f07c 25 *
haarkon 0:6f78c735f07c 26 * CMUCAM 5 - Pixy
haarkon 0:6f78c735f07c 27 *
haarkon 0:6f78c735f07c 28 * Datasheet, FAQ and PC drivers :
haarkon 0:6f78c735f07c 29 *
haarkon 0:6f78c735f07c 30 * http://www.cmucam.org/projects/cmucam5
haarkon 0:6f78c735f07c 31 */
haarkon 0:6f78c735f07c 32
haarkon 0:6f78c735f07c 33 #ifndef PIXY_H
haarkon 0:6f78c735f07c 34 #define PIXY_H
haarkon 0:6f78c735f07c 35
haarkon 0:6f78c735f07c 36 /**
haarkon 2:90355c600404 37 * Include : Mbed Library
haarkon 0:6f78c735f07c 38 */
haarkon 0:6f78c735f07c 39 #include "mbed.h"
haarkon 0:6f78c735f07c 40
haarkon 0:6f78c735f07c 41 /**
haarkon 0:6f78c735f07c 42 * Defines
haarkon 0:6f78c735f07c 43 */
haarkon 0:6f78c735f07c 44 #define CC_BLOCSIZE 14
haarkon 10:b7c012987c5d 45 #define NM_BLOCSIZE 12
haarkon 0:6f78c735f07c 46
haarkon 7:c809db7aa1c0 47 #define NM_BLOCCODE 0xAA55
haarkon 0:6f78c735f07c 48 #define CC_BLOCCODE 0xAA56
haarkon 0:6f78c735f07c 49
haarkon 6:f16f250fe2cb 50 #define NM_MAXOBJECT 20
haarkon 6:f16f250fe2cb 51 #define CC_MAXOBJECT 20
haarkon 6:f16f250fe2cb 52
haarkon 0:6f78c735f07c 53 /**
haarkon 2:90355c600404 54 * \struct Byte -> Short hand for unsigned char
haarkon 2:90355c600404 55 * \struct Word -> Short hand for unsigned short
haarkon 2:90355c600404 56 * \struct lWord -> Short hand for unsigned long
haarkon 0:6f78c735f07c 57 */
haarkon 0:6f78c735f07c 58 typedef unsigned char Byte;
haarkon 0:6f78c735f07c 59 typedef unsigned short Word;
haarkon 0:6f78c735f07c 60 typedef unsigned long lWord;
haarkon 2:90355c600404 61 /**
haarkon 2:90355c600404 62 * \struct T_pixyCCBloc
haarkon 2:90355c600404 63 * \brief Structured type to match pixy Color Code bloc organisation
haarkon 2:90355c600404 64 * \param Checksum (Word) : modulus sum of all words of the message
haarkon 2:90355c600404 65 * \param signature (Word) : identity (ie : numbers associated with colors) of the detected color code bloc
haarkon 2:90355c600404 66 * \param x (Word) : X coordinate (in pixel) of the center of the color code bloc
haarkon 2:90355c600404 67 * \param y (Word) : Y coordinate (in pixel) of the center of the color code bloc
haarkon 2:90355c600404 68 * \param width (Word) : Width (in pixel) of the color code bloc
haarkon 2:90355c600404 69 * \param height (Word) : Height (in pixel) of the color code bloc
haarkon 2:90355c600404 70 * \param angle (Word) : Angle (in degree, referenced to horizontal) of the color code bloc
haarkon 2:90355c600404 71 */
haarkon 0:6f78c735f07c 72 typedef struct {
haarkon 0:6f78c735f07c 73 Word checksum;
haarkon 0:6f78c735f07c 74 Word signature;
haarkon 0:6f78c735f07c 75 Word x;
haarkon 0:6f78c735f07c 76 Word y;
haarkon 0:6f78c735f07c 77 Word width;
haarkon 0:6f78c735f07c 78 Word height;
haarkon 0:6f78c735f07c 79 Word angle;
haarkon 0:6f78c735f07c 80 } T_pixyCCBloc;
haarkon 0:6f78c735f07c 81
haarkon 2:90355c600404 82 /**
haarkon 2:90355c600404 83 * \struct T_pixyNMBloc
haarkon 2:90355c600404 84 * \brief Structured type to match pixy normal bloc organisation
haarkon 2:90355c600404 85 * \param Checksum (Word) : modulus sum of all words of the message
haarkon 2:90355c600404 86 * \param signature (Word) : identity (ie : number associated with the color) of the detected bloc
haarkon 2:90355c600404 87 * \param x (Word) : X coordinate (in pixel) of the center of the color code bloc
haarkon 2:90355c600404 88 * \param y (Word) : Y coordinate (in pixel) of the center of the color code bloc
haarkon 2:90355c600404 89 * \param width (Word) : Width (in pixel) of the color code bloc
haarkon 2:90355c600404 90 * \param height (Word) : Height (in pixel) of the color code bloc
haarkon 2:90355c600404 91 */
haarkon 0:6f78c735f07c 92 typedef struct {
haarkon 0:6f78c735f07c 93 Word checksum;
haarkon 0:6f78c735f07c 94 Word signature;
haarkon 0:6f78c735f07c 95 Word x;
haarkon 0:6f78c735f07c 96 Word y;
haarkon 0:6f78c735f07c 97 Word width;
haarkon 0:6f78c735f07c 98 Word height;
haarkon 0:6f78c735f07c 99 } T_pixyNMBloc;
haarkon 0:6f78c735f07c 100
haarkon 2:90355c600404 101 /**
haarkon 2:90355c600404 102 * \union T_pixyCCData
haarkon 5:d395fe0fb0e0 103 * \brief Overlaped type to access a structured pixy Color Code bloc type with both byte and structured
haarkon 2:90355c600404 104 * \param CCBloc (T_pixyCCBloc) : Color Code bloc structured element
haarkon 2:90355c600404 105 * \param tab[14] (Byte) : Byte access to the Color Code bloc
haarkon 2:90355c600404 106 */
haarkon 0:6f78c735f07c 107 typedef union {
haarkon 8:db82754a3671 108 Byte tab[CC_BLOCSIZE];
haarkon 0:6f78c735f07c 109 T_pixyCCBloc CCbloc;
haarkon 0:6f78c735f07c 110 } T_pixyCCData;
haarkon 0:6f78c735f07c 111
haarkon 2:90355c600404 112 /**
haarkon 2:90355c600404 113 * \union T_pixyNMData
haarkon 5:d395fe0fb0e0 114 * \brief Overlaped type to access a structured pixy Normal bloc type with both byte and structured
haarkon 5:d395fe0fb0e0 115 * \param NMBloc (T_pixyNMBloc) : Normal bloc structured element
haarkon 2:90355c600404 116 * \param tab[12] (Byte) : Byte access to the normal bloc
haarkon 2:90355c600404 117 */
haarkon 0:6f78c735f07c 118 typedef union {
haarkon 8:db82754a3671 119 Byte tab[NM_BLOCSIZE];
haarkon 0:6f78c735f07c 120 T_pixyNMBloc NMbloc;
haarkon 0:6f78c735f07c 121 } T_pixyNMData;
haarkon 0:6f78c735f07c 122
haarkon 5:d395fe0fb0e0 123 typedef enum {none, begin, normal, colorCode, doubleZero, waitForStart} T_pixyState;
haarkon 0:6f78c735f07c 124
haarkon 0:6f78c735f07c 125 typedef union {
haarkon 5:d395fe0fb0e0 126 lWord motlong;
haarkon 5:d395fe0fb0e0 127 Word mot[2];
haarkon 5:d395fe0fb0e0 128 Byte octet[4];
haarkon 5:d395fe0fb0e0 129 } T_msgBuffer;
haarkon 0:6f78c735f07c 130
haarkon 0:6f78c735f07c 131 typedef union {
haarkon 0:6f78c735f07c 132 Word mot;
haarkon 5:d395fe0fb0e0 133 Byte octet[2];
haarkon 5:d395fe0fb0e0 134 } T_wordBuffer;
haarkon 0:6f78c735f07c 135
haarkon 0:6f78c735f07c 136 /**
haarkon 2:90355c600404 137 * Pixy : CMU CAM 5 - Smart camera
haarkon 2:90355c600404 138 * More informations at http://cmucam.org/projects/cmucam5
haarkon 0:6f78c735f07c 139 */
haarkon 0:6f78c735f07c 140 class PIXY {
haarkon 0:6f78c735f07c 141
haarkon 0:6f78c735f07c 142 protected :
haarkon 0:6f78c735f07c 143
haarkon 5:d395fe0fb0e0 144 Serial* _Pixy;
haarkon 0:6f78c735f07c 145
haarkon 0:6f78c735f07c 146 public :
haarkon 0:6f78c735f07c 147
haarkon 2:90355c600404 148 /** Direct access to the CC FIFO
haarkon 2:90355c600404 149 * @note A FIFO is a circular buffer where data are stored in the order in witch they arrive and can be read in the same order. A FIFO need 2 variables (call read and write pointers) to point both read and write registers.
haarkon 2:90355c600404 150 * @note The library manage both read and write pointers with FIFO functions, but users can access the FIFO with their own code, users must then manage their own read pointers while write pointers (that are strictly private) can only be managed by the library functions.
haarkon 2:90355c600404 151 *
haarkon 6:f16f250fe2cb 152 * \var Pixy_CCFIFO[CC_MAXOBJECT]
haarkon 6:f16f250fe2cb 153 * \brief is a FIFO where the Color Code objects are stored. FIFO can store up to 20 objects (CC_MAXOBJECT = 20)
haarkon 2:90355c600404 154 */
haarkon 2:90355c600404 155
haarkon 2:90355c600404 156 /** Direct access to the NM FIFO
haarkon 2:90355c600404 157 * @note A FIFO is a circular buffer where data are stored in the order in witch they arrive and can be read in the same order. A FIFO need 2 variables (call read and write pointers) to point both read and write registers.
haarkon 2:90355c600404 158 * @note The library manage both read and write pointers with FIFO functions, but users can access the FIFO with their own code, users must then manage their own read pointers while write pointers (that are strictly private) can only be managed by the library functions.
haarkon 6:f16f250fe2cb 159 * \var Pixy_NMFIFO[NM_MAXOBJECT]
haarkon 6:f16f250fe2cb 160 * \brief is a FIFO where the Normal objects are stored. FIFO can store up to 20 objects (NM_MAXOBJECT = 20)
haarkon 2:90355c600404 161 */
haarkon 2:90355c600404 162
haarkon 2:90355c600404 163 /**
haarkon 2:90355c600404 164 * \var Pixy_CCObjet
haarkon 2:90355c600404 165 * \brief is the number of CC object read by the library ISR from the CAM during the last frame
haarkon 2:90355c600404 166 * @note not Thread Safe
haarkon 2:90355c600404 167 * \var Pixy_NMObjet
haarkon 2:90355c600404 168 * \brief is the number of normal object read by the library ISR from the CAM during the last frame
haarkon 2:90355c600404 169 * @note not Thread Safe
haarkon 2:90355c600404 170 */
haarkon 6:f16f250fe2cb 171 T_pixyCCData Pixy_CCFIFO[CC_MAXOBJECT]; // FIFO des objets ColorCode
haarkon 6:f16f250fe2cb 172 T_pixyNMData Pixy_NMFIFO[NM_MAXOBJECT]; // FIFO des objets Normaux
haarkon 0:6f78c735f07c 173 Byte Pixy_CCObjet; // Nombre d'objets de type Color Code
haarkon 0:6f78c735f07c 174 Byte Pixy_NMObjet; // Nombre d'objets Normaux
haarkon 0:6f78c735f07c 175 Byte Pixy_FirstCCObjet; // Position dans la FIFO du premier objet de la trame suivante
haarkon 0:6f78c735f07c 176 Byte Pixy_FirstNMObjet; // Position dans la FIFO du premier objet de la trame suivante
haarkon 0:6f78c735f07c 177
haarkon 0:6f78c735f07c 178
haarkon 2:90355c600404 179 /** A new frame as been read
haarkon 0:6f78c735f07c 180 *
haarkon 2:90355c600404 181 * @note Semaphore are global variable used to communicate from ISR with main routine. All semaphores must be cleared by user.
haarkon 2:90355c600404 182 * \var FlagPixy
haarkon 2:90355c600404 183 * \brief is automaticly set after each received image frame
haarkon 2:90355c600404 184 */
haarkon 2:90355c600404 185
haarkon 2:90355c600404 186 /** Data have been lost
haarkon 0:6f78c735f07c 187 *
haarkon 2:90355c600404 188 * @note Semaphore are global variable used to communicate from ISR with main routine. All semaphores must be cleared by user.
haarkon 2:90355c600404 189 * \var FlagPixyOverflow
haarkon 9:a298df20a2dd 190 * \brief is automaticly set if any FIFO oveflows (more than object of a kind than allowed by NM_MAXOBJECT or CC_MAXOBJECT).
haarkon 0:6f78c735f07c 191 */
haarkon 0:6f78c735f07c 192 int FlagPixy, FlagPixyOverflow;
haarkon 0:6f78c735f07c 193 int Pixy_check;
haarkon 0:6f78c735f07c 194
haarkon 0:6f78c735f07c 195 /**
haarkon 2:90355c600404 196 * Constructor of pixy object.
haarkon 0:6f78c735f07c 197 *
haarkon 2:90355c600404 198 * @param tx : the Mbed pin used as TX
haarkon 2:90355c600404 199 * @param rx : the Mbed pin used as RX
haarkon 2:90355c600404 200 * @param debit : the bitrate of the serial (max value is 230400 b/s)
haarkon 0:6f78c735f07c 201 */
haarkon 0:6f78c735f07c 202 PIXY(PinName tx, PinName rx, int debit =230400);
haarkon 0:6f78c735f07c 203
haarkon 0:6f78c735f07c 204 /**
haarkon 0:6f78c735f07c 205 * Return the number of objects (normal and ColorCode) that have been
haarkon 5:d395fe0fb0e0 206 * received from the PIXY and stored in the FIFO during the last frame
haarkon 0:6f78c735f07c 207 *
haarkon 5:d395fe0fb0e0 208 * @param nbNM (int - passed by reference) : number of normal object detected
haarkon 5:d395fe0fb0e0 209 * @param nbCC (int - passed by reference) : number of color code object detected
haarkon 2:90355c600404 210 * @return 0 if sucessfull,
haarkon 0:6f78c735f07c 211 * -1 if no PIXY is talking,
haarkon 5:d395fe0fb0e0 212 * -2 sucessfull but object(s) have been lost from previous frames (missed frame) or from actual (too many objects)
haarkon 0:6f78c735f07c 213 */
haarkon 2:90355c600404 214 int detectedObject (int* nbNM, int* nbCC);
haarkon 0:6f78c735f07c 215
haarkon 0:6f78c735f07c 216 /**
haarkon 0:6f78c735f07c 217 * Reads the oldest Color Code object from the last frame received
haarkon 0:6f78c735f07c 218 *
haarkon 2:90355c600404 219 * @note this function also manage the read pointer of the Color Code FIFO (ie : after use function point the next object to be read)
haarkon 0:6f78c735f07c 220 *
haarkon 0:6f78c735f07c 221 * @return a T_PixyCCBloc (see .h for details)
haarkon 0:6f78c735f07c 222 */
haarkon 0:6f78c735f07c 223 T_pixyCCBloc getCCBloc (void);
haarkon 0:6f78c735f07c 224
haarkon 0:6f78c735f07c 225 /**
haarkon 0:6f78c735f07c 226 * Reads the oldest Normal object from the last frame received
haarkon 0:6f78c735f07c 227 *
haarkon 2:90355c600404 228 * @note this function also manage the read pointer of the Normal FIFO (ie : after use function point the next object to be read)
haarkon 0:6f78c735f07c 229 *
haarkon 0:6f78c735f07c 230 * @return a T_PixyNMBloc (see .h for details)
haarkon 0:6f78c735f07c 231 */
haarkon 0:6f78c735f07c 232 T_pixyNMBloc getNMBloc (void);
haarkon 0:6f78c735f07c 233
haarkon 0:6f78c735f07c 234 /**
haarkon 0:6f78c735f07c 235 * Set the Brightness of the Pixy
haarkon 0:6f78c735f07c 236 *
haarkon 0:6f78c735f07c 237 * @param brightness level (between 0 and 255)
haarkon 5:d395fe0fb0e0 238 */
haarkon 5:d395fe0fb0e0 239 void setBrightness (Byte brightness);
haarkon 5:d395fe0fb0e0 240
haarkon 5:d395fe0fb0e0 241 /**
haarkon 5:d395fe0fb0e0 242 * Gives information about a new image taken by Pixy.
haarkon 5:d395fe0fb0e0 243 * @return 0 if no new image or 1 if there is a new image
haarkon 0:6f78c735f07c 244 */
haarkon 5:d395fe0fb0e0 245 int checkNewImage (void);
haarkon 5:d395fe0fb0e0 246
haarkon 5:d395fe0fb0e0 247 /**
haarkon 5:d395fe0fb0e0 248 * Gives information about a detected Pixy.
haarkon 5:d395fe0fb0e0 249 * @return -1 if no Pixy detected or 0 if Pixy detected
haarkon 5:d395fe0fb0e0 250 */
haarkon 5:d395fe0fb0e0 251 int checkPixy (void);
haarkon 0:6f78c735f07c 252
haarkon 0:6f78c735f07c 253 private :
haarkon 0:6f78c735f07c 254
haarkon 0:6f78c735f07c 255 int Pixy_CCFrameIsNew, Pixy_NMFrameIsNew;
haarkon 0:6f78c735f07c 256
haarkon 0:6f78c735f07c 257 /**
haarkon 0:6f78c735f07c 258 * Fonction d'interruption de lecture sur la liaison série.
haarkon 0:6f78c735f07c 259 */
haarkon 0:6f78c735f07c 260 void getPixyByte();
haarkon 0:6f78c735f07c 261
haarkon 0:6f78c735f07c 262
haarkon 0:6f78c735f07c 263 };
haarkon 0:6f78c735f07c 264
haarkon 0:6f78c735f07c 265 #endif /* PIXY_H */