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