openmv串口检测到数据
-
import sensor, image, time from pyb import UART import json uart = UART(3, 19200) uart.init(4800, bits=8, parity=None, stop=1, timeout_char=1000) target_threshold = (58, 76, -13, 8, -84, 70) ball_threshold =(58, 76, -13, 8, -84, 70)#待修改 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(10) sensor.set_auto_whitebal(False) clock = time.clock() def find_c(threshold,roi): number=0 max_size=0 blobs = img.find_blobs(threshold,roi=roi,x_stride=10, y_stride=50) for b in blobs: if b[2]*b[3] > max_size: max_size = b[2]*b[3] if max_size>2000: max_size=max_size img.draw_rectangle(b[0:4], color = (0, 0, 0)) img.draw_cross(b[5], b[6], color = (0, 0, 0)) number=b[5] return number def delay(a): #对画面进行延迟,不影响ide中的画面流畅度 b=0 for i in range(20): sensor.snapshot() if i == 18: b=a return b def delay1(a):#对画面进行延迟,不影响ide中的画面流畅度,并对串口发送数据进行延迟 for i in range(20): sensor.snapshot() if i == 18: output_str=json.dumps(a) uart.write(output_str+'\r\n') def average_zhi(num,n): sum=0 a=[] i=0 if num: a.append(num) i=i+1 if i==n-1: a.sort() for i in a[1,b-1]: sum=sum+i c=b-2 sum=int(sum/c) return sum while(True): clock.tick() img = sensor.snapshot() roi1=[0,0,70,60] roi2=[90,0,60,60] roi_ball=[0,0,160,60]#待修改 blobs1 = img.find_blobs([target_threshold],roi=roi1,x_stride=10, y_stride=50) blobs2 = img.find_blobs([target_threshold],roi=roi2,x_stride=10, y_stride=50) blobs_blob = img.find_blobs([ball_threshold],roi=roi_ball) if blobs1 and blobs2: first_number=find_c([target_threshold],roi1) second_number=find_c([target_threshold],roi2) average_number=(first_number+second_number)/2 average_number=int(average_number) if 60<average_number<100 and first_number and second_number: average=average_zhi(average_number,10) #delay1(average_number) #if average print(average_number) #uart.write('1') # time.sleep(1500) '''uart.write('2') time.sleep(1500) uart.write('3') time.sleep(1500) uart.write('4g') time.sleep(1500)'''
这是代码
openmv串口检测到数据,现在为了确保数据的稳定性,我想把十个数放到一个数组中,然后通过串口向单片机发送,我写好时候为什么只返回0,并没有我想要的坐标值
-
@3icr 我的这个函数为什么一直返回零
def average_zhi(num,n): sum=0 a=[] i=0 if num: a.append(num) i=i+1 if i==n-1: a.sort() for i in a[1,b-1]: sum=sum+i c=b-2 sum=int(sum/c) return sum
-
我不知道你的函数为什么错了,我没法检查。
-
@kidswong999 def average_zhi(num,n):
sum=0
a=[]
i=0
if num:
a.append(num)
i=i+1
if i==n-1:
a.sort()
for i in a[1,b-1]:
sum=sum+i
c=b-2
sum=int(sum/c)return sum
这个函数为什么一直返回零