pdf-icon

UIFlow Guide

UiFlow1 Blockly

Event

Unit

UiFlow1 Project

Module13.2 Stepmotor Driver

Example

Initialize the I2C address and frequency configuration for the stepper motors, set the rotation direction for Motor X and Motor Y, read the device's firmware version and I2C address, then enter a control loop to perform reset operations on Motors X and Y, enable/disable motor operation, and toggle their activation states at predefined time intervals.

from m5stack import *
from m5ui import *
from uiflow import *
import module

import time

setScreenColor(0x000000)

stepmotor = module.get(module.STEPMOTORDRIVER)

stepmotor.initDevice(0x27)
stepmotor.setStepPulse(500, 0)
stepmotor.setStepPulse(500, 1)
stepmotor.setStepPulse(500, 2)
stepmotor.setStepDir(0, 1)
stepmotor.setStepDir(1, 1)
stepmotor.enableMotor(1)
print(stepmotor.readStatus(0XFE))
print(stepmotor.readStatus(0XFF))
while True:
  stepmotor.resetMotor(0, 1)
  wait(2)
  stepmotor.resetMotor(1, 1)
  wait(2)
  stepmotor.enableMotor(0)
  wait(2)
  stepmotor.enableMotor(1)
  wait(2)
  stepmotor.resetMotor(0, 0)
  wait(2)
  stepmotor.resetMotor(1, 0)
  wait(2)
  wait_ms(2)

API

stepmotor.enableMotor(0)
  • Disable the running state of all stepper motors
stepmotor.initDevice(0x27)
  • Initialize the I2C address of the stepper motor driver to 0x27
stepmotor.modbus_init(26, 34, 115200, 1, 1)
  • Initialize the communication parameters of the stepper motor driver as follows:
    • Tx 26:指定用于发送数据的引脚编号
    • Rx 34:指定用于接收数据的引脚编号
    • baudrate 115200:设定 Modbus 通信的波特率为115200bps
    • mode Master:设置通信模式为主模式
    • slave addr 1:指定从设备的地址为1
stepmotor.readStatus(0XFE)
  • Read the firmware version number of the stepper motor driver
stepmotor.readFaultPinStatus(0)
  • Read the fault status of a specified motor
stepmotor.readIOstatus()
  • Read the input/output (IO) states of all limit switches
stepmotor.readPinStatus(0)
  • Read the state of a specific limit switch to determine if it is triggered
stepmotor.resetMotor(0, 1)
  • Reset motor X and set its state to TRUE
stepmotor.setI2cAddress(0x27)
  • Set the I2C communication address of the stepper motor driver to 0x27
stepmotor.setMicroStepSelect(0)
  • Set the microstepping mode of the stepper motor to "full-step" mode
stepmotor.singleMotorCtrl(0, 0)
  • Set the state of a single motor to pause
stepmotor.setStepDir(0, 0)
  • Set the running direction of motor X to "reverse"
stepmotor.setStepPulse(500, 0)
  • Set the pulse frequency of motor X to 500Hz
modbus.read_coils(1, 1, 0)
  • Read the coil status from a slave device with address 1
modbus.read_discrete_inputs(1, 1, 0)
  • Read discrete inputs starting from Modbus slave device address 1
modbus.read_holding_registers(1, 1, 0, True)
  • Read holding registers from a slave device with address
modbus.read_input_registers(1, 1, 0, True)
  • Read input registers from a Modbus slave device with address
modbus.write_multiple_coils(1, 1, 0)
  • Write output values to multiple coils of a slave device with address
modbus.write_multiple_registers(1, 1, 0, True)
  • Write values to multiple registers of a slave device with address
modbus.write_single_coil(1, 1, 0)
  • Write a value to a single coil of a slave device with address
modbus.write_single_register(1, 1, 0, True)
  • Write a value to a single holding register of a slave device with address
print((str('code:') + str((1))))
  • Define the function codes used in Modbus communication, set here as READ_COILS_STATUS, which indicates reading coil status
    • READ_COILS_STATUS:Read coil status
    • READ_INPUT_STATUS:Read input status
    • READ_HOLDING_REGISTERS:Read holding registers
    • READ_INPUT_REGISTERS: Read input registers
    • WRITE_SINGLE_COIL:Write to a single coil
    • WRITE_SINGLE_REGISTER:Write to a single holding register
    • WRITE_MULTIPLE_COILS:Write to multiple coils
    • WRITE_MULTIPLE_REGISTERS:Write to multiple holding registers
modbus.find_address
  • Get the address of the Modbus slave device
modbus.find_function
  • Get the function code currently used in the Modbus request
modbus.find_quantity
  • Get the number of registers or coils to be read or written
modbus.function_init(1, 0, 0)
  • this statement initializes the function code operation of the Modbus slave device, setting the starting address to 0
modbus.receive_req_create_pdu()
  • Receive an Application Data Unit (ADU) request, which is the data frame transmitted in the Modbus protocol
modbus.create_slave_response(0)
  • Send an ADU response, which contains the response data from the Modbus slave device
modbus.update_process(1, 0, 0, [0, 0, 0])
  • Update the function code of the Modbus slave device
modbus._mdbus_uart.any()
  • Check if there is data in the UART buffer
modbus._mdbus_uart.read()
  • Read all available data from the UART buffer
modbus._mdbus_uart.readline()
  • Read a line of data from the UART
modbus._mdbus_uart.read(10)
  • Read a specified number of characters from the UART
modbus._mdbus_uart.write('')
  • Write data to the UART for data transmission in serial communication
modbus._mdbus_uart.write(''+"\r\n")
  • Write a line of data to the UART and automatically add a newline character
modbus._mdbus_uart.write(bytes([0, 0, 0]))
  • Send raw data through the UART
On This Page