大家好,又见面了,我是你们的朋友全栈君。
ODrive 官方入门指南中,采用的是 USB 连接控制模式(中文翻译版本链接)使用的是 Native Protocol。当我们需要尝试串口通信实现时,需要专程 ASCII 协议来进行串口通信实现相关的命令,结合着入门指南以及 ODrive 中的相关属性方法参数,将流程中用到的相关方法整理如下:
import serial
import time
ted = serial.Serial(port='/dev/tty.wchusbserial1470', baudrate=115200)
ted.write("r vbus_voltage;ted's words\n".encode('ascii'))
vbus_voltage = float(ted.readline().decode('ascii'))
print("vbus_voltage:",vbus_voltage,type(vbus_voltage))
# Current limit
ted.write("r axis0.motor.config.current_lim\n".encode('ascii'))
axis0_motor_config_current_lim = float(ted.readline().decode('ascii'))
print("axis0.motor.config.current_lim:",axis0_motor_config_current_lim,type(axis0_motor_config_current_lim))
# Velocity limit
ted.write("r axis0.controller.config.vel_limit\n".encode('ascii'))
axis0_controller_config_vel_limit = float(ted.readline().decode('ascii'))
print("axis0.controller.config.vel_limit:",axis0_controller_config_vel_limit,type(axis0_controller_config_vel_limit))
# Calibration current
ted.write("r axis0.motor.config.calibration_current\n".encode('ascii'))
axis0_motor_config_calibration_current = float(ted.readline().decode('ascii'))
print("axis0.motor.config.calibration_current:",axis0_motor_config_calibration_current,type(axis0_motor_config_calibration_current))
# brake_resistance
ted.write("r config.brake_resistance\n".encode('ascii'))
config_brake_resistance = float(ted.readline().decode('ascii'))
print("config.brake_resistance:",config_brake_resistance,type(config_brake_resistance))
# pole_pairs
ted.write("r axis0.motor.config.pole_pairs\n".encode('ascii'))
axis0_motor_config_pole_pairs = int(ted.readline().decode('ascii'))
print("axis0.motor.config.pole_pairs:",axis0_motor_config_pole_pairs,type(axis0_motor_config_pole_pairs))
# motor_type
ted.write("r axis0.motor.config.motor_type\n".encode('ascii'))
axis0_motor_config_motor_type = int(ted.readline().decode('ascii'))
print("axis0.motor.config.motor_type:",axis0_motor_config_motor_type,type(axis0_motor_config_motor_type))
print("~"*20)
# cpr
ted.write("r axis0.encoder.config.cpr\n".encode('ascii'))
axis0_encoder_config_cpr = int(ted.readline().decode('ascii'))
print("axis0.encoder.config.cpr:",axis0_encoder_config_cpr,type(axis0_encoder_config_cpr))
ted.write("w axis0.encoder.config.cpr 4096\n".encode('ascii'))
ted.write("r axis0.encoder.config.cpr\n".encode('ascii'))
axis0_encoder_config_cpr2 = int(ted.readline().decode('ascii'))
print("axis0.encoder.config.cpr modified:",axis0_encoder_config_cpr2,type(axis0_encoder_config_cpr2))
print("~"*20)
# Save configuration
ted.write("ss\n".encode('ascii'))
# Position control
""" AXIS_STATE_UNDEFINED = 0 AXIS_STATE_IDLE = 1 AXIS_STATE_STARTUP_SEQUENCE = 2 AXIS_STATE_FULL_CALIBRATION_SEQUENCE = 3 AXIS_STATE_MOTOR_CALIBRATION = 4 AXIS_STATE_SENSORLESS_CONTROL = 5 AXIS_STATE_ENCODER_INDEX_SEARCH = 6 AXIS_STATE_ENCODER_OFFSET_CALIBRATION = 7 AXIS_STATE_CLOSED_LOOP_CONTROL = 8 AXIS_STATE_LOCKIN_SPIN = 9 AXIS_STATE_ENCODER_DIR_FIND = 10 """
ted.write("r axis0.current_state\n".encode('ascii'))
axis0_current_state = int(ted.readline().decode('ascii'))
print("axis0.current_state:",axis0_current_state,type(axis0_current_state))
# AXIS_STATE_FULL_CALIBRATION_SEQUENCE
ted.write("w axis0.requested_state 3\n".encode('ascii'))
print("校准电机...")
time.sleep(15)
ted.write("r axis0.current_state\n".encode('ascii'))
axis0_current_state2 = int(ted.readline().decode('ascii'))
print("axis0.current_state modified:",axis0_current_state2,type(axis0_current_state2))
# AXIS_STATE_CLOSED_LOOP_CONTROL
ted.write("w axis0.requested_state 8\n".encode('ascii'))
ted.write("r axis0.current_state\n".encode('ascii'))
axis0_current_state3 = int(ted.readline().decode('ascii'))
print("axis0.current_state modified again:",axis0_current_state3,type(axis0_current_state3))
ted.write("w axis0.controller.pos_setpoint 10000\n".encode('ascii'))
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/127520.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...