1

我有一个正在研究的机器人,并且正在使用 PWM 控制它。我控制它的方式是使用设置 PWM 然后退出的脚本。我需要它来设置 PWM 然后继续运行。我需要它退出的原因是因为每次 x 和 y 值更改时我只是通过 ssh 连接调用此脚本。程序退出后正常数字输出继续,但 PWM 与我设置的方式不同。

到目前为止,这是我的代码。它包含许多打印语句,试图帮助我找出问题所在。

#filename: setMotors.py
import RPi.GPIO as GPIO
from sys import argv
from time import sleep

MOTOR_EN_1_PIN = 14
MOTOR_A_1_PIN = 15
MOTOR_B_1_PIN = 18

MOTOR_EN_2_PIN = 23
MOTOR_A_2_PIN = 24
MOTOR_B_2_PIN = 25


def mixXY(x, y):
    """
    mixes x and y from a joystick to values for a 2 motor drive system
    input: x (int or float), y (int or float)
    output: (leftMotor (float), rightMotor (float)) tuple
    """
    leftMotor = y + x
    rightMotor = y - x

    return (leftMotor, rightMotor)

def setMotorPWMS(leftMotor, rightMotor):
    #left motor
    if leftMotor == 0:
        print("left motor 0")
        GPIO.output(MOTOR_EN_1_PIN, 0)
        motor1A.stop()
        motor1B.stop()
    elif leftMotor < 0:
        print("left motor < 0")
        GPIO.output(MOTOR_EN_1_PIN, 1)
        motor1A.stop()
        motor1B.ChangeDutyCycle(abs(leftMotor))
    else:
        print("left motor else")
        GPIO.output(MOTOR_EN_1_PIN, 1)
        motor1A.ChangeDutyCycle(leftMotor)
        motor1B.stop()

    #right motor
    if rightMotor == 0:
        print("right motor 0")
        GPIO.output(MOTOR_EN_2_PIN, 0)
        motor2A.stop()
        motor2B.stop()
    elif rightMotor < 0:
        print("right motor < 0")
        GPIO.output(MOTOR_EN_2_PIN, 1)
        motor2A.stop()
        motor2B.ChangeDutyCycle(abs(rightMotor))
    else:
        print("right motor else")
        GPIO.output(MOTOR_EN_2_PIN, 1)
        motor2A.ChangeDutyCycle(rightMotor)
        motor2B.stop()



GPIO.setwarnings(False)

#setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(MOTOR_EN_1_PIN, GPIO.OUT)
GPIO.setup(MOTOR_A_1_PIN, GPIO.OUT)
GPIO.setup(MOTOR_B_1_PIN, GPIO.OUT)

GPIO.setup(MOTOR_EN_2_PIN, GPIO.OUT)
GPIO.setup(MOTOR_A_2_PIN, GPIO.OUT)
GPIO.setup(MOTOR_B_2_PIN, GPIO.OUT)

motor1A = GPIO.PWM(MOTOR_A_1_PIN, 50)
motor1B = GPIO.PWM(MOTOR_B_1_PIN, 50)
motor2A = GPIO.PWM(MOTOR_A_2_PIN, 50)
motor2B = GPIO.PWM(MOTOR_B_2_PIN, 50)
motor1A.start(0)
motor1B.start(0)
motor2A.start(0)
motor2B.start(0)


if len(argv) <= 2:
    print("Need to call with x and y from commandline")
else:
    motorPWM = mixXY(int(argv[1]), int(argv[2]))
    leftMotorPWM = motorPWM[0]
    rightMotorPWM = motorPWM[1]
    print("left motor:",leftMotorPWM)
    print("right motor:", rightMotorPWM)
    setMotorPWMS(leftMotorPWM, rightMotorPWM)
    sleep(5)
    print("done")

它的调用方式是 with sudo python setMotors.py x y。有没有办法在程序退出后保持 PWM 运行,或者有更好的方法?

4

1 回答 1

0

RaspberryPi 不支持硬件 PWM,因此使用软件循环进行仿真。基本上,它设置 GPIO,休眠一点,重置 GPIO,休眠一点,然后循环。这是在一个单独的线程中完成的,该线程在程序结束时被终止。

因此,您必须找到一种方法让您的程序在后台保持活跃。如果您查看有关使用 PWM 的官方示例,您会注意到使程序保持活动状态直到手动终止的无限循环。

您还应该添加类似

try:
    while 1:
        time.sleep(0.5)
except KeyboardInterrupt:
    pass
p.stop()
GPIO.cleanup()

到程序的末尾,或者使用信号模块更好地构建。

然后,在销毁控制台之前将进程留在后台

sudo python setMotors.py x y &

你也可以让你的程序被守护

于 2015-03-17T00:37:08.823 回答