4EncoderMotor Module

4EncoderMotor Module is a 4-channel encoder motor driver module that utilizes the STM32+BL5617 solution. It is suitable for various applications such as robot motion control, automation equipment, smart vehicles, laboratory equipment, and industrial automation systems.

This is the driver library for the 4EncoderMotor Module, use to control motor and read encoder value.

Support the following products:

4EncoderMotor

4EncoderMotor-V11

UiFlow2 Example

Motor control

Open the encoder4motor_core2_example.m5f2 project in UiFlow2.

This example shows how to control the motor and read the encoder value.

UiFlow2 Code Block:

example.png

Example output:

None

MicroPython Example

Motor control

This example shows how to control the motor and read the encoder value.

MicroPython Code Block:

 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")

Example output:

None

API

Encoder4MotorModule

class module.encoder4_motor.Encoder4MotorModule(address=36)

Bases: object

Create an Encoder4MotorModule object

Parameters:

address (int) – The I2C address of the device. Default is 0x24.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from module import Encoder4MotorModule

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

Set the motor mode.

Parameters:
  • motor (int) – The motor to set the mode.

  • mode (int) –

    The mode of the motor.

    Options:
    • NORMAL_MODE: 0

    • POSITION_MODE: 1

    • SPEED_MODE: 2

UiFlow2 Code Block:

set_motor_mode.png

MicroPython Code Block:

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

Set the mode of all motors.

Parameters:

mode (int) –

The mode of the motors.

Options:
  • NORMAL_MODE: 0

  • POSITION_MODE: 1

  • SPEED_MODE: 2

UiFlow2 Code Block:

set_all_motors_mode.png

MicroPython Code Block:

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

Set the PWM duty cycle of a motor.

Parameters:
  • motor (int) – The motor to set the PWM duty cycle.

  • duty (int) – The PWM duty cycle.

UiFlow2 Code Block:

set_motor_pwm_dutycycle.png

MicroPython Code Block:

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

Get the encoder value of a motor.

Parameters:

pos (int) – The motor to get the encoder value.

Returns:

The encoder value.

Return type:

int

UiFlow2 Code Block:

get_motor_encoder_value.png

MicroPython Code Block:

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

Set the encoder value of a motor.

Parameters:
  • pos (int) – The motor to set the encoder value.

  • value (int) – The encoder value.

UiFlow2 Code Block:

set_motor_encoder_value.png

MicroPython Code Block:

encoder4_motor.set_motor_encoder_value(0, 100)
get_encoder_mode()

Get the encoder mode.

Returns:

The encoder mode.

Return type:

int

UiFlow2 Code Block:

get_encoder_mode.png

MicroPython Code Block:

encoder4_motor.get_encoder_mode()
set_encoder_mode(mode)

Set the encoder mode.

Parameters:

mode (int) –

The mode of the encoder.

Options:
  • AB: 0

  • BA: 1

UiFlow2 Code Block:

set_encoder_mode.png

MicroPython Code Block:

encoder4_motor.set_encoder_mode(0x00)
get_motor_speed_value(pos)

Get the speed value of a motor.

Parameters:

pos (int) – The motor to get the speed value.

Returns:

The speed value.

Return type:

int

UiFlow2 Code Block:

get_motor_speed_value.png

MicroPython Code Block:

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

Set the position encoder value of a motor.

Parameters:
  • pos (int) – The motor to set the position encoder value.

  • value (int) – The position encoder value.

UiFlow2 Code Block:

set_position_encoder_value.png

MicroPython Code Block:

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

Set the maximum speed value of a motor.

Parameters:
  • pos (int) – The motor to set the maximum speed value.

  • value (int) – The maximum speed value.

UiFlow2 Code Block:

set_position_max_speed_value.png

MicroPython Code Block:

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

Get the position PID value of a motor.

Parameters:

pos (int) – The motor to get the position P,I,D value.

Returns:

The position PID value.

Return type:

list[int, int, int]

UiFlow2 Code Block:

get_position_PID_value.png

MicroPython Code Block:

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

Set the position P,I,D value of a motor.

Parameters:
  • pos (int) – The motor to set the position P,I,D value.

  • p (int) – The P value.

  • i (int) – The I value.

  • d (int) – The D value.

UiFlow2 Code Block:

set_position_PID_value.png

MicroPython Code Block:

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

Get the speed PID value of a motor.

Parameters:

pos (int) – The motor to get the speed P,I,D value.

Returns:

The speed P,I,D value.

Return type:

list[int, int, int]

UiFlow2 Code Block:

get_speed_PID_value.png

MicroPython Code Block:

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

Set the speed PID value of a motor.

Parameters:
  • pos (int) – The motor to set the speed PID value.

  • p (int) – The P value.

  • i (int) – The I value.

  • d (int) – The D value.

UiFlow2 Code Block:

set_speed_PID_value.png

MicroPython Code Block:

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

Set the speed point value of a motor.

Parameters:
  • pos (int) – The motor to set the speed point value.

  • point (int) – The speed point value.

UiFlow2 Code Block:

set_speed_point_value.png

MicroPython Code Block:

encoder4_motor.set_speed_point_value(0, 127)
get_vin_current_float_value()

Get the input current value in float.

Returns:

The input current value.

Return type:

float

UiFlow2 Code Block:

get_vin_current_float_value.png

MicroPython Code Block:

encoder4_motor.get_vin_current_float_value()
get_vin_current_int_value()

Get the input current value in int.

Returns:

The input current value.

Return type:

int

UiFlow2 Code Block:

get_vin_current_int_value.png

MicroPython Code Block:

encoder4_motor.get_vin_current_int_value()
get_vin_adc_raw8_value()

Get the input voltage ADC raw value in 8-bit.

Returns:

The input voltage ADC raw value.

Return type:

int

UiFlow2 Code Block:

get_vin_adc_raw8_value.png

MicroPython Code Block:

encoder4_motor.get_vin_adc_raw8_value()
get_vin_adc_raw12_value()

Get the input voltage ADC raw value in 12-bit.

Returns:

The input voltage ADC raw value.

Return type:

int

UiFlow2 Code Block:

get_vin_adc_raw12_value.png

MicroPython Code Block:

encoder4_motor.get_vin_adc_raw12_value()
get_vin_voltage()

Get the input voltage value.

Returns:

The input voltage value.

Return type:

float

UiFlow2 Code Block:

get_vin_voltage.png

MicroPython Code Block:

encoder4_motor.get_vin_voltage()
get_device_spec(info)

Get the device specification.

Parameters:

info (int) – The information to get.

Returns:

The device specification(firmware version/I2C address).

Return type:

int

UiFlow2 Code Block:

get_device_spec.png

MicroPython Code Block:

encoder4_motor.get_device_spec(0xFE)
get_soft_start_state(motor)

Get the soft start state of a motor.

Parameters:

motor (int) – The motor to get the soft start state.

Returns:

The soft start state.

Return type:

bool

UiFlow2 Code Block:

get_soft_start_state.png

MicroPython Code Block:

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

Set the soft start state of a motor.

Parameters:
  • motor (int) – The motor to set the soft start state.

  • state (int) –

    The soft start state.

    Options:
    • True: 1

    • False: 0

UiFlow2 Code Block:

set_soft_start_state.png

MicroPython Code Block:

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

Set the I2C address of the device.

Parameters:

addr (int) – The I2C address to set.

UiFlow2 Code Block:

set_i2c_address.png

MicroPython Code Block:

encoder4_motor.set_i2c_address(0x24)