1 Star 0 Fork 0

visitor009/esp32 micropython

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Stepping_motor.py 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
visitor009 提交于 2024-03-28 18:31 +08:00 . update Stepping_motor.py.
'''
步进电机
型号:28byj-48
from Stepping_motor import SteppingMotor
motor = SteppingMotor(17,18,19,22)
motor.step_angle(360)
'''
'''
步进电机
型号:28byj-48
'''
from machine import Pin
import time
class SteppingMotor:
def __init__(self,in1,in2,in3,in4):
# 引脚初始化
self.in1 = Pin(in1, Pin.OUT)
self.in2 = Pin(in2, Pin.OUT)
self.in3 = Pin(in3, Pin.OUT)
self.in4 = Pin(in4, Pin.OUT)
# 延时
self.delay = 1
# 电机旋转一圈所需的步数,(约360°),会有轻微偏差.
self.ROUND_VALUE = 509
# 四相八拍步进电机顺序值:A-AB-B-BC-C-CD-D-DA-A.
self.STEP_VALUE = [
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[1, 0, 0, 1],
]
# 引脚输出电平低
def reset(self):
self.in1(0)
self.in2(0)
self.in3(0)
self.in4(0)
# 如果count是正整数,则顺时针旋转,如果count是负整数,则逆时针旋转
# 0 - 509 为 0 -360
def step_run(self,count):
STEP_VALUE = self.STEP_VALUE
direction = 1 # 顺时针转
if count < 0:
direction = -1 # 逆时针旋转
count = -count
for x in range(count):
for bit in STEP_VALUE[::direction]:
self.in1(bit[0])
self.in2(bit[1])
self.in3(bit[2])
self.in4(bit[3])
time.sleep_ms(self.delay)
self.reset()
# 如果a是正整数,则顺时针旋转,如果a是负整数,则逆时针旋转
# 在现有位置旋转度数
# a 度数。0- 360 和step_run
def step_angle(self,a):
self.step_run(int(self.ROUND_VALUE * a / 360))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/visitor009/esp32-micropython.git
git@gitee.com:visitor009/esp32-micropython.git
visitor009
esp32-micropython
esp32 micropython
master

搜索帮助