logo

两个MV之间通信怎么清空接收方UART.ANY()里的数据从而重新查询。

2018-05-07 12:46
G
_18229941233

import sensor, image, time
from pyb import UART
from pyb import Servo
import cpufreq
import pyb
threshold_index = 0 # 0 for red, 1 for green, 2 for blue

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
thresholds = [(58, 88, 57, -4, 20, 70)] # generic_red_thresholds
                                            # generic_green_thresholds
                                            # generic_blue_thresholds
threshold2 = [(30, 47, -39, 19, 20, 42)]
avg=[0,0,0,0,0,0,0,0,0,0,0,0]
sort2_servo=Servo(1)
led = pyb.LED(3)
#cpufreq.set_frequency(cpufreq.CPUFREQ_216MHZ)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. "merge=True" merges all overlapping blobs in the image.

uart=UART(3,115200)

def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3] > max_size:
            max_blob=blob
            max_size = blob[2]*blob[3]
    return max_blob

ROI=(0,0,1,1)

while(True):
    data = 0
    clock.tick()
    img = sensor.snapshot()
    sort2_servo.angle(-900)
    time.sleep(20)
    #time.sleep(10)
    blobs = img.find_blobs([thresholds[0]], pixels_threshold=80, area_threshold=850, merge=True)
    if blobs:
              max_blob = find_max(blobs)
              ROI = max_blob.rect()
              xc = max_blob.cx()
              yc = max_blob.cy()
              x = max_blob.x()
              y = max_blob.y()
              xn = int(xc-(xc-x)*7/10)
              yn = int(yc-(yc-y)*7/10)
              wn = (xc-xn)*2
              hn = (yc-yn)*2
              ROI = (xn, yn, wn, hn)
              img.binary(thresholds)
              #Bimg.erode(2)
              #img.dilate(1)
              statistics=img.get_statistics(roi=ROI)
              avg = statistics.mean()
              print(avg)
              if(uart.any()):
                   data = uart.readline()
                   
              if ( data!=0):#avg>=50 and avg<=95 or
                  print("bad")
                  print(data)
                  print(uart.any())
                  #led.on()
                  #time.sleep(150)
                  #ed.off()
                  sort2_servo.angle(0)       #添加时期2018.4.20
                  time.sleep(1000)        #添加时期2018.4.20
                  sort2_servo.angle(-900)           #添加时期2018.4.20
                  time.sleep(1000)        #添加时期2018.4.20
                  data = 0
              elif (avg>95):
                  sort2_servo.angle(-900)           #添加时期2018.4.20
                  time.sleep(100)
                  print("good")
                  print(uart.any())
    #print(clock.fps())
    #print(cpufreq.get_frequency())
2018-05-07 13:13
I
网红小智智 星瞳科技

直接uart.read(timeout_char=0)应该可以立即把数据读出来,就清空了缓冲区。

2018-05-07 13:31
G
_18229941233

@kidswong999 在 [两个MV之间通信怎么清空接收方UART\.ANY()里的数据从而重新查询。](/post/2185) 中说:

> uart.read(timeout_char=0)

e414431c-52aa-402c-9b4f-972fc59bf52a.jpg

但是报错了大佬。