pdf-icon

UIFlow Guide

UIFlow 1.0 Blockly

Event

Unit

Atomic Base

UIFlow 1.0 Project

Module 4Encoder Motor

案例程序

4个编码点击分别设置不同的pwm值,控制不同转速和方向,以及打印输出输入电压值
from m5stack import *
from m5stack_ui import *
from uiflow import *
import module

screen = M5Screen()
screen.clean_screen()
screen.set_screen_bg_color(0xFFFFFF)

encoder4_motor = module.get(module.ENCODER4MOTOR)

encoder4_motor.init_i2c_address(0x24)
encoder4_motor.set_all_motors_mode(0x00)
while True:
  encoder4_motor.set_motor_pwm_dutycycle(0x00, (-127))
  encoder4_motor.set_motor_pwm_dutycycle(0x01, 50)
  encoder4_motor.set_motor_pwm_dutycycle(0x02, 95)
  encoder4_motor.set_motor_pwm_dutycycle(0x03, 127)
  print((str('Vin value:') + str((encoder4_motor.get_vin_current_int_value()))))
  wait_ms(2)

功能说明

encoder4_motor.get_device_spec(0xFE)
  • 获取设备的固件版本信息
encoder4_motor.get_encoder_mode()
  • 获取编码器模式的方向信息
encoder4_motor.get_motor_encoder_value(0x00)
  • 获取指定电机的编码器值
encoder4_motor.get_position_PID_value(0x00)
  • 获取指定电机的位置控制PID值
encoder4_motor.get_speed_PID_value(0x00)
  • 获取指定电机的速度控制PID值
encoder4_motor.get_motor_speed_value(0x00)
  • 获取指定电机的当前速度值
encoder4_motor.get_vin_adc_raw12_value()
  • 获取输入电压的12位ADC(模数转换)原始值
encoder4_motor.get_vin_adc_raw8_value()
  • 获取输入电压的8位ADC(模数转换)原始值
encoder4_motor.get_vin_current_float_value()
  • 获取输入电压的电流值(单位为安培)
encoder4_motor.get_vin_current_int_value()
  • 获取输入电压的电流值(单位为毫安)
encoder4_motor.get_vin_voltage()
  • 获取输入电压的电压值,返回一个整数值
encoder4_motor.init_i2c_address(0x24)
  • 初始化设备的I2C地址,范围是0x01到0x7F
encoder4_motor.set_encoder_mode(0x00)
  • 设置编码器模式的方向,图中选择的是 "AB" 或 “BA” 模式
encoder4_motor.set_i2c_address(0x24)
  • 设置设备的I2C地址
encoder4_motor.set_motor_mode(0x00, 0x00)
  • 设置电机1的运行模式。在下拉菜单中,有三个选项:
    • NORMAL: 正常模式,电机按默认设置运行。
    • POSITION: 位置控制模式,通过指定的位置控制电机的运动。
    • SPEED: 速度控制模式,通过指定的速度控制电机的转动。
encoder4_motor.set_all_motors_mode(0x00)
  • 设置所有电机的模式
    • NORMAL: 正常模式,电机按默认设置运行。
    • POSITION: 位置控制模式,通过指定的位置控制电机的运动。
    • SPEED: 速度控制模式,通过指定的速度控制电机的转动。
encoder4_motor.set_motor_encoder_value(0x00, 1000)
  • 设置电机的编码器值,这个值通常表示电机的当前位置或累计转动量。
encoder4_motor.set_position_encoder_value(0x00, 1000)
  • 设置电机的位置编码器值。这个值用于指定电机的目标位置或位置控制中的当前位置。
encoder4_motor.set_position_max_speed_value(0x00, 100)
  • 设置电机在位置控制模式下的最大速度值,范围是-127到127
encoder4_motor.set_position_PID_value(0x00, 3, 1, 15)
  • 设置电机的位置控制PID参数,P(比例)、I(积分)、D(微分),这些参数用于调整电机的响应特性。
encoder4_motor.set_motor_pwm_dutycycle(0x00, 50)
  • 设置电机1的PWM占空比,范围是-127到127。占空比控制电机的转速和方向
encoder4_motor.set_speed_PID_value(0x00, 10, 1, 15)
  • 设置电机的速度控制PID参数,P(比例)、I(积分)、D(微分)。用于速度控制时的调整
encoder4_motor.set_speed_point_value(0x00, 50)
  • 设置电机1的速度设定值,范围是-127到127。这个值表示目标速度
On This Page