4EncoderMotor 模块

4EncoderMotor 模块是一款采用 STM32+BL5617 解决方案的 4 通道编码器电机驱动器模块。它适用于机器人运动控制、自动化设备、智能车辆、实验室设备和工业自动化系统等各种应用。

这是 4EncoderMotor 模块的驱动库,用于控制电机并读取编码器值。

支持以下产品:

4EncoderMotor

4EncoderMotor-V11

UiFlow2 应用示例

电机控制

在 UiFlow2 中打开 encoder4motor_core2_example.m5f2 项目。

这个示例展示了如何控制电机并读取编码器值。

UiFlow2 代码块:

example.png

示例输出:

None

MicroPython 应用示例

电机控制

这个示例展示了如何控制电机并读取编码器值。

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 Encoder4MotorModule
 9
10
11title0 = None
12label0 = None
13label1 = None
14label2 = None
15label3 = None
16encoder4_motor_0 = None
17
18
19def setup():
20    global title0, label0, label1, label2, label3, encoder4_motor_0
21
22    M5.begin()
23    Widgets.fillScreen(0x222222)
24    title0 = Widgets.Title(
25        "4EncoderMotor Core2 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
26    )
27    label0 = Widgets.Label("label0", 1, 56, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
28    label1 = Widgets.Label("label1", 1, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
29    label2 = Widgets.Label("label2", 1, 144, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
30    label3 = Widgets.Label("label3", 1, 185, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
31
32    encoder4_motor_0 = Encoder4MotorModule(address=0x24)
33    encoder4_motor_0.set_all_motors_mode(0x02)
34    encoder4_motor_0.set_speed_point_value(0x00, 50)
35    encoder4_motor_0.set_speed_point_value(0x01, 50)
36    encoder4_motor_0.set_speed_point_value(0x02, 50)
37    encoder4_motor_0.set_speed_point_value(0x03, 50)
38
39
40def loop():
41    global title0, label0, label1, label2, label3, encoder4_motor_0
42    M5.update()
43    label0.setText(
44        str((str("Motor1 Speed:") + str((encoder4_motor_0.get_motor_speed_value(0x00)))))
45    )
46    label1.setText(
47        str((str("Motor2 Speed:") + str((encoder4_motor_0.get_motor_speed_value(0x01)))))
48    )
49    label2.setText(
50        str((str("Motor3 Speed:") + str((encoder4_motor_0.get_motor_speed_value(0x02)))))
51    )
52    label3.setText(
53        str((str("Motor4 Speed:") + str((encoder4_motor_0.get_motor_speed_value(0x03)))))
54    )
55
56
57if __name__ == "__main__":
58    try:
59        setup()
60        while True:
61            loop()
62    except (Exception, KeyboardInterrupt) as e:
63        try:
64            from utility import print_error_msg
65
66            print_error_msg(e)
67        except ImportError:
68            print("please update to latest firmware")

示例输出:

None

API参考

Encoder4MotorModule

class module.encoder4_motor.Encoder4MotorModule(address=36)

基类:object

创建一个 Encoder4MotorModule 对象

参数:

address (int) – 设备的 I2C 地址。默认是 0x24。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from module import Encoder4MotorModule

encoder4_motor = Encoder4MotorModule(0x24)
set_motor_mode(motor, mode)

设置电机的工作模式。

参数:
  • motor (int) – 要设置工作模式的电机。

  • mode (int) – 电机的工作模式。选项: - NORMAL_MODE: 0 - POSITION_MODE: 1 - SPEED_MODE: 2

UiFlow2 代码块:

set_motor_mode.png

MicroPython 代码块:

encoder4_motor.set_motor_mode(0, encoder4_motor.NORMAL_MODE)
set_all_motors_mode(mode)

设置所有电机的模式。

参数:

mode (int) – 电机的工作模式。选项: - NORMAL_MODE: 0 - POSITION_MODE: 1 - SPEED_MODE: 2

UiFlow2 代码块:

set_all_motors_mode.png

MicroPython 代码块:

encoder4_motor.set_all_motors_mode(encoder4_motor.NORMAL_MODE)
set_motor_pwm_dutycycle(motor, duty)

设置电机的 PWM 占空比。

参数:
  • motor (int) – 要设置 PWM 占空比的电机。

  • duty (int) – PWM 占空比。

UiFlow2 代码块:

set_motor_pwm_dutycycle.png

MicroPython 代码块:

encoder4_motor.set_motor_pwm_dutycycle(0, 127)
get_motor_encoder_value(pos)

获取电机的编码器值。

参数:

pos (int) – 要获取编码器值的电机。

返回:

编码器值。

返回类型:

int

UiFlow2 代码块:

get_motor_encoder_value.png

MicroPython 代码块:

encoder4_motor.get_motor_encoder_value(0)
set_motor_encoder_value(pos, value)

设置电机的编码器值。

参数:
  • pos (int) – 要设置编码器值的电机。

  • value (int) – 编码器值。

UiFlow2 代码块:

set_motor_encoder_value.png

MicroPython 代码块:

encoder4_motor.set_motor_encoder_value(0, 100)
get_encoder_mode()

获取编码器模式。

返回:

编码器模式。

返回类型:

int

UiFlow2 代码块:

get_encoder_mode.png

MicroPython 代码块:

encoder4_motor.get_encoder_mode()
set_encoder_mode(mode)

设置编码器模式。

参数:

mode (int) – 编码器模式。选项: - AB: 0 - BA: 1

UiFlow2 代码块:

set_encoder_mode.png

MicroPython 代码块:

encoder4_motor.set_encoder_mode(0x00)
get_motor_speed_value(pos)

获取电机的速度值。

参数:

pos (int) – 要获取速度值的电机。

返回:

速度值。

返回类型:

int

UiFlow2 代码块:

get_motor_speed_value.png

MicroPython 代码块:

encoder4_motor.get_motor_speed_value(0)
set_position_encoder_value(pos, value)

设置电机的位置编码器值。

参数:
  • pos (int) – 要设置位置编码器值的电机。

  • value (int) – 位置编码器值。

UiFlow2 代码块:

set_position_encoder_value.png

MicroPython 代码块:

encoder4_motor.set_position_encoder_value(0, 100)
set_position_max_speed_value(pos, value)

设置电机的最大速度值。

参数:
  • pos (int) – 电机设置的最高转速值。

  • value (int) – 最大速度值。

UiFlow2 代码块:

set_position_max_speed_value.png

MicroPython 代码块:

encoder4_motor.set_position_max_speed_value(0, 127)
get_position_pid_value(pos)

获取电机的位置 PID 值。

参数:

pos (int) – 获取位置 P,I,D 值的电机。

返回:

位置 PID 值。

返回类型:

list[int, int, int]

UiFlow2 代码块:

get_position_PID_value.png

MicroPython 代码块:

encoder4_motor.get_position_pid_value(0)
set_position_pid_value(pos, p, i, d)

设置电机的位置 P,I,D 值。

参数:
  • pos (int) – 设置位置 P,I,D 值的电机。

  • p (int) – 比例值。

  • i (int) – 积分值。

  • d (int) – 微分值。

UiFlow2 代码块:

set_position_PID_value.png

MicroPython 代码块:

encoder4_motor.set_position_pid_value(0, 100, 100, 100)
get_speed_pid_value(pos)

获取电机的速度 PID 值。

参数:

pos (int) – 获取速度 P,I,D 值的电机。

返回:

速度 P,I,D 值。

返回类型:

list[int, int, int]

UiFlow2 代码块:

get_speed_PID_value.png

MicroPython 代码块:

encoder4_motor.get_speed_PID_value(0)
set_speed_pid_value(pos, p, i, d)

设置电机的速度 PID 值。

参数:
  • pos (int) – 设置速度 PID 值的电机。

  • p (int) – 比例值。

  • i (int) – 积分值。

  • d (int) – 微分值。

UiFlow2 代码块:

set_speed_PID_value.png

MicroPython 代码块:

encoder4_motor.set_speed_PID_value(0, 100, 100, 100)
set_speed_point_value(pos, point)

设置电机的速度目标值。

参数:
  • pos (int) – 设置速度目标值的电机。

  • point (int) – 速度目标值。

UiFlow2 代码块:

set_speed_point_value.png

MicroPython 代码块:

encoder4_motor.set_speed_point_value(0, 127)
get_vin_current_float_value()

获取输入电流值(浮点数)。

返回:

输入电流值。

返回类型:

float

UiFlow2 代码块:

get_vin_current_float_value.png

MicroPython 代码块:

encoder4_motor.get_vin_current_float_value()
get_vin_current_int_value()

获取输入电流值(整数)。

返回:

输入电流值。

返回类型:

int

UiFlow2 代码块:

get_vin_current_int_value.png

MicroPython 代码块:

encoder4_motor.get_vin_current_int_value()
get_vin_adc_raw8_value()

获取输入电压的 ADC 原始值(8 位)。

返回:

输入电压的 ADC 原始值。

返回类型:

int

UiFlow2 代码块:

get_vin_adc_raw8_value.png

MicroPython 代码块:

encoder4_motor.get_vin_adc_raw8_value()
get_vin_adc_raw12_value()

获取输入电压的 ADC 原始值(12 位)。

返回:

输入电压的 ADC 原始值。

返回类型:

int

UiFlow2 代码块:

get_vin_adc_raw12_value.png

MicroPython 代码块:

encoder4_motor.get_vin_adc_raw12_value()
get_vin_voltage()

获取输入电压值。

返回:

输入电压值。

返回类型:

float

UiFlow2 代码块:

get_vin_voltage.png

MicroPython 代码块:

encoder4_motor.get_vin_voltage()
get_device_spec(info)

获取设备规格。

参数:

info (int) – 要获取的信息。

返回:

设备规格(固件版本/I2C 地址)。

返回类型:

int

UiFlow2 代码块:

get_device_spec.png

MicroPython 代码块:

encoder4_motor.get_device_spec(0xFE)
get_soft_start_state(motor)

获取电机的软启动状态。

参数:

motor (int) – 电机的软启动状态。

返回:

软启动状态。

返回类型:

bool

UiFlow2 代码块:

get_soft_start_state.png

MicroPython 代码块:

encoder4_motor.get_soft_start_state(0)
set_soft_start_state(motor, state)

设置电机的软启动状态。

参数:
  • motor (int) – 要设置软启动状态的电机。

  • state (int) – 软启动状态。选项: - True: 1 - False: 0

UiFlow2 代码块:

set_soft_start_state.png

MicroPython 代码块:

encoder4_motor.set_soft_start_state(0, True)
set_i2c_address(addr)

设置设备的 I2C 地址。

参数:

addr (int) – 设置的 I2C 地址。

UiFlow2 代码块:

set_i2c_address.png

MicroPython 代码块:

encoder4_motor.set_i2c_address(0x24)