樹莓派下安裝PCA9685的python驅動程序

樹莓派下安裝PCA9685的python驅動程序


# Adafruit Python PCA9685  
Python code to use the PCA9685 PWM servo/LED controller with a Raspberry Pi or BeagleBone black.
#使用樹莓派或者BeagleBone black 的Python 源碼 驅動PCA9685 輸出PWM波控制舵機(伺服電機)/LED燈
## Installation#安裝配置


To install the library from source (recommended) run the following commands on a Raspberry Pi or other Debian-based OS system:
#從源碼庫安裝,在樹莓派或者其他Debian-based系統上運行以下指令


    sudo apt-get install git build-essential python-dev
    cd ~
    git clone https://github.com/adafruit/Adafruit_Python_PCA9685.git
    cd Adafruit_Python_PCA9685
    sudo python setup.py install
#完成  以下部分爲不帶例子程序安裝方法
Alternatively you can install from pip with:


    sudo pip install adafruit-pca9685


Note that the pip install method **won't** install the example code.





#以下爲python源程序

#輸入通道與角度。即可選通並使該通道的舵機轉動到相應的角度
from __future__ import division								#導入 __future__ 文件的 division 功能函數(模塊、變量名....)   #新的板庫函數  //=
import time

# Import the PCA9685 module.
import Adafruit_PCA9685										#導入Adafruit_PCA9685模塊


# Uncomment to enable debug output.
#import logging
#logging.basicConfig(level=logging.DEBUG)					#調試打印日誌輸出

# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()							#把Adafruit_PCA9685.PCA9685()引用地址賦給PWM標籤

# Alternatively specify a different address and/or bus:
#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)


#2^12精度  角度轉換成數值  #angle輸入的角度值(0--180)  #pulsewidth高電平佔空時間(0.5ms--2.5ms)   #/1000將us轉換爲ms  #20ms時基脈衝(50HZ)
#pulse_width=((angle*11)+500)/1000;  //將角度轉化爲500(0.5)<-->2480(2.5)的脈寬值(高電平時間)   angle=180時  pulse_width=2480us(2.5ms)
#date/4096=pulse_width/20 ->有上pulse_width的計算結果得date=4096*( ((angle*11)+500)/1000 )/20   -->int date=4096((angle*11)+500)/20000;
	 	 
def set_servo_angle(channel, angle):					#輸入角度轉換成12^精度的數值
	date=4096*((angle*11)+500)/20000				#進行四捨五入運算 date=int(4096*((angle*11)+500)/(20000)+0.5)	
	pwm.set_pwm(channel, 0, date)


# Set frequency to 50hz, good for servos.
pwm.set_pwm_freq(50)

print('Moving servo on channel x, press Ctrl-C to quit...')
while True:
    	# Move servo on channel O between extremes.
	channel=int(input('pleas input channel:'))
	angle=int(input('pleas input angle:'))
	set_servo_angle(channel, angle)
	#time.sleep(1)




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章