datetime:2025/12/21 16:00
author:nzb

配置键盘鼠标控制

本节内容

  • 配置键盘/鼠标控制
    • 键盘控制配置参数
    • 键盘控制按键映射
    • 鼠标控制

Python 脚本与 YAML 配置文件

  • 键盘控制
import irsim

env = irsim.make()

for i in range(1000):

    env.step()
    env.render(0.05)

    # Check current status
    print(f"Environment status: {env.status}")

    if env.done():
        break

env.end()
world:
  height: 50  
  width: 50   
  control_mode: 'keyboard'  
  obstacle_map: 'cave.png'
  mdownsample: 2

gui:
  keyboard:
    backend: 'pynput'      # 'pynput' (default) or 'mpl'
    global_hook: true      # capture keys globally (may require OS permissions)
    key_id: 0              # initial robot control id
    # key_lv_max: 3.0      # maximum linear velocity
    # key_ang_max: 1.0     # maximum angular velocity
    # key_lv: 0.0          # initial linear velocity
    # key_ang: 0.0         # initial angular velocity

robot:
  - kinematics: {name: 'acker'} 
    shape: {name: 'rectangle', length: 4.6, width: 1.6, wheelbase: 3}
    state: [5, 5, 0, 0]
    goal: [40, 40, 0]
    vel_max: [4, 1]
    plot:
      show_trail: True
      traj_color: 'g'
      show_trajectory: True
      show_goal: False

    sensors: 
      - type: 'lidar2d'
        range_min: 0
        range_max: 20
        angle_range: 3.14
        number: 100
        alpha: 0.4

Keyboard Control Key Mapping

Key Function
w Forward
s Backward
a Turn Left
d Turn Right
q Decrease Linear Velocity
e Increase Linear Velocity
z Decrease Angular Velocity
c Increase Angular Velocity
alt+num Change Current Control Robot ID
r Reset the Environment
space Toggle Pause/Resume Environment
esc Quit the Environment (sets quit flag)
x Switch Keyboard/Auto Control
l Reload the Environment
F5 Debug the Environment (fn+f5 for mac)
v Save the current figure
  • 鼠标控制
import irsim

env = irsim.make()

for i in range(10000):
    env.step()
    env.render(0.05, show_goal=False)

    if env.mouse_left_pos is not None:
        env.robot.set_goal(env.mouse_left_pos)

    if env.done():
        break

env.end()
world:
  height: 50 
  width: 50  
  step_time: 0.1 
  sample_time: 0.1 
  offset: [0, 0] 
  collision_mode: 'stop' 
  control_mode: 'auto' 
  plot:
    saved_figure:
      bbox_inches: null

robot:
  - kinematics: {name: 'diff'} 
    shape: {name: 'circle', radius: 1}
    state: [5, 5, 0]
    vel_max: [4, 1]
    behavior: {name: 'dash'} 
    plot:
      show_trajectory: True
      traj_color: 'g'
      show_goals: True

    sensors: 
      - type: 'lidar2d'
        range_min: 0
        range_max: 20
        angle_range: 3.14
        number: 100
        noise: False
        std: 1   
        angle_std: 0.2
        offset: [0, 0, 0]
        alpha: 0.4


obstacle:
  - number: 10
    distribution: {name: 'manual'}
    shape:
      - {name: 'polygon', random_shape: true, center_range: [5, 10, 40, 30], avg_radius_range: [0.5, 2]}

鼠标控制映射

鼠标操作 功能
移动鼠标 跟踪鼠标位置并更新显示坐标
鼠标中键 重置缩放至默认视图
滚轮上滑 以鼠标位置为中心放大
滚轮下滑 以鼠标位置为中心缩小

鼠标位置属性

属性 类型 说明
mouse_left_pos tuple 左键点击位置 (x, y)
mouse_right_pos tuple 右键点击位置 (x, y)
mouse_pos tuple 当前鼠标位置 (x, y)

results matching ""

    No results matching ""