方法

  • 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 。

编程示范

  1. from thunbot import motor
  2. import thunbot
  3. import time
  4. # control_motor_running 函数默认模式测试
  5. for i in range(-100,100):
  6. motor.control_motor_running(thunbot.PORT_L_R,motor.DEFAULT_MODE,0,i,-i)
  7. print("motor_l speed:",motor.get_speed(thunbot.PORT_L,10),"angle:",motor.get_rotate_angle(thunbot.PORT_L))
  8. print("motor_r speed:",motor.get_speed(thunbot.PORT_R,10),"angle:",motor.get_rotate_angle(thunbot.PORT_R))
  9. time.sleep_ms(100)
  10. # 刹车测试
  11. print("motor_l brake")
  12. print("motor_r free")
  13. motor.brake(thunbot.PORT_L)
  14. motor.free(thunbot.PORT_R)
  15. time.sleep(5)
  16. # control_motor_running 函数控制时间测试
  17. # motor_l运动3s motor_r运动6s
  18. start_time = time.ticks_ms()
  19. motor.control_motor_running(thunbot.PORT_L,motor.CONTROL_TIME_MODE,3,20,0)
  20. print("motor_l run time(3000ms):",(time.ticks_ms() - start_time)) # 3000ms
  21. motor.control_motor_running(thunbot.PORT_R,motor.CONTROL_TIME_MODE,3,0,20)
  22. print("motor_r run time(6000ms):",(time.ticks_ms() - start_time)) # 6000ms
  23. time.sleep(5)
  24. # control_motor_running 函数控制角度测试
  25. start_angle = motor.get_rotate_angle(thunbot.PORT_L)
  26. motor.control_motor_running(thunbot.PORT_L,motor.CONTROL_ANGLE_MODE,180,20,0)
  27. print("motor_l run angle(180`):",abs(motor.get_rotate_angle(thunbot.PORT_L) - start_angle))
  28. start_angle = motor.get_rotate_angle(thunbot.PORT_R)
  29. motor.control_motor_running(thunbot.PORT_R,motor.CONTROL_ANGLE_MODE,180,0,20)
  30. print("motor_r run angle(180`):",abs(motor.get_rotate_angle(thunbot.PORT_R) - start_angle))
  31. time.sleep(5)
  32. # control_motor_running 函数控制圈数测试
  33. start_angle = motor.get_rotate_angle(thunbot.PORT_L)
  34. motor.control_motor_running(thunbot.PORT_L,motor.CONTROL_TURNS_MODE,1,20,0)
  35. print("motor_l run angle(360`):",abs(motor.get_rotate_angle(thunbot.PORT_L) - start_angle))
  36. start_angle = motor.get_rotate_angle(thunbot.PORT_R)
  37. motor.control_motor_running(thunbot.PORT_R,motor.CONTROL_TURNS_MODE,1,0,20)
  38. print("motor_r run angle(360`):",abs(motor.get_rotate_angle(thunbot.PORT_R) - start_angle))
  39. time.sleep(5)
  40. # motor_control_motor_turnning 默认模式测试
  41. motor.control_motor_turnning(motor.DEFAULT_MODE,0,0,30)
  42. for i in range(0,100):
  43. print("motor_l speed:",motor.get_speed(thunbot.PORT_L,10),"angle:",motor.get_rotate_angle(thunbot.PORT_L))
  44. print("motor_r speed:",motor.get_speed(thunbot.PORT_R,10),"angle:",motor.get_rotate_angle(thunbot.PORT_R))
  45. time.sleep_ms(100)
  46. # motor_control_motor_turnning 控制时间测试
  47. start_time = time.ticks_ms()
  48. motor.control_motor_turnning(motor.CONTROL_TIME_MODE,10,0,-30)
  49. print("motor_l run time(10000ms):",(time.ticks_ms() - start_time))
  50. time.sleep_ms(2000)
  51. # motor_control_motor_turnning 控制角度测试
  52. start_angle = motor.get_rotate_angle(thunbot.PORT_L)
  53. motor.control_motor_turnning(motor.CONTROL_ANGLE_MODE,180,0,-30)
  54. print("motor_l run angle(180`):",abs(motor.get_rotate_angle(thunbot.PORT_L) - start_angle))
  55. time.sleep_ms(3000)
  56. # motor_control_motor_turnning 控制圈数测试
  57. start_angle = motor.get_rotate_angle(thunbot.PORT_L)
  58. motor.control_motor_turnning(motor.CONTROL_TURNS_MODE,1,0,-30)
  59. print("motor_l run angle(360`):",abs(motor.get_rotate_angle(thunbot.PORT_L) - start_angle))
  60. time.sleep_ms(3000)
  61. motor.control_motor_turnning(motor.DEFAULT_MODE,0,0,100)
  62. time.sleep_ms(3000)
  63. motor.free(thunbot.PORT_L)
  64. motor.brake(thunbot.PORT_R)
  65. time.sleep(5)
  66. # motor_control_motor_turnning 控制角度测试
  67. start_angle = motor.get_rotate_angle(thunbot.PORT_L)
  68. motor.control_motor_turnning(motor.DEFAULT_MODE,0,100,-30)
  69. while True:
  70. 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))
  71. 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))
  72. time.sleep_ms(100)