I'm trying to make a motion tracking camera using a CMUcam5 Pixy with servos controlled by the arduino rather than the pixy itself.
The pixy works by finding groups of pixels of the same color and drawing a box around it. It then gives values for the center in x,y coordinates called "pixy.blocks[j].x" and "pixy.blocks[j].y". I'm starting with the x axis only first. The pixy gives x coordinate from 0 to 319 pixels so I'm first scaling them to -90 to 90 degrees and then trying to add that scaled value to the servo's position variable which starts at 90.
For example, let's say the box is left of center at 80 pixels, it gets scaled to -45 which is added to the servo position at home (90). The servo (nicknamed xaxis) should move as if you said xaxis.write (45).
Instead the servo wants to jump to one of its limits and stay there. I have also tried to program it such that it simply adds or subtracts a number to the position variable depending on whether the tracked object is left or right of center, but small numbers make it track way too slowly and large numbers make it overshoot back and forth.
Does anyone have an idea of how to get the camera to track smoothly?
here's my current code;
#include <Servo.h>
Servo yaxis;
Servo xaxis;
#include <SPI.h>
#include <Pixy.h>
int xpos = 90;
// This is the main Pixy object
Pixy pixy;
void setup()
{
Serial.begin(9600);
Serial.print("Starting...\n");
yaxis.attach (9);
xaxis.attach (10);
pixy.init();
}
void loop()
{
static int i = 0;
int j;
uint16_t blocks;
char buf[32];
int x= ((pixy.blocks[j].x*.5625)-90); // scale pixels to +/- 90 degrees
// grab blocks!
blocks = pixy.getBlocks();
// If there are detect blocks, print them!
if (blocks)
{
i++;
// do this (print) every 20 frames because printing every
// frame would bog down the Arduino
if (i%20==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(); // prints x and y coordinates of blocks
xpos+=x; // change servo position variable
xaxis.write (xpos); //move servo to position
}
}
}
}