<< return to Pixycam.com

Manual Control of Pixy Pan/Tilt Servos

Hey Devs, just got my Pixy in and I’m enjoying playing with it. A quick question: what is the logic behind the arguments for the .setServos function? I’ve found that calling the function makes the servos snap to a certain orientation, however I cannot find a rhyme or reason as to why they go to a certain position.

Also, calling the same values through the function causes the servos to twitch severely every couple of seconds. How do I wrangle these things into moving smoothly, and what determines the ‘center’ position?

Thanks!

Hey Wes,

Sounds like you need the basics of servo explanation. Fortunately Adafruit has a nice tutorial on Servos that eventually goes down to the low level details of what type of waveform servos need to control them:

TL;DR - To answer your question quickly, the .setServos() function will ultimately create a PWM waveform on the control input of the servo. This waveform basically tells the servo to set itself to a particular orientation. The servo knows when it’s at the correct orientation because it has a potentiometer inside of it that relays position feedback of the output shaft. Essentially it turns a geared motor one direction or another until it sees the correct voltage on the potentiometer. It glitches about because it’s randomly driving the motor back and fourth around this control point. The more load you put on the servo, the more it will “fight” to retain it’s position and the more noise you may hear.

The twitch you are experiencing may be due to a glitch in the signal sent to the servo. This is fairly common when updating the duty cycle of a PWM output when you do it in such a way that it re-initializes the PWM and then sets the duty cycle. This may be the case inside Pixy, but I’m sure it’s easy to resolve. To verify this, you would need to monitor the PWM output with an oscilloscope to look for irregularities in the waveform around the intervals when you are updating the .setServos() function. I will offer to help out here since I have a 'scope. Would you post your Arduino Code so that I may duplicate your setup? Just paste it in, select it all, and click the [PRE] button to format it like this (so we can read it easily):

if(someTest == true) {
  // do stuff
}
else {
  // don't do stuff
}

Even if there is a glitch in the PWM, you can avoid transferring this to your servo without a fix to Pixy’s code. To do this you can just not update the .setServos() function if your servo values have not changed since the last time you sent them. Just keep track of them with a newPos and oldPos value, like this:

if(newPosX != oldPosX || newPosY != oldPosY) {
  pixy.setServos(newPosX, newPosY);
  oldPosX = newPosX;
  oldPosY = newPosY;
}

Hi BDub, thanks so much for the reply! The duty cycle interruption explains the issues I was having, and implementing a ‘change servo signal only when necessary’ check fixed the twitching perfectly.

I dug into the PanTilt Pixy example last night (which I should have done in the first place) and found that the arguments are between 0 and 1000 (from the #define constants at the head of the script), so if anyone is looking for the ‘center’ position as I was, it’s setServos(500, 500), with 0 and 1000 being the limits of the servos.

Another source of twitching I found was the Pixy itself reading garbage blocks from my poorly-lit room; adding a bright LED to the face of the Pixy solved that issue as well.

Thanks again for the help! I’ve had my CMUcam5 for a few days now and it continues to impress.

Just wanted to say that Make Magazine just posted a nice article on servos with the Arduino:
http://makezine.com/2014/04/23/arduinos-servo-library-angles-microseconds-and-optional-command-parameters/