这边是嫁接过去的关于串口和超声波部分的代码,将其中处理图像的部分改掉了
# gray_blob - By: jinyuying - 鍛ㄤ簲 浜旀湀 12 2017
import sensor,time,pyb,math,time,utime
from pyb import Pin, Timer, LED, UART
#榛戣壊鐐归槇鍊?
#black_threshold = [(0, 64)]
black_threshold = [(0, 84)]
#楂樺害鏁版嵁
highcnt = 0.0
high = 0
#xy骞抽潰璇樊鏁版嵁
err_x = 0
err_y = 0
#瓒呭0娉㈠洖璋冨鐞嗗嚱鏁?
def Ultrasound(line):
if(Echo.value()==1):
tim_count.init(prescaler=1799, period=1176)#鎵撳紑瀹氭椂鍣?
if(Echo.value()==0):
global highcnt
tim_count.deinit()
highcnt = tim_count.counter()#璁℃暟
uart.writechar(0x55)
uart.writechar(0xAA)
uart.writechar(0x10)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(high>>8)
uart.writechar(high)
uart.writechar(err_x>>8)
uart.writechar(err_x)
uart.writechar(err_y>>8)
uart.writechar(err_y)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0xAA)
print(high)
##閫夋嫨鎸夐敭鍥炶皟澶勭悊鍑芥暟
#def selection(IRQ_RISING):
#if(selectpin.value()==1) :
#pyb.delay(100)
#if(selectpin.value()==1):
##纭鎸夐敭鍥炶皟澶勭悊鍑芥暟
#def enter(IRQ_RISING):
#if(enterpin.value()==1) :
#pyb.delay(100)
#if(enterpin.value()==1):
#瓒呭0娉㈠彂灏勭鍙i厤缃?
timpwm = Timer(4, freq=60) #瓒呭0娉?0璧吂鍙戝皠棰戠巼
ch1 = timpwm.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width=80) #100us鍙戝皠瑙?
#瓒呭0娉㈡帴鏀剁鍙i厤缃?
tim_count = pyb.Timer(1) #瀹氭椂鍣ㄨ鏁?
extint = pyb.ExtInt('P0', pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_DOWN,Ultrasound)#寮€鍚閮ㄤ腑鏂?
Echo = Pin('P0', Pin.IN, Pin.PULL_DOWN)
#鎸夐敭閰嶇疆
#select_Key = pyb.ExtInt('P2', pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_UP, selection)
#selectpin = Pin('P2', Pin.IN, Pin.PULL_UP)#閫夋嫨鎸夐敭
#enter_Key = pyb.ExtInt('P3', pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_UP, enter)
#enterpin = Pin('P3', Pin.IN, Pin.PULL_UP)#纭鎸夐敭
#涓插彛涓夐厤缃?
uart = UART(3, 115200)
uart.init(115200, bits=8, parity=None, stop=1)
#115200
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)#璁剧疆鐏板害淇℃伅
sensor.set_framesize(sensor.QQVGA)#璁剧疆鍥惧儚澶у皬
sensor.skip_frames(20)#鐩告満鑷鍑犲紶鍥剧墖
sensor.set_auto_whitebal(False)#鍏抽棴鐧藉钩琛?
clock = time.clock()#鎵撳紑鏃堕挓
while(True):
clock.tick()
img = sensor.snapshot()
high = int(1.7*highcnt)
blobs = img.find_blobs(black_threshold)
if blobs:
most_pixels = 0
largest_blob = 0
for i in range(len(blobs)):
#鐩爣鍖哄煙鎵惧埌鐨勯鑹插潡鍙兘涓嶆涓€涓紝鎵惧埌鏈€澶х殑涓€涓?
if blobs[i].pixels() > most_pixels:
most_pixels = blobs[i].pixels()
largest_blob = i
#浣嶇疆鐜敤鍒扮殑鍙橀噺
err_x = int(80 - blobs[largest_blob].cx())
err_y = int(60 - blobs[largest_blob].cy())
print(blobs[largest_blob])
img.draw_rectangle(blobs[largest_blob].rect())
img.draw_cross(blobs[largest_blob].cx(),blobs[largest_blob].cy())#璋冭瘯浣跨敤
else:
err_x = 0
err_y = 0