code for testing the pixy

Dependencies:   mbed

Committer:
huynh270
Date:
Tue May 30 09:56:04 2017 +0000
Revision:
0:214d306295fa
hello

Who changed what in which revision?

UserRevisionLine numberNew contents of line
huynh270 0:214d306295fa 1 #include "Pixy.h"
huynh270 0:214d306295fa 2
huynh270 0:214d306295fa 3 Pixy::Pixy(Serial& _cam) : cam(_cam), detects(0)
huynh270 0:214d306295fa 4 {
huynh270 0:214d306295fa 5 //cam.baud( 230400 );
huynh270 0:214d306295fa 6 cam.baud( 460800 );
huynh270 0:214d306295fa 7 cam.attach(this, &Pixy::rxCallback, Serial::RxIrq);
huynh270 0:214d306295fa 8 }
huynh270 0:214d306295fa 9
huynh270 0:214d306295fa 10
huynh270 0:214d306295fa 11 // This function is called when a character goes into the RX buffer.
huynh270 0:214d306295fa 12 void Pixy::rxCallback()
huynh270 0:214d306295fa 13 {
huynh270 0:214d306295fa 14 static const int buffersize = 256;
huynh270 0:214d306295fa 15 static int startPoint = 0;
huynh270 0:214d306295fa 16 static uint8_t buffer[buffersize] = {};
huynh270 0:214d306295fa 17 static bool startFound = false;
huynh270 0:214d306295fa 18 static int ii = 1;
huynh270 0:214d306295fa 19
huynh270 0:214d306295fa 20 while( cam.readable() ) {
huynh270 0:214d306295fa 21 buffer[ii] = cam.getc();
huynh270 0:214d306295fa 22 if( buffer[ii-1] == 85 && (buffer[ii] == 170 ) ) {
huynh270 0:214d306295fa 23 startPoint = ii-1;
huynh270 0:214d306295fa 24
huynh270 0:214d306295fa 25 //check if detection was on the edge of buffer. Skip package if so.
huynh270 0:214d306295fa 26 if( ii<(buffersize-14))
huynh270 0:214d306295fa 27 startFound = true;
huynh270 0:214d306295fa 28 else
huynh270 0:214d306295fa 29 ii = 1;
huynh270 0:214d306295fa 30
huynh270 0:214d306295fa 31 detects++;
huynh270 0:214d306295fa 32 }
huynh270 0:214d306295fa 33 ++ii;
huynh270 0:214d306295fa 34
huynh270 0:214d306295fa 35 //reset ii
huynh270 0:214d306295fa 36 if( ii>=(buffersize-1))
huynh270 0:214d306295fa 37 ii = 1;
huynh270 0:214d306295fa 38 }
huynh270 0:214d306295fa 39
huynh270 0:214d306295fa 40 //start not found, reset ii to 1
huynh270 0:214d306295fa 41 if( !startFound && ii >= 3 || ii >= (buffersize-1)) {
huynh270 0:214d306295fa 42 ii = 1;
huynh270 0:214d306295fa 43 return;
huynh270 0:214d306295fa 44 }
huynh270 0:214d306295fa 45
huynh270 0:214d306295fa 46 //start is found but not enough bytes received - return
huynh270 0:214d306295fa 47 if( (ii-startPoint) <= 13 )
huynh270 0:214d306295fa 48 return;
huynh270 0:214d306295fa 49
huynh270 0:214d306295fa 50 //copy memory to pixy struct
huynh270 0:214d306295fa 51 memcpy( &pixy, buffer + startPoint+2, 12);
huynh270 0:214d306295fa 52
huynh270 0:214d306295fa 53 //reset variables
huynh270 0:214d306295fa 54 startFound = false;
huynh270 0:214d306295fa 55 ii = 1;
huynh270 0:214d306295fa 56 }
huynh270 0:214d306295fa 57
huynh270 0:214d306295fa 58 /**
huynh270 0:214d306295fa 59 returns the width of the detected object
huynh270 0:214d306295fa 60 **/
huynh270 0:214d306295fa 61 int Pixy::getWidth( )
huynh270 0:214d306295fa 62 {
huynh270 0:214d306295fa 63 return pixy.width;
huynh270 0:214d306295fa 64 }
huynh270 0:214d306295fa 65
huynh270 0:214d306295fa 66 /**
huynh270 0:214d306295fa 67 returns the height of the detected object
huynh270 0:214d306295fa 68 **/
huynh270 0:214d306295fa 69 int Pixy::getHeight( )
huynh270 0:214d306295fa 70 {
huynh270 0:214d306295fa 71 return pixy.height;
huynh270 0:214d306295fa 72 }
huynh270 0:214d306295fa 73
huynh270 0:214d306295fa 74 /**
huynh270 0:214d306295fa 75 returns the Y coordinates in respect to the pixy recorded image
huynh270 0:214d306295fa 76 **/
huynh270 0:214d306295fa 77 int Pixy::getY( )
huynh270 0:214d306295fa 78 {
huynh270 0:214d306295fa 79 return pixy.y;
huynh270 0:214d306295fa 80 }
huynh270 0:214d306295fa 81
huynh270 0:214d306295fa 82 /**
huynh270 0:214d306295fa 83 returns the Y coordinates in respect to the pixy recorded image
huynh270 0:214d306295fa 84 **/
huynh270 0:214d306295fa 85 int Pixy::getX( )
huynh270 0:214d306295fa 86 {
huynh270 0:214d306295fa 87 return pixy.x;
huynh270 0:214d306295fa 88 }
huynh270 0:214d306295fa 89
huynh270 0:214d306295fa 90
huynh270 0:214d306295fa 91 /**
huynh270 0:214d306295fa 92 returns the signature of the detected object
huynh270 0:214d306295fa 93 **/
huynh270 0:214d306295fa 94 int Pixy::getSignature()
huynh270 0:214d306295fa 95 {
huynh270 0:214d306295fa 96 return pixy.signature;
huynh270 0:214d306295fa 97 }
huynh270 0:214d306295fa 98
huynh270 0:214d306295fa 99 /**
huynh270 0:214d306295fa 100 returns 1 if an object has detectd
huynh270 0:214d306295fa 101 **/
huynh270 0:214d306295fa 102 bool Pixy::objectDetected()
huynh270 0:214d306295fa 103 {
huynh270 0:214d306295fa 104 bool ret = false;
huynh270 0:214d306295fa 105 static int oldDetection = 0;
huynh270 0:214d306295fa 106 ret = (detects != oldDetection);
huynh270 0:214d306295fa 107 oldDetection = detects;
huynh270 0:214d306295fa 108 return ret;
huynh270 0:214d306295fa 109 }