from ipywidgets import interact, interactive, HBox, VBox
import ipywidgets as widgets
text = {'Motor': 'success', 'Forward': 'info', 'Reverse': 'warning'}
buttons, colors = list(text.keys()), list(text.values())
toggle = [
widgets.ToggleButton(description=f'{buttons[i]}',
button_style=f'{colors[i]}')
for i in range(3)]
mode = widgets.Dropdown(options=['Speed', 'Current'])
def clicked(toggle_0=toggle[0], mode=mode, toggle_1=toggle[1],
toggle_2=toggle[2], RPM=None, Torque=None):
if toggle_0:
if mode == 'Speed':
motor.set_mode('rpm_mode')
motor.set_rpm(RPM)
elif mode == 'Current':
motor.set_mode('torque_mode')
motor.set_torque(Torque)
else:
motor.stop()
w = interactive(clicked,
RPM=widgets.IntSlider(min=-5000, max=5000, step=1, value=1000),
Torque=widgets.IntSlider(min=-400, max=400, step=1, value=0))
VBox([HBox(w.children[:2]), w.children[2], w.children[3], w.children[4],
w.children[5]])
读取状态寄存器的相关信息
motor_status = [(motor._read_controlreg(i + ANGLE.offset)) for i in
range(0, 16, 4)]
high_sp, low_sp = bytesplit(motor_status[1])
high_id, low_id = bytesplit(motor_status[2])
high_iq, low_iq = bytesplit(motor_status[3])
print(f'Angle in degrees : {motor_status[0] * 0.36}')
print(f'Angle in steps per thousand: {(motor_status[0])}')
print(f'Id : {np.int16(low_id) * 0.00039} Amp')
print(f'Iq : {np.int16(low_iq) * 0.00039} Amp')
print(f'Speed in RPM : {-(np.int16(low_sp))}')
通过一个循环测试电机的运转
import time
motor.set_mode('rpm_mode')
for i in range(2):
motor.set_rpm(1000)
time.sleep(1)
motor.set_rpm(0)
time.sleep(2)
motor.set_rpm(-50)
time.sleep(2)
motor.set_rpm(0)
time.sleep(2)
motor.stop()