0

我在树莓派上使用两个直流电机,一个带有 L298N,第二个带有 BTS 驱动器。我创建了一个单独的文件并创建了单独的函数来启动和停止单个电机。我已经通过单独调用它们以及一起调用它们来运行两个电机来测试该功能。现在问题来了。我在我的主文件中调用 L298N 电机驱动器的函数。但它不起作用。这是我的电机代码将 RPi.GPIO 导入为 GPIO

GPIO.setwarnings(False)
从时间导入睡眠

导入库

从 RpiMotorLib 导入 rpiservolib

myservotest = rpiservolib.SG90servo("servoone", 50, 2, 12)

rPwm = 24
lPwm = 23
rEn = 25
lEn = 22

这些引脚用于新的 BTS 而不是 L298N 驱动器

rPwm2 = 6
lPwm2 = 5
rEn2 = 16
lEn2 = 12

用于主直流电机移动臂

GPIO.setmode(GPIO.BCM)
GPIO.setup(rPwm, GPIO.OUT)
GPIO.setup(lPwm, GPIO.OUT)
GPIO.setup(rPwm2, GPIO.OUT)
GPIO.setup(lPwm2, GPIO.OUT)

GPIO.setup(rEn, GPIO.OUT)
GPIO.setup(lEn, GPIO.OUT)
GPIO.setup(rEn2, GPIO.OUT)
GPIO.setup(lEn2, GPIO.OUT)

GPIO.output(rPwm, GPIO.LOW)
GPIO.output(lPwm, GPIO.LOW)
GPIO.output(rEn, GPIO.HIGH)
GPIO.output(lEn, GPIO.HIGH)

GPIO.output(rPwm2, GPIO.LOW)
GPIO.output(lPwm2, GPIO.LOW)
GPIO.output(rEn2, GPIO.HIGH)
GPIO.output(lEn2, GPIO.HIGH)

rSpeed = GPIO.PWM(rPwm, 1000)
rSpeed2 = GPIO.PWM(rPwm2, 1000)
lSpeed = GPIO.PWM(lPwm, 1000)
lSpeed2 = GPIO.PWM(lPwm2, 1000)
rSpeed.start(100)
lSpeed.start( 100)

用于第 2 直流电机

GPIO.setup(rPwm2, GPIO.OUT)
GPIO.setup(lPwm2, GPIO.OUT)
GPIO.setup(rEn2, GPIO.OUT)
GPIO.setup(lEn2, GPIO.OUT)

GPIO.output(rPwm2, GPIO.LOW)
GPIO.output(lPwm2, GPIO.LOW)
GPIO.output(rEn2, GPIO.HIGH)
GPIO.output(lEn2, GPIO.HIGH)

rSpeed2.start(100)
lSpeed2.start(100)

def backwardFullSpeedBTS():
'''这个函数将在正向全速移动 BTS 电机'''
rSpeed.ChangeDutyCycle(10)
lSpeed.ChangeDutyCycle(0)
# GPIO.output(rPwm,GPIO.HIGH)
# GPIO.output (lPWM,GPIO.LOW)

def forwardFullSpeedBTS():
'''这个函数将使 BTS 电机向后全速移动'''
rSpeed.ChangeDutyCycle(0)
lSpeed.ChangeDutyCycle(10)
# GPIO.output(rPwm,GPIO.LOW)
# GPIO.output (lPWM,GPIO.HIGH)

def stopBTSMotor():
'''这个函数将停止 BTS 电机'''
# GPIO.output(rPwm,GPIO.LOW)
# GPIO.output(lPwm,GPIO.LOW)
rSpeed.ChangeDutyCycle(0)
lSpeed.ChangeDutyCycle(0 )

def backwardAtSpeedBTS(speed):
'''这个函数将在正向全速移动 BTS 电机'''
rSpeed.ChangeDutyCycle(speed)
lSpeed.ChangeDutyCycle(0)
# GPIO.output(rPwm,GPIO.HIGH)
# GPIO.输出(lPwm,GPIO.LOW)

def forwardAtSpeedBTS(speed):
'''这个函数将在向后方向全速移动 BTS 电机'''
rSpeed.ChangeDutyCycle(0)
lSpeed.ChangeDutyCycle(speed)
# GPIO.output(rPwm,GPIO.LOW)
# GPIO。输出(lPwm,GPIO.HIGH)


def start2ndMotor():
'''此函数将在正向全速移动第二个 BTS 电机'''
rSpeed2.ChangeDutyCycle(80)
lSpeed2.ChangeDutyCycle(0)

def stop2ndMotor():
# 停止连接在 BTS 的第二个直流电机
rSpeed2.ChangeDutyCycle(0)
lSpeed2.ChangeDutyCycle(0)


def closeServo():
'''这个函数将关闭手臂来捡球'''
# 将伺服从 120 移动到 60
myservotest.servo_move(7, 11, 1, False, 1)

def openServo():
# 将伺服从 90 移动到 120
myservotest.servo_move(7, 7, 1, True, 1)
<\code>

这就是我导入函数的
方式 **from motorFunction import * ** from MotorBTS import *
def onStart():
global motorPosition
print("On Start")
root.update()
closeServo()
while GPIO.input(gpio_ButtonMStop) != 0:
"""将手臂向外移动直到按下停止按钮"""
forwardAtSpeedBTS(20)
pass
# 按下停止按钮后停止移动直流电机
stopBTSMotor() # 停止电机
cTime = float(time.time())
*** start2ndMotor()***
while checkDelay(cTime, 0.5):
# 等待 0.5 秒
通过
stop2ndMotor()
while GPIO.input(gpio_ButtonPuckMotor) != 0:
"""将圆盘电机移动到零位置"""
motorPuck.motor_go(False, # True=顺时针, False=逆时针
"Full", # Step type (Full,Half ,1/4,1/8,1/16,1/32)
100, # 步数
.0005, # step delay [sec]
False, # True = print verbose output
.05) # initial delay [sec]
print (“完成”)
当我调用 **start2ndMotor()** 在启动时它不会运行电机,只有第一个被称为 **forwardAtSpeedBTS(20)** 的电机工作。
4

0 回答 0