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:
Tue May 22 17:15:30 2018 +0000
Revision:
2:90355c600404
Parent:
1:183ebbbc9c2e
Child:
3:1e4c6d7a0053
added callback treatment of interrupt

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 0:6f78c735f07c 45 #define N_BLOCSIZE 12
haarkon 0:6f78c735f07c 46
haarkon 0:6f78c735f07c 47 #define N_BLOCCODE 0xAA55
haarkon 0:6f78c735f07c 48 #define CC_BLOCCODE 0xAA56
haarkon 0:6f78c735f07c 49
haarkon 0:6f78c735f07c 50 /**
haarkon 2:90355c600404 51 * \struct Byte -> Short hand for unsigned char
haarkon 2:90355c600404 52 * \struct Word -> Short hand for unsigned short
haarkon 2:90355c600404 53 * \struct lWord -> Short hand for unsigned long
haarkon 0:6f78c735f07c 54 */
haarkon 0:6f78c735f07c 55 typedef unsigned char Byte;
haarkon 0:6f78c735f07c 56 typedef unsigned short Word;
haarkon 0:6f78c735f07c 57 typedef unsigned long lWord;
haarkon 2:90355c600404 58 /**
haarkon 2:90355c600404 59 * \struct T_pixyCCBloc
haarkon 2:90355c600404 60 * \brief Structured type to match pixy Color Code bloc organisation
haarkon 2:90355c600404 61 * \param Checksum (Word) : modulus sum of all words of the message
haarkon 2:90355c600404 62 * \param signature (Word) : identity (ie : numbers associated with colors) of the detected color code bloc
haarkon 2:90355c600404 63 * \param x (Word) : X coordinate (in pixel) of the center of the color code bloc
haarkon 2:90355c600404 64 * \param y (Word) : Y coordinate (in pixel) of the center of the color code bloc
haarkon 2:90355c600404 65 * \param width (Word) : Width (in pixel) of the color code bloc
haarkon 2:90355c600404 66 * \param height (Word) : Height (in pixel) of the color code bloc
haarkon 2:90355c600404 67 * \param angle (Word) : Angle (in degree, referenced to horizontal) of the color code bloc
haarkon 2:90355c600404 68 */
haarkon 0:6f78c735f07c 69 typedef struct {
haarkon 0:6f78c735f07c 70 Word checksum;
haarkon 0:6f78c735f07c 71 Word signature;
haarkon 0:6f78c735f07c 72 Word x;
haarkon 0:6f78c735f07c 73 Word y;
haarkon 0:6f78c735f07c 74 Word width;
haarkon 0:6f78c735f07c 75 Word height;
haarkon 0:6f78c735f07c 76 Word angle;
haarkon 0:6f78c735f07c 77 } T_pixyCCBloc;
haarkon 0:6f78c735f07c 78
haarkon 2:90355c600404 79 /**
haarkon 2:90355c600404 80 * \struct T_pixyNMBloc
haarkon 2:90355c600404 81 * \brief Structured type to match pixy normal bloc organisation
haarkon 2:90355c600404 82 * \param Checksum (Word) : modulus sum of all words of the message
haarkon 2:90355c600404 83 * \param signature (Word) : identity (ie : number associated with the color) of the detected bloc
haarkon 2:90355c600404 84 * \param x (Word) : X coordinate (in pixel) of the center of the color code bloc
haarkon 2:90355c600404 85 * \param y (Word) : Y coordinate (in pixel) of the center of the color code bloc
haarkon 2:90355c600404 86 * \param width (Word) : Width (in pixel) of the color code bloc
haarkon 2:90355c600404 87 * \param height (Word) : Height (in pixel) of the color code bloc
haarkon 2:90355c600404 88 */
haarkon 0:6f78c735f07c 89 typedef struct {
haarkon 0:6f78c735f07c 90 Word checksum;
haarkon 0:6f78c735f07c 91 Word signature;
haarkon 0:6f78c735f07c 92 Word x;
haarkon 0:6f78c735f07c 93 Word y;
haarkon 0:6f78c735f07c 94 Word width;
haarkon 0:6f78c735f07c 95 Word height;
haarkon 0:6f78c735f07c 96 } T_pixyNMBloc;
haarkon 0:6f78c735f07c 97
haarkon 2:90355c600404 98 /**
haarkon 2:90355c600404 99 * \union T_pixyCCData
haarkon 2:90355c600404 100 * \brief Structured type to match pixy Color Code bloc organisation
haarkon 2:90355c600404 101 * \param CCBloc (T_pixyCCBloc) : Color Code bloc structured element
haarkon 2:90355c600404 102 * \param tab[14] (Byte) : Byte access to the Color Code bloc
haarkon 2:90355c600404 103 */
haarkon 0:6f78c735f07c 104 typedef union {
haarkon 0:6f78c735f07c 105 Byte tab[14];
haarkon 0:6f78c735f07c 106 T_pixyCCBloc CCbloc;
haarkon 0:6f78c735f07c 107 } T_pixyCCData;
haarkon 0:6f78c735f07c 108
haarkon 2:90355c600404 109 /**
haarkon 2:90355c600404 110 * \union T_pixyNMData
haarkon 2:90355c600404 111 * \brief Structured type to match pixy normal bloc organisation
haarkon 2:90355c600404 112 * \param CCBloc (T_pixyCCBloc) : Normal bloc structured element
haarkon 2:90355c600404 113 * \param tab[12] (Byte) : Byte access to the normal bloc
haarkon 2:90355c600404 114 */
haarkon 0:6f78c735f07c 115 typedef union {
haarkon 0:6f78c735f07c 116 Byte tab[12];
haarkon 0:6f78c735f07c 117 T_pixyNMBloc NMbloc;
haarkon 0:6f78c735f07c 118 } T_pixyNMData;
haarkon 0:6f78c735f07c 119
haarkon 0:6f78c735f07c 120 typedef enum {none, begin, normal, colorCode, doubleZero} T_pixyState;
haarkon 0:6f78c735f07c 121
haarkon 0:6f78c735f07c 122 typedef union {
haarkon 0:6f78c735f07c 123 lWord mot;
haarkon 0:6f78c735f07c 124 Byte tab[4];
haarkon 0:6f78c735f07c 125 } T_tmpBuffer;
haarkon 0:6f78c735f07c 126
haarkon 0:6f78c735f07c 127 typedef union {
haarkon 0:6f78c735f07c 128 Word mot;
haarkon 0:6f78c735f07c 129 Byte tab[2];
haarkon 0:6f78c735f07c 130 } T_structBuffer;
haarkon 0:6f78c735f07c 131
haarkon 0:6f78c735f07c 132
haarkon 0:6f78c735f07c 133
haarkon 0:6f78c735f07c 134 /**
haarkon 2:90355c600404 135 * Pixy : CMU CAM 5 - Smart camera
haarkon 2:90355c600404 136 * More informations at http://cmucam.org/projects/cmucam5
haarkon 0:6f78c735f07c 137 */
haarkon 0:6f78c735f07c 138 class PIXY {
haarkon 0:6f78c735f07c 139
haarkon 0:6f78c735f07c 140 protected :
haarkon 0:6f78c735f07c 141
haarkon 0:6f78c735f07c 142 Serial* _cmucam;
haarkon 0:6f78c735f07c 143
haarkon 0:6f78c735f07c 144 public :
haarkon 0:6f78c735f07c 145
haarkon 2:90355c600404 146 /** Direct access to the CC FIFO
haarkon 2:90355c600404 147 * @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 148 * @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 149 *
haarkon 2:90355c600404 150 * \var Pixy_CCFIFO[20]
haarkon 2:90355c600404 151 * \brief is a FIFO where the Color Code objects are stored. FIFO can store up to 20 objects
haarkon 2:90355c600404 152 */
haarkon 2:90355c600404 153
haarkon 2:90355c600404 154 /** Direct access to the NM FIFO
haarkon 2:90355c600404 155 * @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 156 * @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 157 * \var Pixy_NMFIFO[20]
haarkon 2:90355c600404 158 * \brief is a FIFO where the Normal objects are stored. FIFO can store up to 20 objects
haarkon 2:90355c600404 159 */
haarkon 2:90355c600404 160
haarkon 2:90355c600404 161 /**
haarkon 2:90355c600404 162 * \var Pixy_CCObjet
haarkon 2:90355c600404 163 * \brief is the number of CC object read by the library ISR from the CAM during the last frame
haarkon 2:90355c600404 164 * @note not Thread Safe
haarkon 2:90355c600404 165 * \var Pixy_NMObjet
haarkon 2:90355c600404 166 * \brief is the number of normal object read by the library ISR from the CAM during the last frame
haarkon 2:90355c600404 167 * @note not Thread Safe
haarkon 2:90355c600404 168 */
haarkon 0:6f78c735f07c 169 T_pixyCCData Pixy_CCFIFO[20]; // FIFO des objets ColorCode
haarkon 0:6f78c735f07c 170 T_pixyNMData Pixy_NMFIFO[20]; // FIFO des objets Normaux
haarkon 0:6f78c735f07c 171 Byte Pixy_CCObjet; // Nombre d'objets de type Color Code
haarkon 0:6f78c735f07c 172 Byte Pixy_NMObjet; // Nombre d'objets Normaux
haarkon 0:6f78c735f07c 173 Byte Pixy_FirstCCObjet; // Position dans la FIFO du premier objet de la trame suivante
haarkon 0:6f78c735f07c 174 Byte Pixy_FirstNMObjet; // Position dans la FIFO du premier objet de la trame suivante
haarkon 0:6f78c735f07c 175
haarkon 0:6f78c735f07c 176
haarkon 2:90355c600404 177 /** A new frame as been read
haarkon 0:6f78c735f07c 178 *
haarkon 2:90355c600404 179 * @note Semaphore are global variable used to communicate from ISR with main routine. All semaphores must be cleared by user.
haarkon 2:90355c600404 180 * \var FlagPixy
haarkon 2:90355c600404 181 * \brief is automaticly set after each received image frame
haarkon 2:90355c600404 182 */
haarkon 2:90355c600404 183
haarkon 2:90355c600404 184 /** Data have been lost
haarkon 0:6f78c735f07c 185 *
haarkon 2:90355c600404 186 * @note Semaphore are global variable used to communicate from ISR with main routine. All semaphores must be cleared by user.
haarkon 2:90355c600404 187 * \var FlagPixyOverflow
haarkon 2:90355c600404 188 * \brief is automaticly set if any FIFO oveflows (more than 20 obj of a kind).
haarkon 0:6f78c735f07c 189 */
haarkon 0:6f78c735f07c 190 int FlagPixy, FlagPixyOverflow;
haarkon 0:6f78c735f07c 191 int Pixy_check;
haarkon 0:6f78c735f07c 192
haarkon 0:6f78c735f07c 193 /**
haarkon 2:90355c600404 194 * Constructor of pixy object.
haarkon 0:6f78c735f07c 195 *
haarkon 2:90355c600404 196 * @param tx : the Mbed pin used as TX
haarkon 2:90355c600404 197 * @param rx : the Mbed pin used as RX
haarkon 2:90355c600404 198 * @param debit : the bitrate of the serial (max value is 230400 b/s)
haarkon 0:6f78c735f07c 199 */
haarkon 0:6f78c735f07c 200 PIXY(PinName tx, PinName rx, int debit =230400);
haarkon 0:6f78c735f07c 201
haarkon 0:6f78c735f07c 202 /**
haarkon 0:6f78c735f07c 203 * Return the number of objects (normal and ColorCode) that have been
haarkon 0:6f78c735f07c 204 * received from the PIXY during the last frame
haarkon 0:6f78c735f07c 205 *
haarkon 2:90355c600404 206 * @param nbNM (passed by reference) : number of normal object detected
haarkon 2:90355c600404 207 * @param nbCC (passed by reference) : number of color code object detected
haarkon 2:90355c600404 208 * @return 0 if sucessfull,
haarkon 0:6f78c735f07c 209 * -1 if no PIXY is talking,
haarkon 0:6f78c735f07c 210 * -2 if object(s) have been lost (from previous frame or from actual)
haarkon 0:6f78c735f07c 211 */
haarkon 2:90355c600404 212 int detectedObject (int* nbNM, int* nbCC);
haarkon 0:6f78c735f07c 213
haarkon 0:6f78c735f07c 214 /**
haarkon 0:6f78c735f07c 215 * Reads the oldest Color Code object from the last frame received
haarkon 0:6f78c735f07c 216 *
haarkon 2:90355c600404 217 * @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 218 *
haarkon 0:6f78c735f07c 219 * @return a T_PixyCCBloc (see .h for details)
haarkon 0:6f78c735f07c 220 */
haarkon 0:6f78c735f07c 221 T_pixyCCBloc getCCBloc (void);
haarkon 0:6f78c735f07c 222
haarkon 0:6f78c735f07c 223 /**
haarkon 0:6f78c735f07c 224 * Reads the oldest Normal object from the last frame received
haarkon 0:6f78c735f07c 225 *
haarkon 2:90355c600404 226 * @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 227 *
haarkon 0:6f78c735f07c 228 * @return a T_PixyNMBloc (see .h for details)
haarkon 0:6f78c735f07c 229 */
haarkon 0:6f78c735f07c 230 T_pixyNMBloc getNMBloc (void);
haarkon 0:6f78c735f07c 231
haarkon 0:6f78c735f07c 232 /**
haarkon 0:6f78c735f07c 233 * Set the Brightness of the Pixy
haarkon 0:6f78c735f07c 234 *
haarkon 0:6f78c735f07c 235 * @param brightness level (between 0 and 255)
haarkon 0:6f78c735f07c 236 * @return 0 if successfull, else -1
haarkon 0:6f78c735f07c 237 */
haarkon 0:6f78c735f07c 238 int setBrightness (Byte brightness);
haarkon 0:6f78c735f07c 239
haarkon 0:6f78c735f07c 240 private :
haarkon 0:6f78c735f07c 241
haarkon 0:6f78c735f07c 242 int Pixy_CCFrameIsNew, Pixy_NMFrameIsNew;
haarkon 0:6f78c735f07c 243
haarkon 0:6f78c735f07c 244 /**
haarkon 0:6f78c735f07c 245 * Fonction d'interruption de lecture sur la liaison série.
haarkon 0:6f78c735f07c 246 */
haarkon 0:6f78c735f07c 247 void getPixyByte();
haarkon 0:6f78c735f07c 248
haarkon 0:6f78c735f07c 249
haarkon 0:6f78c735f07c 250 };
haarkon 0:6f78c735f07c 251
haarkon 0:6f78c735f07c 252 #endif /* PIXY_H */