Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

ESP32(MicroPython) 步进电机调速


ESP32(MicroPython) 步进电机调速

不同于常见的步进电机调速程序,本程序实现了更多级调速并且提供了毫秒延时与微秒延时的切换(KEY4),由于本人使用的步进电机(28BYJ48)最少使用600微秒延时(使用500微秒延时不能正常运行),微秒延时做成最短600微秒。另外,毫秒延时最多46毫秒。


代码如下


'''

接线说明:电机模块-->ESP32 IO

         (IN1-IN4)-->(15,2,0,4)

         

         电机模块输出-->28BYJ-48步进电机

         (5V)-->红线

         (O1)-->依次排序

         

         按键模块-->ESP32 IO

         (K1-K4)-->(14,27,26,25)

按键功能:KEY1切换正反转,KEY2加速,KEY3减速,KEY4切换高、低速模式

'''

#导入Pin模块

from machine import Pin

import time

 

#定义按键控制对象

key1=Pin(14,Pin.IN,Pin.PULL_UP)

key2=Pin(27,Pin.IN,Pin.PULL_UP)

key3=Pin(26,Pin.IN,Pin.PULL_UP)

key4=Pin(25,Pin.IN,Pin.PULL_UP)

 

#定义步进电机控制对象

motor_a=Pin(15,Pin.OUT,Pin.PULL_DOWN)

motor_b=Pin(2,Pin.OUT,Pin.PULL_DOWN)

motor_c=Pin(0,Pin.OUT,Pin.PULL_DOWN)

motor_d=Pin(4,Pin.OUT,Pin.PULL_DOWN)

 

 

#定义按键键值

KEY1_PRESS,KEY2_PRESS,KEY3_PRESS,KEY4_PRESS=1,2,3,4

key_en=1

#按键扫描函数

def key_scan():

    global key_en

    if key_en==1 and (key1.value()==0 or key2.value()==0 or

                      key3.value()==0 or key4.value()==0 ):

        time.sleep_ms(10)

        key_en=0

        if key1.value()==0:

            return KEY1_PRESS

        elif key2.value()==0:

            return KEY2_PRESS

        elif key3.value()==0:

            return KEY3_PRESS

        elif key4.value()==0:

            return KEY4_PRESS

    elif key1.value()==1 and key2.value()==1 and key3.value()==1 and key4.value()==1:

        key_en=1

    return 0

    

 

#步进电机发送脉冲函数

def step_motor_send_pulse(step,fx):

    temp=step

    if fx==0:

        temp=7-step

    if temp==0:

        motor_a.value(1)

        motor_b.value(0)

        motor_c.value(0)

        motor_d.value(0)

    elif temp==1:

        motor_a.value(1)

        motor_b.value(1)

        motor_c.value(0)

        motor_d.value(0)

    elif temp==2:

        motor_a.value(0)

        motor_b.value(1)

        motor_c.value(0)

        motor_d.value(0)

    elif temp==3:

        motor_a.value(0)

        motor_b.value(1)

        motor_c.value(1)

        motor_d.value(0)

    elif temp==4:

        motor_a.value(0)

        motor_b.value(0)

        motor_c.value(1)

        motor_d.value(0)

    elif temp==5:

        motor_a.value(0)

        motor_b.value(0)

        motor_c.value(1)

        motor_d.value(1)

    elif temp==6:

        motor_a.value(0)

        motor_b.value(0)

        motor_c.value(0)

        motor_d.value(1)

    elif temp==7:

        motor_a.value(1)

        motor_b.value(0)

        motor_c.value(0)

        motor_d.value(1)

            

#程序入口

if __name__=="__main__":

    key=0

    fx1=1

    speed1=1

    step1=0

    while True:

      if speed1<4:

        key=key_scan()

        if key==KEY1_PRESS:

            fx1=not fx1 

        elif key==KEY2_PRESS:

            if speed1>1:

                speed1-=1

        elif key==KEY3_PRESS:

                speed1+=1

        elif key==KEY4_PRESS:

                speed1=900        

        step_motor_send_pulse(step1,fx1)

        step1+=1

        if step1==8:

            step1=0

        time.sleep_ms(speed1)

      elif 3<speed1<10:

        key=key_scan()

        if key==KEY1_PRESS:

            fx1=not fx1 

        elif key==KEY2_PRESS:

             speed1-=2

        elif key==KEY3_PRESS:

             speed1+=2

        elif key==KEY4_PRESS:

                speed1=900     

        step_motor_send_pulse(step1,fx1)

        step1+=1

        if step1==8:

            step1=0

        time.sleep_ms(speed1)

      elif 9<speed1<22:

        key=key_scan()

        if key==KEY1_PRESS:

            fx1=not fx1 

        elif key==KEY2_PRESS:

             speed1-=4

        elif key==KEY3_PRESS:

             speed1+=4

        elif key==KEY4_PRESS:

                speed1=900     

        step_motor_send_pulse(step1,fx1)

        step1+=1

        if step1==8:

            step1=0

        time.sleep_ms(speed1)

      elif 21<speed1<47:

        key=key_scan()

        if key==KEY1_PRESS:

            fx1=not fx1 

        elif key==KEY2_PRESS:

             speed1-=8

        elif key==KEY3_PRESS:

            if speed1<46:

              speed1+=8

        elif key==KEY4_PRESS:

                speed1=900     

        step_motor_send_pulse(step1,fx1)

        step1+=1

        if step1==8:

            step1=0

        time.sleep_ms(speed1)

      elif speed1>599:

        key=key_scan()

        if key==KEY1_PRESS:

            fx1=not fx1 

        elif key==KEY2_PRESS:

            if speed1>600:

             speed1-=100

        elif key==KEY3_PRESS:

            if speed1<900:

             speed1+=100

        elif key==KEY4_PRESS:

                speed1=1     

        step_motor_send_pulse(step1,fx1)

        step1+=1

        if step1==8:

            step1=0

        time.sleep_us(speed1)  


————————————————

版权声明:本文为CSDN博主「路易斯720」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_74155302/article/details/130954164



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.0098221302032471 seconds