需要帮助合并两个代码吗

2024-04-28 12:37:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力使一个自动汽车,有线传感器和超声波传感器。如果左右两侧的线路传感器被激活,汽车需要向前行驶

if pin16.read_digital == 1 & pin 15.read_digital == 1
   pin2.write_anlog(180)

上面的代码工作正常。完成了线传感器和超声波传感器的集成。完成代码如下:

from microbit import *
from neopixel import NeoPixel
import time
import machine

def setup():
    display.off()
    np = NeoPixel(pin0, 5)
    np.clear()

    pin4.set_pull(pin4.PULL_UP)
    pin1.set_analog_period(20)
    pin2.set_analog_period(20)

    pin15.set_pull(pin15.PULL_UP)
    pin16.set_pull(pin16.PULL_UP)

def distance():
    pin3.write_digital(0)
    time.sleep_us(2)

    pin3.write_digital(1)

    time.sleep_us(10)
    pin3.write_digital(0)

    output = machine.time_pulse_us(pin4, 1)

    return output * 0.034 / 2

def forward():
    pin2.write_analog(1)

def backward():
    pin2.write_analog(180)

def stop():
    pin2.write_analog(90)

def line():
    if pin15.read_digital() == 1 & pin16.read_digital() == 1:
        return True
else:
    return False

setup()
# pin1.write_analog(90)
sleep(200)
# pin1.write_analog(70) right
# pin1.write_analog(150) right


while True:
    if (distance() > 5) & line():
        forward()
    else:
        stop()

这很好用,但是当我试着把左边和右边放进去时,它不起作用。我该怎么解决这个问题?左边和右边的代码如下

while True:
if pin15.read_digital() == 1 & pin16.read_digital() == 0:
    pin1.write_analog(180)
    pin2.write_analog(95)
    time.sleep(500)
    pin2.write_analog(90)
elif pin15.read_digital() == 0 & pin16.read_digital() == 1:
    pin1.write_analog(70)
    pin2.write_analog(180)
    time.sleep(500)
    pin2.write_analog(90)

Tags: importreadiftimedefsleep传感器write
2条回答

我不是Arduino和Neopix图书馆的专家。但是,您的第三个代码块中是否缺少forward case?另外,最好使用and而不是&,因为&是位运算符,在运算符优先规则中的排名高于==,正如tobias泷k所说(如果愿意,可以使用括号来解决这个问题,但为什么更喜欢位运算符呢?)。顺便说一下,不需要==运算符。如果整数为0,则在if语句中会自动计算为False,否则为True:

while True:
    read_left, read_right = pin15.read_digital(), pin16.read_digital()
    if read_left and read_right:
        # your code to go forward
    elif read_left and not read_right:
        # your code to turn left
    elif not read_left and read_right:
        # your code to turn right
    else:
        # your code when no line is detected, if you want to handle this case

您应该使用and而不是&&位and,其结合力强于==,而and逻辑and,其结合力弱于==。你知道吗

>>> a, b = 0, 1                                                             
>>> a == 0 and b == 1                                                       
True
>>> a == 0 & b == 1                                                         
False

后一个例子对应于您的if语句,它被解析为a == (0 & 1) == 1,即0 == 0 == 1(通过Python比较链,它不是(0 == 0) == 1,它将计算为True,而是(0 == 0) and (0 == 1))。你知道吗

对于a == 1 & b == 1来说,这不是问题,但在所有其他情况下,它的行为都会出人意料。你知道吗

>>> cases = [(0, 0), (0, 1), (1, 0), (1, 1)]                               
>>> [a == 0 & b == 0 for a, b in cases] # should be [True, False, False, False]
[True, True, False, False]              # but evaluates as "a == 0 == 0"
>>> [a == 0 & b == 1 for a, b in cases] # should be [False, True, False, False]
[False, False, False, False]            # but evaluates as "a == 0 == 1"
>>> [a == 1 & b == 0 for a, b in cases] # should be [False, False, True, False]
[True, False, False, False]             # but evaluates as "a == b == 0"
>>> [a == 1 & b == 1 for a, b in cases] # should be [False, False, False, True]
[False, False, False, True]             # evaluates as "a == b == 1" ("correct")

相关问题 更多 >