每次运行都显示内存分配错误,能解决么?
-
(删除一些东西就好了,但又不太想删,有不删东西的解决办法么)
-
既然你用的是OpenMV3,那么你换成OpenMV4,应该就没问题。
-
请提供你的代码。我会测试一下。
-
import sensor, image, time, math from pyb import LED,Timer from struct import pack, unpack from pyb import UART #import Message,DotFollowing uart = UART(3,500000)#初始化串口 波特率 500000 class Receive(object): uart_buf = [] _data_len = 0 _data_cnt = 0 state = 0 uart_data=[5,6,7,8,9] R=Receive() def UartSendData(Data): uart.write(Data) def jiexi(data): if R.state==0: if data==0xee : R.state+=1 elif R.state<=5 : R.uart_data[R.state-1]=data R.state+=1 else : R.state=0 if R.state>5: R.state=0 #读取串口缓存 def UartReadBuffer(): i = 0 Buffer_size = uart.any() while i<Buffer_size: #ReceivePrepare(uart.readchar()) jiexi(uart.readchar()) i = i + 1 #点检测数据打包 def DotDataPack(color,flag,x,y,yaw): print("found: x=",x," y=",y) #x=-x #y=-y pack_data=bytearray([0xAA,0x29,0x05,0x41,0x00,color,flag,x>>8,x,(-y)>>8,(-y),0x00]) lens = len(pack_data)#数据包大小 pack_data[4] = 6;#有效数据个数 i = 0 sum = 0 #和校验 while i<(lens-1): sum = sum + pack_data[i] i = i+1 pack_data[lens-1] = sum; return pack_data sensor.reset() sensor.set_pixformat(sensor.RGB565) #阈值内数据(白色像素)较多时,QQVGA帧率降到5以下。线较细时可以使用(7·19测试) #QQQVGA较稳定,帧率保持高速(50以上) sensor.set_framesize(sensor.QQVGA) # 80x60 (4,800 pixels) - O(N^2) max = 2,3040,000. sensor.skip_frames(time = 2000) # WARNING: If you use QQVGA it may take seconds clock = time.clock() # to process a frame sometimes. sensor.set_hmirror(True) #水平方向翻转 sensor.set_vflip(True) #垂直方向翻转 #sensor.set_auto_whitebal(False) #sensor.set_auto_gain(False) sensor.skip_frames(n=10) #Prevention_go_out_up=(0,30,160,30) #Prevention_go_out_down=(0,60,160,30) #Prevention_go_out_left=(40,0,40,120) #Prevention_go_out_right=(80,0,40,120) #Prevention_go_out_up_flag=0 #Prevention_go_out_down_flag=0 #Prevention_go_out_left_flag=0 #Prevention_go_out_right_flag=0 #roi_find_line_out=(0,0,160,30) color_red_x=(52, 92, 110, 19, 86, -36) #red_x_x=0 #red_x_y=0 fly_mode=0 color_red_x_end=0 red_x_time_flag=0 class red_x(object): flag = 0 color = 0 x = 0 y = 0 red_X=red_x() ################################################################### #色块识别函数定红X #定义函数:找到画面中最大的指定色块 def FindMax(blobs): max_size=1 if blobs: max_blob = 0 for blob in blobs: blob_size = blob.w()*blob.h() if ( (blob_size > max_size) & (blob_size > 20) ) :#& (blob.density()<1.2*math.pi/4) & (blob.density()>0.8*math.pi/4) if ( math.fabs( blob.w() / blob.h() - 1 ) < 2.0 ) : max_blob=blob max_size = blob.w()*blob.h() return max_blob def LineFilter(src, dst): for i in range(0, len(dst), 1): dst[i] = src[i<<1] start_i=0 start=0 delate=0 #点检测 def find_red_x(): global start_i global start global delate img = sensor.snapshot(line_filter = LineFilter)#拍一张图像 red_blobs = img.find_blobs([color_red_x], pixels_threshold=3, area_threshold=3, merge=True, margin=5)#识别红色物体 max_blob=FindMax(red_blobs)#找到最大的那个 if max_blob: img.draw_cross(max_blob.cx(), max_blob.cy())#物体中心画十字 img.draw_rectangle(max_blob.rect())#画圈 #获取坐标并转换为+-200 red_x.x = max_blob.cx()-80 red_x.y = max_blob.cy()-60 red_x.flag = 1 #LED灯闪烁 LED(3).toggle() #LED灯闪烁 LED(2).toggle() else: red_x.flag = 0 LED(2).off() LED(3).off() #串口发送数据给飞控 #################################################################### ''' def Prevention_go_out(img): global Prevention_go_out_up_flag global Prevention_go_out_down_flag global Prevention_go_out_left_flag global Prevention_go_out_right_flag Prevention_go_out_up_flag=0 Prevention_go_out_down_flag=0 Prevention_go_out_left_flag=0 Prevention_go_out_right_flag=0 lines=img.find_lines(Prevention_go_out_up,x_stride=4,y_stride=1,threshold=1000,theta_margin=45,rho_maigin=20) if len(lines)>0 : for line in lines: if line.theta()>90: if abs(line.theta()-180)>35: Prevention_go_out_up_flag=1 else : if line.theta()>35 : Prevention_go_out_up_flag=1 #Prevention_go_out_up_flag=1 #line=max(lines,lambda x:x.magnitude()) #img.draw_line(line.line(), color=(255,0,0)) else : Prevention_go_out_up_flag=0 lines=img.find_lines(Prevention_go_out_down,x_stride=4,y_stride=1,threshold=1000,theta_margin=45,rho_maigin=20) if len(lines)>0 : for line in lines: if line.theta()>90: if abs(line.theta()-180)>35: Prevention_go_out_down_flag=1 else : if line.theta()>35 : Prevention_go_out_down_flag=1 #line=max(lines,lambda x:x.magnitude()) #img.draw_line(line.line(), color=(255,0,0)) else : Prevention_go_out_down_flag=0 lines=img.find_lines(Prevention_go_out_left,x_stride=1,y_stride=4,threshold=1000,theta_margin=45,rho_maigin=20) if len(lines)>0 : for line in lines: if line.theta()>90: if abs(line.theta()-180)<35: Prevention_go_out_left_flag=1 else : if line.theta()<35 : Prevention_go_out_left_flag=1 #Prevention_go_out_left_flag=1 #line=max(lines,lambda x:x.magnitude()) #img.draw_line(line.line(), color=(255,0,0)) else : Prevention_go_out_left_flag=0 lines=img.find_lines(Prevention_go_out_right,x_stride=1,y_stride=4,threshold=1000,theta_margin=45,rho_maigin=20) if len(lines)>0 : for line in lines: if line.theta()>90: if 180-line.theta()<35: Prevention_go_out_right_flag=1 else : if line.theta()<35 : Prevention_go_out_right_flag=1 print(line.theta(), line.theta()) img.draw_line(line.line(), color=(255,0,0)) #Prevention_go_out_right_flag=1 #line=max(lines,lambda x:x.magnitude()) #img.draw_line(line.line(), color=(255,0,0)) else : Prevention_go_out_right_flag=0 ''' ######################################################################### find_color_time_start=0 find_color_time_now=0 find_color_time_flag=0 color_fire=(62, 100, 8, 55, -22, 82)#(77, 100, 7, 45, -8, 79)#(84, 100, 8, 53, -50, 60)#(52, 92, 110, 19, 86, -36)#(33, 90, 7, 59, -60, 74)#(33,100,7,59,-62,74)#(59, 42, -17, 92, 16, 94)#(89, 99, 39, 3, 7, -2) (33, 100, 7, 59, -62, 74) class find_color_fire(object): x=0 y=0 flag=0 daxiao=0 find_color_fire = find_color_fire() def find_color(img): global find_color_time_start global find_color_time_now global find_color_time_flag find_color_fire.x=0 find_color_fire.y=0 find_color_fire.flag=0 find_color_fire.daxiao=0 #拍一张图像 red_blobs = img.find_blobs([color_fire], pixels_threshold=10, area_threshold=8 , merge=True, margin=5)#识别红色物体 max_blob=FindMax(red_blobs)#找到最大的那个 if max_blob: img.draw_cross(max_blob.cx(), max_blob.cy())#物体中心画十字 img.draw_rectangle(max_blob.rect())#画圈 #获取坐标并转换为+-200 find_color_fire.x = max_blob.cx()-80 find_color_fire.y = max_blob.cy()-60 find_color_fire.flag = 1 find_color_fire.daxiao=max_blob.area() #LED灯闪烁 #LED灯闪烁 LED(2).toggle() else: #Dot.flag = LED(3).off() LED(2).off() def Send_out(color,sta,x,y,yaw): send_out_1=int(color) send_out_2=int(sta) send_out_x=int(x) send_out_y=int(y) send_out_yaw=int(yaw) UartSendData(DotDataPack(send_out_1,send_out_2,send_out_x,send_out_y,send_out_yaw)) ''' fly_mode=0 find_color_mode=0 fly_x=0 fly_y=0 find_color_flag=0 fire_time_start=0 fire_time_now=0 fire_time_flag=0 fire_laser=0 fire_laser_flag=0 jiaoyan=0 back_home=0 while True : fire_laser=0 #fly_mode=2 UartReadBuffer() for i in range(0,5): print(R.uart_data[i] , i , "fly_mode=",fly_mode) #if fly_mode==0: if R.uart_data[0]==0 or fly_mode==0: find_red_x() if red_x.flag==1: if start_i==0 : start = time.ticks() start_i=1 else : delate=time.ticks() if delate-start>5000: fly_mode=1 start_i=0 start=0 delate=0 print(delate-start) Send_out(0,1,red_x.x,red_x.y) else : # LED().on() if fly_mode==0: flymode+=1 if fly_mode==1: #sensor.set_pixformat(sensor.RGB565) img = sensor.snapshot() #lines=img.find_lines((0,60,160,60),x_stride=40,y_stride=1,threshold=1000,theta_margin=40,rho_margin=20) blobs=img.find_blobs([color_fire],roi=(0,0,160,10), pixels_threshold=3, area_threshold=3, merge=True, margin=5) if len(blobs): fly_mode=2 sensor.set_pixformat(sensor.RGB565) LED(3).toggle() Send_out(0,1,0,-30) if fly_mode==2 : find_color() if find_color_fire.daxiao<300 and find_color_fire.flag==1: fly_x=find_color_fire.x/2 fly_y=find_color_fire.y/2 if abs(find_color_fire.x)<16 and abs(find_color_fire.y<12) : if(fire_time_flag==0): fire_time_start=time.ticks() fire_time_flag=1 else : fire_time_now=time.ticks() if fire_laser_flag==0: fire_laser=2 fire_laser_flag=1 jiaoyan=5 else : fire_time_start=0 fire_time_flag=0 fire_time_now=0 fire_laser=0 if fire_time_now-fire_time_start>2000: find_color_flag=1 print(find_color_flag,"start time is",fire_time_s . .. tart,"now time is",fire_time_now) else : fire_laser_flag=0 fire_time_start=0 fire_time_flag=0 fire_time_now=0 #fly_x=0 #fly_y=-30 print("jlkjlskadkkhksdh",find_color_fire.daxiao,find_color_fire.flag,) #if find_color_fire.flag==0: if find_color_flag==1: find_color_flag=0 find_color_mode+=1 if find_color_mode==0: fly_x=0 fly_y=-30 elif find_color_mode==1: fly_x=0 fly_y=-30 elif find_color_mode==2: fly_x=-30 fly_y=0 elif find_color_mode==3: fly_x=0 fly_y=30 elif find_color_mode==4: fly_x=0 fly_y=30 else: print("ajsjhdalsdlasdljasldjlasjdljalsfhakjshfkahslfdhakjshdkjashdkjahsdkjhasdjggaiuyh") if find_color_mode>=4: for line in img.find_lines(roi_find_line_out,x_stride=50,y_stride=2,threshold=2000,theta_margin=30,rho_margin=20): if line.theta()>40: back_home=1 fly_mode=3 Send_out(fire_laser,1,fly_x,fly_y,) print("alsdjlashdljassldjalskjd",jiaoyan,find_color_mode) if fly_mode==3: find_red_x() if red_x.flag==1: Send_out(1,1,red_x.x,red_x.y) else: Send_out(1,1,30,0) #else: #fly_x=find_color_fire.x #fly_y=find_color_fire.y #find_color_flag=1 #if find_color_mode==4: #for line in find_lines(roi=[(0,0,160,120)],x_stride=20,y_stride=1,threshold=1000,theta_margin=20,rho_maigin=10): #if line.theta()>35 and line.theta()<125: #fly_mode=3 #Send_out(fire_laser,1,fly_x,fly_y,) #print("alsdjlashdljassldjalskjd",jiaoyan) #if fly_mode==3 : ''' roi_jinchang=(0,60,160,60) fly_mode=0 qifei=0 jinchang=0 qianjin=0 end_fire=0 find_fire_xun=1 find_fire_xun_will=1 find_fire_xun_rorl=-30 fire_time_start=0 fire_time_now=0 fire_time_flag=0 fire_time_end=0 #fire_end_time_start=0 #fire_end_time_now=0 #fire_end_time_flag=0 class send_data(object): color=0 sta=1 x=0 y=0 yaw=0 send_data=send_data() while True: Send_out(send_data.color,send_data.sta,send_data.x,send_data.y,send_data.yaw) send_data.color=0 send_data.sta=1 send_data.x=0 send_data.y=0 send_data.yaw=0 UartReadBuffer() for i in range(0,5): print(R.uart_data[i],i,"fly_mode=",fly_mode) if qifei==0: find_red_x() send_data.x=red_x.x send_data.y=red_x.y if R.uart_data[0]==1: if red_x.flag==1: if start_i==0 : start = time.ticks() start_i=1 else : delate=time.ticks() if delate-start>5000: qifei=1 if qifei==1 and qianjin==0: img = sensor.snapshot() for line in img.find_lines(roi=roi_jinchang,x_stride=20,y_stride=1,threshold=1500,theta_margin=25,rho_margin=25): if line.theta()>40 and line.theta()<120 and line.magnitude()>100: qianjin=1 if qianjin==0: send_data.x=0 send_data.y=-30 if qianjin==1 and end_fire<4: img = sensor.snapshot(line_filter = LineFilter) find_color(img) if find_color_fire.flag==1: send_data.x=find_color_fire.x send_data.y=find_color_fire.y if abs(find_color_fire.x)<16 and abs(find_color_fire.y)<12: if fire_time_flag==0: fire_time_start = time.ticks() fire_time_flag=1 else : fire_time_now=time.ticks() #fire_end_time_start=0 #fire_end_time_now=0 #fire_end_time_flag=0 start=0 start_i=0 delate=0 else: fire_time_start=0 fire_time_now=0 fire_time_flag=0 fire_time_end=0##########################逻辑有待斟酌 if fire_time_now-fire_time_start>5000: fire_time_end=1 else: if fire_time_end==1: if start_i==0:#if fire_end_time_flag==0: start=time.ticks()#fire_end_time_start=time.ticks() else : delate=time.ticks()#fire_end_time_now=time.ticks() if delate-start>5000:#fire_end_time_now-fire_end_time_start>1000: end_fire+=1 fire_time_end=0 send_data.x=0 send_data.y=0 else: fire_time_start=0 fire_time_now=0 fire_time_flag=0 #fire_end_time_start=0 #fire_end_time_now=0 #fire_end_time_flag=0 start=0 stare_i=0 delate=0 #Prevention_go_out(img): if find_fire_xun==1: for line in img.find_lines(roi=(0,0,160,30),x_stride=20,y_stride=1,threshold=1000,theta_margin=30,rho_maigin=10): if line.theta()<120 and line.theta()>40 and line.magnitude()>100: for i in range(0,40): img = sensor.snapshot() Send_out(0,1,find_fire_xun_rorl,1,0) find_fire_xun_will=2 if find_fire_xun_rorl==-30: for l in img.find_lines(roi=(80,0,40,120),x_stride=1,y_stride=20,threshold=1500,theta_nargin=30,rho_maigin=100): if l.theta()<30 or l.theta()>150: find_fire_xun_rorl=30 elif find_fire_xun_rorl==30: for l in img.find_lines(roi=(40,0,40,120),x_stride=1,y_stride=20,threshold=1500,theta_nargin=30,rho_maigin=100): if l.theta()<30 or l.theta()>150: find_fire_xun_rorl=-30 for i in range(0,40): img=sensor.snpshot() Send_out(0,1,0,0,0) find_fire_xun=find_fire_xun_will if find_fire_xun==1: send_data.x=-find_fire_xun_rorl/30 send_data.y=-30 #if Prevention_go_up_flag==1: #find_fire_xun_will=1 if find_fire_xun==2: for line in img.find_lines(roi=(0,90,160,30),x_stride=20,y_stride=1,threshold=1000,theta_margin=30,rho_maigin=10): if line.theta()<120 and line.theta()>40 and line.magnitude()>100: for i in range(0,40): img = sensor.snapshot() Send_out(0,1,find_fire_xun_rorl,-1,0) find_fire_xun_will=1 if find_fire_xun_rorl==-30: for l in img.find_lines(roi=(80,0,40,120),x_stride=1,y_stride=20,threshold=1500,theta_nargin=30,rho_maigin=100): if l.theta()<30 or l.theta()>150: find_fire_xun_rorl=30 elif find_fire_xun_rorl==30: for l in img.find_lines(roi=(40,0,40,120),x_stride=1,y_stride=20,threshold=1500,theta_nargin=30,rho_maigin=100): if l.theta()<30 or l.theta()>150: find_fire_xun_rorl=-30 for i in range(0,40): img=sensor.snpshot() Send_out(0,1,0,0,0) find_fire_xun=find_fire_xun_will if find_fire_xun==2: send_data.x=-find_fire_xun_rorl/30 send_data.y=30 if end_fire>=4: send_data.x=0 send_data.y=0
-
@kidswong999 谢谢大佬
-
我测试了,用OpenMV4可以运行,OpenMV3不能运行。
-
@kidswong999 是什么原因导致的呢?
-
此回复已被删除!
-
因为OpenMV4的RAM更大。