简体   繁体   English

运行我的机器人的python代码会返回语法错误,但我不知道该错误是什么

[英]Running the python code of my robot returned a syntax error and I do not know what the error is

I am doing a obstacle avoiding robot as a project for school. 我在避开机器人作为学校项目的障碍。 I am trying to run this Python code below but a syntax error has shown up. 我正在尝试在下面运行此Python代码,但是出现了语法错误。 The robot is supposed to move but it is not working hence no movement from the robot. 机器人应该移动,但不能工作,因此机器人也没有运动。 I cannot find the reason for this error. 我找不到此错误的原因。 Can you suggest what I might have wrong in the code, please? 您能建议我在代码中有什么问题吗?

import RPi.GPIO as GPIO  #Import GPIO library
import time              #Import time library
import traceback
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)   #GPIO are set in BCM format
TRIG = 24
ECHO = 27
##led = 22
m11 = 4
m12 = 26
m21 = 22
m22 = 23
GPIO.setup(TRIG,GPIO.OUT)   #Initialize GPIO Pin as outputs
GPIO.setup(ECHO,GPIO.IN)    #Initialize GPIO Pin as input
##GPIO.setup(led,GPIO.OUT)                  
GPIO.setup(m11,GPIO.OUT)
GPIO.setup(m12,GPIO.OUT)
GPIO.setup(m21,GPIO.OUT)
GPIO.setup(m22,GPIO.OUT)
##GPIO.output(led, True)
time.sleep(5)
def stop(): ##Stops the robot
    print("STOP")
    GPIO.output(m11, False)
    GPIO.output(m12, False)
    GPIO.output(m21, False)
    GPIO.output(m22, False)
def forward(): ##The robot moves forward
    GPIO.output(m11, True)
    GPIO.output(m12, False)
    GPIO.output(m21, True)
    GPIO.output(m22, False)
    print("Forward")
def back(): ##The robot moves backwards
    GPIO.output(m11, False)
    GPIO.output(m12, True)
    GPIO.output(m21, False)
    GPIO.output(m22, True)
    print("Back")
def left(): ##The robot turns to the left
    GPIO.output(m11, False)
    GPIO.output(m12, False)
    GPIO.output(m21, True)
    GPIO.output(m22, False)
    print("Left")
def right(): ##The robot turns to the right
    GPIO.output(m11, True)
    GPIO.output(m12, False)
    GPIO.output(m21, False)
    GPIO.output(m22, False)
    print("Right")
stop()
count = 0

try:
    while(True):
        i = 0
        avgDistance = 0
        for i in range(5):
            GPIO.output(TRIG, False)  #Set TRIG as LOW
            time.sleep(0.1)           #Delay
            GPIO.output(TRIG, True)   #Set TRIG as HIGH
            time.sleep(0.00001)       #Delay of 0.00001 seconds
            GPIO.output(TRIG, False)  #Set TRIG as LOW
        while(GPIO.input(ECHO)==False): #Check whether the ECHO is LOW             
            pulse_start = time.time()
        while(GPIO.input(ECHO)==True): #Check whether the ECHO is HIGH
            pulse_end = time.time()
        pulse_duration = pulse_end - pulse_start #time to get back the pulse to sensor
        distance = pulse_duration * 17150        #Multiply pulse duration by 17150 (34300/2) to get distance
        distance = round(distance,2)  #Round to two decimal points
        avgDistance = avgDistance + distance
        avgDistance = avgDistance / 5
        print(avgDistance)
        flag = 0
        if(avgDistance < 15): #Check whether the distance is within 15 cm range
            count = count + 1
            stop()
            time.sleep(1)
            back()
            time.sleep(1.5)
        if((count%3 == 1) & (flag == 0)):
            right()
            flag = 1
        else:
            left()
            flag = 0
            time.sleep(1.5)
            stop()
            time.sleep(1)
        else:
            forward()
            flag = 0
except Exception:
    traceback.print_exc()
finally:
    GPIO.cleanup()

You have two closing else blocks, bad indentation, probably you meant: 您有两个关闭else块,缩进不良,可能是您的意思:

    ...
    if(avgDistance < 15): #Check whether the distance is within 15 cm range
        count = count + 1
        stop()
        time.sleep(1)
        back()
        time.sleep(1.5)
        if((count%3 == 1) & (flag == 0)):
            right()
            flag = 1
        else:
            left()
            flag = 0
            time.sleep(1.5)
            stop()
            time.sleep(1)
    else:
        forward()
        flag = 0
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM