<< return to Pixycam.com

Using pixy on a biped robot

Hi,
I have a biped robot that uses Arduino programing. It originally had an IR sensor to detect objects and walk towards them. I have replaced the IR sensor with the Pixy cam so it will only walk towards a certain object. The camera is installed on a panning servo and plugged into the ICSP on the Arduino board and it tracks objects perfectly using the “Pan Tilt” demo. However, I’m not sure how to replace the IR sensor in the code to the pixy. For example…

     }
else if(detpos > 110)
{
  Turn_Right(detpos);
  scandir = 1;
}
else if(detpos < 75)
{
  Turn_Left(detpos);
  scandir = 0;
}

In my code with the IR sensor if the pan servo was turned greater than 110 the robot would turn right, less than 75 and it would turn left. How would I write this in the new code for pixy? Also, my current code is written like this…

 Scan();
read_Ir();

What would I replace “read_Ir();” with?

I appreciate an helpful replies. At this point I’m just trying to get the robot to turn and face an object.

Thank you

Hello Scott,
The robot looks great!

You should take a look at the “pantilt” Arduino program. It uses a simple PID loop to track things (and it’s essentially the same code as the pan/tilt demo that is included in Pixy’s firmware.) A PID loop uses a simple math expression instead of if statements (like you have above) to track things quickly/accurately. A PID loop will allow your robot to follow things really well.

Or you can use an if statement:

if (block[0].x < 140) 
   turnLeft();
else if (block[0].x > 180)
   turnRight();

That is, use the x position to determine how to turn. The x position will range between 1 and 320, so 160 is the center.

Hope this helps!

Edward

Edward,

Thank you very much for the reply!

The “if” statement was exactly what I needed.

Now I’m just trying to copy and paste the needed info from the pan tilt program onto my old program that has the robots movement code on it. Thank you, this gets me pointed in the right direction.

Scott

A quick update with some positive news (thanks to Edward). The pixy cam and robot are communicating after tweaking with the code. What I had to do was write it like this in the loop.

if (pixy.blocks[0].x < 100)
{
  Turn_Left(130);
}
else if (pixy.blocks[0].x > 220)
{
  Turn_Right(130);

It kind of works. The robot does turn right and left but only when the object is on the edges of the cameras field of view. The panning servos orientation has no effect on which way the robot turns. So the panning servo can be faced to the extreme right but the robot won’t turn to the right until the object moves out of the center of the cameras frame. Even then if the object moves to the left of the cameras view the robot turns left even though the camera is facing to the right. More adjustments are needed but I’m happy that it seems to be on the right track.

Ok, another update. I got some help from Bill at Adafruit with this. He wrote the Pixy Pet code which is very cool. What I need to do was change from using blocks to using the followError. Now the robot turns to the direction the pan servo is facing. Below is a small portion of the code.
if(followError > 200)

@if(followError > 200)
{
Turn_Left(130);
}
else if(followError < -200)
{
Turn_Right(130);
}
else if(followError >=-199 && followError <=199)
{
Walk(10);
}@

Thank you again for this forum and your help.

Scott

The robot is tracking and walking towards objects perfectly now. I have been trying to implement an IR distance sensor for obstacle avoidance at first, then move on to having the robot perform a task when it is close to a red object. However, even after scouring the internet for weeks and trying different code this is harder than I expected. I did however see a thesis paper on a wheeled robot from Rohit Prakash who is also on this form. He coded an array with an IR sensor on pan servo for obstacle avoidance which is exactly what I’m trying to do. I’ll post more if I get it working properly.

Scott