<< return to Pixycam.com

Following an object using pixycam and arduino

Hello,
i’m doing a project about a robot who follows an object using pixy and arduino and i found the code https://learn.adafruit.com/pixy-pet-robot-color-vision-follower-using-pixycam/the-code
the probleme is that i want just the detection on axis X
also i’m using a adafruitShield V2 anyone can help me
thanks a lot

Hi, the X values are returned from the pixy.getBlocks() function. You can read the values by using pixy.blocks[j].x, where j is an integer less than blocks.

Here’s an example of retrieving the X values, from our Arduino demo here: https://docs.pixycam.com/wiki/doku.php?id=wiki:v1:hooking_up_pixy_to_a_microcontroller_-28like_an_arduino-29

for (j=0; j<blocks; j++)
      {
        Serial.print(pixy.blocks[j].x);
      }

Hope this helps!

Cheers,
Jesse

Hello Jesse thanks for your reply , i still found a problem, i don’t know know to modify the code i sent and introduce the fonction that you sent to just get the X values . could you tell me how to do it because i still have an error on Arduino command
thank you very much for your help

Hi hamid, please post your code & send a picture of the error you’re getting.

Thanks!
Jesse

that’s the code i try to use , to know i’m using two dc motors with arduino uno,adafruit Shield V2 and i want to modify the code to have just the detection of the object on X values (the robot will follow the object )
that’s the error :
exit status 1
‘PIXY_MAX_X’ was not declared in this scope
and i have another question : i can use a normal dc motor without changing the code or i have because in the code there is written Zumomotors
That’s the code and thanks a lot for your help :

#include <SPI.h>

#include <PIDLoop.h>
#include <Pixy2.h>
#include <Pixy2CCC.h>
#include <Pixy2I2C.h>
#include <Pixy2Line.h>
#include <Pixy2SPI_SS.h>
#include <Pixy2UART.h>
#include <Pixy2Video.h>
#include <TPixy2.h>
#include <ZumoBuzzer.h>
#include <ZumoMotors.h>



//
// begin license header
//
// This file is part of Pixy CMUcam5 or "Pixy" for short
//
// All Pixy source code is provided under the terms of the
// GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).
// Those wishing to use Pixy source code, software and/or
// technologies under different licensing terms should contact us at
// [email protected] Such licensing terms are available for
// all portions of the Pixy codebase presented here.
//
// end license header
//
// This sketch is a simple tracking demo that uses the pan/tilt unit.  For
// more information, go here:
//
// http://cmucam.org/projects/cmucam5/wiki/Run_the_Pantilt_Demo
//

#include <SPI.h>  
#include <Pixy2.h>

Pixy2 pixy;

#define X_CENTER        ((PIXY_MAX_X-PIXY_MIN_X)/2)      
#define Y_CENTER        ((PIXY_MAX_Y-PIXY_MIN_Y)/2)

class ServoLoop
{
public:
 ServoLoop(int32_t pgain, int32_t dgain);

 void update(int32_t error);
 
 int32_t m_pos;
 int32_t m_prevError;
 int32_t m_pgain;
 int32_t m_dgain;
};


ServoLoop panLoop(300, 500);
ServoLoop tiltLoop(500, 700);

ServoLoop::ServoLoop(int32_t pgain, int32_t dgain)
{
 m_pos = PIXY_RCS_CENTER_POS;
 m_pgain = pgain;
 m_dgain = dgain;
 m_prevError = 0x80000000L;
}

void ServoLoop::update(int32_t error)
{
 long int vel;
 char buf[32];
 if (m_prevError!=0x80000000)
 {
   vel = (error*m_pgain + (error - m_prevError)*m_dgain)>>10;
   //sprintf(buf, "%ld\n", vel);
   //Serial.print(buf);
   m_pos += vel;
   if (m_pos>PIXY_RCS_MAX_POS)
     m_pos = PIXY_RCS_MAX_POS;
   else if (m_pos<PIXY_RCS_MIN_POS)
     m_pos = PIXY_RCS_MIN_POS;
 }
 m_prevError = error;
}



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

void loop()
{
 static int i = 0;
 int j;
 uint16_t blocks;
 char buf[32];
 int32_t panError, tiltError;

pixy.ccc.getBlocks();
 
 
 if (blocks)
 {
   panError = X_CENTER-pixy.blocks[0].x;
   tiltError = pixy.blocks[0].y-Y_CENTER;
   
   panLoop.update(panError);
   tiltLoop.update(tiltError);
   
   pixy.setServos(panLoop.m_pos, tiltLoop.m_pos);
   
   i++;
   
   // do this (print) every 50 frames because printing every
   // frame would bog down the Arduino
   if (i%50==0)
   {
     sprintf(buf, "Detected %d:\n", blocks);
     Serial.print(buf);
     for (j=0; j<blocks; j++)
     {
       sprintf(buf, "  block %d: ", j);
       Serial.print(buf);
       pixy.blocks[j].print();
     }
       }
     }  
    }

Hi, it looks like you need to declare PIXY_MAX_X as well as the other 3 variables you’re referencing in the #define statements on lines 42 and 43 of the code you posted.

This would look something like:

int PIXY_MAX_X = 0;

or whatever value you want.

About just tracking the X axis - it looks like all you need to do is comment out the parts of the code that deal with the Tilt axis - for example, comment out this line as follows:

// tiltLoop.update(tiltError);

That should keep the code from doing anything with the Y axis.

Hope this helps!

Cheers,
Jesse