Pixy Camera

Hi,

I bought me a ‘CMUcam5 Pixy’. I understand the most of the code in the examples, but I want to use the value of the angle the camera brings to me for a driving robot. So I need it as an Integer value, right?

Here’s the code, that displays the value in Serial monitor:

#include <SPI.h>  
#include <Pixy.h>

Pixy pixy;

void setup()
{
  Serial.begin(9600);
  Serial.print("Starting...\n");

  pixy.init();
}

void loop()
{ 
  int i;
  uint16_t blocks;
  char buf[32]; 
  
  blocks = pixy.getBlocks();
  
  if (blocks)
  {
    sprintf(buf, "%d", pixy.blocks[i].angle);
    Serial.println(buf);
    
    delay(200);
  }  
}

And if you need it that’s the Pixy.h libary:

//http://cmucam.org/projects/cmucam5/wiki/Hooking_up_Pixy_to_a_Microcontroller_(like_an_Arduino)
//

#ifndef PIXY_H
#define PIXY_H

#include "TPixy.h"
#include "SPI.h"


#define PIXY_SYNC_BYTE              0x5a
#define PIXY_SYNC_BYTE_DATA         0x5b
#define PIXY_BUF_SIZE               16

template <class BufType> struct CircularQ
{
  CircularQ()
  {
    len = 0;
	writeIndex = 0;
	readIndex = 0;
  }
  
  bool read(BufType *c)
  {
    if (len)
	{
      *c = buf[readIndex++];
      len--;
      if (readIndex==PIXY_BUF_SIZE)
        readIndex = 0;
	  return true;
	}
	else
	  return false;
  }
  
  uint8_t freeLen()
  {
    return PIXY_BUF_SIZE-len;
  } 
  
  bool write(BufType c)
  {
    if (freeLen()==0)
      return false;

    buf[writeIndex++] = c;
	len++;
    if (writeIndex==PIXY_BUF_SIZE)
      writeIndex = 0;
    return true;
  }

    BufType buf[PIXY_BUF_SIZE];
    uint8_t len;
    uint8_t writeIndex;
    uint8_t readIndex;
};

class LinkSPI
{
  public:
    void init()
    {
      SPI.begin();

      #ifdef __SAM3X8E__
      // DUE clock divider //
      SPI.setClockDivider(84);
      #else
      // Default clock divider //
      SPI.setClockDivider(SPI_CLOCK_DIV16);
      #endif
    }
    
	uint16_t getWord()
    {
      // ordering is different (big endian) because Pixy is sending 16 bits through SPI 
      // instead of 2 bytes in a 16-bit word as with I2C
      uint16_t w;

	  if (inQ.read(&w))
	    return w;
		
	  return getWordHw();
    }

    uint8_t getByte()
    {
      return SPI.transfer(0x00);
    }
    
    int8_t send(uint8_t *data, uint8_t len)
    {
      int i;

      // check to see if we have enough space in our circular queue
      if (outQ.freeLen()<len)
        return -1;

      for (i=0; i<len; i++)
		outQ.write(data[i]);
	  flushSend();
      return len;
    }

    void setArg(uint16_t arg)
    {
    }

  private:
    uint16_t getWordHw()
    {
      // ordering is different (big endian) because Pixy is sending 16 bits through SPI 
      // instead of 2 bytes in a 16-bit word as with I2C
      uint16_t w;
      uint8_t c, cout = 0;
		
	  if (outQ.read(&cout))
        w = SPI.transfer(PIXY_SYNC_BYTE_DATA);
      else
        w = SPI.transfer(PIXY_SYNC_BYTE);
		
      w <<= 8;
      c = SPI.transfer(cout);
      w |= c;

      return w;
    }

	void flushSend()
    {
      uint16_t w;
      while(outQ.len)
      {
        w = getWordHw();
		inQ.write(w);
	  }
	}
	
	// we need a little circular queues for both directions
	CircularQ<uint8_t> outQ;
	CircularQ<uint16_t> inQ;
};


typedef TPixy<LinkSPI> Pixy;

#endif

Thanks for your help!

Chris

sprintf(buf, "%d", pixy.blocks*.angle);[/color]*

  • Serial.println(buf);[/quote]*
    What wrong with this?
    * *Serial.println( int(pixy.blocks[i].angle) );* *

abs() ?

Thank you!
You helped me very much!

  1. I am using Pixycam which is hooked up with Arduino. Pixy is working nicely with color clues it can differentiate between color by setting signature. now i want to get information about shape of object detected? please suggest me how can i do that?

can i use opencv libraries in Arduino?

  1. How can I get pixel area covered by object(which is detected by pixycam)?
    your suggestion will be highly appreciated.

Harish_dhanian:
I want to use opencv libraries with Arduino, which is hooked up with Pixycam?

Is that a question or a statement? If it is a statement, what's stopping you? If it's a question, it needs some work, so that it really asks a question.