<< return to Pixycam.com

Pixy with PIC24 UART

Hello,

I am attempting to use the Pixy with a PIC24 Microchip controller in order to do some simple object position recording. I’m having a hard time because the example codes that are written require headers and such which then require more headers within those and so on.

I’ve been looking to find a basic initialization that I need to send to the pixy (if there is one at all) in order to get the block bytes 0-15. I was going to use the UART example of an Arduino to see if it would provide me the info I needed through the logic analyzer on the oscilloscope but the Arduino UART example wouldn’t load correctly. One of the include files had serial1 undefined in it.

I did see the example code in the porting guide and saw a function called getBlock:

uint16_t getBlocks(uint16_t maxBlocks)
{
uint8_t i;
uint16_t w, blockCount, checksum, sum;
Block *block;

if (!g_skipStart)
{
if (getStart()==0)
return 0;
}
else
g_skipStart = 0;

for(blockCount=0; blockCount<maxBlocks && blockCount<PIXY_ARRAYSIZE;)
{
checksum = getWord();
if (checksum==PIXY_START_WORD) // we’ve reached the beginning of the next frame
{
g_skipStart = 1;
g_blockType = NORMAL_BLOCK;
return blockCount;
}
else if (checksum==PIXY_START_WORD_CC)
{
g_skipStart = 1;
g_blockType = CC_BLOCK;
return blockCount;
}
else if (checksum==0)
return blockCount;

block = g_blocks + blockCount;

for (i=0, sum=0; i<sizeof(Block)/sizeof(uint16_t); i++)
{
  if (g_blockType==NORMAL_BLOCK && i>=5) // no angle for normal block
  {
    block->angle = 0;
    break;
  }
  w = getWord();
  sum += w;
  *((uint16_t *)block + i) = w;
}

// check checksum
if (checksum==sum)
  blockCount++;
else
  printf("checksum error!\n");

w = getWord();
if (w==PIXY_START_WORD)
  g_blockType = NORMAL_BLOCK;
else if (w==PIXY_START_WORD_CC)
  g_blockType = CC_BLOCK;
else
  return blockCount;

}
}

Unfortunately coding is not my strongest suit and so I haven’t quite determined what exactly is going on in this function. Does anyone know if this is what I need to be translating into my IDE in order to get the blocks for the recognized object?

Hello Erik,
You are probably getting the Serial1 error because your Arduino doesn’t have an extra UART. Which Arduino were you using?

Have you seen the porting guide? It has some good description of the protocol and how to talk to Pixy.

http://cmucam.org/projects/cmucam5/wiki/Porting_Guide

Edward

So I found some new info that’s gotten me going a bit further.

First of all, I made the stupid mistake of not changing the “DATA OUT PORT” in pixymon so that was what was keeping me from receiving any data on my tx line with UART.

So after I changed that I seem to be receiving some constant data stream to the logic analyzer on my oscope. Now I’m just trying to utilize the example code shown on the porting page and I came across an issue that I wanted to point out.

extern uint8_t getByte(void);
extern int sendByte(uint8_t byte);

uint16_t getWord(void)
{
// this routine assumes little endian
uint16_t w;
uint8_t c;
c = readByte();
w = readByte();
w <<= 8;
w |= c;
return w;
}

If you look you’ll notice that in the getWord() function there is a funciton called readByte(). This is the only time that readByte() is called. I assumed it’s an error and it’s supposed to be the same as getByte(). Which brings me to another problem I ran into. simply having extern did not make getByte() or sendByte() usable for my IDE (MPLABX xc16). I had to delete the inttypes.h header because it started a string of needing headers for headers. All I had to do was replace them with my IDE’s UART read and write functions that were created and it seems to work just fine.

Let me know if I’m wrong here because I am trying to change this code for how I need to use it and coding is definitely not my strongest suit.

Thank you

Hello Erik,
Wow! You’re the first person to point this out. I just fixed it on the wiki. It was pasted from working code, but there were some final edits for consistency. Thanks for the heads-up!

getByte() and sendByte() are functions that you need to implement for your platform. (we can’t implement.)

Edward