A library for interfacing with the Pixy color recognition camera

Dependents:   PixyCamera MbedOS_Robot_Team ManualControlFinal PlayBack ... more

Committer:
swilkins8
Date:
Mon Mar 14 17:42:33 2016 +0000
Revision:
1:6587541f3aa8
Parent:
0:ef0e3c67dc5b
Child:
2:fa582d9d91b5
Added comments+documentation to the Pixy header file; Removed unnecessary TPixy.cpp file

Who changed what in which revision?

UserRevisionLine numberNew contents of line
swilkins8 0:ef0e3c67dc5b 1 #ifndef _PIXY_H
swilkins8 0:ef0e3c67dc5b 2 #define _PIXY_H
swilkins8 0:ef0e3c67dc5b 3
swilkins8 0:ef0e3c67dc5b 4 #include "TPixy.h"
swilkins8 0:ef0e3c67dc5b 5 #include "TPixyInterface.h"
swilkins8 0:ef0e3c67dc5b 6
swilkins8 0:ef0e3c67dc5b 7 #define X_CENTER ((PIXY_MAX_X-PIXY_MIN_X)/2)
swilkins8 0:ef0e3c67dc5b 8 #define Y_CENTER ((PIXY_MAX_Y-PIXY_MIN_Y)/2)
swilkins8 0:ef0e3c67dc5b 9
swilkins8 1:6587541f3aa8 10 /** The SPI interface for the Pixy camera
swilkins8 1:6587541f3aa8 11 * Used for instantiating a Pixy object that interfaces via an SPI connection
swilkins8 1:6587541f3aa8 12 * @param spi the pointer to the SPI connection
swilkins8 1:6587541f3aa8 13 * @param serialOut the optional serial output pointer to print out Pixy camera data
swilkins8 1:6587541f3aa8 14 * @param arg an optional integer argument used for custom implementations of the Pixy library
swilkins8 1:6587541f3aa8 15 * @code
swilkins8 1:6587541f3aa8 16 * #include "Pixy.h"
swilkins8 1:6587541f3aa8 17 * Serial serial(USBTX, USBRX);
swilkins8 1:6587541f3aa8 18 * SPI spi(p5, p6, p7);
swilkins8 1:6587541f3aa8 19 * PixySPI pixy(&spi, &serial);
swilkins8 1:6587541f3aa8 20 * @endcode
swilkins8 1:6587541f3aa8 21 */
swilkins8 1:6587541f3aa8 22
swilkins8 0:ef0e3c67dc5b 23 class PixySPI : public TPixy<PixyInterfaceSPI>
swilkins8 0:ef0e3c67dc5b 24 {
swilkins8 0:ef0e3c67dc5b 25 public:
swilkins8 0:ef0e3c67dc5b 26 PixySPI(SPI* spi, Serial* serialOut = NULL, uint16_t arg = PIXY_DEFAULT_ARGVAL) :
swilkins8 0:ef0e3c67dc5b 27 TPixy<PixyInterfaceSPI>((new PixyInterfaceSPI(spi)), serialOut, arg) {}
swilkins8 0:ef0e3c67dc5b 28 };
swilkins8 0:ef0e3c67dc5b 29
swilkins8 0:ef0e3c67dc5b 30
swilkins8 0:ef0e3c67dc5b 31 /* Not Implemented */
swilkins8 0:ef0e3c67dc5b 32 /*
swilkins8 0:ef0e3c67dc5b 33 class PixyI2C : TPixy<PixyInterfaceI2C> {
swilkins8 0:ef0e3c67dc5b 34 public:
swilkins8 0:ef0e3c67dc5b 35 PixyI2C(I2C* i2c, Serial* serialOut = NULL, uint16_t arg = PIXY_DEFAULT_ARGVAL) : i2cInterface(i2c), TPixy<PixyInterfaceI2C>(i2cInterface, serialOut, arg);
swilkins8 0:ef0e3c67dc5b 36 private:
swilkins8 0:ef0e3c67dc5b 37 TPixyInterface<I2C>* i2cInterface;
swilkins8 0:ef0e3c67dc5b 38 };
swilkins8 0:ef0e3c67dc5b 39 */
swilkins8 0:ef0e3c67dc5b 40
swilkins8 0:ef0e3c67dc5b 41 class ServoLoop
swilkins8 0:ef0e3c67dc5b 42 {
swilkins8 0:ef0e3c67dc5b 43 public:
swilkins8 1:6587541f3aa8 44 /** Constructor for a ServoLoop object
swilkins8 1:6587541f3aa8 45 * Creates a ServoLoop object for easy control of the Pixy servos
swilkins8 1:6587541f3aa8 46 * @param pgain the proportional gain for the control
swilkins8 1:6587541f3aa8 47 * @param dgain the derivative gain for the control
swilkins8 1:6587541f3aa8 48 */
swilkins8 0:ef0e3c67dc5b 49 ServoLoop(int32_t pgain, int32_t dgain);
swilkins8 1:6587541f3aa8 50 /** Update method for a ServoLoop object
swilkins8 1:6587541f3aa8 51 * Updates the m_pos variable for setting a Pixy servo
swilkins8 1:6587541f3aa8 52 * @param error the error between the center of the camera and the position of the tracking color
swilkins8 1:6587541f3aa8 53 */
swilkins8 0:ef0e3c67dc5b 54 void update(int32_t error);
swilkins8 0:ef0e3c67dc5b 55
swilkins8 0:ef0e3c67dc5b 56 int32_t m_pos;
swilkins8 0:ef0e3c67dc5b 57 int32_t m_prevError;
swilkins8 0:ef0e3c67dc5b 58 int32_t m_pgain;
swilkins8 0:ef0e3c67dc5b 59 int32_t m_dgain;
swilkins8 0:ef0e3c67dc5b 60 };
swilkins8 0:ef0e3c67dc5b 61
swilkins8 1:6587541f3aa8 62 /** Basic Pan/Tilt Demo code
swilkins8 1:6587541f3aa8 63 * Runs the Pan/Tilt demo code
swilkins8 1:6587541f3aa8 64 * @param pixy the pointer to the pixy object to run the demo on
swilkins8 1:6587541f3aa8 65 * @param serial the optional serial pointer to display output to
swilkins8 1:6587541f3aa8 66 */
swilkins8 1:6587541f3aa8 67 template <class TPixyInterface> void runPanTiltDemo(TPixy<TPixyInterface>* pixy, Serial* serial = NULL)
swilkins8 0:ef0e3c67dc5b 68 {
swilkins8 0:ef0e3c67dc5b 69 ServoLoop panLoop(-150, -300);
swilkins8 0:ef0e3c67dc5b 70 ServoLoop tiltLoop(200, 250);
swilkins8 0:ef0e3c67dc5b 71 if (serial != NULL) {
swilkins8 0:ef0e3c67dc5b 72 serial->printf("Starting...\n");
swilkins8 0:ef0e3c67dc5b 73 }
swilkins8 0:ef0e3c67dc5b 74 static int i = 0;
swilkins8 0:ef0e3c67dc5b 75 int j;
swilkins8 0:ef0e3c67dc5b 76 uint16_t blocks;
swilkins8 0:ef0e3c67dc5b 77 int32_t panError, tiltError;
swilkins8 0:ef0e3c67dc5b 78 pixy->init();
swilkins8 0:ef0e3c67dc5b 79 while(true) {
swilkins8 0:ef0e3c67dc5b 80 blocks = pixy->getBlocks();
swilkins8 0:ef0e3c67dc5b 81 if (blocks) {
swilkins8 0:ef0e3c67dc5b 82 panError = X_CENTER - pixy->blocks[0].x;
swilkins8 0:ef0e3c67dc5b 83 tiltError = pixy->blocks[0].y - Y_CENTER;
swilkins8 0:ef0e3c67dc5b 84
swilkins8 0:ef0e3c67dc5b 85 panLoop.update(panError);
swilkins8 0:ef0e3c67dc5b 86 tiltLoop.update(tiltError);
swilkins8 0:ef0e3c67dc5b 87
swilkins8 0:ef0e3c67dc5b 88 pixy->setServos(panLoop.m_pos, tiltLoop.m_pos);
swilkins8 0:ef0e3c67dc5b 89 i++;
swilkins8 0:ef0e3c67dc5b 90
swilkins8 0:ef0e3c67dc5b 91 if (i % 50 == 0 && serial != NULL) {
swilkins8 0:ef0e3c67dc5b 92 serial->printf("Detected %d:\n", blocks);
swilkins8 0:ef0e3c67dc5b 93 //toPC.printf(buf);
swilkins8 0:ef0e3c67dc5b 94 for (j = 0; j < blocks; j++) {
swilkins8 0:ef0e3c67dc5b 95 serial->printf(" block %d: ", j);
swilkins8 0:ef0e3c67dc5b 96 pixy->printBlock(pixy->blocks[j]);
swilkins8 0:ef0e3c67dc5b 97 }
swilkins8 0:ef0e3c67dc5b 98 }
swilkins8 0:ef0e3c67dc5b 99 }
swilkins8 0:ef0e3c67dc5b 100 }
swilkins8 0:ef0e3c67dc5b 101 }
swilkins8 0:ef0e3c67dc5b 102 #endif