Correctly controlling servos with a Adafruit PCA9685 and a P

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
MethylRed
 
Posts: 4
Joined: Thu Mar 13, 2014 3:44 pm

Correctly controlling servos with a Adafruit PCA9685 and a P

Post by MethylRed »

Hi All,

So i have the PCA9685 hooked up with two servos working correctly, my problem however is they are not moving as I expected.

I am using the Adafruit example code from https://github.com/adafruit/Adafruit-Ra ... rvo_Driver with a Futaba S3003

Code: Select all

servoMin = 140  # Min pulse length out of 4096
servoMax = 600  # Max pulse length out of 4096
I have adapted the code so that the servo should turn in increments of 30 degrees by adding 92 to the serverMin on every turn which through 180 should give me 30 degree increments.

The servo does not move evenly though the first movement seems the largest and they get smaller on each turn until 180 degrees.

Am I controlling them incorrectly?

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Correctly controlling servos with a Adafruit PCA9685 and

Post by adafruit_support_mike »

Servos aren't guaranteed to be precise, but that does sound wierd.

What PWM values give you the actual 30 degree positions you want?

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Correctly controlling servos with a Adafruit PCA9685 and

Post by adafruit_support_bill »

I have adapted the code so that the servo should turn in increments of 30 degrees by adding 92 to the serverMin on every turn which through 180 should give me 30 degree increments.

The servo does not move evenly though the first movement seems the largest and they get smaller on each turn until 180 degrees.
Hard to say without seeing your actual code, but that does sound right. If you keep reducing the output range, the increments will get smaller.

MethylRed
 
Posts: 4
Joined: Thu Mar 13, 2014 3:44 pm

Re: Correctly controlling servos with a Adafruit PCA9685 and

Post by MethylRed »

adafruit_support_bill wrote:
I have adapted the code so that the servo should turn in increments of 30 degrees by adding 92 to the serverMin on every turn which through 180 should give me 30 degree increments.

The servo does not move evenly though the first movement seems the largest and they get smaller on each turn until 180 degrees.
Hard to say without seeing your actual code, but that does sound right. If you keep reducing the output range, the increments will get smaller.
Ill post the code im using later but what would be the correct way to get a servo to move to a position lets say 120 degrees from 0? or 150?

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Correctly controlling servos with a Adafruit PCA9685 and

Post by adafruit_support_bill »

what would be the correct way to get a servo to move to a position lets say 120 degrees from 0? or 150?
// move to 120 degrees
pulselength = map(120, 0, 180, SERVOMIN, SERVOMAX);
pwm.setPWM(servonum, 0, pulselen);

// move to 150 degrees
pulselength = map(150, 0, 180, SERVOMIN, SERVOMAX);
pwm.setPWM(servonum, 0, pulselen);

MethylRed
 
Posts: 4
Joined: Thu Mar 13, 2014 3:44 pm

Re: Correctly controlling servos with a Adafruit PCA9685 and

Post by MethylRed »

Thanks for the reply Bill, sorry for being slow on the uptake I have never played with servos before.

Could you explain the above? The way I was doing it was clearly wrong (in python),

Code: Select all

servoMin = 150  # Min pulse length out of 4096
servoMax = 600  # Max pulse length out of 4096

#150 = 0 degrees?
#600 = 180 degrees?
#(600 - 150) / 5 = 90
# So pwm.setPWM(0, 0, servoMin + 90) should move the servo 30 degrees? + 180 should move it 60 degrees?

pwm.setPWM(0, 0, servoMin) # 0
time.sleep(5)
pwm.setPWM(0, 0, servoMin + 90) # 30 
time.sleep(5)
pwm.setPWM(0, 0, servoMin + 180) # 60
time.sleep(5)
pwm.setPWM(0, 0, servoMin + 270) # 90
time.sleep(5)
...
...
pwm.setPWM(0, 0, servoMax) # 180

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Correctly controlling servos with a Adafruit PCA9685 and

Post by adafruit_support_bill »

Sorry, that was Arduino code that I posted. The map() function is:

Code: Select all

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
I'm not a Python programmer, but it should be fairly straightforward to translate the formula.

MethylRed
 
Posts: 4
Joined: Thu Mar 13, 2014 3:44 pm

Re: Correctly controlling servos with a Adafruit PCA9685 and

Post by MethylRed »

Thanks Bill, I have it now. It is giving me much more consistent results.

Code: Select all

def mapAngle(angle, minAngle, maxAngle, servoMin, servoMax):
    return (angle - minAngle) * (servoMax - ServoMin) / (maxAngle - minAngle) + servoMin

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”