Can I read the PCA9685 to determine the current position of

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
doug_wyman
 
Posts: 36
Joined: Wed Nov 05, 2014 10:42 pm

Can I read the PCA9685 to determine the current position of

Post by doug_wyman »

I have two camera servos controlled by Adafruit_PCA9685.PCA9685().
The positions are set by html request.
I want to determine in my python code, the current position of each servo
before I set the new position.
Here is my test python code that is called by a PHP script.
Please forgive my terrible python code it could probably be a lot better.
It is mostly cut and paste code.

Code: Select all

from __future__ import division
import sys
import time

# Import the PCA9685 module.
import Adafruit_PCA9685
# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()
hservo_min = 150  # Min pulse length out of 4096
vervo_min = 150  # Min pulse length out of 4096
hservo_max = 600  # Max pulse length out of 4096
vservo_max = 600  # Max pulse length out of 4096
vpan = 1
hpan = 0
# Helper function to make setting a servo pulse width simpler.
def set_servo_pulse(channel, pulse):
    pulse_length = 1000000    # 1,000,000 us per second
    pulse_length //= 60       # 60 Hz
    print('{0}us per period'.format(pulse_length))
    pulse_length //= 4096     # 12 bits of resolution
    print('{0}us per bit'.format(pulse_length))
    pulse *= 1000
    pulse //= pulse_length
    pwm.set_pwm(channel, 0, pulse)

# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
if not(len(sys.argv) == 3):
  print("2 values must be entered")
  quit()
try:
  tmp1 = int(sys.argv[1])
except:
  print("first value was not an integer!")
  quit()
try:
  tmp2 = int(sys.argv[2])
except:
  print("second value was not an integer!")
  quit()
if (139 <= tmp1 <= 600) and (90 <= tmp2 <= 600):
  newtmp = 690 - tmp2
  pwm.set_pwm(vpan, 0, tmp1)
  pwm.set_pwm(hpan, 0, newtmp)
else:
  print("numbers not in range") 

User avatar
doug_wyman
 
Posts: 36
Joined: Wed Nov 05, 2014 10:42 pm

Re: Can I read the PCA9685 to determine the current position

Post by doug_wyman »

Yes, I am going to clean up the code and remove all the useless junk

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

Re: Can I read the PCA9685 to determine the current position

Post by adafruit_support_bill »

There is no position feedback from the servo itself. In theory, you could read the on/off registers for each channel and calculate the pulse width currently being sent. But there is no support for that in the library. It would be much simpler to just store the last commanded position in a variable in your code.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”