0

我正在使用涂鸦机器人并用 Python 编写代码。当它看到障碍物时,我试图让它停下来

所以我为左障碍物传感器、中心障碍物传感器和右障碍物传感器创建了变量

    left = getObstacle(0)
    center = getObstacle(1)
    right = getObstacle(2)

然后是一个 if 语句

if (left < 6400 & center < 6400 & right < 6400):
        forward(1,1)
    else:
        stop()

基本上这个想法是,如果传感器读数小于 6400,它应该向前移动,否则,它应该停止。在使用该功能测试涂鸦器时senses,我注意到当我将机器人靠近一个物体时,它的读数约为 6400。

这是我的main()代码

def main():
      while True: 
        left = getObstacle(0)
        center = getObstacle(1)
        right = getObstacle(2)
        lir = getIR(0)
        rir = getIR(1)
    if (left < 6400 & center < 6400 & right < 6400):
        forward(1,1)
    else:
        stop()

为什么我的机器人没有响应?当我将 Python 代码放入 shell 时,它没有显示任何错误,但我的机器人没有发生任何事情。

编辑:

一些代码更改。到目前为止,机器人会移动,但不会停止。我的 if 和 else 语句不正确吗?

center = getObstacle(1)
def main():


    if (center < 5400):
        forward(0.5)
    else:
        stop()
4

2 回答 2

0

听起来您与原始代码很接近:

def main():
    while True: 
        left = getObstacle(0)
        center = getObstacle(1)
        right = getObstacle(2)
        #lir = getIR(0)
        #rir = getIR(1)
        if (left < 6400 and center < 6400 and right < 6400):
            forward(1, 0.1)
        else:
            stop()
            break

这个循环在每次循环中都测量left执行比较。我已将调用修改为仅移动十分之一秒,然后再进行更多测量。同样,当条件不满足时,机器人和循环都将停止。centerright forward

顺便说一句,您似乎不使用lirand rir

于 2014-10-12T21:10:39.440 回答
0

&位运算符

and逻辑 AND 运算符

所以你的情况应该是:

if (left < 6400 and center < 6400 and right < 6400):
    forward(1,1)
else:
    stop()
于 2014-10-12T16:44:33.210 回答