我希望我的程序是: 当我从主菜单选择中输入"3"键时>"N"键返回主菜单选择。
但现在它声明"预期的缩进块"
def Menu():
这是我的完整编码:
import RPi.GPIO as GPIO
import Slush
import math
import time
import sys
from inputs import get_key
from time import sleep
b = Slush.sBoard()
m = [Slush.Motor(0), Slush.Motor(1), Slush.Motor(2), Slush.Motor(3)]
m[0].setMaxSpeed(150)
m[1].setMaxSpeed(150)
m[2].setMaxSpeed(250)
m[3].setMaxSpeed(150)
m[0].setCurrent(150, 150, 150, 150)
m[1].setCurrent(100, 100, 100, 100)
m[2].setCurrent(150, 150, 150, 150)
m[3].setCurrent(100, 100, 100, 100)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50)
pwm.start(0)
GPIO.output(18, 1)
def Menu():
print("Please Enter Number From List Below:")
print("1: Start Program")
print("2: Control Manually")
print("3: Exit Program")
inputkey = int(input("Enter a number & Press Enter:"))
#START PROGRAM
if inputkey == 1:
print("Press Home Key To Start Program")
#def_StartProgram()
while 1:
events = get_key()
for event in events:
if event.code == 'KEY_HOME':
print("PROGRAM START")
print("nMotor0 Position is", m[0].getPosition())
print("nMotor1 Position is", m[1].getPosition())
print("nMotor2 Position is", m[2].getPosition())
print("nMotor3 Position is", m[3].getPosition())
m[1].goTo(-10000)
time.sleep(3)
print("nMotor1 Position is", m[1].getPosition())
#CONTROL MANUAL
if inputkey == 2:
print("Manual Mode")
while 1:
events = get_key()
for event in events:
if event.code == 'KEY_Q':
value = event.state
if value == 1:
if not m[0].isBusy(): m[0].run(1, 35)
if event.code == 'KEY_A':
value = event.state
if value == 1:
if not m[0].isBusy(): m[0].softStop()
if event.code == 'KEY_Z':
value = event.state
if value == 1:
if not m[0].isBusy(): m[0].run(0, 35)
if event.code == 'KEY_W':
value = event.state
if value == 1:
if not m[1].isBusy(): m[1].run(1, 20)
if event.code == 'KEY_S':
value = event.state
if value == 1:
if not m[1].isBusy(): m[1].softStop()
if event.code == 'KEY_X':
value = event.state
if value == 1:
if not m[1].isBusy(): m[1].run(0, 20)
if event.code == 'KEY_E':
value = event.state
if value == 1:
if not m[2].isBusy(): m[2].run(1, 100)
if event.code == 'KEY_D':
value = event.state
if value == 1:
if not m[2].isBusy(): m[2].softStop()
if event.code == 'KEY_C':
value = event.state
if value == 1:
if not m[2].isBusy(): m[2].run(0, 100)
if event.code == 'KEY_R':
value = event.state
if value == 1:
if not m[3].isBusy(): m[3].run(1, 10)
if event.code == 'KEY_F':
value = event.state
if value == 1:
if not m[3].isBusy(): m[3].softStop()
if event.code == 'KEY_V':
value = event.state
if value == 1:
if not m[3].isBusy(): m[3].run(0, 10)
if event.code == 'KEY_1':
value = event.state
if value == 1:
pwm.ChangeDutyCycle(1)
sleep(0.5)
if not pwm.stop(): pwm.start(0)
if event.code == 'KEY_3':
value = event.state
if value == 1:
if not pwm.ChangeDutyCycle(0): pwm.ChangeDutyCycle(99)
#EXIT PROGRAM
if inputkey == 3:
print("Press 'Y/N' key = Exit/Back To Menu ")
while 1:
events = get_key()
for event in events:
if event.code == 'KEY_Y':
sys.exit(0)
if event.code == 'KEY_N':
Menu()
这就是我希望我的程序工作的方式。流程图: 在此处输入图像描述
您错过了def 菜单中的缩进。请尝试以下代码,因为我不知道您的确切逻辑,因此该程序可能无法根据您的需要工作,但您会对逻辑有一个公平的想法。
import RPi.GPIO as GPIO
import Slush
import math
import time
import sys
from inputs import get_key
from time import sleep
b = Slush.sBoard()
m = [Slush.Motor(0), Slush.Motor(1), Slush.Motor(2), Slush.Motor(3)]
m[0].setMaxSpeed(150)
m[1].setMaxSpeed(150)
m[2].setMaxSpeed(250)
m[3].setMaxSpeed(150)
m[0].setCurrent(150, 150, 150, 150)
m[1].setCurrent(100, 100, 100, 100)
m[2].setCurrent(150, 150, 150, 150)
m[3].setCurrent(100, 100, 100, 100)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50)
pwm.start(0)
GPIO.output(18, 1)
def Menu():
while 1:
print("Please Enter Number From List Below:")
print("1: Start Program")
print("2: Control Manually")
print("3: Exit Program")
inputkey = int(input("Enter a number & Press Enter:"))
#START PROGRAM
if inputkey == 1:
print("Press Home Key To Start Program")
#def_StartProgram()
while 1:
events = get_key()
for event in events:
if event.code == 'KEY_HOME':
print("PROGRAM START")
print("nMotor0 Position is", m[0].getPosition())
print("nMotor1 Position is", m[1].getPosition())
print("nMotor2 Position is", m[2].getPosition())
print("nMotor3 Position is", m[3].getPosition())
m[1].goTo(-10000)
time.sleep(3)
print("nMotor1 Position is", m[1].getPosition())
#CONTROL MANUAL
if inputkey == 2:
print("Manual Mode")
while 1:
events = get_key()
for event in events:
if event.code == 'KEY_Q':
value = event.state
if value == 1:
if not m[0].isBusy(): m[0].run(1, 35)
if event.code == 'KEY_A':
value = event.state
if value == 1:
if not m[0].isBusy(): m[0].softStop()
if event.code == 'KEY_Z':
value = event.state
if value == 1:
if not m[0].isBusy(): m[0].run(0, 35)
if event.code == 'KEY_W':
value = event.state
if value == 1:
if not m[1].isBusy(): m[1].run(1, 20)
if event.code == 'KEY_S':
value = event.state
if value == 1:
if not m[1].isBusy(): m[1].softStop()
if event.code == 'KEY_X':
value = event.state
if value == 1:
if not m[1].isBusy(): m[1].run(0, 20)
if event.code == 'KEY_E':
value = event.state
if value == 1:
if not m[2].isBusy(): m[2].run(1, 100)
if event.code == 'KEY_D':
value = event.state
if value == 1:
if not m[2].isBusy(): m[2].softStop()
if event.code == 'KEY_C':
value = event.state
if value == 1:
if not m[2].isBusy(): m[2].run(0, 100)
if event.code == 'KEY_R':
value = event.state
if value == 1:
if not m[3].isBusy(): m[3].run(1, 10)
if event.code == 'KEY_F':
value = event.state
if value == 1:
if not m[3].isBusy(): m[3].softStop()
if event.code == 'KEY_V':
value = event.state
if value == 1:
if not m[3].isBusy(): m[3].run(0, 10)
if event.code == 'KEY_1':
value = event.state
if value == 1:
pwm.ChangeDutyCycle(1)
sleep(0.5)
if not pwm.stop(): pwm.start(0)
if event.code == 'KEY_3':
value = event.state
if value == 1:
if not pwm.ChangeDutyCycle(0): pwm.ChangeDutyCycle(99)
#EXIT PROGRAM
if inputkey == 3:
print("Press 'Y/N' key = Exit/Back To Menu ")
exit_flag = True
while exit_flag:
events = get_key()
for event in events:
if event.code == 'KEY_Y':
sys.exit(0)
elif event.code == 'KEY_N':
exit_flag = False
break
Menu()