esp32+Micorpyhon控制步进电机旋转精确角度(八拍)

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步进电机 工作方式为八拍

  • 18
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值