RobotController上位机控制器

简介

当使用外部独立主控时,可通过串口与机器人控制器进行通信。 RobotController类提供了一系列方法,通过串口指令调用机器人的运动控制、巡线控制等功能。

使用时,在下位机的main.py中运行 asyncio.run(communication_loop()) 以启动通信循环。

上位机中,创建RobotController对象,即可调用其方法控制机器人。

class RobotController(port)
__init__(port)

初始化机器人控制器

参数:

port (uart) -- 串口对象,例如UART(2, baudrate=115200, tx=22, rx=21, timeout=10)

示例

>>> from machine import UART
>>> uart = UART(2, baudrate=115200, tx=22, rx=21, timeout=10)
>>> rc = RobotController(uart)
move(vx, vy, w)

设置底盘运动速度

车头方向为x正方向,右侧为y正方向,z轴角速度顺时针为正

X
^
+------------> Y
参数:
  • vx (float) -- X方向平移速度

  • vy (float) -- Y方向平移速度

  • w (float) -- Z轴旋转角速度

move_straight(distance, vel=30)

直行指定距离

参数:
  • distance (float) -- 直行距离,前进为正后退为负,单位cm.

  • vel (float) -- 直行速度. 默认值为30,建议值20-60.

move_side(distance, vel=30)

平移指定距离

参数:
  • distance (float) -- 平移距离,向右为正向左为负,单位cm.

  • vel (float) -- 平移速度. 默认值为30,建议值20-60.

turn(angle, vel=80)

转弯指定角度

参数:
  • angle (float) -- 转弯角度,单位度. 向右为正,向左为负.

  • vel (float) -- 转弯速度. 默认值为80,建议值20-120.

track_line(speed=80, mode='cross', over=500)

自动巡线

参数:
  • speed (float) -- 巡线速度. 默认值为80,建议值60-100.

  • mode (str) -- 巡线模式. 可选值为'cross', 'left', 'right',或时间模式. 时间模式下,over为时间,单位s.

  • over (float) -- 过线时间,到达结束位置之后继续向前运行一段时间,让机器人停在路口中间,单位ms. 默认值为500ms.

示例

>>> rc.track_line() #默认速度巡线一格
>>> rc.track_line(speed=80, mode='left', over=500) #80速度巡线到左路口,过线500ms停
>>> rc.track_line(speed=60, mode='right', over=0) #60速度巡线到右路口,到线即停
>>> rc.track_line(speed=80, '3.5s', 0) #80速度巡线3.5s停
stop()

停止运动

set_heading(target_yaw)

面向指定角度

参数:

target_yaw (float) -- 目标角度,单位度.启动时的初始角度为0°,顺时针为正.

move_to(x, y, t)

移动到指定坐标