@kidswong999 这个是main.py,具体型号为openMV4H7plus
import sensor, image, time, pyb, struct, math
import pid, data_pack, t2017_task1, t2017_task2, t2017_task3, t2017_task_plus
######################初始化程序##############################
sensor.reset() #对openmv进行复位
sensor.set_pixformat(sensor.GRAYSCALE) #以RGG565的格式读取像素信息
sensor.set_framesize(sensor.QQVGA) #分辨率为QQVGA:160*120
sensor.skip_frames(10) #前10帧数据舍去,避免刚刚启动,数据不稳定造成误判
sensor.set_auto_whitebal(False) #关闭自动白平衡
clock = time.clock()
uart = pyb.UART(3, 500000, timeout_char = 1000) #打开串口3
red_threshold_01 = (0,55) #目标色块的灰度值参数范围
flag_x_y = 0 #这是一个标志位,循环调整无人机的x方向和y方向
num_stop = 0 #罚站次数,罚站即不对无人机进行位置调整
time_flag = 0
pid_x = pid.PID(80,0.5,0,0,30) #x轴方向的pid控制,pid参数要自己调
pid_y = pid.PID(60,0.5,0,0,30) #y轴方向的pid控制
task_number = 0 #任务切换
MV_ctrl_flag = 0
Buzzer = pyb.Pin("P6", pyb.Pin.OUT_PP)
##################各个字母所对应的ASCII值######################
R = ord('R')
L = ord('L')
S = ord('S')
B = ord('B')
G = ord('G')
E = ord('E')
H = ord('H')
task1 = ord('a')
task2 = ord('b')
task3 = ord('c')
task4 = ord('d')
task5 = ord('e')
###########################################################
def send_direction_packet(direct,velcity): #与无人机mcu的通信协议,数据打包并发送
s = 0xAA+0x8C+direct+(int(velcity/256))+(int(velcity%256)) #s是校验字节,是数据包里s之前所有数据的和
s = int(s % 256) #因为协议里s是8位的,这里取低八位
temp_flow = struct.pack("<BBBBhB", #数据包的封装格式,B:unsigned char; h:short
0xAA,
0x89,
03,
direct,
int(velcity),
s)
uart.write(temp_flow)
while(True):
times = clock.avg()
clock.reset() #用来测量一个循环所用的时间,单位ms,不用的话这里也可以删掉
clock.tick()
print("time: %d" %times)
#task_number = 4 ######
while(task_number == 0): #获取任务ID
task_char = uart.readchar()
Buzzer.value(0)
if task_char == task1:
task_number = 1
elif task_char == task2:
task_number = 2
elif task_char == task3:
task_number = 3
elif task_char == task4:
task_number = 4
elif task_char == task5:
task_number = 5
else :
task_number =0
print("task_number : %d" %task_number)
if task_number != 2: #2
while(MV_ctrl_flag == 0): #判断openMV是否获得控制权
MV_ctrl_char = uart.readchar()
if MV_ctrl_char == H:
MV_ctrl_flag = 1
print("start work")
if task_number == 1:
t2017_task1.task_2017_1_1()
elif task_number == 2:
t2017_task2.task_2017_1_2()
elif task_number == 3:
t2017_task3.task_2017_1_3()
elif (task_number == 4) or (task_number == 5):
print("task number : %d" %task_number)
t2017_task_plus.task_2017_2()
##############################################################################################
#############################################################
## #
## 此文件实现对黑色动态物体的跟踪 #
## #
#############################################################
#import sensor, image, time, pyb, struct, math
#import pid, data_pack
#from machine import Pin
#######################初始化程序#############################
#sensor.reset() #对openmv进行复位
#sensor.set_pixformat(sensor.GRAYSCALE) #以RGG565的格式读取像素信息
#sensor.set_framesize(sensor.QQVGA) #分辨率为QQVGA:160*120
#sensor.skip_frames(10) #前10帧数据舍去,避免刚刚启动,数据不稳定造成误判
#sensor.set_auto_whitebal(False) #关闭自动白平衡
#clock = time.clock()
#uart = pyb.UART(3, 500000, timeout_char = 1000) #打开串口3
#red_threshold_01 = (0,55) #目标色块的灰度值参数范围
#flag_x_y = 0 #这是一个标志位,循环调整无人机的x方向和y方向
#num_stop = 0 #罚站次数,罚站即不对无人机进行位置调整
#start_MV_flag = 0
#time_flag = 0
#prepare_flag = 0
#pid_x = pid.PID(80,0.3,0,0,30) #x轴方向的pid控制,pid参数要自己调
#pid_y = pid.PID(60,0.3,0,0,30) #y轴方向的pid控制
#Buzzer = pyb.Pin("P6", pyb.Pin.OUT_PP)
#none_blob_flag = 0
###################各个字母所对应的ASCII值######################
#R = ord('R')
#L = ord('L')
#S = ord('S')
#B = ord('B')
#G = ord('G')
#E = ord('E')
#H = ord('H')
#task1 = ord('a')
#task2 = ord('b')
#task3 = ord('c')
#task4 = ord('d')
#task5 = ord('e')
############################################################
#def send_direction_packet(direct,velcity): #与无人机mcu的通信协议,数据打包并发送
#s = 0xAA+0x8C+direct+(int(velcity/256))+(int(velcity%256)) #s是校验字节,是数据包里s之前所有数据的和
#s = int(s % 256) #因为协议里s是8位的,这里取低八位
#temp_flow = struct.pack("<BBBBhB", #数据包的封装格式,B:unsigned char; h:short
#0xAA,
#0x89,
#03,
#direct,
#int(velcity),
#s)
#uart.write(temp_flow)
#def compareBlob(blob1, blob2): #比较两个色块大小的函数
#tmp = blob1.pixels() - blob2.pixels()
#if tmp == 0:
#return 0
#elif tmp > 0:
#return 1
#else:
#return -1
#while(True):
#times = clock.avg()
#clock.reset() #用来测量一个循环所用的时间,单位ms,不用的话这里也可以删掉
#clock.tick()
#print("time: %d" %times)
#img = sensor.snapshot().lens_corr(1.8)
#blobs = img.find_blobs([red_threshold_01],
#pixels_threshold=100, merge=True) #寻找目标色块,低于150像素的视为噪声
#img.binary([red_threshold_01], invert = True) #二值化处理
## Buzzer.value(1)
#while(start_MV_flag == 0):
#value_from_flyctr = uart.readchar()
#print("value_from_flyctr : %d" %value_from_flyctr)
#if value_from_flyctr == H :
#start_MV_flag = 1 #悬停计时开始标志位
#if len(blobs) == 2: #如果识别到目标色块
#print("the number of blobs is %d" %len(blobs))
#if(abs(blobs[0].cx()-80)<5) and \
#(abs(blobs[0].cy()-60)<5):
#time_flag = 1
#if flag_x_y == 0 : #调节x与y方向的切换标志
#speed_x = pid_x.IncPIDCalc(blobs[0].cx())
## print("speed_x: %f" %speed_x)
#if speed_x > 0: #说明目标当前x值偏小,无人机偏右,需要向左调整
#send_direction_packet(L,speed_x)
#else:
#send_direction_packet(R,abs(speed_x))
#flag_x_y = 1 #标志位置1,表示下次循环调整y轴方向
#else :
#speed_y = pid_y.IncPIDCalc(blobs[0].cy())
##print("speed_y: %f" %speed_y)
#if speed_y >= 0: #说明目标当前y值偏小,无人机偏后,需要向前调整
#send_direction_packet(G,speed_y)
#else:
#send_direction_packet(B,abs(speed_y))
#flag_x_y = 0 #标志位置0,表示下次循环调整x轴方向
#elif len(blobs) == 0:
#print("the number of blobs is %d" %len(blobs))
#if prepare_flag == 0:
#send_direction_packet(E,0) #没有检测到目标色块,这里让无人机原地罚站
#else:
#send_direction_packet(G,5)
#prepare_flag = 2
#else :
#print("the number of blobs is %d" %len(blobs))
#bigBlob = blobs[0] #将第一个色块赋值给最大色块
#for blob_temp in blobs: #此循环找出最大色块,进一步滤除噪声
#if compareBlob(bigBlob, blob_temp) == -1:
#bigBlob = blob_temp
#if(abs(blobs[0].cx()-80)<5) and \
#(abs(blobs[0].cy()-60)<5) and \
#prepare_flag == 0:
#prepare_flag = 1
#if(abs(blobs[0].cx()-80)<5) and \
#(abs(blobs[0].cy()-60)<5) and \
#prepare_flag == 2:
#time_flag = 1
#if prepare_flag == 0 or prepare_flag == 2:
#if flag_x_y == 0 : #调节x与y方向的切换标志
#speed_x = pid_x.IncPIDCalc(bigBlob.cx())
## print("speed_x: %f" %speed_x)
#if speed_x > 0: #说明目标当前x值偏小,无人机偏右,需要向左调整
#send_direction_packet(L,speed_x)
#else:
#send_direction_packet(R,abs(speed_x))
#flag_x_y = 1 #标志位置1,表示下次循环调整y轴方向
#else :
#speed_y = pid_y.IncPIDCalc(bigBlob.cy())
##print("speed_y: %f" %speed_y)
#if speed_y >= 0: #说明目标当前y值偏小,无人机偏后,需要向前调整
#send_direction_packet(G,speed_y)
#else:
#send_direction_packet(B,abs(speed_y))
#flag_x_y = 0 #标志位置0,表示下次循环调整x轴方向
#else: #准备就绪,进入8区
#send_direction_packet(G,5)
#if time_flag == 1:
#num_stop += 1
#if num_stop >= 120 : #100次差不多5秒钟,降落
#send_direction_packet(L,15)
#time.sleep(2000)
#num_stop = 0
#send_direction_packet(E,0)