前言
由于要参加比赛(主要是控制类的),有用到视觉部分,前来学习。了解到了OpenMV这个视觉模块,学习一下如何高效地运用它。特此来写一篇文章来回顾一下OpenMV的内容.
框架
OpenMV内部包含了一个Python解释器,使用的是MicroPython,可以使用python代码来操作OpenMV上的IO口、外设,接下来简单了解一下OpenMV的框架。
OpenMV的外设
OpenMV的外设有GPIO口、UART、IIC、SPI、DAC、ADC、PWM、外部中断,计时器。OpenMV的功能主要是进行图像处理,将处理结果传输到主控芯片上,完成特定功能。
OpenMV的外设控制基本在pyb模块中,在使用这些外设之前import pyb就行了。
GPIO
from pyb import Pin
p_out = Pin('P7', Pin.OUT_PP)#'P7'对应的IO口看官方手册
p_out.high()#设置为高电平
p_out.low()#设置为低电平
'''
Pin('P0') -> P0 (PB15) - 5V 耐压,3.3V 输出,高达 25 mA 驱 动。
Pin('P1') -> P1 (PB14) - 5V 耐压,3.3V 输出,高达 25 mA 驱 动。
Pin('P2') -> P2 (PB13) - 5V 耐压,3.3V 输出,高达 25 mA 驱 动。
Pin('P3') -> P3 (PB12) - 5V 耐压,3.3V 输出,高达 25 mA 驱 动。
Pin('P4') -> P4 (PB10) - 5V 耐压,3.3V 输出,高达 25 mA 驱 动。
Pin('P5') -> P5 (PB11) - 5V 耐压,3.3V 输出,高达 25 mA 驱 动。
Pin('P6') -> P6 (PA5) - 5V 耐压,3.3V 输出,高达 25 mA 驱 动。
Pin('P7') -> P7 (PD12) - 5V 耐压,3.3V 输出,高达 25 mA 驱 动。
Pin('P8') -> P8 (PD13) - 5V 耐压,3.3V 输出,高达 25 mA 驱 动。
'''
p_in = Pin('P6', Pin.PULL_UP)
Pin.value()
外部中断
from pyb import Pin, ExtInt
callback = lambda e :print('hello world')
ext = ExtInt(Pin('P7'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
#设置P7口为外部中断引脚, 上升沿触发,输入无没有上拉或下拉电阻,中断时运行callback
计时器
from pyb import Timer
tim = Timer(4, freq=1000)#使用定时器4, 以1000Hz的频率来定时
tim.count()#获取定时器的值
tim.freq(0.5)#将频率改为0.5Hz
tim.callback(lambda t: pyb.LED(1).toggle())#定时运行这个函数
'''
这是定时器引脚分配:
Timer 1 Channel 3 Negative -> P0
Timer 1 Channel 2 Negative -> P1
Timer 1 Channel 1 Negative -> P2
Timer 2 Channel 3 Positive -> P4
Timer 2 Channel 4 Positive -> P5
Timer 2 Channel 1 Positive -> P6
Timer 4 Channel 1 Negative -> P7
Timer 4 Channel 2 Negative -> P8
'''
UART
from pyb import UART
uart = UART(3, 9600)#串口3(OpenMV只引出了串口3)设置波特率为9600
uart.write('hello')#发送hello
uart.read(6)#读取六个字节
IIC
from pyb import I2C
i2c = I2C(2, I2C.MASTER, baudrate=100000)
i2c.scan() # returns list of slave addresses
i2c.send('hello', 0x42) # send 5 bytes to slave with ad dress 0x42
i2c.recv(5, 0x42) # receive 5 bytes from slave
i2c.mem_read(2, 0x42, 0x10) # read 2 bytes from slave 0 x42, slave memory 0x10 i2c.mem_write('xy', 0x42, 0x10) # write 2 bytes to slav e 0x42, slave memory 0x10
SPI
from pyb import SPI
spi = SPI(2, SPI.MASTER, baudrate=200000, polarity=1, p hase=0)
spi.send('hello') spi.recv(5) # receive 5 bytes on the bus
spi.send_recv('hello') # send a receive 5 bytes
ADC
from pyb import Pin, ADC
adc = ADC('P6') adc.read() # read value, 0-4095
DAC
from pyb import Pin, DAC
dac = DAC('P6') dac.write(120) # output between 0 and 255
'''
DAC('P6') -> P6 (PA5) - 只有 3.3V(非 5V)电压在这个模式!
'''
PWM
from pyb import Pin, Timer
p = Pin('P7') # P7 has TIM4, CH1
tim = Timer(4, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)
后续会记录OpenMV视觉部分的学习过程