Mechatronics Robotics / Mbed 2 deprecated BrobotV1

Dependencies:   mbed UniServ

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Pixy.cpp Source File

Pixy.cpp

00001 #include "Pixy.h"
00002  
00003 Pixy::Pixy(Serial& _cam) : cam(_cam), detects(0)
00004 {
00005     cam.baud( 230400 );
00006     //cam.baud( 460800 );
00007     cam.attach(this, &Pixy::rxCallback, Serial::RxIrq);
00008 }
00009  
00010  
00011 // This function is called when a character goes into the RX buffer.
00012 void Pixy::rxCallback()
00013 {
00014     static const int buffersize = 256;
00015     static int startPoint = 0;
00016     static uint8_t buffer[buffersize] = {};
00017     static bool startFound = false;
00018     static int ii = 1;
00019  
00020     while( cam.readable() ) {
00021         buffer[ii] = cam.getc();
00022         if( buffer[ii-1] == 85  && (buffer[ii] == 170 )  ) {
00023             startPoint = ii-1;
00024  
00025             //check if detection was on the edge of buffer. Skip package if so.
00026             if( ii<(buffersize-14))
00027                 startFound = true;
00028             else
00029                 ii = 1;
00030  
00031             detects++;
00032         }
00033         ++ii;
00034  
00035         //reset ii
00036         if( ii>=(buffersize-1))
00037             ii = 1;
00038     }
00039  
00040     //start not found, reset ii to 1
00041     if( !startFound && ii >= 3 || ii >= (buffersize-1)) {
00042         ii = 1;
00043         return;
00044     }
00045  
00046     //start is found but not enough bytes received - return
00047     if( (ii-startPoint) <= 13 )
00048         return;
00049  
00050     //copy memory to pixy struct
00051     memcpy( &pixy, buffer + startPoint+2, 12);
00052  
00053     //reset variables
00054     startFound = false;
00055     ii = 1;
00056 }
00057  
00058 /**
00059 returns the width of the detected object
00060 **/
00061 int Pixy::getWidth(  )
00062 {
00063     return pixy.width;
00064 }
00065  
00066 /**
00067 returns the height of the detected object
00068 **/
00069 int Pixy::getHeight(  )
00070 {
00071     return pixy.height;
00072 }
00073  
00074 /**
00075 returns the Y coordinates in respect to the pixy recorded image
00076 **/
00077 int Pixy::getY( )
00078 {
00079     return pixy.y;
00080 }
00081  
00082 /**
00083 returns the Y coordinates in respect to the pixy recorded image
00084 **/
00085 int Pixy::getX(  )
00086 {
00087     return pixy.x;
00088 }
00089  
00090  
00091 /**
00092 returns the signature of the detected object
00093 **/
00094 int Pixy::getSignature()
00095 {
00096     return pixy.signature;
00097 }
00098  
00099 /**
00100 returns 1 if an object has detectd
00101 **/
00102 bool Pixy::objectDetected()
00103 {
00104     bool ret = false;
00105     static int oldDetection = 0;
00106     ret = (detects != oldDetection);
00107     oldDetection = detects;
00108     return ret;
00109 }