The pixy camera can only track objects within the current field of view. Therefore, objects that are not within the field of view cannot be tracked.
So when there is no object recognized by the camera, I want the left and right motors to rotate to look for other objects.
So I tried to fix pixy’s code, but there was no solution. Is there a good solution?
Thanks for reading.
I’ve been spending almost two weeks just on this problem…
This is my code and my system use Aduino R3.
#include <Pixy2.h>
#include <PIDLoop.h>
#include <Servo.h>
Servo servo_p;
Pixy2 pixy;
PIDLoop panLoop(400, 0, 400, true);
PIDLoop tiltLoop(500, 0, 500, true);
int motor = 7;
int angle = 0;
void setup()
{
servo_p.attach(motor);
Serial.begin(115200);
Serial.print(“Starting…\n”);
// We need to initialize the pixy object
pixy.init();
// Use color connected components program for the pan tilt to track
pixy.changeProg(“color_connected_components”);
}
void loop()
{
static int i = 0;
int j;
int k;
char buf[64];
int32_t panOffset, tiltOffset;
int32_t xCoordinate = 0;
const int PAN_THRESHOLD = 10;
const int TILT_THRESHOLD = 5;
// get active blocks from Pixy
pixy.ccc.getBlocks();
if (pixy.ccc.numBlocks)
{
Serial.print("Detected ");
Serial.println(pixy.ccc.numBlocks);
for (k=0; k<pixy.ccc.numBlocks; k++)
{
Serial.print(" block ");
Serial.print(k+1);
Serial.print(": ");
pixy.ccc.blocks[k].print();
Serial.print("xCoordinate: ");
Serial.println(xCoordinate);
}
i++;
if (i%60==0)
Serial.println(i);
panOffset = (int32_t)pixy.frameWidth/2 - (int32_t)pixy.ccc.blocks[0].m_x;
tiltOffset = (int32_t)pixy.ccc.blocks[0].m_y - (int32_t)pixy.frameHeight/2;
if (abs(panOffset) < PAN_THRESHOLD && abs(tiltOffset) < TILT_THRESHOLD)
{
if (pixy.ccc.numBlocks >= 0)
{
delay(5000);
}
else if (pixy.ccc.numBlocks == 0)
{
// Servo 위치를 panLoop.m_command 및 tiltLoop.m_command 값으로 설정
pixy.setServos(panLoop.m_command, tiltLoop.m_command);
}
}
else
{
// If the errors are outside the threshold, update the servo positions
panLoop.update(panOffset);
tiltLoop.update(tiltOffset);
// Servo 위치를 panLoop.m_command 및 tiltLoop.m_command 값으로 설정
pixy.setServos(panLoop.m_command, tiltLoop.m_command);
}
#if 0 // for debugging
sprintf(buf, “%ld %ld %ld %ld”, rotateLoop.m_command, translateLoop.m_command, left, right);
Serial.println(buf);
#endif
}
switch (pixy.ccc.numBlocks) {
case 0:
for (int z = 0; z < 180; z++) {
angle = angle + 1;
if (angle >= 180)
angle = 0;
servo_p.write(angle);
delay(50);
}
break;
case 1:
angle = angle;
servo_p.write(angle);
break;
}
}