目录
前言
一、材料
二、接线?
三、二维码制作
?四、openmv代码
前言
学校开了门openmv的课,最后要做个东西算最后成绩。一开始准备做个基于openmv的手势识别电梯的,模型啥的都做好了最后导出时发现Edge里面没openmv了,没办法就做了一个二维码控制的了
一、材料
1.openmv(我的是openmv4要做手势识别要openmv4plus)
2.两相四线步进电机
3.THB6128步进电机驱动
步进电机驱动说明
4.电源
二、接线
?
?1.A+-、B+-连接步进电机四根线(一般红蓝为一组),VG、VM连接电源(我用了USB-TTL接了一块充电宝)
?2.驱动板连接openmv,DIR+-分别接P2P3,CP+-分别接P7P8
三、二维码制作
制作二维码
我这里是制作完二维码后图方便将四个二维码放在了一个app里
对应APK文件
APP源码
?四、openmv代码
话不多说直接上代码
# 电机驱动板输出PWM例程
#
# 此示例显示如何控制OpenMV Cam上的电机扩展板。
# 通过使用PYB模块控制电机扩展板,可以通过PWM控制速度并设置数字I/O引脚状态。
# 电机扩展板需要6个I/O引脚用于两个电机。
import pyb,sensor, image
import time
from pyb import UART
uart = UART(3, 4800)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # can be QVGA on M7...
sensor.skip_frames(30)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
# 这些引脚控制我们的方向,而下面的其他PWN引脚控制速度。
# 每个电机的方向由H桥设置,其中A0/1是一个H桥驱动器的两侧。 B0/1是另一个H桥。
pinADir0 = pyb.Pin('P3', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
pinADir1 = pyb.Pin('P2', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
# 对于向前或向后操作,Dir0/1必须彼此不相等。如果它们相等则那就是刹车操作。
# 如果它们不相等,则电机将以另一个方式旋转,这取决于它的连接和dir 0的值。
pinADir0.value(0)
pinADir1.value(1)
# 对于向前或向后操作,Dir0/1必须彼此不相等。如果它们相等则那就是刹车操作。
# 如果它们不相等,则电机将以另一个方式旋转,这取决于它的连接和dir 0的值。
# 创建一个以1KHz运行的定时器对象,它将为我们的OpenMV Cam上的PWM输出供电。
# 只需要创建一次。
tim = pyb.Timer(4, freq=1000)
# 使用timer对象在OpenMV Cam上创建两个PWM输出。这些定时器控制电机的速度。
# 您将在循环中重复设置这些定时器的PWM百分比。
chA = tim.channel(1, pyb.Timer.PWM, pin=pyb.Pin("P7"))
chB = tim.channel(2, pyb.Timer.PWM, pin=pyb.Pin("P8"))
num=0
floor=0
while (True):
img = sensor.snapshot()
img.lens_corr(1.8)
for code in img.find_qrcodes():
output_str="%s" % code.payload()
num=output_str
#output_str为二维码内容
print(num)
if(num=="1"):
uart.write("1")
#for i in range(50):
#pyb.delay(50)
#chA.pulse_width_percent(i)
#chB.pulse_width_percent(49-i)
#print(i)
#print("--------------------------")
#for i in range(50):
#pyb.delay(50)
#chA.pulse_width_percent(49-i)
#chB.pulse_width_percent(i)
if(floor==0):
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if(floor==2):
pinADir0.value(1)
pinADir1.value(0)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if(floor==3):
pinADir0.value(1)
pinADir1.value(0)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if(floor==4):
pinADir0.value(1)
pinADir1.value(0)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
floor=1
pinADir0.value(0)
pinADir1.value(1)
num=0
if(num=="2"):
uart.write("2")
if(floor==4):
pinADir0.value(1)
pinADir1.value(0)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if(floor==3):
pinADir0.value(1)
pinADir1.value(0)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if(floor==1):
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if(floor==0 ) :
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
num=0
floor=2
pinADir0.value(0)
pinADir1.value(1)
if(num=="3"):
uart.write("3")
if (floor==0):
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------1")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------2")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------3")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if (floor==1):
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------1")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------2")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if (floor==2):
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------1")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if (floor==4):
pinADir0.value(1)
pinADir1.value(0)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------1")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
num=0
floor=3
pinADir0.value(0)
pinADir1.value(1)
if(num=="4"):
uart.write("4")
if (floor==0):
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------1")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------2")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------3")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------4")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if (floor==1):
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------1")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------2")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------3")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if (floor==2):
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------1")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------1")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
if (floor==3):
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(i)
chB.pulse_width_percent(49-i)
print(i)
print("--------------------------1")
for i in range(50):
pyb.delay(50)
chA.pulse_width_percent(49-i)
chB.pulse_width_percent(i)
num=0
floor=4
#for i in range(100):
#pyb.delay(100)
#chA.pulse_width_percent(99-i)
#chB.pulse_width_percent(i)
?哈哈,代码有点low,欢迎大家多多来交流交流
|