在Raspberry pi Python中连接Adafruit_PCA9685 I2C接口

2024-06-16 09:12:59 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用覆盆子Pi和Python连接Adafruit_PCA9685-I2C?


Tags: adafruit覆盆子pii2cpca9685
1条回答
网友
1楼 · 发布于 2024-06-16 09:12:59

你可以使用Raspberry pi和python一次单独运行16个伺服电机。我使用reconnect\u io\u func()函数,该函数将检查是否与Raspberry pi连接,如果没有,则将继续尝试连接,直到成功连接。然后将伺服电机的运行值

from __future__ import division
import Adafruit_PCA9685


def reconnect_io_func():
    try:
        pwm = Adafruit_PCA9685.PCA9685()
        return pwm
    except Exception as error:
        if "Remote I/O error" in (error):
            reconnect_io = True
            while reconnect_io:
                try:
                    # print("while Error: "+str(error))
                    pwm = Adafruit_PCA9685.PCA9685()
                    # print(pwm)
                    reconnect_io = False
                    return pwm
                except Exception as error:
                    # print((error))
                    reconnect_io = True


servo_min = 150  # Min pulse length out of 4096
servo_max = 600  # Max pulse length out of 4096
pwm = reconnect_io_func()
pwm.set_pwm_freq(60)

if __name__ == '__main__':
    pwm.set_pwm(0, 0, 420)
    pwm.set_pwm(1, 0, 420)
    pwm.set_pwm(2, 0, 307)
    pwm.set_pwm(3, 0, 420)
    pwm.set_pwm(4, 0, 307)
    pwm.set_pwm(5, 0, 307)
    pwm.set_pwm(6, 0, 307)
    pwm.set_pwm(7, 0, 307)
    pwm.set_pwm(8, 0, 0)
    # print (" i am here")

相关问题 更多 >