一、Openmv开发一之LED篇
作者:Playcodes
时间:2021年6月13日
用途:2021年全国大学生电子设计竞赛
版权所有,转载附带原有地址
用于在开发途中工程量的问题,所以将单独的功能写成模块的形式,最后通过main.py调用即可,由于LED功能实现简单,此文章的主要用途是对openmv中模块调用进行了解
1.硬件准备
- OPENMV4
- 高速内存卡SD_Card(存放代码——离线运行)
2.简述OPENMV的两种运行方式
2.1 通过OPENMV_IDE在线运行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aVt6XWKt-1623570305653)(https://i.loli.net/2021/06/13/hiR6AEz481oNFjf.png)]
2.2 脱机运行(此时需要一个SD_Card)
- 我的内存卡存放的东西如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0fpecqnZ-1623570305654)(https://i.loli.net/2021/06/13/dkjCOaTh1Ho2XrD.png)]
其中mian.py是openmv运行时的主程序,而myled是我自己写的LED外设包,可以通过main直接导包调用即可
类似于C语言中的导入文件库。
附件1:main.py中的代码
# Hello World Example
#
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
import sensor, image, time # Create a clock object to track the FPS.
from myled import MYLED
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000) # Wait for settings take effect.
clock = time.clock()
while(True):
MYLED.ON(MYLED.LED_Red)
附件2:myled.py中的代码
# Hello World Example
#
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
from pyb import LED
class MYLED:
LED_Red = LED(1) # 红色 led
LED_Green = LED(2)
LED_Blue = LED(3)
LED_IR = LED(4)
@staticmethod
def ON(led):
if led == MYLED.LED_Red:
MYLED.LED_Red.on()
if led == MYLED.LED_Green:
MYLED.LED_Green.on()
if led == MYLED.LED_Blue:
MYLED.LED_Blue.on()
@staticmethod
def OFF(led):
if led == MYLED.LED_Red:
MYLED.LED_Red.off()
if led == MYLED.LED_Green:
MYLED.LED_Green.off()
if led == MYLED.LED_Blue:
MYLED.LED_Blue.off()
@staticmethod
def TOGGLE(led):
if led == MYLED.LED_Red:
MYLED.LED_Red.toggle()
if led == MYLED.LED_Green:
MYLED.LED_Green.toggle()
if led == MYLED.LED_Blue:
MYLED.LED_Blue.toggle()
实验现象很简单,仅仅是LED灯常亮为红色