编码电机转动
方法
control_motor_running(port,mode,parameter,left_speed,right_speed)
port可取thunbot.PORT_L、thunbot.PORT_R、thunbot.PORT_L_R
如下表:
mode的取值 |
parameter |
left_speed取值范围 |
right_speed取值范围 |
DEFAULT_MODE |
默认模式,参数忽略 |
-100~100 |
-100~100 |
CONTROL_TIME_MODE |
运行的时间 |
-100~100 |
-100~100 |
CONTROL_ANGLE_MODE |
转动的角度 |
-100~100 |
-100~100 |
CONTROL_TURNS_MODE |
转动的圈数 |
-100~100 |
-100~100 |
motor_control_motor_turnning(mode,parameter,direction,speed)
如下表:
mode的取值 |
parameter |
direction取值范围 |
speed取值范围 |
DEFAULT_MODE |
默认模式,参数忽略 |
-100~100 |
-100~100 |
CONTROL_TIME_MODE |
运行的时间 |
-100~100 |
-100~100 |
CONTROL_ANGLE_MODE |
转动的角度 |
-100~100 |
-100~100 |
CONTROL_TURNS_MODE |
转动的圈数 |
-100~100 |
-100~100 |
注:此函数运行一个电机,另一个电机会根据direction值产生联动
brake(port)
port可取thunbot.PORT_L、thunbot.PORT_R
电机刹车。
free(port)
port可取thunbot.PORT_L、thunbot.PORT_R
电机惯性停止。
get_rotate_angle(port)
port可取thunbot.PORT_L、thunbot.PORT_R
获取电机旋转的角度。
clear_rotate_value(port)
port可取thunbot.PORT_L、thunbot.PORT_R
复位旋转角度,重新开始计算电机旋转角度。
get_speed(port,number)
port可取thunbot.PORT_L、thunbot.PORT_R
获取电机转速,number 设置取样次数,根据多次取样计算平均值,一般设置为 10 。
编程示范
from thunbot import motor
import thunbot
import time
for i in range(-100,100):
motor.control_motor_running(thunbot.PORT_L_R,motor.DEFAULT_MODE,0,i,-i)
print("motor_l speed:",motor.get_speed(thunbot.PORT_L,10),"angle:",motor.get_rotate_angle(thunbot.PORT_L))
print("motor_r speed:",motor.get_speed(thunbot.PORT_R,10),"angle:",motor.get_rotate_angle(thunbot.PORT_R))
time.sleep_ms(100)
print("motor_l brake")
print("motor_r free")
motor.brake(thunbot.PORT_L)
motor.free(thunbot.PORT_R)
time.sleep(5)
start_time = time.ticks_ms()
motor.control_motor_running(thunbot.PORT_L,motor.CONTROL_TIME_MODE,3,20,0)
print("motor_l run time(3000ms):",(time.ticks_ms() - start_time))
motor.control_motor_running(thunbot.PORT_R,motor.CONTROL_TIME_MODE,3,0,20)
print("motor_r run time(6000ms):",(time.ticks_ms() - start_time))
time.sleep(5)
start_angle = motor.get_rotate_angle(thunbot.PORT_L)
motor.control_motor_running(thunbot.PORT_L,motor.CONTROL_ANGLE_MODE,180,20,0)
print("motor_l run angle(180`):",abs(motor.get_rotate_angle(thunbot.PORT_L) - start_angle))
start_angle = motor.get_rotate_angle(thunbot.PORT_R)
motor.control_motor_running(thunbot.PORT_R,motor.CONTROL_ANGLE_MODE,180,0,20)
print("motor_r run angle(180`):",abs(motor.get_rotate_angle(thunbot.PORT_R) - start_angle))
time.sleep(5)
start_angle = motor.get_rotate_angle(thunbot.PORT_L)
motor.control_motor_running(thunbot.PORT_L,motor.CONTROL_TURNS_MODE,1,20,0)
print("motor_l run angle(360`):",abs(motor.get_rotate_angle(thunbot.PORT_L) - start_angle))
start_angle = motor.get_rotate_angle(thunbot.PORT_R)
motor.control_motor_running(thunbot.PORT_R,motor.CONTROL_TURNS_MODE,1,0,20)
print("motor_r run angle(360`):",abs(motor.get_rotate_angle(thunbot.PORT_R) - start_angle))
time.sleep(5)
motor.control_motor_turnning(motor.DEFAULT_MODE,0,0,30)
for i in range(0,100):
print("motor_l speed:",motor.get_speed(thunbot.PORT_L,10),"angle:",motor.get_rotate_angle(thunbot.PORT_L))
print("motor_r speed:",motor.get_speed(thunbot.PORT_R,10),"angle:",motor.get_rotate_angle(thunbot.PORT_R))
time.sleep_ms(100)
start_time = time.ticks_ms()
motor.control_motor_turnning(motor.CONTROL_TIME_MODE,10,0,-30)
print("motor_l run time(10000ms):",(time.ticks_ms() - start_time))
time.sleep_ms(2000)
start_angle = motor.get_rotate_angle(thunbot.PORT_L)
motor.control_motor_turnning(motor.CONTROL_ANGLE_MODE,180,0,-30)
print("motor_l run angle(180`):",abs(motor.get_rotate_angle(thunbot.PORT_L) - start_angle))
time.sleep_ms(3000)
start_angle = motor.get_rotate_angle(thunbot.PORT_L)
motor.control_motor_turnning(motor.CONTROL_TURNS_MODE,1,0,-30)
print("motor_l run angle(360`):",abs(motor.get_rotate_angle(thunbot.PORT_L) - start_angle))
time.sleep_ms(3000)
motor.control_motor_turnning(motor.DEFAULT_MODE,0,0,100)
time.sleep_ms(3000)
motor.free(thunbot.PORT_L)
motor.brake(thunbot.PORT_R)
time.sleep(5)
start_angle = motor.get_rotate_angle(thunbot.PORT_L)
motor.control_motor_turnning(motor.DEFAULT_MODE,0,100,-30)
while True:
print("motor_l speed:",motor.get_speed(thunbot.PORT_L,10),"postion:",motor.get_rotate_value(thunbot.PORT_L),"angle:",motor.get_rotate_angle(thunbot.PORT_L))
print("motor_r speed:",motor.get_speed(thunbot.PORT_R,10),"postion:",motor.get_rotate_value(thunbot.PORT_R),"angle:",motor.get_rotate_angle(thunbot.PORT_R))
time.sleep_ms(100)