我有一个正在研究的机器人,并且正在使用 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 运行,或者有更好的方法?