<< return to Pixycam.com

Goalkeeper via pixy

Hello to the community.I want to make something like this https://www.youtube.com/watch?v=eVJrafabBkg with my pixy.It s an intresting project. I wrote a program and i tested it with a simple servo an i didnt make it work. Any ideas why it doesnt work ? here is my code. Thanks for the help.

#include  
#include   
#include 
#include  

double x;
double y;
int i = 0;
int j;
uint16_t blocks;
Servo myservo;  
                
 int pos = 0;  

Pixy pixy;      
void setup() 
{ 
  myservo.attach(9); 
  Serial.begin(9600);
  Serial.print("Starting...\n");

  pixy.init();
}

void loop(){ 
  blocks=pixy.getBlocks();
  if (blocks){ i++;}
  if(i%50==0){
  
      x=pixy.blocks[0].x;
      y=pixy.blocks[0].y;
      pos=atan ((x-160)/y);
   if(pos>0){
      myservo.write(90+pos);     
      delay(15);
}
else{
     myservo.write(90+pos);
}
 } 
  
  else{
     myservo.write(90);
}}

Hi John,

could you describe in more detail what happens when you run this program? Are you able to move the servo with other test code?

I’m not an Arduino expert but two things stick out. One, your main “if” statement where all the action is - @if(i%50==0)@ - only runs every 50 frames, or once per second. You could either delete / replace this condition entirely, since you have a delay built into your code, or just decrease the number 50 to something smaller.

Two, why don’t you just directly tie the x-position of @block[0]@ - the biggest block that Pixy detects - to the position of the servo? Instead of messing with atan and such.

Hope this helps!

Cheers,
Jesse

Thanks for your help my friend! I need atan so i can get the angle that the servo need to move. Though atan is not working and it gives me too small numbers. I dont know what to do. If you have some idea tell me!

Hi John,

please see my reply above for my ideas on what to do. Since, as you say, atan is not working, you can at least get some idea of what’s wrong if you simplify your code as much as possible.

Cheers,
Jesse

Hi John,
Your code looks like that you interpret the angle returned by the atan function as given in degree: “myservo.write(90+pos)”. But “atan” usually returns the angle in radian (-pi/2…pi/2).
Add a “pos*=180/3.1416” after the atan call.
Cheers,
Helmut