8Servos Unit

这是 8Servos Unit 的驱动库。它是一款 8 通道舵机控制器,最多可控制 8 个舵机。可用于控制舵机角度、设置脉宽,以及设置舵机的模式。

支持以下产品:

8Servos Unit

UiFlow2 应用示例

伺服电机控制

在 UiFlow2 中打开 cores3_servo_example.m5f2 项目。

该示例控制 8Servos Unit 的舵机角度。

UiFlow2 代码块:

example_servo.png

示例输出:

None

rgb 控制

在 UiFlow2 中打开 cores3_rgb_example.m5f2 项目。

该示例控制 8Servos Unit 的 RGB LED。

UiFlow2 代码块:

example_rgb.png

示例输出:

None

MicroPython 应用示例

伺服电机控制

该示例控制 8Servos Unit 的舵机角度。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import I2C
 9from hardware import Pin
10from unit import Servos8Unit
11import time
12
13
14label0 = None
15i2c0 = None
16servos8_0 = None
17
18
19def setup():
20    global label0, i2c0, servos8_0
21
22    M5.begin()
23    Widgets.fillScreen(0x222222)
24    label0 = Widgets.Label(
25        "8Servos Unit", 92, 109, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
26    )
27
28    i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
29    servos8_0 = Servos8Unit(i2c0, 0x25)
30    servos8_0.set_mode(3, 3)
31    servos8_0.set_mode(3, 7)
32
33
34def loop():
35    global label0, i2c0, servos8_0
36    M5.update()
37    servos8_0.set_servo_angle(45, 3)
38    servos8_0.set_servo_angle(45, 7)
39    time.sleep(1)
40    servos8_0.set_servo_angle(150, 3)
41    servos8_0.set_servo_angle(150, 7)
42    time.sleep(1)
43
44
45if __name__ == "__main__":
46    try:
47        setup()
48        while True:
49            loop()
50    except (Exception, KeyboardInterrupt) as e:
51        try:
52            from utility import print_error_msg
53
54            print_error_msg(e)
55        except ImportError:
56            print("please update to latest firmware")

示例输出:

None

rgb 控制

该示例控制 8Servos Unit 的 RGB LED。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import I2C
 9from hardware import Pin
10from unit import Servos8Unit
11import time
12
13
14label0 = None
15i2c0 = None
16servos8_0 = None
17
18
19def setup():
20    global label0, i2c0, servos8_0
21
22    M5.begin()
23    Widgets.fillScreen(0x222222)
24    label0 = Widgets.Label(
25        "8Servos Unit", 92, 109, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
26    )
27
28    i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
29    servos8_0 = Servos8Unit(i2c0, 0x25)
30    servos8_0.set_mode(4, 0)
31
32
33def loop():
34    global label0, i2c0, servos8_0
35    M5.update()
36    servos8_0.set_rgb_led(0xFF0000, 0)
37    time.sleep(1)
38    servos8_0.set_rgb_led(0x33FF33, 0)
39    time.sleep(1)
40    servos8_0.set_rgb_led(0x3366FF, 0)
41    time.sleep(1)
42
43
44if __name__ == "__main__":
45    try:
46        setup()
47        while True:
48            loop()
49    except (Exception, KeyboardInterrupt) as e:
50        try:
51            from utility import print_error_msg
52
53            print_error_msg(e)
54        except ImportError:
55            print("please update to latest firmware")

示例输出:

None

API参考

Servos8Unit

class unit.servos8.Servos8Unit(i2c, address=37)

基类:object

创建一个 servos 8 unit 对象。

参数:
  • i2c (machine.I2C | PAHUBUnit) – servos 8 unit 所连接的 I2C 总线。

  • address (int) – 设备的 I2C 地址。默认为 0x25。

抛出:

UnitError – 如果未连接 servos 8 unit。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from hardware import I2C
from unit import Servos8Unit

i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
servos8_0 = Servos8Unit(i2c0, 0x25)
get_mode(channel)

获取指定通道的当前模式。

参数:

channel (int) – 要获取模式的通道编号(0 到 7)。

返回:

指定通道的模式。

返回类型:

int

UiFlow2 代码块:

get_mode.png

MicroPython 代码块:

servos8_0.get_mode(0)
set_mode(mode, channel)

设置指定通道的模式。

参数:
  • mode (int) – 要为该通道设置的模式。

  • channel (int) – 要设置模式的通道编号(0 到 7)。

返回类型:

None

UiFlow2 代码块:

set_mode.png

MicroPython 代码块:

servos8_0.set_mode(0, 0)
get_digital_input(channel)

获取指定通道的数字输入值。

参数:

channel (int) – 要获取数字输入的通道号(0 到 7)。

返回:

指定通道的数字输入值(True 或 False)。

返回类型:

bool

UiFlow2 代码块:

get_digital_input.png

MicroPython 代码块:

servos8_0.get_digital_input(0)
set_output_value(value, channel)

设置指定通道的数字输出值。

参数:
  • value (int) – 为该通道设置的数字输出值(0 或 1)。

  • channel (int) – 用于设置数字输出的通道编号(0 到 7)。

返回类型:

None

UiFlow2 代码块:

set_output_value.png

MicroPython 代码块:

servos8_0.set_output_value(1, 0)
get_8bit_adc_raw(channel)

获取指定通道的 8 位 ADC 值。

参数:

channel (int) – 要获取其 8 位 ADC 值的通道编号(0 到 7)。

返回:

指定通道的 8 位 ADC 值(0 到 255)。

返回类型:

int

UiFlow2 代码块:

get_8bit_adc_raw.png

MicroPython 代码块:

servos8_0.get_8bit_adc_raw(0)
get_12bit_adc_raw(channel)

获取指定通道的 12-bit ADC 值。

参数:

channel (int) – 要获取 12-bit ADC 值的通道号(0 到 7)。

返回:

指定通道的 12 位 ADC 值(0 到 4095)。

返回类型:

int

UiFlow2 代码块:

get_12bit_adc_raw.png

MicroPython 代码块:

servos8_0.get_12bit_adc_raw(0)
set_servo_angle(angle, channel)

设置指定通道的舵机角度。

参数:
  • angle (int) – 要为该通道设置的舵机角度(0 到 180)。

  • channel (int) – 要设置舵机角度的通道号(0 到 7)。

返回类型:

None

UiFlow2 代码块:

set_servo_angle.png

MicroPython 代码块:

servos8_0.set_servo_angle(90, 0)
get_servo_angle(channel)

获取指定通道的舵机角度。

参数:

channel (int) – 要获取舵机角度的通道号(0 到 7)。

返回:

指定通道的舵机角度(0 到 180)。

返回类型:

int

UiFlow2 代码块:

get_servo_angle.png

MicroPython 代码块:

servos8_0.get_servo_angle(0)
set_servo_pulse(pulse, channel)

设置指定通道的舵机脉冲。

参数:
  • pulse (int) – 用于设置通道的舵机脉冲(500 到 2500)。

  • channel (int) – 要设置舵机脉冲的通道号(0 到 7)。

返回类型:

None

UiFlow2 代码块:

set_servo_pulse.png

MicroPython 代码块:

servos8_0.set_servo_pulse(1500, 0)
get_servo_pulse(channel)

获取指定通道的舵机脉冲。

参数:

channel (int) – 要获取舵机脉冲的通道编号(0 到 7)。

返回:

指定通道的舵机脉宽(500 到 2500)。

返回类型:

int

UiFlow2 代码块:

get_servo_pulse.png

MicroPython 代码块:

servos8_0.get_servo_pulse(0)
set_rgb_led(rgb, channel)

设置指定通道的 RGB LED 颜色。

参数:
  • rgb (int) – 为该通道设置的 RGB 颜色值(0x000000 到 0xffffff)。

  • channel (int) – 用于设置 RGB LED 颜色的通道编号(0 到 7)。

返回类型:

None

UiFlow2 代码块:

set_rgb_led.png

MicroPython 代码块:

servos8_0.set_rgb_led(0xFF0000, 0)
get_rgb_led(channel)

获取指定通道的 RGB LED 颜色。

参数:

channel (int) – 要获取 RGB LED 颜色的通道编号(0 到 7)。

返回:

指定通道的 RGB 颜色值(0x000000 至 0xffffff)。

返回类型:

int

UiFlow2 代码块:

get_rgb_led.png

MicroPython 代码块:

servos8_0.get_rgb_led(0)
set_pwm_dutycycle(duty, channel)

设置指定通道的 PWM 占空比。

参数:
  • duty (int) – 为该通道设置的 PWM 占空比(0 到 100)。

  • channel (int) – 要设置 PWM 占空比的通道编号(0 到 7)。

返回类型:

None

UiFlow2 代码块:

set_pwm_dutycycle.png

MicroPython 代码块:

servos8_0.set_pwm_dutycycle(50, 0)
get_input_current()

获取 servos 8 unit 的输入电流。

返回:

输入电流,单位为安培(A)。

返回类型:

float

UiFlow2 代码块:

get_input_current.png

MicroPython 代码块:

servos8_0.get_input_current()
get_device_spec(mode)

获取设备规格。:param int mode: 用于获取规格的模式。:return: 设备规格值。:rtype: int

UiFlow2 代码块:

get_device_spec.png

MicroPython 代码块:

servos8_0.get_device_spec(0xFE)
set_i2c_address(addr)

设置 servos 8 unit 的 I2C 地址。

参数:

addr (int) – 为 servos 8 unit 设置的新 I2C 地址(1 到 127)。

UiFlow2 代码块:

set_i2c_address.png

MicroPython 代码块:

servos8_0.set_i2c_address(0x25)