P7 if 条件 P8 if else 条件 P9 if elif else

本文介绍了Python中的条件控制结构,包括基本的if和if-else语句,以及使用比较运算符的实例。通过实例演示了如何判断变量之间的大小关系,并探讨了嵌套条件的情况。此外,还讲解了if-elif-else结构,用于根据多个条件进行程序流程控制。

P7 if条件 

x = 1
y = 2

if x > y:
    print('x is greater than y.')

输出为empty

x = 1
y = 2

if x < y:
    print('x is less than y.')

输出为x is less than y.

<

>

<=

>=

!=

==        注意等于不是一个=而是==

P8  if else 条件

x = 1
y = 2

if x > y:
    print('x is greater than y.')
else:
    print('x is less than or equal to y.')

注意if和else语句都以冒号结尾

注意格式

P9 if elif else

x = 1

if x > 1:
    print('x > 1')
elif x < -1:
    print('x < -1')
elif x < 1:
    print('x < 1')
else:
    print('x = 1')
print('finish running')

当满足其中一个条件后就会跳出if,继续执行后面的程序

import sensor, time from pyb import LED import math from pyb import UART #串口模块导入 #定义串口 uart = UART(1,115200) #串口配置 uart.init(115200, bits=8, parity=None, stop=1) # 设置相机 sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) sensor.set_framesize(sensor.VGA) # 800 600 #sensor.skip_frames(time = 333 )#跳过100张图片 sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking sensor.set_hmirror(True)#水平方向翻转 sensor.set_vflip(True)#垂直方向翻转 #clock = time.clock() # Create a clock object to track the FPS. #sensor.set_hmirror(True) #pin1 = Pin(&#39;D8&#39;, Pin.IN, Pin.PULL_UP) #初始化每个格子的标志变量 flag1 = 0 flag2 = 0 flag3 = 0 flag4 = 0 flag5 = 0 flag6 = 0 flag7 = 0 flag8 = 0 flag9 = 0 rx_buff=[] #标志位2 a = [1]*18 red_led = LED(1) clock = time.clock() CONXX = 1 Board_Flag = False black = [(149, 199)] white = [(194, 0)] gray = [(38, 255)] players = [&#39;X&#39;, &#39;O&#39;] x=0 lu_x=640-x lu_y=480-x rd_x=2*x rd_y=2*x roi1 = [200,90,285,275] #巡线敏感区 #def sending_data(cx,cy,c): # global uart; # data = ustruct.pack("<bbhhhb", # 0x2C, # 0x12, # int(cx), # int(cy), # 0, # 0x5B) # uart.write(data); ########串口接收数据函数处理######### def Receive_Prepare(): #data global state global x global tx_flag global data global Find_Task global Target_Num if state==0: data[0]=uart.readchar() if data[0] == 0x0d:#帧头 state = 1 else: state = 0 rx_buff.clear() elif state==1: data[1]=uart.readchar() #rx_buff.append(data) Target_Num=data[x+1] state = 2 elif state==2: data[2]=uart.readchar() #rx_buff.append(data) Find_Task=data[x+2] state = 3 elif state == 3: data[4]=uart.readchar() if data[4] == 0x5b: tx_flag = int(data[0]) state = 4 elif state == 4: state=0 else: state = 0 rx_buff.clear() # 定义黑框颜色的阈值 thresholds = ((115, 0)) openmv_flag=6 def draw_chess(x,y): #(197, 255)白色 #(255, 98)黑色 a=80#黑色 b=170#白色 pixel = img.get_pixel(x, y) #print(f"坐标: ({x}, {y}), 像素值: {pixel}") if pixel is None: # 处理无效像素 return -1 if pixel<a: #img.draw_circle(x,y,15,thickness=3) #print("黑色棋子") flag=1 elif pixel>b: #img.draw_circle(x,y,15,thickness=3) #print("白色棋子") flag=2 else: #print("无效棋子") flag=0 return flag def find_box_corners(edges): if len(edges) < 1: return None # 初始化四个角点的坐标 top_left = edges[0] top_right = edges[0] bottom_left = edges[0] bottom_right = edges[0] for edge in edges: x, y = edge # 找到最左上角的角点 if x + y < top_left[0] + top_left[1]: top_left = edge # 找到最右上角的角点 if x - y > top_right[0] - top_right[1]: top_right = edge # 找到最左下角的角点 if x - y < bottom_left[0] - bottom_left[1]: bottom_left = edge # 找到最右下角的角点 if x + y > bottom_right[0] + bottom_right[1]: bottom_right = edge return top_left, top_right, bottom_left, bottom_right # 定义控制舵机缓慢移动到目标角度的函数 #lcd = display.SPIDisplay() #屏幕显示 while(True): # 拍摄图像 #clock.tick() # Update the FPS clock. img = sensor.snapshot() #img.lens_corr(strength=1.8, zoom=1) img.draw_rectangle(lu_x,lu_y,rd_x,rd_y) # 找到黑框区域 blobs = img.find_blobs([thresholds], roi=roi1, pixels_threshold=5000, area_threshold=100,merge=True) if not blobs: # 检查是否为空 continue # 跳过当前帧 # 如果找到黑框 if blobs: # 绘制黑框的边界 img.draw_edges(blobs[0].min_corners(), thickness=3) # 获取角点坐标 edges = blobs[0].min_corners() if edges is None or len(edges) < 4: # 确保有至少4个角点 continue corners = find_box_corners(edges) if corners is None: continue top_left, top_right, bottom_left, bottom_right = corners # 检查角点是否成功获取 if top_left is not None: # 绘制角点 #img.draw_cross(top_left[0], top_left[1]) # 左上角 #img.draw_cross(top_right[0], top_right[1]) # 右上角 #img.draw_cross(bottom_left[0], bottom_left[1]) # 左下角 #img.draw_cross(bottom_right[0], bottom_right[1]) # 右下角 Board_Flag== True # 打印四个角点的坐标 #print("Top left:", top_left) #print("Top right:", top_right) #print("Bottom left:", bottom_left) #print("Bottom right:", bottom_right) point_x=[top_left[0],top_right[0],bottom_left[0],bottom_right[0]] point_y=[top_left[1],top_right[1],bottom_left[1],bottom_right[1]] length=math.sqrt((top_left[0]-top_right[0])**2+(top_left[1]-top_right[1])**2) theta=math.atan2((top_left[1]-top_right[1]),(top_left[0]-top_right[0])) x_step=math.cos(theta)*length/3 y_step=math.sin(theta)*length/3 p5_x=int((max(point_x)+min(point_x))/2) p5_y=int((max(point_y)+min(point_y))/2) wc5_x=(p5_x - p5_x) + 100 wc5_y=(p5_y - p5_y) + 100 img.draw_string(p5_x, p5_y, "5", color=(0,0,0), scale=3)#区域1 FH5 =bytearray([0x2C,0x12,5,wc5_x,wc5_y,0x5B]) p4_x=int(p5_x+x_step) p4_y=int(p5_y+y_step) wc4_x=p4_x - p5_x + 100 wc4_y=p4_y - p5_y + 100 img.draw_string(p4_x, p4_y, "4", color=(0,0,0), scale=3)#区域4 FH4 = bytearray([0x2C,0x12,4,wc4_x,wc4_y,0x5B]) p6_x=int(p5_x-x_step) p6_y=int(p5_y-y_step) img.draw_string(p6_x, p6_y, "6", color=(0,0,0), scale=3)#区域6 wc6_x=p6_x - p5_x + 100 wc6_y=p6_y - p5_y + 100 FH6 = bytearray([0x2C,0x12,6,wc6_x,wc6_y,0x5B]) p2_x=int(p5_x-y_step) p2_y=int(p5_y+x_step) img.draw_string(p2_x, p2_y, "2", color=(0,0,0), scale=3)#区域2 wc2_x=p2_x - p5_x + 100 wc2_y=p2_y - p5_y + 100 FH2 = bytearray([0x2C,0x12,2,wc2_x,wc2_y,0x5B]) p8_x=int(p5_x+y_step) p8_y=int(p5_y-x_step) img.draw_string(p8_x, p8_y, "8", color=(0,0,0), scale=3)#区域8 wc8_x=p8_x - p5_x + 100 wc8_y=p8_y - p5_y + 100 FH8 = bytearray([0x2C,0x12,8,wc8_x,wc8_y,0x5B]) #uart.write(FH8) p1_x=int(p2_x+x_step) p1_y=int(p2_y+y_step) img.draw_string(p1_x, p1_y, "1", color=(0,0,0), scale=3)#区域1 wc1_x=p1_x - p5_x + 100 wc1_y=p1_y - p5_y + 100 FH1 = bytearray([0x2C,0x12,1,wc1_x,wc1_y,0x5B]) p3_x=int(p2_x-x_step) p3_y=int(p2_y-y_step) img.draw_string(p3_x, p3_y, "3", color=(0,0,0), scale=3)#区域3 wc3_x=p3_x - p5_x + 100 wc3_y=p3_y - p5_y + 100 FH3 = bytearray([0x2C,0x12,3,wc3_x,wc3_y,0x5B]) p7_x=int(p8_x+x_step) p7_y=int(p8_y+y_step) img.draw_string(p7_x, p7_y, "7", color=(0,0,0), scale=3)#区域7 wc7_x=p7_x - p5_x + 100 wc7_y=p7_y - p5_y + 100 FH7 = bytearray([0x2C,0x12,7,wc7_x,wc7_y,0x5B]) p9_x=int(p8_x-x_step) p9_y=int(p8_y-y_step) img.draw_string(p9_x, p9_y, "9", color=(0,0,0), scale=3)#区域9 wc9_x=p9_x - p5_x + 100 wc9_y=p9_y - p5_y + 100 FH9 = bytearray([0x2C,0x12,9,wc9_x,wc9_y,0x5B]) #print("p1_x,p1_y:",p1_x,p1_y) ##########串口发坐标 draw_chess(p1_x,p1_y) draw_chess(p2_x,p2_y) draw_chess(p3_x,p3_y) draw_chess(p4_x,p4_y) draw_chess(p5_x,p5_y) draw_chess(p6_x,p6_y) draw_chess(p7_x,p7_y) draw_chess(p8_x,p8_y) draw_chess(p9_x,p9_y) #黑棋是1,白棋是2 F1 = "PB1PE1PO\n" F2 = "PB2PE1PO\n" F3 = "PB3PE1PO\n" F4 = "PB4PE1PO\n" F5 = "PB5PE1PO\n" F6 = "PB6PE1PO\n" F7 = "PB7PE1PO\n" F8 = "PB8PE1PO\n" F9 = "PB9PE1PO\n" F11 = "PB1PE2PO\n" F22 = "PB2PE2PO\n" F33 = "PB3PE2PO\n" F44 = "PB4PE2PO\n" F55 = "PB5PE2PO\n" F66 = "PB6PE2PO\n" F77 = "PB7PE2PO\n" F88 = "PB8PE2PO\n" F99 = "PB9PE2PO\n" #定义识别时间 time_shijue = 5 #print(draw_chess(p5_x,p5_y)) #串口收到了数据,则接收数据 #if(uart.any()>0): #Receive_Prepare() dat = uart.readline() if dat: char_dat = dat.decode().strip().lower() # 统一转小写并去除空格/换行 print(char_dat) if char_dat == "yes" : CONXX = 1 elif char_dat == "no" : CONXX = 0 if CONXX == 1 : if (draw_chess(p1_x,p1_y) == 2) and (a[0]==1): #白棋 if flag1 < time_shijue : uart.write(F11) print(F11) flag1 = flag1 + 1 if flag1 == time_shijue: CONXX = 0 a[0]=0 elif (draw_chess(p1_x,p1_y) == 1) and (a[1]==1): #黑棋 if flag1 < time_shijue : uart.write(F1) print(F1) flag1 = flag1 + 1 if flag1 == time_shijue: CONXX = 0 a[1]=0 if (draw_chess(p2_x,p2_y) == 2) and (a[2]==1): #白棋 if flag2 < time_shijue : uart.write(F22) print(F22) flag2 = flag2 + 1 if flag2 == time_shijue: CONXX = 0 a[2]=0 elif (draw_chess(p2_x,p2_y) == 1) and (a[3]==1): #黑棋 if flag2 < time_shijue : uart.write(F2) print(F2) flag2 = flag2 + 1 if flag2 == time_shijue: CONXX = 0 a[3]=0 if (draw_chess(p3_x,p3_y) == 2) and (a[4]==1): #白棋 if flag3 < time_shijue : uart.write(F33) print(F33) flag3 = flag3 + 1 if flag3 == time_shijue: CONXX = 0 a[4]=0 elif (draw_chess(p3_x,p3_y) == 1) and (a[5]==1): #黑棋 if flag3 < time_shijue : uart.write(F3) print(F3) flag3 = flag3 + 1 if flag3 == time_shijue: CONXX = 0 a[5]=0 if (draw_chess(p4_x,p4_y) == 2) and (a[6]==1): #白棋 if flag4 < time_shijue : uart.write(F44) print(F44) flag4 = flag4 + 1 if flag4 == time_shijue: CONXX = 0 a[6]=0 elif (draw_chess(p4_x,p4_y) == 1) and (a[7]==1): #黑棋 if flag4 < time_shijue : uart.write(F4) print(F4) flag4 = flag4 + 1 if flag4 == time_shijue: CONXX = 0 a[7]=0 if (draw_chess(p5_x,p5_y) == 2) and (a[8]==1): #白棋 if flag5 < time_shijue : uart.write(F55) print(F55) flag5 = flag5 + 1 if flag5 == time_shijue: CONXX = 0 a[8]=0 elif (draw_chess(p5_x,p5_y) == 1) and (a[9]==1): #黑棋 if flag5 < time_shijue : uart.write(F5) print(F5) flag5 = flag5 + 1 if flag5 == time_shijue: CONXX = 0 a[9]=0 if (draw_chess(p6_x,p6_y) == 2) and (a[10]==1): #白棋 if flag6 < time_shijue : uart.write(F66) print(F66) flag6 = flag6 + 1 if flag6 == time_shijue: CONXX = 0 a[10]=0 elif (draw_chess(p6_x,p6_y) == 1) and (a[11]==1): #黑棋 if flag6 < time_shijue : uart.write(F6) print(F6) flag6 = flag6 + 1 if flag6 == time_shijue: CONXX = 0 a[11]=0 if (draw_chess(p7_x,p7_y) == 2) and (a[12]==1): #白棋 if flag7 < time_shijue : uart.write(F77) print(F77) flag7 = flag7 + 1 if flag7 == time_shijue: CONXX = 0 a[12]=0 elif (draw_chess(p7_x,p7_y) == 1) and (a[13]==1): #黑棋 if flag7 < time_shijue : uart.write(F7) print(F7) flag7 = flag7 + 1 if flag7 == time_shijue: CONXX = 0 a[13]=0 if (draw_chess(p8_x,p8_y) == 2) and (a[14]==1): #白棋 if flag8 < time_shijue : uart.write(F88) print(F88) flag8 = flag8 + 1 if flag8 == time_shijue: CONXX = 0 a[14]=0 elif (draw_chess(p8_x,p8_y) == 1) and (a[15]==1): #黑棋 if flag8 < time_shijue : uart.write(F8) print(F8) flag8 = flag8 + 1 if flag8 == time_shijue: CONXX = 0 a[15]=0 if (draw_chess(p9_x,p9_y) == 2) and (a[16]==1): #白棋 if flag9 < time_shijue : uart.write(F99) print(F99) flag9 = flag9 + 1 if flag9 == time_shijue: CONXX = 0 a[16]=0 elif (draw_chess(p9_x,p9_y) == 1) and (a[17]==1): #黑棋 if flag9 < time_shijue : uart.write(F9) print(F9) flag9 = flag9 + 1 if flag9 == time_shijue: CONXX = 0 a[17]=0 elif CONXX == 0: print("待机中\n")
07-22
#例程序使用OpenMV4,OpenMV4Plus均可 import sensor, image, time from pyb import millis, Pin, Timer, LED sensor.reset() sensor.set_hmirror(False) sensor.set_vflip(False) sensor.set_transpose(False) sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.VGA) sensor.set_windowing([200,120,240,240]) sensor.set_vflip(True) #垂直翻转 sensor.set_hmirror(True) #水平翻转 clock = time.clock() #设置频率,初始化定时器4,将其设置为50HZ,也就是说一个PWM周期为20ms TIM4 = Timer(4, freq=50) ## 生成50HZ方波,使用TIM4,channels 1,2分别是1.5ms脉宽,对应舵机90度 ch1 = TIM4.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width=0) #旋转舵机 ch2 = TIM4.channel(2, Timer.PWM, pin=Pin("P8"), pulse_width=0) #俯仰舵机 P9_Out = Pin(&#39;P9&#39;, Pin.OUT_PP) P9_Out.low() # 设置P9为低电平,关闭激光 red_led = LED(1) green_led = LED(2) blue_led = LED(3) #控制舵机驱动函数,输入舵机角度 def servo_ctrl(angle_x, angle_y): #1000对应0.5ms,舵机0° ch1.pulse_width(int(1000+4000*angle_x/180)) ch2.pulse_width(int(1000+4000*angle_y/180)) #根据输入激光点与中心点的x,y距离,计算出云台调节的增量,输出要增加的角度 def trace_add(distance): #正值 if distance == 1000.0: return 0.7; #特定的量,云台扫描使用这个角度增量 elif distance > 50.0: return 0.6; elif distance > 40.0: return 0.3; elif distance > 30.0: return 0.2; elif distance > 20.0: return 0.1; elif distance > 10.0: return 0.05; elif distance > 5.0: return 0.03; elif distance > 2.0: return 0.02; #负值 elif distance == -1000.0: return -0.7; #特定的量,云台扫描使用这个角度增量 elif distance < -50.0: return -0.6; elif distance < -40.0: return -0.3; elif distance < -30.0: return -0.2; elif distance < -20.0: return -0.1; elif distance < -10.0: return -0.05; elif distance < -5.0: return -0.03; elif distance < -2.0: return -0.02; return 0.0; #颜色阈值,可以宽泛一些。如果效果不好,则自行调阈值 thr_write = (41, 100, -71, 46, -60, 39)#白色阈值 thr_black = (40, 7, -68, 50, -66, 31)#黑色阈值 thr_purple = (85, 100, -12, 127, -128, 19)#紫色阈值(紫外线激光点) target = [0,0] #目标坐标,自动计算 purple_point = [0,0] #紫色激光点坐标,自动计算 vision_point = [114,125]#视野中心点,在激光未开启时用于追踪,自行调整 distance = [0,0] #激光点与中心的距离 add_angle = [0,0] #舵机增量角度 now_servo = [90,100] #当前舵机角度 flag = [0,0] #检测成功标志,前者为激光,后者为中心点,检测成功为1,否则为0 count = 0; #计数,无目标时云台要旋转寻找目标 direction = [0,0]; #旋转方向,在寻找目标时来回旋转的方向确定 blue_led.on() time.sleep_ms(100) blue_led.off() while(True): timer = millis() clock.tick() img = sensor.snapshot() black_blobs = img.find_blobs([thr_black], merge =False) #找黑框 if black_blobs: #如果有目标 #找紫色激光点 purple_blobs = img.find_blobs([thr_purple], merge=True) if purple_blobs: for blob in purple_blobs: purple_point = [blob[5], blob[6]] #激光中心点 flag[0] = 1 #激光点检测成功标志置1 break else: flag[0] = 0 #激光点检测成功标志置0 #寻找中心点 for single_black_blob in black_blobs: #找到的目标中,符合阈值的面积和总的区域之间的比值。因为黑框内部不是黑色,所以这个比值不会很大。 if single_black_blob.pixels() / (single_black_blob.w()*single_black_blob.h()) < 0.3: #img.draw_rectangle(single_black_blob.rect(),color=(0,255,255))#绘制符合条件的区域 #在区域内找白色 write_blobs = img.find_blobs([thr_write],area_threshold=2,roi =single_black_blob.rect(), merge =False) if write_blobs:#如果有目标 largest_white = max(write_blobs, key=lambda b: b.area())#找到最大的块 #绘制黑框的中心点 #判断条件1:黑色区域面积和白色区域面积的比例;判断条件2:黑框和白色区域中心坐标的差值 if (2 < largest_white.pixels() / single_black_blob.pixels() < 4) and\ abs( largest_white.cx() - single_black_blob.cx() ) < 10 and \ abs( largest_white.cy() - single_black_blob.cy() ) < 10 : target = [largest_white.cx(), largest_white.cy()] #白色区域中心坐标 img.draw_cross(target,color=(255,0,0),thickness=3) #绘制在画布上 flag[1] = 1 #中心点检测成功标志置1 green_led.on() break else: green_led.off() flag[1] = 0 #中心点检测成功标志置0 else: green_led.off() flag[1] = 0 #中心点检测成功标志置0 else: flag[1] = 0 #中心点检测成功标志置0 else: flag[1] = 0 #中心点检测成功标志置0 flag[0] = 0 #激光点检测成功标志置0 #计算误差距离 if flag[1] == 1:#有中心点,追踪视觉中心,小于一定的值打开激光 count = 0; #把计数清零,避免误识别后进入搜索模式 distance[0] = vision_point[0] - target[0] distance[1] = vision_point[1] - target[1] if abs(distance[0]) < 4 and abs(distance[1]) < 4: P9_Out.high() #设置P9为高电平,打开激光 else: P9_Out.low() # 设置P9为低电平,关闭激光 elif flag[1] == 0: #无中心点,旋转搜索黑框中心点 if count > 20: P9_Out.low() # 设置P9为低电平,关闭激光 if direction[0] == 0: #正向自增,增X旋转 distance[0] = 1000 elif direction[0] == 1: #反向自增,增X旋转 distance[0] = -1000 if now_servo[0] >= 120: #到达角度,翻转方向 direction[0] = 1; elif now_servo[0] <= 60: direction[0] = 0; distance[1] = 0 #pitch不自增,直接定角度,距离需要设为0 add_angle[1] = 100 # if direction[1] == 0: #正向自增,增Y俯仰 # distance[1] = 1000 # elif direction[1] == 1: #反向自增,增Y俯仰 # distance[1] = -1000 # if now_servo[1] >= 105: #到达角度,翻转方向 # direction[1] = 1; # elif now_servo[1] <= 95: # direction[1] = 0; elif count < 50: count = count+1 else: distance[0] = 0 distance[1] = 0 #根据中心距离计算舵机增量 add_angle[0] = trace_add(distance[0]) add_angle[1] = trace_add(distance[1]) #将舵机角度增量加到当前舵机角度 now_servo[0] = now_servo[0] + add_angle[0] now_servo[1] = now_servo[1] + add_angle[1] #角度限幅 if now_servo[0] > 140: now_servo[0] = 140 elif now_servo[0] < 40: now_servo[0] = 40 if now_servo[1] > 110: now_servo[1] = 110 elif now_servo[1] < 90: now_servo[1] = 90 #舵机执行 servo_ctrl(now_servo[0],now_servo[1])根据这个代码激光接线应该接在哪里和openmv4h7plus
08-03
#例程序使用OpenMV4,OpenMV4Plus均可 import sensor, image, time from pyb import millis, Pin, Timer sensor.reset() sensor.set_hmirror(False) sensor.set_vflip(False) sensor.set_transpose(False) sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.VGA) sensor.set_windowing([200,120,240,240]) clock = time.clock() #设置频率,初始化定时器4,将其设置为50HZ,也就是说一个PWM周期为20ms TIM4 = Timer(4, freq=50) ## 生成50HZ方波,使用TIM4,channels 1,2分别是1.5ms脉宽,对应舵机90度 ch1 = TIM4.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width=3000) #旋转舵机 ch2 = TIM4.channel(2, Timer.PWM, pin=Pin("P8"), pulse_width=3000) #俯仰舵机 P9_Out = Pin(&#39;P9&#39;, Pin.OUT_PP) P9_Out.low() # 设置P9为低电平,关闭激光 #控制舵机驱动函数,输入舵机角度 def servo_ctrl(angle_x, angle_y): #1000对应0.5ms,舵机0° ch1.pulse_width(int(1000+4000*angle_x/180)) ch2.pulse_width(int(1000+4000*angle_y/180)) #根据输入激光点与中心点的x,y距离,计算出云台调节的增量,输出要增加的角度 def trace_add(distance): #正值 if distance > 50.0: return 0.6; elif distance > 40.0: return 0.3; elif distance > 30.0: return 0.2; elif distance > 20.0: return 0.1; elif distance > 10.0: return 0.05; elif distance > 5.0: return 0.03; elif distance > 3.0: return 0.02; #负值 elif distance < -50.0: return -0.6; elif distance < -40.0: return -0.3; elif distance < -30.0: return -0.2; elif distance < -20.0: return -0.1; elif distance < -10.0: return -0.05; elif distance < -5.0: return -0.03; elif distance < -3.0: return -0.02; return 0.0; #颜色阈值,可以宽泛一些。如果效果不好,则自行调阈值 thr_write = (41, 100, -71, 46, -60, 39)#白色阈值 thr_black = (40, 7, -68, 50, -66, 31)#黑色阈值 thr_purple = (85, 100, -12, 127, -128, 19)#紫色阈值(紫外线激光点) target = [0,0] #目标坐标,自动计算 purple_point = [0,0] #紫色激光点坐标,自动计算 vision_point = [120,120]#视野中心点,在激光未开启时用于追踪,自行调整 distance = [0,0] #激光点与中心的距离 add_angle = [0,0] #舵机增量角度 now_servo = [90,110] #当前舵机角度 flag = [0,0] #检测成功标志,前者为激光,后者为中心点,检测成功为1,否则为0 count = 0; #计数,无目标时云台要旋转寻找目标 direction = 0; #旋转方向,在寻找目标时来回旋转的方向确定 while(True): timer = millis() clock.tick() img = sensor.snapshot() black_blobs = img.find_blobs([thr_black], merge =False) #找黑框 if black_blobs: #如果有目标 #找紫色激光点 purple_blobs = img.find_blobs([thr_purple], merge=True) if purple_blobs: for blob in purple_blobs: purple_point = [blob[5], blob[6]] #激光中心点 flag[0] = 1 #激光点检测成功标志置1 break else: flag[0] = 0 #激光点检测成功标志置0 #寻找中心点 for single_black_blob in black_blobs: #找到的目标中,符合阈值的面积和总的区域之间的比值。因为黑框内部不是黑色,所以这个比值不会很大。 if single_black_blob.pixels() / (single_black_blob.w()*single_black_blob.h()) < 0.3: #img.draw_rectangle(single_black_blob.rect(),color=(0,255,255))#绘制符合条件的区域 #在区域内找白色 write_blobs = img.find_blobs([thr_write],area_threshold=2,roi =single_black_blob.rect(), merge =False) if write_blobs:#如果有目标 largest_white = max(write_blobs, key=lambda b: b.area())#找到最大的块 #绘制黑框的中心点 #判断条件1:黑色区域面积和白色区域面积的比例;判断条件2:黑框和白色区域中心坐标的差值 if (2 < largest_white.pixels() / single_black_blob.pixels() < 4) and\ abs( largest_white.cx() - single_black_blob.cx() ) < 10 and \ abs( largest_white.cy() - single_black_blob.cy() ) < 10 : target = [largest_white.cx(), largest_white.cy()] #白色区域中心坐标 img.draw_cross(target,color=(255,0,0),thickness=3) #绘制在画布上 flag[1] = 1 #中心点检测成功标志置1 break else: flag[1] = 0 #中心点检测成功标志置0 else: flag[1] = 0 #中心点检测成功标志置0 else: flag[1] = 0 #中心点检测成功标志置0 else: flag[1] = 0 #中心点检测成功标志置0 flag[0] = 0 #激光点检测成功标志置0 #计算误差距离 if flag[0] == 1 and flag[1] == 1:#有激光点,有中心点,追踪目标 P9_Out.high() #设置P9为高电平,打开激光 count = 0; #把计数清零,避免误识别后进入搜索模式 distance[0] = purple_point[0] - target[0] distance[1] = purple_point[1] - target[1] elif flag[0] == 0 and flag[1] == 1:#无激光点,有中心点,使用镜头中心点追踪,后打开激光 count = 0; #把计数清零,避免误识别后进入搜索模式 #在云台未调至中心时,使用视觉中心点进行追踪,调至中心后打开激光 if abs(vision_point[0] - target[0]) > 10 and abs(vision_point[1] - target[1]) > 10: P9_Out.low() # 设置P9为低电平,关闭激光 distance[0] = vision_point[0] - target[0] distance[1] = vision_point[1] - target[1] else: P9_Out.high() # 设置P9为高电平,打开激光 elif flag[0] == 0 and flag[1] == 0: #无激光点,无中心点,旋转搜索黑框中心点 if count > 20: P9_Out.low() # 设置P9为低电平,关闭激光 if direction == 0: #正向自增,只增X旋转,不增Y俯仰 distance[0] = 50 distance[1] = 0 elif direction == 1: #反向自增,只增X旋转,不增Y俯仰 distance[0] = -50 distance[1] = 0 if now_servo[1] == 180: #翻转方向 direction = 1; if now_servo[1] == 0: direction = 0; elif count < 50: count = count+1 else: #其他情况,不予理会 distance[0] = 0 distance[1] = 0 #根据中心距离计算舵机增量 add_angle[0] = trace_add(distance[0]) add_angle[1] = trace_add(distance[1]) #将舵机角度增量加到当前舵机角度 now_servo[0] = now_servo[0] + add_angle[0] now_servo[1] = now_servo[1] + add_angle[1] #角度限幅 if now_servo[0] > 180: now_servo[0] = 180 elif now_servo[0] < 0: now_servo[0] = 0 if now_servo[1] > 180: now_servo[1] = 180 elif now_servo[1] < 0: now_servo[1] = 0 #舵机执行 servo_ctrl(now_servo[0],now_servo[1]) print(&#39;舵机X角度&#39;,now_servo[0],&#39;舵机Y角度&#39;,now_servo[1],&#39;激光标志&#39;,flag[0],&#39;中心点标志&#39;,flag[1],&#39;目标&#39;,target,&#39;激光点&#39;,purple_point) # timer = millis() - timer # print(&#39;用时&#39;,timer,&#39;实时帧速&#39;,1000/timer,&#39;平均帧速&#39;,clock.fps())这段代码有什么问题
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值