logo

tb6612

2019-01-12 21:16
Y
_5w2a

我用Stepper模块实例化驱动步进电机,IDE为什么提示要加载tb6612模块?

I
网红小智智 星瞳科技

如果你说的是IDE内置的例子的话,Stepper是类,tb6612是模块。

Y
_5w2a

我自己写了一个模块stepper,Stepper类在stepper中

Y
_5w2a
# 在图像上使用morph函数来进行边缘检测。
# 然后在进行阈值和核滤波
# 最后进行特征点检测
import sensor, image, time,lcd
from pyb import Pin
from stepper import Stepper     #加载模块

step_pin = Pin('P4')    #步进电机脉冲脚
dir_pin = Pin('P5')     #步进电机方向脚
my_stepper = Stepper(step_pin,dir_pin)  #实例化
#设置核函数滤波,核内每个数值值域为[-128,127],核需为列表或元组
kernel_size = 1 # kernel width = (size*2)+1, kernel height = (size*2)+1
kernel = [-1, -1, -1,\
          -1, +8, -1,\
          -1, -1, -1]
# 这个一个高通滤波器。见这里有更多的kernel
# http://www.fmwconcepts.com/imagemagick/digital_image_filtering.pdf
thresholds = [(100, 255)]   # grayscale thresholds设置阈值

sensor.reset() # 初始化 sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565
sensor.set_framesize(sensor.QVGA) # or sensor.QVGA (or others)
sensor.skip_frames(10) # 让新的设置生效
lcd.init()
clock = time.clock() # 追踪FPS

# 在OV7725 sensor上, 边缘检测可以通过设置sharpness/edge寄存器来增强。
# 注意:这个会edge detection can be enhanced
# significantly by setting the sharpness/edge registers.
# Note: This will be implemented as a function later.
if (sensor.get_id() == sensor.OV7725):
    sensor.__write_reg(0xAC, 0xDF)           #在address(int)向相机寄存器中写入value(int)
    sensor.__write_reg(0x8F, 0xFF)           #在address(int)上读取摄像头数据表

kpts1 = image.load_descriptor("/desc.orb")

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().

    my_stepper.steps(400)      #步进脉冲控制
    time.sleep(1000)

    img = sensor.snapshot() #拍照
    img.morph(kernel_size, kernel)
    #morph(size, kernel, mul=Auto, add=0),morph变换,mul根据图像对比度
    #进行调整,mul使图像每个像素乘mul;add根据明暗度调整,使得每个像素值加上add值。
    #如果不设置则不对morph变换后的图像进行处理。
    img.binary(thresholds)
    #利用binary函数对图像进行分割

    # Erode pixels with less than 2 neighbors using a 3x3 image kernel
    img.erode(1, threshold = 2)
    #侵蚀函数erode(size, threshold=Auto),去除边缘相邻处多余的点。threshold
    #用来设置去除相邻点的个数,threshold数值越大,被侵蚀掉的边缘点越多,边缘旁边
    #白色杂点少;数值越小,被侵蚀掉的边缘点越少,边缘旁边的白色杂点越多。

    kpts2 = img.find_keypoints(max_keypoints=150, threshold=10, normalized=True)
    #如果检测到特征物体
    if (kpts2):
        #匹配当前找到的特征和最初的目标特征的相似度
        match = image.match_descriptor(kpts1, kpts2, threshold=85)
        #image.match_descriptor(descritor0, descriptor1, threshold=70, filter_outliers=False)。
        #本函数返回kptmatch对象。
        #threshold阈值设置匹配的准确度,用来过滤掉有歧义的匹配。这个值越小,准确度越高。阈值范围0~100,默认70
        #filter_outliers默认关闭。

        #match.count()是kpt1和kpt2的匹配的近似特征点数目。
        #如果大于18,证明两个特征相似,匹配成功。
        if(match.count()>18):
        # If we have at least n "good matches"
        # Draw bounding rectangle and cross.
        #在匹配到的目标特征中心画十字和矩形框。
            img.draw_rectangle(match.rect())
            img.draw_cross(match.cx(), match.cy(), size=10)
            res=img.draw_string(100,100,'good',color=255,scale=3,x_spacing=1)
            lcd.display(res)
            time.sleep(2000)
            lcd.clear()
            time.sleep(2000)
        else:
            res=img.draw_string(100,100,'empth',color=255,scale=3,x_spacing=1)
            lcd.display(res)
            time.sleep(2000)
            lcd.clear()
            time.sleep(2000)
                #match.theta()是匹配到的特征物体相对目标物体的旋转角度。
        print(kpts2, "matched:%d dt:%d"%(match.count(), match.theta()))
                #不建议draw_keypoints画出特征角点。
                # NOTE: uncomment if you want to draw the keypoints
                #img.draw_keypoints(kpts2, size=KEYPOINTS_SIZE, matched=True)
    print(clock.fps())


实例化在里面
2019-01-12 21:27
I
网红小智智 星瞳科技

那你就不用管IDE的这个提示