from machine import Pin
import time
class Motor:
motor_steps = [[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 __init__(self,motor_a,motor_b,motor_c,motor_d):
self.a = Pin(motor_a, Pin.OUT)
self.b = Pin(motor_b, Pin.OUT)
self.c = Pin(motor_c, Pin.OUT)
self.d = Pin(motor_d, Pin.OUT)
self.a.value(0)
self.b.value(0)
self.c.value(0)
self.d.value(0)
def motor_move(self,motor_flag):
self.a.value(self.motor_steps[motor_flag][0])
self.b.value(self.motor_steps[motor_flag][1])
self.c.value(self.motor_steps[motor_flag][2])
self.d.value(self.motor_steps[motor_flag][3])
def motor_move_angle(self,target_angle,clockwise = True,speed = 2):
# target_angle 旋转角度
# clockwise True为顺时针旋转 False为逆时针旋转
# speed 转转速度 速度越大越慢
now_angle = 0
motor_count = 0
while now_angle < target_angle:
self.motor_move(motor_count)
if clockwise:
if motor_count > 0:
motor_count -= 1
else:
motor_count = 7
else:
if motor_count < 7:
motor_count += 1
else:
motor_count = 0
now_angle += 5.625/64
time.sleep_ms(speed)
基于旋转角为5.625步进电机 工作方式为八拍