本文基于本人在视觉学习中所遇到的问题以及所完成的题目所作
1.关于代码整体结构
以下是我为了让是自己使用更加方便所以写的一个比较简单实用的代码框架
#main.py -- put your code here!
import cpufreq
import pyb
import sensor,image, time,math,struct
from image import SEARCH_EX, SEARCH_DS
from pyb import LED,Timer,UART
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQVGA) # Set frame size to QVGA (320x240)
sensor.skip_frames(time=2000) #延时跳过一些帧,等待感光元件变稳定
sensor.set_auto_gain(False) #黑线不易识别时,将此处写False
sensor.set_auto_whitebal(False)
#sensor.set_auto_exposure(False, exposure_us=111000)#关闭自动曝光
clock = time.clock() # Create a clock object to track the FPS.
#sensor.set_auto_exposure(True, exposure_us=5000) # 设置自动曝光sensor.get_exposure_us()
uart=UART(3,115200)
THRESHOLD = (0,100) # Grayscale threshold for dark things... (5, 70, -23, 15, -57, 0)(18, 100, 31, -24, -21, 70)
IMAGE_WIDTH=sensor.snapshot().width()
IMAGE_HEIGHT=sensor.snapshot().height()
IMAGE_DIS_MAX=(int)(math.sqrt(IMAGE_WIDTH*IMAGE_WIDTH+IMAGE_HEIGHT*IMAGE_HEIGHT)/2)
class target_check(object):
x=0 #int16_t
y=0 #int16_t
pixel=0 #uint16_t
flag=0 #uint8_t
state=0 #uint8_t
angle=0 #int16_t
distance=0 #uint16_t
apriltag_id=0#uint16_t
img_width=0 #uint16_t
img_height=0 #uint16_t
reserved1=0 #uint8_t
reserved2=0 #uint8_t
reserved3=0 #uint8_t
reserved4=0 #uint8_t
fps=0 #uint8_t
range_sensor1=0
range_sensor2=0
range_sensor3=0
range_sensor4=0
camera_id=0
reserved1_int32=0
reserved2_int32=0
reserved3_int32=0
reserved4_int32=0
class rgb(object):
def __init__(self):
self.red=LED(1)
self.green=LED(2)
self.blue=LED(3)
class uart_buf_prase(object):
uart_buf = []
_data_len = 0
_data_cnt = 0
state = 0
class singleline_check():
rho_err = 0
theta_err = 0
state = 0
class mode_ctrl(object):
work_mode = 0x01 #工作模式.默认是点检测,可以通过串口设置成其他模式
check_show = 1 #开显示,在线调试时可以打开,离线使用请关闭,可提高计算速度
class KalmanFilter:
def __init__(self):
self.x = 0 # initial state
self.p = 1 # initial state covariance
self.q = 0.1 # process noise covariance
self.r = 1 # measurement noise covariance
self.k = 0 # kalman gain
def update(self, measurement):
# Prediction update
self.p = self.p + self.q
# Measurement updat