<< return to Pixycam.com

How To Stop Servo continous When pixy got the object

i have a final project with arduino, Pixy cam, and 3pcs of servos. 2 servo to move x and y, and 1 servo for airsoft gun trigger.

i wanna ask how to make servo stop when pixy cam get the block/object centered?
because if i upload pan n tilt program to arduino and it wont stop, servo just move left right up down when the laser was in center.

the project its about automatic shooting rest with arduino microcontroller, so the shooting rest will move by servo and camera looking for laser (laser move by operator to test the pixy cam), then when camera was detect object in center servo must be stop and servo trigger on.

Hopefully you can help me.
Thanks :slight_smile:

Hello,
Are you using your own pan-tilt setup (or similar)? Getting your own pan-tilt to work can be a fun project, but it helps to know how basic closed-loop servo controls work.

Please describe your setup. A picture might be useful.

Edward

diagbner

i use the code from instructable.com and just little edited by me.

this is the code

Blockquote

  • GND - Pixy Black Wire
  • VCC 5V - Pixy Red Wire
  • DIO 3 - Trigger Servo (180 Servo)
  • DIO 5 - Pan Servo (Servo continous)
  • DIO 6 - Tilt Servo (Servo continous)
  • DIO 10 - OPTIONAL: RobotGeek Laser White -‘S’ Black -‘G’
  • DIO 11 - Pixy Yellow Wire
  • DIO 12 - Pixy Brown Wire
  • DIO 13 - Pixy Orange Wire
  • Use an external power supply and set the jumper for pins 3/5/6 to ‘VIN’, 9/10/11 to ‘5V’
  • For more information and wiring diagrams see
  • http://learn.robotgeek.com/getting-started/29-desktop-roboturret/159-robotgeek-foam-dart-gun-getting-started-guide.html
    ***********************************************************************************/

#include <SPI.h> //Serial Peripheral Interface for connecting to Pixy
#include <Pixy.h> //Pixy Communication library
#include <Servo.h> //include the servo library to control the RobotGeek Servos

Pixy pixy; //create the pixy class

//Constant variables - these all use ‘const’ because they will not change during the program operation, allowing us to save RAM
const int TRIGGER_PIN = 3; //Trigger Servo Digital Pin
const int PAN_PIN = 5; //Pan Servo Digital Pin
const int TILT_PIN = 6; //Tilt Servo Digital Pin

//max/min puse values in microseconds to send to the servo
const int FIRE_POSITION = 1000; //default position in microseconds the servo will move to in order to fire the dart gun
const int PAN_MIN = 600; //full counterclockwise for RobotGeek 180 degree servo
const int PAN_MAX = 2400; //full clockwise for RobotGeek 180 degree servo
const int TILT_MIN = 600; //full counterclockwise for RobotGeek 180 degree servo
const int TILT_MAX = 2400; //full clockwise for RobotGeek 180 degree servo

const int TRACKING_TIMEOUT = 50; //time in milliseconds after object is lost that tracking stops
const int TIME_BFORE_FIRE = 3000;//time in milliseconds between first seeing the object and firing

int speed = 5; //alter this value to change the speed of the system. Higher values mean higher speeds 5-500 approximate recommended range

Servo panServo, tiltServo, triggerServo; // create servo objects to control the pan and tilt servos

int panValue = 1200; //current positional value being sent to the pan servo.
int tiltValue = 1200; //current positional value being sent to the tilt servo. makin tinggi makin merunduk
int triggerValue = 1500; //current positional value being sent to the tilt servo.

int pixyCenterY; //y coordinate of the block, centered
int pixyCenterX; //x coordinate of the block, centered
long pixySize; //size of the block
long lastSeen; //last time the pixy block was located
long trackStartTime; //time that tracking started
bool tracking = true; //whether or not the code is currently tracking an object
bool shots = 3; //number of shots remaing - by default 1 for the foam dart gun

//setup servo objects and set initial position
void setup()
{

panServo.attach(PAN_PIN, PAN_MIN, PAN_MAX); // attaches/activates the pan servo on pin PAN_PIN and sets lower/upper limits that can be sent to the servo
tiltServo.attach(TILT_PIN, TILT_MIN, TILT_MAX); // attaches/activates the tilt servo on pin TILT_PIN and sets lower/upper limits that can be sent to the servo
triggerServo.attach(TRIGGER_PIN); //attach the trigger servo on pin SERVOPIN

//the pixy class will initialize all of the pixy realated pins

//write initial servo positions to set the servos to ‘home’
panServo.writeMicroseconds(panValue); //sets the pan servo position to the default ‘home’ value
tiltServo.writeMicroseconds(tiltValue);//sets the tilt servo position to the default ‘home’ value
triggerServo.writeMicroseconds(triggerValue); //sets the servo position to 90 degress, centered, ‘home’ value
Serial.begin(9600);
Serial.print(“Starting…\n”);
pixy.init();
}

void loop()
{
uint16_t blocks; //block holds tracked data for the pixy
char buf[32]; //pixy data buffer

blocks = pixy.getBlocks();

//if blocks exist
if(blocks)
{
lastSeen = millis(); //a block has been ‘seen’ so record the time

//if the code wasn't tracking before
if(tracking == false)
{
  trackStartTime = lastSeen;  //record tracking time start - this will be the same as lastSeen right now, but lastSeen will keep updated
  tracking = true;               //turn tracking flag on
  Serial.println("Start Tracking! Time");
  Serial.println(trackStartTime);
}

pixyCenterX = 160 - pixy.blocks[0].x  ; //get x data (0-320) and center it by subtracting 160. Values 0-159 (to the left) will be negative while values 161-320 (to the right) will be positive. This will help in tracking objects
pixyCenterY = pixy.blocks[0].y - 100; //get y data (0-200) and center it by subtracting 100. Values 0-99 (down) will be negative while values 101-200 (up) will be positive. This will help in tracking objects
pixySize = pixy.blocks[0].height *  pixy.blocks[0].width; //get the relative size of the object by multiplying width * height

panValue = panValue + pixyCenterX/speed;
tiltValue = tiltValue + pixyCenterY/speed; 

 //even though the servos have min/max value built in when servo.attach() was called, the program must still keep the
 //panValue variable within the min/max bounds, or the turret may become unresponsive
 panValue = max(panValue, PAN_MIN);  //use the max() function to make sure the value never falls below PAN_MIN (0 degrees)
 panValue = min(panValue, PAN_MAX);  //use the min() function to make sute the value never goes above PAN_MAX (180 degrees)

 //even though the servos have min/max value built in when servo.attach() was called, the program must still keep the
 //tiltValue variable within the min/max bounds, or the turret may become unresponsive
 tiltValue = max(tiltValue, TILT_MIN);//use the max() function to make sure the value never falls below 0
 tiltValue = min(tiltValue, TILT_MAX);//use the min() function to make sute the value never goes above 180
 
 panServo.writeMicroseconds(panValue);  //sets the pan servo position to the default 'home' value
 tiltServo.writeMicroseconds(tiltValue);//sets the tilt servo position to the default 'home' value

 //if the object has been tracked for at least TIME_BFORE_FIRE millieseconds, and the dart gun has a shot, proceed
 if((millis() - trackStartTime  > TIME_BFORE_FIRE) && (shots > 0))
 {
  Serial.print("FIRE!");
  triggerServo.writeMicroseconds(FIRE_POSITION); // move the servo to FIRE_POSITION to fire the dart gun / durasi nge trigger nya
  delay(300);                        //wait for 300ms for servo to move
  triggerServo.writeMicroseconds(triggerValue);            // sets the servo position to 90 degress, centered
  shots = shots - 3;                  //decrement the shot counter so the gun doesn't keep trying to fire / jumlah yg ditembakan
 }

}
//if no blocks exist
else
{
//check if it has beeen TRACKING_TIMEOUT milliseconds before setting the tracking flag false - this helps to mitigate lost data packets or short connectiviety losses
if(millis()-lastSeen > TRACKING_TIMEOUT)
{
tracking = false;
Serial.println(“Object Lost, no longer tracking”);
}
}

}

Hello,
Thanks for the picture! very cool :slight_smile:

One detail that’s important is to have Pixy mounted on the pantilt device, so that Pixy pans and tilts with (in your case) the gun.

For example, let’s focus on the pan movement. The pan-tilt program will move the servo left (or right) until the object is centered in the middle “column” of the image. Once it’s centered, and the “x-error” is zero, the commands to the pan servo are also zero, and the pan servo will stop. If the object moves, the x-error will no longer be zero and the servo will move to “re-zero” the x-error. This is why it’s important to have Pixy move with the pan-tilt instead of be stationary.

Hope this helps!

Edward

yup thats true i already did (if i using servo standard) for pan tilt.

now i using slider system but with pan tilt program (so i just change the servos to servo continous)
when i upload that program the servo continous just move left n right when pixy detect column in center, so how i can make the servo continous stop rotating when pixy detect column in center ?

maybe u wanna see the video of this problem? i can give a link.

Hello,
You’re asking why the servo doesn’t stop when it’s in the center. You might investigate by looking at the commands being sent to the servo, printing out the x-error values, etc.

Edward

thanks for your advice n suggest sir edge
btw i already put this code

panServo.write(90);
tiltServo.write(90);

at my last program on arduino ide. when pixy not detect laser servo stop rotated. it succeed to stop.
btw where i can put that code? so servo can stop when detect laser center? (according the program i attached above.

Hello Nazirama,
I’m sorry but I’m not understanding your questions.

“btw where can put that code?”

I don’t know what code you are referring to, but I think you may have bigger questions. Do you feel like you understand how the pan-tilt mechanism tracks objects using the 2 control loops? I’m not certain that your Pixy is mounted on a pan-tilt mechanism. I can see that you have servos and a Pixy, but I don’t know how things are arranged mechanically. These details need to be correct in order for the right things to happen.

Have you considered getting a pan-tilt mechanism (the one we sell) assembling it and figuring out how it works?

Edward