Hbridge Unit
这个库是 Unit HBridge 的驱动。只有 v1.1 版具备电流测量功能。
支持以下产品:
UiFlow2 应用示例
电机转速和旋转方向控制。
在 UiFlow2 上打开 cores3_hbridge_motor_control.m5f2 项目。
示例程序实现电机转速控制及正反转方向的切换功能。
UiFlow2 代码块:
示例输出:
无
MicroPython 应用示例
电机转速和旋转方向控制。
示例程序实现电机转速控制及正反转方向的切换功能。
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 HbridgeUnit 11import time 12 13 14title0 = None 15label0 = None 16label_speed = None 17i2c0 = None 18hbridge_0 = None 19speed = None 20dir2 = None 21 22 23def setup(): 24 global title0, label0, label_speed, i2c0, hbridge_0, speed, dir2 25 M5.begin() 26 Widgets.fillScreen(0x222222) 27 title0 = Widgets.Title("HBridge Motor Control", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24) 28 label0 = Widgets.Label("Speed:", 35, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 29 label_speed = Widgets.Label("0", 110, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 30 i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000) 31 hbridge_0 = HbridgeUnit(i2c0, 0x20) 32 hbridge_0.set_pwm_freq(1000) 33 speed = 0 34 hbridge_0.set_direction(0) 35 dir2 = True 36 37 38def loop(): 39 global title0, label0, label_speed, i2c0, hbridge_0, speed, dir2 40 M5.update() 41 speed = speed + 1 42 label0.setText(str(speed)) 43 if speed > 99: 44 speed = 0 45 dir2 = not dir2 46 if dir2: 47 hbridge_0.set_direction(1) 48 else: 49 hbridge_0.set_direction(2) 50 time.sleep_ms(1000) 51 hbridge_0.set_percentage_pwm(speed, 8) 52 time.sleep_ms(50) 53 54 55if __name__ == "__main__": 56 try: 57 setup() 58 while True: 59 loop() 60 except (Exception, KeyboardInterrupt) as e: 61 try: 62 from utility import print_error_msg 63 64 print_error_msg(e) 65 except ImportError: 66 print("please update to latest firmware")
示例输出:
无
API应用
HbridgeUnit
- class unit.hbridge.HbridgeUnit(i2c, address=32)
基类:
object
创建一个 HbridgeUnit 对象。
- 参数:
i2c (machine.I2C | PAHUBUnit) – I2C 端口。
UiFlow2 代码块:
MicroPython 代码块:
from unit import HbridgeUnit unit_hbridge_0 = HbridgeUnit(i2c0, 0x20)
- get_adc_value(raw=0, res=8)
获取 ADC 数值。
此方法根据指定的分辨率获取 ADC 值。支持 8 位和 16 位的 ADC 分辨率。如果将 raw 设置为 1,则返回原始 ADC 值;否则返回计算后的电压值。
- 参数:
- 返回:
根据 raw 参数,返回原始 ADC 值或计算后的电压。
- 返回类型:
UiFlow2 代码块:
MicroPython 代码块:
unit_hbridge_0.get_adc_value(raw, res)
- get_device_status(mode)
获取设备状态。
获取固件版本和 I2C 地址。
UiFlow2 代码块:
MicroPython 代码块:
unit_hbridge_0.get_device_status(mode)
- get_driver_config(reg=0)
获取驱动配置。
UiFlow2 代码块:
MicroPython 代码块:
unit_hbridge_0.get_driver_config(reg)
- get_vin_current()
获取输入电流(单位:A)。
- 返回:
输入电流。
- 返回类型:
UiFlow2 代码块:
MicroPython 代码块:
unit_hbridge_0.get_vin_current()
- set_16bit_pwm(duty=0)
设置 PWM 占空比,范围为 16 位(0~65535)
- 参数:
duty (int) – PWM 占空比,范围:0~65535。
- 返回类型:
None
UiFlow2 代码块:
MicroPython 代码块:
unit_hbridge_0.set_16bit_pwm(duty)
- set_8bit_pwm(duty=0)
设置 PWM 占空比,范围为 8 位(0~255)
- 参数:
duty (int) – PWM 占空比,范围:0~255。
- 返回类型:
None
UiFlow2 代码块:
MicroPython 代码块:
unit_hbridge_0.set_8bit_pwm(duty)
- set_direction(dir=0)
设置方向。
该方法用于控制电机的运动方向或使其停止。
- 参数:
dir (int) – 方向控制参数: - 0:停止 - 1:正转 - 2:反转。
- 返回类型:
None
UiFlow2 代码块:
MicroPython 代码块:
unit_hbridge_0.set_direction(dir)
- set_percentage_pwm(duty=0, res=8)
根据百分比设置 PWM 输出。
UiFlow2 代码块:
MicroPython 代码块:
unit_hbridge_0.set_percentage_pwm(duty, reg)