StepMotorDriver 模块
StepMotor Driver Module 13.2 V1.1 是一款适配 M5 主控的步进电机驱动器,采用 STM32 + HR8825 步进电机驱动方案,提供 3 路双极性步进电机控制接口。
支持以下产品:
MicroPython 应用示例:
1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from module import StepMotorDriverModule 9import time 10 11 12stepmotor_driver_0 = None 13 14 15DIR = None 16 17 18def setup(): 19 global stepmotor_driver_0, DIR 20 21 M5.begin() 22 Widgets.fillScreen(0x222222) 23 24 stepmotor_driver_0 = StepMotorDriverModule( 25 address=0x27, step_pin=(16, 12, 15), dir_pin=(17, 13, 0) 26 ) 27 print(stepmotor_driver_0.get_all_limit_switch_state()) 28 print(stepmotor_driver_0.get_limit_switch_state(0)) 29 print(stepmotor_driver_0.get_fault_io_state(StepMotorDriverModule.MOTOR_X)) 30 print(stepmotor_driver_0.get_firmware_version()) 31 stepmotor_driver_0.reset_motor( 32 StepMotorDriverModule.MOTOR_X, StepMotorDriverModule.MOTOR_STATE_ENABLE 33 ) 34 stepmotor_driver_0.set_motor_state(StepMotorDriverModule.MOTOR_STATE_ENABLE) 35 stepmotor_driver_0.set_microstep(StepMotorDriverModule.STEP_FULL) 36 stepmotor_driver_0.set_motor_direction(StepMotorDriverModule.MOTOR_X, 1) 37 stepmotor_driver_0.set_motor_pwm_freq(StepMotorDriverModule.MOTOR_X, 1000) 38 stepmotor_driver_0.motor_control(StepMotorDriverModule.MOTOR_X, 1) 39 DIR = 0 40 41 42def loop(): 43 global stepmotor_driver_0, DIR 44 M5.update() 45 if DIR: 46 stepmotor_driver_0.set_motor_direction(StepMotorDriverModule.MOTOR_X, 1) 47 else: 48 stepmotor_driver_0.set_motor_direction(StepMotorDriverModule.MOTOR_X, 0) 49 DIR = not DIR 50 time.sleep(2) 51 52 53if __name__ == "__main__": 54 try: 55 setup() 56 while True: 57 loop() 58 except (Exception, KeyboardInterrupt) as e: 59 try: 60 from utility import print_error_msg 61 62 print_error_msg(e) 63 except ImportError: 64 print("please update to latest firmware")
UiFlow2 应用示例
class StepMotorDriverModule
构造函数
Methods
- StepMotorDriverModule.reset_motor(motor_id, state)
重置电机。
- 参数:
motor_id – 要复位的电机。可选项:-
X:StepMotorDriverModule.MOTOR_X -Y:StepMotorDriverModule.MOTOR_Y -Z:StepMotorDriverModule.MOTOR_Zstate (bool) – 电机的状态。
UIFLOW2:

- StepMotorDriverModule.set_microstep(step)
设置细分。
- 参数:
step – 微步值。选项:-
FULL:StepMotorDriverModule.STEP_FULL -1/2:StepMotorDriverModule.STEP1_2 -1/4:StepMotorDriverModule.STEP1_4 -1/8:StepMotorDriverModule.STEP1_8 -1/16:StepMotorDriverModule.STEP1_16 -1/32:StepMotorDriverModule.STEP1_32
UIFLOW2:

- StepMotorDriverModule.set_motor_pwm_freq(motor_id, freq)
设置电机 PWM 频率。
- 参数:
motor_id – 用于设置频率的电机。选项:-
X:StepMotorDriverModule.MOTOR_X -Y:StepMotorDriverModule.MOTOR_Y -Z:StepMotorDriverModule.MOTOR_Zfreq (int) – 频率值。
UIFLOW2:

- StepMotorDriverModule.set_motor_direction(motor_id, direction)
设置电机方向。
- 参数:
motor_id – 用于设置方向的电机。选项:-
X:StepMotorDriverModule.MOTOR_X -Y:StepMotorDriverModule.MOTOR_Y -Z:StepMotorDriverModule.MOTOR_Z。direction (bool) – 方向值。选项:-
Positive:1 -Negative:0
UIFLOW2:

- StepMotorDriverModule.get_all_limit_switch_state()
获取所有 IO 状态。
UIFLOW2:

- StepMotorDriverModule.get_limit_switch_state(switch_id)
获取 io 状态。
- 参数:
switch_id (int) – io id。
UIFLOW2:

- StepMotorDriverModule.get_fault_io_state(motor_id)
获取故障 io 状态。
- 参数:
motor_id (int) – 电机 ID。选项:-
X:StepMotorDriverModule.MOTOR_X -Y:StepMotorDriverModule.MOTOR_Y -Z:StepMotorDriverModule.MOTOR_Z
UIFLOW2:

- StepMotorDriverModule.motor_control(motor_id, state)
控制电机旋转/停止。
- 参数:
motor_id – 电机 ID。选项:-
X:StepMotorDriverModule.MOTOR_X -Y:StepMotorDriverModule.MOTOR_Y -Z:StepMotorDriverModule.MOTOR_Zstate (bool) – 状态值。选项:-
Rotate:1 -Stop:0
UIFLOW2:

- StepMotorDriverModule.get_firmware_version()
获取固件版本。
UIFLOW2:

Constants
- StepMotorDriverModule.MOTOR_X
- StepMotorDriverModule.MOTOR_Y
- StepMotorDriverModule.MOTOR_Z
电机 ID
- StepMotorDriverModule.MOTOR_STATE_ENABLE
- StepMotorDriverModule.MOTOR_STATE_DISABLE
电机状态
- StepMotorDriverModule.INPUT_REG
- StepMotorDriverModule.OUTPUT_REG
- StepMotorDriverModule.POLINV_REG
- StepMotorDriverModule.CONFIG_REG
- StepMotorDriverModule.FAULT_REG
- StepMotorDriverModule.RESET_REG
- StepMotorDriverModule.FIRM_REG
- StepMotorDriverModule.I2C_REG
寄存器地址
- StepMotorDriverModule.STEP_FULL
- StepMotorDriverModule.STEP1_2
- StepMotorDriverModule.STEP1_4
- StepMotorDriverModule.STEP1_8
- StepMotorDriverModule.STEP1_16
- StepMotorDriverModule.STEP1_32
微步值。




