pixy2 smart camera library www.pixycam.com

Pixy2 Library project (using UART interface)

Committer:
haarkon
Date:
Mon Mar 11 17:28:59 2019 +0000
Revision:
6:e9e3aa6f1a55
Parent:
5:28e47187bb5c
Child:
7:1163b9c3274b
added error code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
haarkon 0:dde1b9d6c9d6 1 /**
haarkon 0:dde1b9d6c9d6 2 * @author Hugues Angelis
haarkon 0:dde1b9d6c9d6 3 *
haarkon 0:dde1b9d6c9d6 4 * @section LICENSE
haarkon 0:dde1b9d6c9d6 5 *
haarkon 0:dde1b9d6c9d6 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
haarkon 0:dde1b9d6c9d6 7 * of this software and associated documentation files (the "Software"), to deal
haarkon 0:dde1b9d6c9d6 8 * in the Software without restriction, including without limitation the rights
haarkon 0:dde1b9d6c9d6 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
haarkon 0:dde1b9d6c9d6 10 * copies of the Software, and to permit persons to whom the Software is
haarkon 0:dde1b9d6c9d6 11 * furnished to do so, subject to the following conditions:
haarkon 0:dde1b9d6c9d6 12 *
haarkon 0:dde1b9d6c9d6 13 * The above copyright notice and this permission notice shall be included in
haarkon 0:dde1b9d6c9d6 14 * all copies or substantial portions of the Software.
haarkon 0:dde1b9d6c9d6 15 *
haarkon 0:dde1b9d6c9d6 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
haarkon 0:dde1b9d6c9d6 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
haarkon 0:dde1b9d6c9d6 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
haarkon 0:dde1b9d6c9d6 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
haarkon 0:dde1b9d6c9d6 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
haarkon 0:dde1b9d6c9d6 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
haarkon 0:dde1b9d6c9d6 22 * THE SOFTWARE.
haarkon 0:dde1b9d6c9d6 23 *
haarkon 0:dde1b9d6c9d6 24 * @section DESCRIPTION
haarkon 0:dde1b9d6c9d6 25 *
haarkon 0:dde1b9d6c9d6 26 * CMUCAM 5 - Pixy2
haarkon 0:dde1b9d6c9d6 27 *
haarkon 0:dde1b9d6c9d6 28 * Datasheet, FAQ and PC drivers :
haarkon 0:dde1b9d6c9d6 29 *
haarkon 0:dde1b9d6c9d6 30 * http://www.pixycam.com/
haarkon 0:dde1b9d6c9d6 31 */
haarkon 0:dde1b9d6c9d6 32
haarkon 0:dde1b9d6c9d6 33 #ifndef _PIXY2_
haarkon 0:dde1b9d6c9d6 34 #define _PIXY2_
haarkon 0:dde1b9d6c9d6 35
haarkon 0:dde1b9d6c9d6 36 /**
haarkon 0:dde1b9d6c9d6 37 * Include : Mbed Library
haarkon 0:dde1b9d6c9d6 38 */
haarkon 0:dde1b9d6c9d6 39 #include "mbed.h"
haarkon 0:dde1b9d6c9d6 40
haarkon 0:dde1b9d6c9d6 41 /**
haarkon 0:dde1b9d6c9d6 42 * Defines
haarkon 0:dde1b9d6c9d6 43 */
haarkon 0:dde1b9d6c9d6 44 #define PIXY2_NCSHEADERSIZE 4
haarkon 0:dde1b9d6c9d6 45 #define PIXY2_CSHEADERSIZE 4
haarkon 0:dde1b9d6c9d6 46 #define PIXY2_SYNC 0xC1AE
haarkon 0:dde1b9d6c9d6 47 #define PIXY2_CSSYNC 0xC1AF
haarkon 0:dde1b9d6c9d6 48 #define PIXY2_ACK 1
haarkon 0:dde1b9d6c9d6 49 #define PIXY2_ASK_RESOL 12
haarkon 0:dde1b9d6c9d6 50 #define PIXY2_REP_RESOL 13
haarkon 0:dde1b9d6c9d6 51 #define PIXY2_ASK_VERS 14
haarkon 0:dde1b9d6c9d6 52 #define PIXY2_REP_VERS 15
haarkon 0:dde1b9d6c9d6 53 #define PIXY2_SET_BRIGHT 16
haarkon 0:dde1b9d6c9d6 54 #define PIXY2_SET_SERVOS 18
haarkon 0:dde1b9d6c9d6 55 #define PIXY2_SET_LED 20
haarkon 0:dde1b9d6c9d6 56 #define PIXY2_SET_LAMP 22
haarkon 0:dde1b9d6c9d6 57 #define PIXY2_ASK_FPS 24
haarkon 0:dde1b9d6c9d6 58 #define PIXY2_ASK_BLOC 32
haarkon 0:dde1b9d6c9d6 59 #define PIXY2_REP_BLOC 33
haarkon 0:dde1b9d6c9d6 60 #define PIXY2_ASK_LINE 48
haarkon 0:dde1b9d6c9d6 61 #define PIXY2_REP_LINE 49
haarkon 0:dde1b9d6c9d6 62 #define PIXY2_SET_MODE 54
haarkon 0:dde1b9d6c9d6 63 #define PIXY2_SET_TURN 58
haarkon 0:dde1b9d6c9d6 64 #define PIXY2_SET_VECTOR 56
haarkon 0:dde1b9d6c9d6 65 #define PIXY2_SET_DEFTURN 60
haarkon 0:dde1b9d6c9d6 66 #define PIXY2_SET_REVERSE 62
haarkon 0:dde1b9d6c9d6 67 #define PIXY2_ASK_VIDEO 112
haarkon 1:dd81f4065b6b 68
haarkon 6:e9e3aa6f1a55 69 /**************** ERRORS ****************/
haarkon 5:28e47187bb5c 70
haarkon 5:28e47187bb5c 71 /**
haarkon 6:e9e3aa6f1a55 72 * \struct Pixy2ErrorCode
haarkon 6:e9e3aa6f1a55 73 * \brief Explicit error code list :
haarkon 5:28e47187bb5c 74 * \param PIXY2_OK : No error
haarkon 5:28e47187bb5c 75 * \param PIXY2_MISC_ERROR : Unknown error
haarkon 5:28e47187bb5c 76 * \param PIXY2_BUSY : Pixy hasn't finish yet
haarkon 5:28e47187bb5c 77 * \param PIXY2_BAD_CHECKSUM : Checksum is wrong
haarkon 5:28e47187bb5c 78 * \param PIXY2_TYPE_ERROR : unexpected type message
haarkon 5:28e47187bb5c 79 */
haarkon 6:e9e3aa6f1a55 80 typedef int Pixy2ErrorCode;
haarkon 5:28e47187bb5c 81
haarkon 5:28e47187bb5c 82 #define PIXY2_OK 0
haarkon 5:28e47187bb5c 83 #define PIXY2_MISC_ERROR -1
haarkon 5:28e47187bb5c 84 #define PIXY2_BUSY -2
haarkon 5:28e47187bb5c 85 #define PIXY2_BAD_CHECKSUM -3
haarkon 5:28e47187bb5c 86 #define PIXY2_TYPE_ERROR -7
haarkon 1:dd81f4065b6b 87
haarkon 1:dd81f4065b6b 88 /**************** STATE MACHINE ****************/
haarkon 1:dd81f4065b6b 89
haarkon 1:dd81f4065b6b 90 typedef enum {idle, messageSent, receivingHeader, headerReceived, receivingData, dataReceived} T_Pixy2State;
haarkon 1:dd81f4065b6b 91
haarkon 1:dd81f4065b6b 92 /**************** UTILS ****************/
haarkon 1:dd81f4065b6b 93
haarkon 6:e9e3aa6f1a55 94
haarkon 0:dde1b9d6c9d6 95 /**
haarkon 0:dde1b9d6c9d6 96 * \struct Byte -> Short hand for unsigned char
haarkon 2:5281a6289e8b 97 * \struct sByte -> Short hand for char
haarkon 0:dde1b9d6c9d6 98 * \struct Word -> Short hand for unsigned short
haarkon 2:5281a6289e8b 99 * \struct sWord -> Short hand for short
haarkon 0:dde1b9d6c9d6 100 * \struct lWord -> Short hand for unsigned long
haarkon 2:5281a6289e8b 101 * \struct slWord -> Short hand for long
haarkon 0:dde1b9d6c9d6 102 */
haarkon 0:dde1b9d6c9d6 103 typedef unsigned char Byte;
haarkon 2:5281a6289e8b 104 typedef char sByte;
haarkon 0:dde1b9d6c9d6 105 typedef unsigned short Word;
haarkon 2:5281a6289e8b 106 typedef short sWord;
haarkon 0:dde1b9d6c9d6 107 typedef unsigned long lWord;
haarkon 2:5281a6289e8b 108 typedef long slWord;
haarkon 0:dde1b9d6c9d6 109
haarkon 1:dd81f4065b6b 110 /**
haarkon 1:dd81f4065b6b 111 * \union T_Word
haarkon 1:dd81f4065b6b 112 * \brief Structured type to switch from word to bytes
haarkon 1:dd81f4065b6b 113 * \param mot (Word) : 16 bits word
haarkon 1:dd81f4065b6b 114 * \param octet (Byte) : 2 bytes that overlap mot (byte access)
haarkon 1:dd81f4065b6b 115 */
haarkon 0:dde1b9d6c9d6 116 typedef union {
haarkon 0:dde1b9d6c9d6 117 Word mot;
haarkon 0:dde1b9d6c9d6 118 Byte octet[2];
haarkon 0:dde1b9d6c9d6 119 }T_Word;
haarkon 0:dde1b9d6c9d6 120
haarkon 1:dd81f4065b6b 121 /**
haarkon 1:dd81f4065b6b 122 * \union T_lWord
haarkon 1:dd81f4065b6b 123 * \brief Structured type to switch from lword to word or bytes
haarkon 1:dd81f4065b6b 124 * \param motLong (lWord) : 32 bits word
haarkon 1:dd81f4065b6b 125 * \param mot (Word) : 2 x 16 bits words that overlap motLong (word access)
haarkon 1:dd81f4065b6b 126 * \param octet (Byte) : 4 bytes that overlap motLong (byte access)
haarkon 1:dd81f4065b6b 127 */
haarkon 0:dde1b9d6c9d6 128 typedef union {
haarkon 0:dde1b9d6c9d6 129 lWord motLong;
haarkon 0:dde1b9d6c9d6 130 Word mot[2];
haarkon 0:dde1b9d6c9d6 131 Byte octet[4];
haarkon 0:dde1b9d6c9d6 132 }T_lWord;
haarkon 0:dde1b9d6c9d6 133
haarkon 1:dd81f4065b6b 134
haarkon 1:dd81f4065b6b 135 /**************** HEADERS ****************/
haarkon 1:dd81f4065b6b 136
haarkon 1:dd81f4065b6b 137 /**
haarkon 1:dd81f4065b6b 138 * \struct T_pixy2Header
haarkon 1:dd81f4065b6b 139 * \brief Structured type that match pixy2 header without checksum (send message)
haarkon 1:dd81f4065b6b 140 * \param pixSync (Word) : 16 bits synchro word - could be 0xc1ae (PIXY2_SYNC) or 0xc1af (PIXY2_CSSYNC)
haarkon 1:dd81f4065b6b 141 * \param pixType (Byte) : 8 bits message type identifier
haarkon 1:dd81f4065b6b 142 * \param pixLength (Byte) : 8 bits message payload length (payload doesn't include checksum)
haarkon 1:dd81f4065b6b 143 */
haarkon 0:dde1b9d6c9d6 144 typedef struct {
haarkon 0:dde1b9d6c9d6 145 Word pixSync;
haarkon 0:dde1b9d6c9d6 146 Byte pixType;
haarkon 0:dde1b9d6c9d6 147 Byte pixLength;
haarkon 0:dde1b9d6c9d6 148 }T_pixy2Header;
haarkon 0:dde1b9d6c9d6 149
haarkon 1:dd81f4065b6b 150 /**
haarkon 1:dd81f4065b6b 151 * \struct T_pixy2sendFrame
haarkon 1:dd81f4065b6b 152 * \brief Structured type that match frame definition for all kind of message to send to a pixy2
haarkon 1:dd81f4065b6b 153 * \param header (T_pixy2Header) : 4 bytes classical header starting with PIXY2_SYNC
haarkon 1:dd81f4065b6b 154 * \param data (Byte) : 5 bytes payload (to match all usage, not all byte must be used)
haarkon 1:dd81f4065b6b 155 */
haarkon 0:dde1b9d6c9d6 156 typedef struct {
haarkon 0:dde1b9d6c9d6 157 T_pixy2Header header;
haarkon 0:dde1b9d6c9d6 158 Byte data[5];
haarkon 0:dde1b9d6c9d6 159 }T_pixy2sendFrame;
haarkon 0:dde1b9d6c9d6 160
haarkon 1:dd81f4065b6b 161 /**
haarkon 1:dd81f4065b6b 162 * \union T_pixy2sendBuffer
haarkon 1:dd81f4065b6b 163 * \brief Structured type to switch between structured type T_pixy2sendFrame and bytes
haarkon 1:dd81f4065b6b 164 * \param frame (T_pixy2sendFrame) : classical frame (header + payload) starting with PIXY2_SYNC
haarkon 1:dd81f4065b6b 165 * \param data (Byte) : 9 bytes that overlap frame (byte access)
haarkon 1:dd81f4065b6b 166 */
haarkon 0:dde1b9d6c9d6 167 typedef union {
haarkon 0:dde1b9d6c9d6 168 T_pixy2sendFrame frame;
haarkon 0:dde1b9d6c9d6 169 Byte data[9];
haarkon 0:dde1b9d6c9d6 170 }T_pixy2sendBuffer;
haarkon 0:dde1b9d6c9d6 171
haarkon 1:dd81f4065b6b 172 /**
haarkon 1:dd81f4065b6b 173 * \struct T_pixy2RcvHeader
haarkon 1:dd81f4065b6b 174 * \brief Structured type that match pixy2 header with checksum (received message)
haarkon 1:dd81f4065b6b 175 * \param pixSync (Word) : 16 bits synchro word - could be 0xc1ae (PIXY2_SYNC) or 0xc1af (PIXY2_CSSYNC)
haarkon 1:dd81f4065b6b 176 * \param pixType (Byte) : 8 bits message type identifier
haarkon 1:dd81f4065b6b 177 * \param pixLength (Byte) : 8 bits message payload length (payload doesn't include checksum)
haarkon 1:dd81f4065b6b 178 * \param pixSync (Word) : 16 bits checksum (sum of all bytes of the payload)
haarkon 1:dd81f4065b6b 179 */
haarkon 0:dde1b9d6c9d6 180 typedef struct {
haarkon 0:dde1b9d6c9d6 181 Word pixSync;
haarkon 0:dde1b9d6c9d6 182 Byte pixType;
haarkon 0:dde1b9d6c9d6 183 Byte pixLength;
haarkon 0:dde1b9d6c9d6 184 Word pixChecksum;
haarkon 0:dde1b9d6c9d6 185 }T_pixy2RcvHeader;
haarkon 0:dde1b9d6c9d6 186
haarkon 1:dd81f4065b6b 187
haarkon 1:dd81f4065b6b 188 /**************** PAYLOADS ****************/
haarkon 1:dd81f4065b6b 189
haarkon 1:dd81f4065b6b 190 /**
haarkon 1:dd81f4065b6b 191 * \struct T_pixy2returnCode
haarkon 1:dd81f4065b6b 192 * \brief Structured type that match pixy2 error/acknowledge/reply frame (type = 1 or 3) message payload
haarkon 1:dd81f4065b6b 193 * \param pixReturn (lWord) : 32 bits returned value
haarkon 1:dd81f4065b6b 194 */
haarkon 0:dde1b9d6c9d6 195 typedef struct {
haarkon 1:dd81f4065b6b 196 lWord pixReturn;
haarkon 1:dd81f4065b6b 197 }T_pixy2returnCode;
haarkon 0:dde1b9d6c9d6 198
haarkon 1:dd81f4065b6b 199 /**
haarkon 1:dd81f4065b6b 200 * \struct T_Pixy2Version
haarkon 1:dd81f4065b6b 201 * \brief Structured type that match pixy2 version frame (type = 14/15) message payload
haarkon 1:dd81f4065b6b 202 * \param pixHWVersion (Word) : 16 bits hardWare Version of pixy2
haarkon 1:dd81f4065b6b 203 * \param pixFWVersionMaj (Byte) : 8 bits upper part of firmware (before the dot)
haarkon 1:dd81f4065b6b 204 * \param pixFWVersionMin (Byte) : 8 bits lower part of firmware (after the dot)
haarkon 1:dd81f4065b6b 205 * \param pixFWBuild (Word) : 16 bits firmware build information
haarkon 1:dd81f4065b6b 206 * \param pixHFString (String) : 10 bytes user friendly pixy2 firmware type
haarkon 1:dd81f4065b6b 207 */
haarkon 0:dde1b9d6c9d6 208 typedef struct {
haarkon 0:dde1b9d6c9d6 209 Word pixHWVersion;
haarkon 0:dde1b9d6c9d6 210 Byte pixFWVersionMaj;
haarkon 0:dde1b9d6c9d6 211 Byte pixFWVersionMin;
haarkon 0:dde1b9d6c9d6 212 Word pixFWBuild;
haarkon 0:dde1b9d6c9d6 213 char pixHFString[10];
haarkon 0:dde1b9d6c9d6 214 }T_Pixy2Version;
haarkon 0:dde1b9d6c9d6 215
haarkon 1:dd81f4065b6b 216 /**
haarkon 1:dd81f4065b6b 217 * \struct T_Pixy2Resolution
haarkon 1:dd81f4065b6b 218 * \brief Structured type that match pixy2 resolution frame (type = 12/13) message payload
haarkon 1:dd81f4065b6b 219 * \param pixFrameWidth (Word) : 16 bits width (in pixel) of an image
haarkon 5:28e47187bb5c 220 * \param pixFrameHeight (Word) : 16 bits height (in pixel) of an image
haarkon 1:dd81f4065b6b 221 */
haarkon 0:dde1b9d6c9d6 222 typedef struct {
haarkon 0:dde1b9d6c9d6 223 Word pixFrameWidth;
haarkon 5:28e47187bb5c 224 Word pixFrameHeight;
haarkon 0:dde1b9d6c9d6 225 }T_Pixy2Resolution;
haarkon 0:dde1b9d6c9d6 226
haarkon 1:dd81f4065b6b 227 /**
haarkon 1:dd81f4065b6b 228 * \struct T_pixy2Bloc
haarkon 1:dd81f4065b6b 229 * \brief Structured type that match pixy2 blocks frame (type = 32/33) message payload
haarkon 1:dd81f4065b6b 230 * \param pixSignature (Word) : 16 bits signature or color code of the color bloc (signature are between 1 and 7, color code are composed of signature of 2, up to 5, tags so over 10)
haarkon 1:dd81f4065b6b 231 * \param pixX (Word) : 16 bits X (horizontal axis) position of color bloc center, relative to the left of the image (in pixels, between 0 and 315)
haarkon 1:dd81f4065b6b 232 * \param pixY (Word) : 16 bits Y (vertical axis) position of color bloc center, relative to the top of the image (in pixels, between 0 and 207)
haarkon 1:dd81f4065b6b 233 * \param pixWidth (Word) : 16 bits width (in pixels, between 0 and 316) of color bloc
haarkon 1:dd81f4065b6b 234 * \param pixHeight (Word) : 16 bits height (in pixels, between 0 and 208) of color bloc
haarkon 2:5281a6289e8b 235 * \param pixAngle (sWord) : 16 bits angle (in degree, between -180.0 and +180.0) of a color code bloc
haarkon 1:dd81f4065b6b 236 * \param pixIndex (Byte) : 8 bits tracking identification of the color code bloc (set by pixy2 to ease a bloc position following program)
haarkon 1:dd81f4065b6b 237 * \param pixAge (Byte) : 8 bits age (in number of frame) of a bloc (doesn't wrap around).
haarkon 1:dd81f4065b6b 238 * @note More info can be found here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:ccc_api
haarkon 1:dd81f4065b6b 239 * @note or here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:color_connected_components
haarkon 1:dd81f4065b6b 240 */
haarkon 0:dde1b9d6c9d6 241 typedef struct {
haarkon 0:dde1b9d6c9d6 242 Word pixSignature;
haarkon 0:dde1b9d6c9d6 243 Word pixX;
haarkon 0:dde1b9d6c9d6 244 Word pixY;
haarkon 0:dde1b9d6c9d6 245 Word pixWidth;
haarkon 0:dde1b9d6c9d6 246 Word pixHeight;
haarkon 2:5281a6289e8b 247 sWord pixAngle;
haarkon 0:dde1b9d6c9d6 248 Byte pixIndex;
haarkon 0:dde1b9d6c9d6 249 Byte pixAge;
haarkon 0:dde1b9d6c9d6 250 }T_pixy2Bloc;
haarkon 0:dde1b9d6c9d6 251
haarkon 1:dd81f4065b6b 252 /**
haarkon 1:dd81f4065b6b 253 * \struct T_Pixy2Vector
haarkon 1:dd81f4065b6b 254 * \brief Structured type that match pixy2 vector definition - used in Line frame (type 48/49) - message payload
haarkon 2:5281a6289e8b 255 * \param pixX0 (Byte) : 8 bits X (horizontal, relative to the left of image) position of the tail of the vector (number between 0 and 78)
haarkon 2:5281a6289e8b 256 * \param pixY0 (Byte) : 8 bits Y (vertical, relative to the top of image) position of the tail of the vector (number between 0 and 51)
haarkon 2:5281a6289e8b 257 * \param pixX1 (Byte) : 8 bits X (horizontal, relative to the left of image) position of the head of the vector (number between 0 and 78)
haarkon 2:5281a6289e8b 258 * \param pixY1 (Byte) : 8 bits Y (vertical, relative to the top of image) position of the head of the vector (number between 0 and 51)
haarkon 2:5281a6289e8b 259 * \param pixIndex (Byte) : 8 bits tracking identification of the vector (set by pixy2 to ease a vector identification in case of multiple vector in a line following program)
haarkon 2:5281a6289e8b 260 * \param pixFlags (Byte) : 8 bits flag containing possible usefull informations (see notes)
haarkon 4:eed638064b42 261 * @note This structure is a feature of Line API, packed as a feature in a Line Frame, documented here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:protocol_reference
haarkon 2:5281a6289e8b 262 * @note More info can be found here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_api
haarkon 2:5281a6289e8b 263 * @note or here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_tracking
haarkon 1:dd81f4065b6b 264 */
haarkon 0:dde1b9d6c9d6 265 typedef struct {
haarkon 0:dde1b9d6c9d6 266 Byte pixX0;
haarkon 0:dde1b9d6c9d6 267 Byte pixY0;
haarkon 0:dde1b9d6c9d6 268 Byte pixX1;
haarkon 0:dde1b9d6c9d6 269 Byte pixY1;
haarkon 0:dde1b9d6c9d6 270 Byte pixIndex;
haarkon 0:dde1b9d6c9d6 271 Byte pixFlags;
haarkon 0:dde1b9d6c9d6 272 }T_Pixy2Vector;
haarkon 0:dde1b9d6c9d6 273
haarkon 2:5281a6289e8b 274 /**
haarkon 2:5281a6289e8b 275 * \struct T_Pixy2InterLine
haarkon 2:5281a6289e8b 276 * \brief Structured type that match pixy2 intersection line definition - used in Line frame (type 48/49) - message payload
haarkon 2:5281a6289e8b 277 * \param pixIndex (Byte) : 8 bits tracking identification of the intersection line (set by pixy2 to ease a line following program)
haarkon 2:5281a6289e8b 278 * \param pixReserved (Byte) : Not documented by manufacturer
haarkon 2:5281a6289e8b 279 * \param pixAngle (sWord) : 16 bits angle (in degree, between -180.0 and +180.0) of the intersection line
haarkon 4:eed638064b42 280 * @note This structure is a sub feature of Line API, packed as a sub feature of intersection feature in a Line Frame, documented here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:protocol_reference
haarkon 2:5281a6289e8b 281 * @note More info can be found here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_api
haarkon 2:5281a6289e8b 282 * @note or here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_tracking
haarkon 2:5281a6289e8b 283 */
haarkon 0:dde1b9d6c9d6 284 typedef struct {
haarkon 0:dde1b9d6c9d6 285 Byte pixIndex;
haarkon 0:dde1b9d6c9d6 286 Byte pixReserved;
haarkon 2:5281a6289e8b 287 sWord pixAngle;
haarkon 0:dde1b9d6c9d6 288 }T_Pixy2InterLine;
haarkon 0:dde1b9d6c9d6 289
haarkon 3:6ba43af28440 290 /**
haarkon 3:6ba43af28440 291 * \struct T_pixy2Intersection
haarkon 3:6ba43af28440 292 * \brief Structured type that match pixy2 intersection definition - used in Line frame (type 48/49) - message payload
haarkon 3:6ba43af28440 293 * \param pixX (Byte) : X axis coordinate of the intersection (in pixel, between 0 and 78)
haarkon 3:6ba43af28440 294 * \param pixY (Byte) : Y axis coordinate of the intersection (in pixel, between 0 and 51)
haarkon 3:6ba43af28440 295 * \param pixN (Byte) : Number of lines connected to the intersection (between 3 and 5)
haarkon 3:6ba43af28440 296 * \param pixReserved (Byte) : Not documented by manufacturer
haarkon 4:eed638064b42 297 * @note This structure is a feature of Line API, packed as a feature in a Line Frame, documented here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:protocol_reference
haarkon 3:6ba43af28440 298 * @note More info can be found here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_api
haarkon 3:6ba43af28440 299 * @note or here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_tracking
haarkon 3:6ba43af28440 300 */
haarkon 0:dde1b9d6c9d6 301 typedef struct {
haarkon 0:dde1b9d6c9d6 302 Byte pixX;
haarkon 0:dde1b9d6c9d6 303 Byte pixY;
haarkon 0:dde1b9d6c9d6 304 Byte pixN;
haarkon 0:dde1b9d6c9d6 305 Byte pixReserved;
haarkon 0:dde1b9d6c9d6 306 }T_pixy2Intersection;
haarkon 0:dde1b9d6c9d6 307
haarkon 3:6ba43af28440 308 /**
haarkon 3:6ba43af28440 309 * \struct T_pixy2BarCode
haarkon 3:6ba43af28440 310 * \brief Structured type that match pixy2 barcode definition - used in Line frame (type 48/49) - message payload
haarkon 3:6ba43af28440 311 * \param pixX (Byte) : X axis coordinate of the barcode (in pixel, between 0 and 78)
haarkon 3:6ba43af28440 312 * \param pixY (Byte) : Y axis coordinate of the barcode (in pixel, between 0 and 51)
haarkon 3:6ba43af28440 313 * \param pixFlag (Byte) : Flag to indicate if barcode met filtering constraint
haarkon 3:6ba43af28440 314 * \param pixCode (Byte) : Indicate the numeric value associated with the barcode (between 0 and 15)
haarkon 4:eed638064b42 315 * @note This structure is a feature of Line API, packed as a feature in a Line Frame, documented here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:protocol_reference
haarkon 3:6ba43af28440 316 * @note More info can be found here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_api
haarkon 3:6ba43af28440 317 * @note or here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_tracking
haarkon 3:6ba43af28440 318 */
haarkon 0:dde1b9d6c9d6 319 typedef struct {
haarkon 0:dde1b9d6c9d6 320 Byte pixX;
haarkon 0:dde1b9d6c9d6 321 Byte pixY;
haarkon 0:dde1b9d6c9d6 322 Byte pixFlag;
haarkon 0:dde1b9d6c9d6 323 Byte pixCode;
haarkon 0:dde1b9d6c9d6 324 }T_pixy2BarCode;
haarkon 0:dde1b9d6c9d6 325
haarkon 4:eed638064b42 326 /**
haarkon 4:eed638064b42 327 * \struct T_pixy2LineFeature
haarkon 4:eed638064b42 328 * \brief Structured type that match pixy2 feature header for Line API - used in Line frame (type 48/49) - message payload
haarkon 4:eed638064b42 329 * \param pixType (Byte) : Type of the feature (can be 1 -> Vector, 2 -> Intersection or 4 -> Barcode)
haarkon 4:eed638064b42 330 * \param pixLength (Byte) : Number of Bytes for this feature
haarkon 4:eed638064b42 331 * @note This structure is the header of features of Line API in Line Frames. Ther can be up to 4 features in a frame. Documented here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:protocol_reference
haarkon 4:eed638064b42 332 * @note More info can be found here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_api
haarkon 4:eed638064b42 333 * @note or here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:line_tracking
haarkon 4:eed638064b42 334 */
haarkon 0:dde1b9d6c9d6 335 typedef struct {
haarkon 0:dde1b9d6c9d6 336 Byte pixType;
haarkon 0:dde1b9d6c9d6 337 Byte pixLength;
haarkon 0:dde1b9d6c9d6 338 }T_pixy2LineFeature;
haarkon 0:dde1b9d6c9d6 339
haarkon 4:eed638064b42 340 /**
haarkon 4:eed638064b42 341 * \struct T_pixy2Pixel
haarkon 4:eed638064b42 342 * \brief Structured type that match pixy2 video API - used in Video frame (type 112/1) - message payload
haarkon 4:eed638064b42 343 * \param pixBlue (Byte) : Blue RGB value of the average blue component of the 5x5 pixels square centered on X param passes to the function (value between 0 and 255)
haarkon 4:eed638064b42 344 * \param pixGreen (Byte) : Green RGB value of the average blue component of the 5x5 pixels square centered on X param passes to the function (value between 0 and 255)
haarkon 4:eed638064b42 345 * \param pixRed (Byte) : Red RGB value of the average blue component of the 5x5 pixels square centered on X param passes to the function (value between 0 and 255)
haarkon 4:eed638064b42 346 * @note More info can be found here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:video_api
haarkon 4:eed638064b42 347 * @note or here : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:video
haarkon 4:eed638064b42 348 */
haarkon 0:dde1b9d6c9d6 349 typedef struct {
haarkon 0:dde1b9d6c9d6 350 Byte pixBlue;
haarkon 0:dde1b9d6c9d6 351 Byte pixGreen;
haarkon 0:dde1b9d6c9d6 352 Byte pixRed;
haarkon 0:dde1b9d6c9d6 353 }T_pixy2Pixel;
haarkon 0:dde1b9d6c9d6 354
haarkon 0:dde1b9d6c9d6 355 /**
haarkon 0:dde1b9d6c9d6 356 * Pixy2 : CMU CAM 5 - Smart camera
haarkon 0:dde1b9d6c9d6 357 * More informations at http://www.pixycam.com/
haarkon 0:dde1b9d6c9d6 358 */
haarkon 0:dde1b9d6c9d6 359 class PIXY2 {
haarkon 0:dde1b9d6c9d6 360
haarkon 0:dde1b9d6c9d6 361 protected :
haarkon 0:dde1b9d6c9d6 362
haarkon 0:dde1b9d6c9d6 363 Serial* _Pixy2;
haarkon 0:dde1b9d6c9d6 364
haarkon 0:dde1b9d6c9d6 365 public :
haarkon 0:dde1b9d6c9d6 366 /**
haarkon 0:dde1b9d6c9d6 367 * Constructor of pixy2 object.
haarkon 0:dde1b9d6c9d6 368 *
haarkon 0:dde1b9d6c9d6 369 * @param tx : the Mbed pin used as TX
haarkon 0:dde1b9d6c9d6 370 * @param rx : the Mbed pin used as RX
haarkon 0:dde1b9d6c9d6 371 * @param debit : the bitrate of the serial (max value is 230400 b/s)
haarkon 0:dde1b9d6c9d6 372 */
haarkon 0:dde1b9d6c9d6 373 PIXY2(PinName tx, PinName rx, int debit = 230400);
haarkon 0:dde1b9d6c9d6 374
haarkon 0:dde1b9d6c9d6 375 // Fonctions publiques
haarkon 3:6ba43af28440 376
haarkon 3:6ba43af28440 377 /**
haarkon 5:28e47187bb5c 378 * Queries and receives the firmware and hardware version of Pixy2, which is put in the version member variable.
haarkon 5:28e47187bb5c 379 * @note Frame Documentation : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:porting_guide
haarkon 5:28e47187bb5c 380 * @note Function Documentation : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:general_api
haarkon 5:28e47187bb5c 381 * @param version (T_Pixy2Version - passed by reference) : pointer to the version data structure
haarkon 6:e9e3aa6f1a55 382 * @return Pixy2ErrorCode : error code.
haarkon 3:6ba43af28440 383 */
haarkon 6:e9e3aa6f1a55 384 Pixy2ErrorCode pixy2_getVersion (T_Pixy2Version *version);
haarkon 4:eed638064b42 385
haarkon 4:eed638064b42 386 /**
haarkon 5:28e47187bb5c 387 * Gets the width and height of the frames used by the current program.
haarkon 5:28e47187bb5c 388 * @note After calling this function, the width and height can be found in the frameWidth and frameHeight member variables.
haarkon 5:28e47187bb5c 389 * @note Frame Documentation : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:porting_guide
haarkon 5:28e47187bb5c 390 * @note Function Documentation : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:general_api
haarkon 5:28e47187bb5c 391 * @param resolution (T_Pixy2Resolution - passed by reference) : pointer to the resolution data structure
haarkon 6:e9e3aa6f1a55 392 * @return Pixy2ErrorCode : error code.
haarkon 4:eed638064b42 393 */
haarkon 6:e9e3aa6f1a55 394 Pixy2ErrorCode pixy2_getResolution (T_Pixy2Resolution *resolution);
haarkon 5:28e47187bb5c 395
haarkon 5:28e47187bb5c 396 /**
haarkon 5:28e47187bb5c 397 * Sets the relative exposure level of Pixy2's image sensor. Higher values result in a brighter (more exposed) image.
haarkon 5:28e47187bb5c 398 * @note Frame Documentation : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:porting_guide
haarkon 5:28e47187bb5c 399 * @note Function Documentation : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:general_api
haarkon 5:28e47187bb5c 400 * @param brightness (Byte - passed by value) : brightness level
haarkon 6:e9e3aa6f1a55 401 * @return Pixy2ErrorCode : error code.
haarkon 6:e9e3aa6f1a55 402 */
haarkon 6:e9e3aa6f1a55 403 Pixy2ErrorCode pixy2_setCameraBrightness (Byte brightness);
haarkon 6:e9e3aa6f1a55 404
haarkon 6:e9e3aa6f1a55 405 /**
haarkon 6:e9e3aa6f1a55 406 * sets the servo positions of servos plugged into Pixy2's two RC servo connectors.
haarkon 6:e9e3aa6f1a55 407 * @note Frame Documentation : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:porting_guide
haarkon 6:e9e3aa6f1a55 408 * @note Function Documentation : https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:general_api
haarkon 6:e9e3aa6f1a55 409 * @param brightness (Byte - passed by value) : brightness level
haarkon 6:e9e3aa6f1a55 410 * @return Pixy2ErrorCode : error code.
haarkon 5:28e47187bb5c 411 */
haarkon 6:e9e3aa6f1a55 412 Pixy2ErrorCode pixy2_setServos (Word s0, Word s1);
haarkon 6:e9e3aa6f1a55 413 Pixy2ErrorCode pixy2_setLED (Byte red, Byte green, Byte blue);
haarkon 6:e9e3aa6f1a55 414 Pixy2ErrorCode pixy2_setLamp (Byte upper, Byte lower);
haarkon 6:e9e3aa6f1a55 415 Pixy2ErrorCode pixy2_getFPS (T_pixy2returnCode *framerate);
haarkon 6:e9e3aa6f1a55 416 Pixy2ErrorCode pixy2_getBlocks (Byte sigmap, Byte maxBloc);
haarkon 6:e9e3aa6f1a55 417 Pixy2ErrorCode pixy2_getMainFeature (Byte type, Byte features);
haarkon 6:e9e3aa6f1a55 418 Pixy2ErrorCode pixy2_getAllFeature (Byte features);
haarkon 6:e9e3aa6f1a55 419 Pixy2ErrorCode pixy2_setMode (Byte mode);
haarkon 6:e9e3aa6f1a55 420 Pixy2ErrorCode pixy2_setNexTurn (Word angle);
haarkon 6:e9e3aa6f1a55 421 Pixy2ErrorCode pixy2_setDefaultTurn (Word angle);
haarkon 6:e9e3aa6f1a55 422 Pixy2ErrorCode pixy2_setVector (Byte vectorIndex);
haarkon 6:e9e3aa6f1a55 423 Pixy2ErrorCode pixy2_ReverseVector (void);
haarkon 6:e9e3aa6f1a55 424 Pixy2ErrorCode pixy2_getRGB (Word x, Word y, Byte saturate, T_pixy2Pixel *pixel);
haarkon 0:dde1b9d6c9d6 425
haarkon 0:dde1b9d6c9d6 426 // Variables globales Publiques
haarkon 0:dde1b9d6c9d6 427 Byte Pixy2_numBlocks;
haarkon 0:dde1b9d6c9d6 428 T_pixy2Bloc Pixy2_blocks[];
haarkon 0:dde1b9d6c9d6 429 Byte Pixy2_numVectors;
haarkon 0:dde1b9d6c9d6 430 T_Pixy2Vector Pixy2_vectors[];
haarkon 0:dde1b9d6c9d6 431 Byte Pixy2_numIntersections;
haarkon 0:dde1b9d6c9d6 432 T_pixy2Intersection Pixy2_intersections[];
haarkon 0:dde1b9d6c9d6 433 T_Pixy2InterLine Pixy2_intLines[];
haarkon 0:dde1b9d6c9d6 434 Byte Pixy2_numBarcodes;
haarkon 0:dde1b9d6c9d6 435 T_pixy2BarCode Pixy2_barcodes[];
haarkon 0:dde1b9d6c9d6 436
haarkon 0:dde1b9d6c9d6 437 private :
haarkon 0:dde1b9d6c9d6 438 // Variables globales Privées
haarkon 0:dde1b9d6c9d6 439 T_Pixy2State etat;
haarkon 0:dde1b9d6c9d6 440 Byte* Pixy2_buffer;
haarkon 0:dde1b9d6c9d6 441 Byte wPointer, rPointer, dataSize;
haarkon 0:dde1b9d6c9d6 442 Byte frameContainChecksum;
haarkon 0:dde1b9d6c9d6 443
haarkon 0:dde1b9d6c9d6 444 // Fonctions privées
haarkon 6:e9e3aa6f1a55 445 Pixy2ErrorCode pixy2_sndGetVersion (void);
haarkon 6:e9e3aa6f1a55 446 Pixy2ErrorCode pixy2_sndGetResolution (void);
haarkon 6:e9e3aa6f1a55 447 Pixy2ErrorCode pixy2_sndSetCameraBrightness (Byte brightness);
haarkon 6:e9e3aa6f1a55 448 Pixy2ErrorCode pixy2_sndSetServo (Word s0, Word s1);
haarkon 6:e9e3aa6f1a55 449 Pixy2ErrorCode pixy2_sndSetLED (Byte red, Byte green, Byte blue);
haarkon 6:e9e3aa6f1a55 450 Pixy2ErrorCode pixy2_sndSetLamp (Byte upper, Byte lower);
haarkon 6:e9e3aa6f1a55 451 Pixy2ErrorCode pixy2_sndGetFPS (void);
haarkon 6:e9e3aa6f1a55 452 Pixy2ErrorCode pixy2_sndGetBlocks (Byte sigmap, Byte maxBloc);
haarkon 6:e9e3aa6f1a55 453 Pixy2ErrorCode pixy2_sndGetMainFeature (Byte type, Byte feature);
haarkon 6:e9e3aa6f1a55 454 Pixy2ErrorCode pixy2_sndSetMode (Byte mode);
haarkon 6:e9e3aa6f1a55 455 Pixy2ErrorCode pixy2_sndSetNexTurn (Word angle);
haarkon 6:e9e3aa6f1a55 456 Pixy2ErrorCode pixy2_sndSetDefaultTurn (Word angle);
haarkon 6:e9e3aa6f1a55 457 Pixy2ErrorCode pixy2_sndSetVector (Byte vectorIndex);
haarkon 6:e9e3aa6f1a55 458 Pixy2ErrorCode pixy2_sndReverseVector (void);
haarkon 6:e9e3aa6f1a55 459 Pixy2ErrorCode pixy2_sndGetRGB (Word x, Word y, Byte saturate);
haarkon 0:dde1b9d6c9d6 460
haarkon 0:dde1b9d6c9d6 461 void pixy2_getByte ();
haarkon 6:e9e3aa6f1a55 462 Pixy2ErrorCode pixy2_validateChecksum (Byte* tab);
haarkon 0:dde1b9d6c9d6 463 };
haarkon 0:dde1b9d6c9d6 464
haarkon 0:dde1b9d6c9d6 465 #endif