2018-09-19 20:14:51
import sensor, image, time import car import json import time from pyb import UART from pid import PID sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # use RGB565. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed. sensor.skip_frames(30) # Let new settings take affect. sensor.set_auto_whitebal(False) # 追踪颜色需要关闭白平衡 sensor.set_auto_gain(False)#扫码需要关闭自动增益 clock = time.clock() # Tracks FPS. red_threshold = (13, 49, 18, 61, 6, 47) #红色阈值 size_threshold = 2000 x_pid = PID(p=0.5, i=1, imax=100) h_pid = PID(p=0.05, i=0.1, imax=50) uart = UART(3, 115200) ma_flag = 0 ma = [] ''' #串口通信: 测试json数据 {(1,22),(-3,33),(22222,0),(9999,12),(0,0)} data=[] data_out = json.dumps(set(data)) uart.write(data_out +'\n') uart.write("Hello World!\r") ''' ###################### ''' #扫码反馈信息 img.lens_corr(1.8) # 缩小视野 for code in img.find_qrcodes(): print(code) ''' def find_ma(): global ma_flag if ma_flag : print("已完成扫码。") else : ma = img.find_qrcodes() if ma: print("发现二维码即将停车进行扫码") saoma() ma_flag = 1 else: print("正在寻找二维码。。。") def saoma(): img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens. for code in img.find_qrcodes(): print(code) 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 while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() find_ma() ''' blobs = img.find_blobs([red_threshold]) if blobs: max_blob = find_max(blobs) x_error = max_blob[5]-img.width()/2 h_error = max_blob[2]*max_blob[3]-size_threshold # print("x error: ", x_error) print("测试与arduino通信") ###下面代码为测试与arduino通信发送坐标#### data=[] for b in blobs: # Draw a rect around the blob. img.draw_rectangle(b[0:4]) # rect img.draw_cross(b[5], b[6]) # cx, cy data.append((b.cx(),b.cy())) data_out = json.dumps(set(data)) uart.write(data_out +'\n') ################################### img.draw_rectangle(max_blob[0:4]) # rect img.draw_cross(max_blob[5], max_blob[6]) # cx, cy x_output=x_pid.get_pid(x_error,1) h_output=h_pid.get_pid(h_error,1) print("h_output",h_output) car.run(-h_output-x_output,-h_output+x_output) else: car.run(10,-10) #旋转寻找小球 '''