使用PCA9685的ESC的树莓派上的PWM频率和占空比

时间:2019-10-02 08:01:58

标签: raspberry-pi robotics pwm servo

我使用的是树莓派和Adafruit 16Ch PCA9685 PWM帽子,我的任务是为客户的柜台区域设置一个小型演示硬件,我付了一个Python开发人员,他不再在这里写信给我一些从未使用过的代码。

基本上,我们有一个ESC和三个伺服器。该代码具有一些功能,但其核心是需要能够布防ESC(听到蜂鸣声,等待脉冲),从而根据所调用的脚本将伺服器移动到特定位置。

问题是,编写的代码不能使ESC武装,但它什么也没做,但是运行最后一个代码将使esc / motor武装并运行所有伺服器

设置默认代码

    """
@file: gendefs.py

Defines all GPIO and Servo values, ESC speeds plus any additional generics.
"""

from enum import Enum


""" Connect servos to these channels on the PCA9685. """
class ServID(Enum):
    SERV_0 = 0
    SERV_1 = 1
    SERV_2 = 2


""" Set this to desired servo closed and open angles. """
class ServState(Enum):
    CLOSED = 0
    POS_1  = 60
    POS_2  = 120


""" Connect the ESC to this GPIO pin. """
ESC = 4


""" ESC default speeds (pulse width). """
class ESC_PW(Enum):
    STOP = 0
    MIN  = 700  # Change this to your ESC's min value
    IDLE = 1000 # Change this to your preferred speed
    MAX  = 2000 # Change this to your ESC's max value


""" Call this to set all servos to their closed position. """
def close_servos(kit):
    for serv in ServID:
        kit.servo[serv.value].angle = ServState.CLOSED.value


""" Set specific servo to an open position. Close all other servos. """
def set_servo(kit, serv_id, serv_pos):
    if serv_id not in ServID:
        raise RuntimeError("Servo ID must be member of ServID class!")

    if serv_pos not in ServState:
        raise RuntimeError("Servo position must be member of ServState class!")

    # All servos except the selected one will close.
    to_close = list(ServID)
    to_close.remove(serv_id)

    for serv in to_close:
        kit.servo[serv.value].angle = ServState.CLOSED.value

    # Move selected servo to open position.
    kit.servo[serv_id.value].angle = serv_pos.value

手臂ESC代码

    #!/usr/bin/env python

"""
@file: arm.py

Just arms the ESC, nothing else.
"""

import time

import adafruit_servokit
import pigpio

from gendefs import ESC, ESC_PW, close_servos


if __name__ == "__main__":
    pi = pigpio.pi()
    kit = adafruit_servokit.ServoKit(channels=16)

    # All servos are set to closed position.
    close_servos(kit)

    # Arming procedure for the ESC.
    pi.set_servo_pulsewidth(ESC, ESC_PW.STOP.value)
    time.sleep(1)
    pi.set_servo_pulsewidth(ESC, ESC_PW.MAX.value)
    time.sleep(1)
    pi.set_servo_pulsewidth(ESC, ESC_PW.MIN.value)
    time.sleep(1)

以空闲代码运行ESC

    #!/usr/bin/env python

"""
@file: idle.py

Sets the ESC to its normal velocity. To tweak the pulse width, edit
'gendefs.py'.
"""

import adafruit_servokit
import pigpio

from gendefs import ESC, ESC_PW, close_servos


if __name__ == "__main__":
    pi = pigpio.pi()
    kit = adafruit_servokit.ServoKit(channels=16)

    # Servos are set to closed position.
    close_servos(kit)

    # Sets ESC to idle speed.
    pi.set_servo_pulsewidth(ESC, ESC_PW.IDLE.value)

自然位置代码

    #!/usr/bin/env python

"""
@file: natural.py

Sets all servos to closed position. ESC is kept unarmed. Assumes GPIO library
is already launched. Assumes frequency for the PCA9685 is already set.
"""

import adafruit_servokit
import pigpio

from gendefs import ESC, ESC_PW, close_servos


if __name__ == "__main__":
    pi = pigpio.pi()
    kit = adafruit_servokit.ServoKit(channels=16)

    # Servos are set to closed position.
    close_servos(kit)

    # ESC remains unarmed
    pi.set_servo_pulsewidth(ESC, ESC_PW.STOP.value)

位置移动代码的示例(将所有伺服器空挡,将x伺服器移动到该位置启动马达)

    #!/usr/bin/env python

"""
@file: serv0_pos1.py

Sets servo 0 to first open position, all other servos to closed position.
Assumes ESC is already in IDLE state.
"""

import adafruit_servokit

from gendefs import ServID, ServState, set_servo


if __name__ == "__main__":
    kit = adafruit_servokit.ServoKit(channels=16)

    # All servos are set to closed position.
    set_servo(kit, ServID.SERV_0, ServState.POS_1)

如果我在下面的代码中运行此代码,它将移动所有伺服器,武装电调并旋转电动机

    #!/usr/bin/env python

import time

import PCA9685
import pigpio

pi = pigpio.pi()

if not pi.connected:
   exit(0)

pwm = PCA9685.PWM(pi) # defaults to bus 1, address 0x40

pwm.set_frequency(50) # suitable for servos

for dc in range(5, 11):
    pwm.set_duty_cycle(-1, dc) # -1 for all channels
    time.sleep(1)

for pw in range(1000, 2050, 50):
    pwm.set_pulse_width(-1, pw) # -1 for all channels
    time.sleep(1)

pwm.cancel()

pi.stop()

我真的很困,我只剩下一个星期的时间来使这个演示工作,有人可以帮我解决这个问题吗?

0 个答案:

没有答案