logo

关于if语句和led

2019-04-08 15:04
I
zkoc
# Blob Detection and uart transport
import sensor, image, time,pyb
from pyb import UART
import json
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
red_threshold   = (233,255)
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.
led1 =pyb.LED(1)
led2 =pyb.LED(2)
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.


K=1020 #the value should be measured 20*51
K2=0.142
#实际大小=k*直径的像素 k=8cm/51

uart = UART(3, 115200)
def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob.pixels() > max_size:
            max_blob=blob
            max_size = blob.pixels()
    return max_blob

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.
    img.binary([red_threshold])



    blobs = img.find_blobs([red_threshold])
    if len(blobs) == 1:
        # Draw a rect around the blob.
        b = blobs[0]
        img.draw_rectangle(b[0:4]) # rect
        img.draw_cross(b[5], b[6]) # cx, cy
        Lm = (b[2]+b[3])/2
        length = K/Lm
        size=K2*Lm

        h = K2*b[3]
        w = K2*b[2]
        #print("高:%s cm, 宽:%s cm,"% (h,w))
        if h>5:led1.on() and led2.on()     
        elif h<5:led1.off() and led2.off()
            
        output_str="%s" % h #方式1
        #output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2
        print('you send:',output_str)
        uart.write(output_str+'\r\n')
    else:
        print('not found!')

为什么执行这代码只有led1亮,而led2不亮

2019-04-09 20:57
I
网红小智智 星瞳科技
if h>5:led1.on() and led2.on()     
elif h<5:led1.off() and led2.off()

这句代码逻辑不对。