![](/media/cache/profiles/altb_JGUSAOG.jpg.50x50_q85.jpg)
SPI read Pixi Cam V1
Pixy.h@3:66df7d295245, 2016-03-14 (annotated)
- Committer:
- swilkins8
- Date:
- Mon Mar 14 18:53:43 2016 +0000
- Revision:
- 3:66df7d295245
- Parent:
- 2:fa582d9d91b5
- Child:
- 4:478d4d9193a1
Completed comments/documentation for all classes
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:66df7d295245 | 10 | /** The SPI Pixy interface for the Pixy camera |
swilkins8 | 1:6587541f3aa8 | 11 | * @code |
swilkins8 | 1:6587541f3aa8 | 12 | * #include "Pixy.h" |
swilkins8 | 1:6587541f3aa8 | 13 | * Serial serial(USBTX, USBRX); |
swilkins8 | 1:6587541f3aa8 | 14 | * SPI spi(p5, p6, p7); |
swilkins8 | 1:6587541f3aa8 | 15 | * PixySPI pixy(&spi, &serial); |
swilkins8 | 3:66df7d295245 | 16 | * |
swilkins8 | 3:66df7d295245 | 17 | * int main() { |
swilkins8 | 3:66df7d295245 | 18 | * runPanTiltDemo(); |
swilkins8 | 3:66df7d295245 | 19 | * } |
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 | 3:66df7d295245 | 26 | /** Constructor for the PixySPI class |
swilkins8 | 3:66df7d295245 | 27 | * Instantiates a Pixy object that communicates via an SPI connection |
swilkins8 | 3:66df7d295245 | 28 | * @param SPI* spi the pointer to the SPI connection |
swilkins8 | 3:66df7d295245 | 29 | * @param Serial* serialOut the optional serial output pointer to print out Pixy camera data |
swilkins8 | 3:66df7d295245 | 30 | * @param int arg an optional integer argument used for custom implementations of the Pixy library |
swilkins8 | 3:66df7d295245 | 31 | */ |
swilkins8 | 0:ef0e3c67dc5b | 32 | PixySPI(SPI* spi, Serial* serialOut = NULL, uint16_t arg = PIXY_DEFAULT_ARGVAL) : |
swilkins8 | 0:ef0e3c67dc5b | 33 | TPixy<PixyInterfaceSPI>((new PixyInterfaceSPI(spi)), serialOut, arg) {} |
swilkins8 | 0:ef0e3c67dc5b | 34 | }; |
swilkins8 | 0:ef0e3c67dc5b | 35 | |
swilkins8 | 0:ef0e3c67dc5b | 36 | |
swilkins8 | 0:ef0e3c67dc5b | 37 | /* Not Implemented */ |
swilkins8 | 0:ef0e3c67dc5b | 38 | /* |
swilkins8 | 0:ef0e3c67dc5b | 39 | class PixyI2C : TPixy<PixyInterfaceI2C> { |
swilkins8 | 0:ef0e3c67dc5b | 40 | public: |
swilkins8 | 0:ef0e3c67dc5b | 41 | PixyI2C(I2C* i2c, Serial* serialOut = NULL, uint16_t arg = PIXY_DEFAULT_ARGVAL) : i2cInterface(i2c), TPixy<PixyInterfaceI2C>(i2cInterface, serialOut, arg); |
swilkins8 | 0:ef0e3c67dc5b | 42 | private: |
swilkins8 | 0:ef0e3c67dc5b | 43 | TPixyInterface<I2C>* i2cInterface; |
swilkins8 | 0:ef0e3c67dc5b | 44 | }; |
swilkins8 | 0:ef0e3c67dc5b | 45 | */ |
swilkins8 | 0:ef0e3c67dc5b | 46 | |
swilkins8 | 0:ef0e3c67dc5b | 47 | class ServoLoop |
swilkins8 | 0:ef0e3c67dc5b | 48 | { |
swilkins8 | 0:ef0e3c67dc5b | 49 | public: |
swilkins8 | 1:6587541f3aa8 | 50 | /** Constructor for a ServoLoop object |
swilkins8 | 1:6587541f3aa8 | 51 | * Creates a ServoLoop object for easy control of the Pixy servos |
swilkins8 | 1:6587541f3aa8 | 52 | * @param pgain the proportional gain for the control |
swilkins8 | 1:6587541f3aa8 | 53 | * @param dgain the derivative gain for the control |
swilkins8 | 1:6587541f3aa8 | 54 | */ |
swilkins8 | 0:ef0e3c67dc5b | 55 | ServoLoop(int32_t pgain, int32_t dgain); |
swilkins8 | 1:6587541f3aa8 | 56 | /** Update method for a ServoLoop object |
swilkins8 | 1:6587541f3aa8 | 57 | * Updates the m_pos variable for setting a Pixy servo |
swilkins8 | 1:6587541f3aa8 | 58 | * @param error the error between the center of the camera and the position of the tracking color |
swilkins8 | 1:6587541f3aa8 | 59 | */ |
swilkins8 | 0:ef0e3c67dc5b | 60 | void update(int32_t error); |
swilkins8 | 0:ef0e3c67dc5b | 61 | |
swilkins8 | 0:ef0e3c67dc5b | 62 | int32_t m_pos; |
swilkins8 | 0:ef0e3c67dc5b | 63 | int32_t m_prevError; |
swilkins8 | 0:ef0e3c67dc5b | 64 | int32_t m_pgain; |
swilkins8 | 0:ef0e3c67dc5b | 65 | int32_t m_dgain; |
swilkins8 | 0:ef0e3c67dc5b | 66 | }; |
swilkins8 | 0:ef0e3c67dc5b | 67 | |
swilkins8 | 1:6587541f3aa8 | 68 | /** Basic Pan/Tilt Demo code |
swilkins8 | 1:6587541f3aa8 | 69 | * Runs the Pan/Tilt demo code |
swilkins8 | 1:6587541f3aa8 | 70 | * @param pixy the pointer to the pixy object to run the demo on |
swilkins8 | 1:6587541f3aa8 | 71 | * @param serial the optional serial pointer to display output to |
swilkins8 | 1:6587541f3aa8 | 72 | */ |
swilkins8 | 1:6587541f3aa8 | 73 | template <class TPixyInterface> void runPanTiltDemo(TPixy<TPixyInterface>* pixy, Serial* serial = NULL) |
swilkins8 | 0:ef0e3c67dc5b | 74 | { |
swilkins8 | 0:ef0e3c67dc5b | 75 | ServoLoop panLoop(-150, -300); |
swilkins8 | 0:ef0e3c67dc5b | 76 | ServoLoop tiltLoop(200, 250); |
swilkins8 | 0:ef0e3c67dc5b | 77 | if (serial != NULL) { |
swilkins8 | 0:ef0e3c67dc5b | 78 | serial->printf("Starting...\n"); |
swilkins8 | 0:ef0e3c67dc5b | 79 | } |
swilkins8 | 0:ef0e3c67dc5b | 80 | static int i = 0; |
swilkins8 | 0:ef0e3c67dc5b | 81 | int j; |
swilkins8 | 0:ef0e3c67dc5b | 82 | uint16_t blocks; |
swilkins8 | 0:ef0e3c67dc5b | 83 | int32_t panError, tiltError; |
swilkins8 | 0:ef0e3c67dc5b | 84 | pixy->init(); |
swilkins8 | 0:ef0e3c67dc5b | 85 | while(true) { |
swilkins8 | 0:ef0e3c67dc5b | 86 | blocks = pixy->getBlocks(); |
swilkins8 | 0:ef0e3c67dc5b | 87 | if (blocks) { |
swilkins8 | 0:ef0e3c67dc5b | 88 | panError = X_CENTER - pixy->blocks[0].x; |
swilkins8 | 0:ef0e3c67dc5b | 89 | tiltError = pixy->blocks[0].y - Y_CENTER; |
swilkins8 | 0:ef0e3c67dc5b | 90 | |
swilkins8 | 0:ef0e3c67dc5b | 91 | panLoop.update(panError); |
swilkins8 | 0:ef0e3c67dc5b | 92 | tiltLoop.update(tiltError); |
swilkins8 | 0:ef0e3c67dc5b | 93 | |
swilkins8 | 0:ef0e3c67dc5b | 94 | pixy->setServos(panLoop.m_pos, tiltLoop.m_pos); |
swilkins8 | 0:ef0e3c67dc5b | 95 | i++; |
swilkins8 | 0:ef0e3c67dc5b | 96 | |
swilkins8 | 0:ef0e3c67dc5b | 97 | if (i % 50 == 0 && serial != NULL) { |
swilkins8 | 0:ef0e3c67dc5b | 98 | serial->printf("Detected %d:\n", blocks); |
swilkins8 | 0:ef0e3c67dc5b | 99 | //toPC.printf(buf); |
swilkins8 | 0:ef0e3c67dc5b | 100 | for (j = 0; j < blocks; j++) { |
swilkins8 | 0:ef0e3c67dc5b | 101 | serial->printf(" block %d: ", j); |
swilkins8 | 0:ef0e3c67dc5b | 102 | pixy->printBlock(pixy->blocks[j]); |
swilkins8 | 0:ef0e3c67dc5b | 103 | } |
swilkins8 | 0:ef0e3c67dc5b | 104 | } |
swilkins8 | 0:ef0e3c67dc5b | 105 | } |
swilkins8 | 0:ef0e3c67dc5b | 106 | } |
swilkins8 | 0:ef0e3c67dc5b | 107 | } |
swilkins8 | 0:ef0e3c67dc5b | 108 | #endif |