首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于PCA9685和树莓派的伺服控制器

基于PCA9685和树莓派的伺服控制器
EN

Stack Overflow用户
提问于 2016-12-04 06:12:37
回答 1查看 4.7K关注 0票数 0

我试着学习如何使用PCA9685模块来控制带有树莓派的伺服系统。我遵循adafruit教程,其中提供了以下代码:

代码语言:javascript
复制
# Simple demo of of the PCA9685 PWM servo/LED controller library.
# This will move channel 0 from min to max position repeatedly.
# Author: Tony DiCola
# License: Public Domain
from __future__ import division
import time

# Import the PCA9685 module.
import 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()

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

# Configure min and max servo pulse lengths
servo_min = 150  # Min pulse length out of 4096
servo_max = 600  # Max pulse length out of 4096

# 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)

print('Moving servo on channel 0, press Ctrl-C to quit...')
while True:
    # Move servo on channel O between extremes.
    pwm.set_pwm(0, 0, servo_min)
    time.sleep(1)
    pwm.set_pwm(0, 0, servo_max)
time.sleep(1)

一切运行良好,伺服运动正确-然而,当我退出程序时,伺服仍然接收到迫使它进入一个方向的信号。但是,如果尝试添加键盘中断和break语句相结合,则会导致伺服系统严重过热。现在我害怕打碎什么东西。所以我想知道如何在退出程序后停止信号?

EN

回答 1

Stack Overflow用户

发布于 2016-12-04 10:31:59

如果您的程序退出,您肯定希望有一种方法来设置默认状态。在您的例子中,KeyboardInterrupt的处理程序是可行的。确保将伺服输出设置为处理机中的标称值。

代码语言:javascript
复制
servo_middle_position = (servo_max - servo_min) / 2

try:
    while True:
        pwm.set_pwm(0, 0, servo_min)
        time.sleep(1)
        pwm.set_pwm(0, 0, servo_max)
        time.sleep(1)

except KeyboardInterrupt:
    print "User quit! Moving servo to middle position.'
    pwm.set_pwm(0, 0, servo_middle_position)

我猜你实际上可能不是在使用传统的伺服系统,因为PWM占空比决定了一个位置--如果是这样的话,一旦达到了期望的位置,伺服系统就会停止移动。您应该注意到,伺服系统在运行时会产生一定量的热量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40953054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档