コンテンツにスキップ

05 · はじめての Action Graph:キーボードで運転

⏱ 25 分 · レベル:初級 · スクリプト:isaac/05_action_graph.py

目標: 制御ループを書かずに、W A S D キーで Lime を運転する Action Graph (OmniGraph) を作ります。これは、以降のチュートリアルで Isaac Sim と ROS 2 をつなぐのと同じ仕組みです。

W を 3 秒間押し続け、次に A を 2 秒間押し続けます。

考え方

OmniGraph は、Isaac Sim のビジュアルプログラミングの仕組みです。ノードには入力と出力があり、ワイヤーがノードの間でデータを運びます。Action Graph は、イベントが発生したときに実行されます。On Playback Tick は再生中にシミュレーションの 1 フレームごとに 1 回発生し、白い exec ワイヤーがどのノードを実行するかを決めます。

キーボードのグラフは、キーの状態を車輪の速度に変換します。

\[ v = 0.2\,\tfrac{m}{s}\cdot(W - S), \qquad \omega = 0.8\,\tfrac{rad}{s}\cdot(A - D) \]

押されているキーは 1、離されているキーは 0 になります。そのため、W だけを押すと \(v = 0.2\) m/s、S だけを押すと \(-0.2\) m/s になります。

KeyboardDrive グラフ

作ってみる

  1. チュートリアル 01 のステージ (my_stages/01_hello_world.usd) を開くか、新しく作ります。新しく作る場合は、File ▸ New、Create ▸ Physics ▸ Ground Plane を選び、続いて File ▸ Add Reference で assets/usd/turtlebot3_lime_tuned.usda を追加します。
  2. Window ▸ Visual Scripting ▸ Action Graph ▸ New Action Graph を選びます。
  3. 左側のノード検索に名前を入力して、次のノードをドラッグして追加します:4 × Read Keyboard State(key をそれぞれ W、S、A、D に設定)、4 × To Double、2 × Subtract、2 × Constant Double(0.2 と 0.8)、2 × Multiply、Differential Controller、Articulation Controller、On Playback Tick。
  4. 図のとおりに接続します。W と S を一方の Subtract (a − b) に、A と D をもう一方の Subtract につなぎます。それぞれの差に対応する定数を掛け、その積を Differential Controller の linearVelocity と angularVelocity につなぎます。velocityCommand は Articulation Controller に、tick は両方のコントローラーの execIn につなぎます。
  5. Differential Controller では、wheelRadius を 0.06、wheelDistance を 0.29425 にします。Articulation Controller では、targetPrim = …/Geometry/base_footprint/base_link、jointNames = wheel_left_joint、wheel_right_joint にします。
  6. Play を押し、ビューポートをクリックしてから運転します。

近道

Tools ▸ Robotics ▸ OmniGraph Controllers ▸ Differential Controller を使うと、このグラフのうちコントローラーの部分が作られます。

source scripts/isaac_env.sh
python isaac/05_action_graph.py

og.Controller.edit() は、手作業で行ったのと同じこと、つまりノードの作成、値の設定、ポートの接続を行います。

og.Controller.edit(
    {"graph_path": "/World/KeyboardDrive", "evaluator_name": "execution"},
    {
        keys.CREATE_NODES: [("KeyW", "omni.graph.nodes.ReadKeyboardState"), ...],
        keys.SET_VALUES:   [("KeyW.inputs:key", "W"), ...],
        keys.CONNECT:      [("KeyW.outputs:isPressed", "ToDoubleW.inputs:value"), ...],
    },
)
スクリプト全体:isaac/05_action_graph.py
# SPDX-FileCopyrightText: 2026 An (an-robotics)
# SPDX-License-Identifier: Apache-2.0

"""Tutorial 05 - Your first Action Graph: drive TurtleBot3 Lime with the keyboard (no ROS).

    source scripts/isaac_env.sh
    python isaac/05_action_graph.py          # click into the viewport, then W/S = forward/back, A/D = turn

The graph "KeyboardDrive":

    On Playback Tick ------------------------------------------------------------+
    Read Keyboard State (W) -> To Double -+                                       |
    Read Keyboard State (S) -> To Double -+-> Subtract -> Multiply (x 0.2 m/s) --+-> Differential Controller
    Read Keyboard State (A) -> To Double -+                                       |   (linear / angular velocity)
    Read Keyboard State (D) -> To Double -+-> Subtract -> Multiply (x 0.8 rad/s) -+          |
                                                                                    Articulation Controller (wheels)
A key that is held down reads 1.0, otherwise 0.0, so (W - S) is +1 / 0 / -1.
"""

import sys

from common.cli import parse_args

args = parse_args(__doc__.splitlines()[0])

from isaacsim import SimulationApp

simulation_app = SimulationApp({"headless": args.headless, "width": 1280, "height": 720})

import isaacsim.core.experimental.utils.app as app_utils
import isaacsim.core.experimental.utils.stage as stage_utils
import numpy as np
import omni.graph.core as og
import usdrt.Sdf
from common import lime
from isaacsim.core.experimental.objects import Cube, GroundPlane
from isaacsim.core.experimental.prims import Articulation
from isaacsim.core.simulation_manager import SimulationManager
from pxr import UsdLux

stage = stage_utils.create_new_stage(template="sunlight")
UsdLux.DomeLight.Define(stage, "/World/DomeLight").GetIntensityAttr().Set(500.0)
GroundPlane("/World/GroundPlane")
for i, (x, y) in enumerate([(1.5, 0.0), (0.0, 1.5), (-1.5, 0.0), (0.0, -1.5)]):
    Cube(f"/World/Landmark_{i}", sizes=0.2, positions=[x, y, 0.1], colors=[0.6, 0.85, 0.2])
articulation_root = lime.add_lime("/World/turtlebot3_lime")

MAX_SPEED, MAX_TURN = 0.2, 0.8  # m/s, rad/s
keys = og.Controller.Keys
og.Controller.edit(
    {"graph_path": "/World/KeyboardDrive", "evaluator_name": "execution"},
    {
        keys.CREATE_NODES: [
            ("OnPlaybackTick", "omni.graph.action.OnPlaybackTick"),
            *[(f"Key{k}", "omni.graph.nodes.ReadKeyboardState") for k in "WSAD"],
            *[(f"ToDouble{k}", "omni.graph.nodes.ToDouble") for k in "WSAD"],
            ("Forward", "omni.graph.nodes.Subtract"),  # W - S
            ("Turn", "omni.graph.nodes.Subtract"),  # A - D (left is positive)
            ("SpeedScale", "omni.graph.nodes.ConstantDouble"),
            ("TurnScale", "omni.graph.nodes.ConstantDouble"),
            ("Speed", "omni.graph.nodes.Multiply"),
            ("TurnRate", "omni.graph.nodes.Multiply"),
            ("DiffController", "isaacsim.robot.wheeled_robots.DifferentialController"),
            ("ArticulationController", "isaacsim.core.nodes.IsaacArticulationController"),
        ],
        keys.SET_VALUES: [
            *[(f"Key{k}.inputs:key", k) for k in "WSAD"],
            ("SpeedScale.inputs:value", MAX_SPEED),
            ("TurnScale.inputs:value", MAX_TURN),
            ("DiffController.inputs:wheelRadius", lime.WHEEL_RADIUS),
            ("DiffController.inputs:wheelDistance", lime.WHEEL_BASE),
            ("ArticulationController.inputs:targetPrim", [usdrt.Sdf.Path(articulation_root)]),
            ("ArticulationController.inputs:jointNames", lime.WHEEL_JOINTS),
        ],
        keys.CONNECT: [
            *[(f"Key{k}.outputs:isPressed", f"ToDouble{k}.inputs:value") for k in "WSAD"],
            ("ToDoubleW.outputs:converted", "Forward.inputs:a"),
            ("ToDoubleS.outputs:converted", "Forward.inputs:b"),
            ("ToDoubleA.outputs:converted", "Turn.inputs:a"),
            ("ToDoubleD.outputs:converted", "Turn.inputs:b"),
            ("Forward.outputs:difference", "Speed.inputs:a"),
            ("SpeedScale.inputs:value", "Speed.inputs:b"),
            ("Turn.outputs:difference", "TurnRate.inputs:a"),
            ("TurnScale.inputs:value", "TurnRate.inputs:b"),
            ("OnPlaybackTick.outputs:tick", "DiffController.inputs:execIn"),
            ("Speed.outputs:product", "DiffController.inputs:linearVelocity"),
            ("TurnRate.outputs:product", "DiffController.inputs:angularVelocity"),
            ("OnPlaybackTick.outputs:tick", "ArticulationController.inputs:execIn"),
            ("DiffController.outputs:velocityCommand", "ArticulationController.inputs:velocityCommand"),
        ],
    },
)

recorder = None
if args.record:
    from common.recorder import Recorder

    recorder = Recorder(args.record, eye=(2.2, -2.2, 1.6), target=(0.3, 0.2, 0.1), every_n=3, fps=20)

SimulationManager.setup_simulation(dt=1.0 / 60.0)
app_utils.play()
simulation_app.update()
robot = Articulation(articulation_root)
print("[05] ready: click into the viewport and use W/S/A/D", flush=True)


# --- --test / --record: "press" keys from the script, the way a user would --------------------------------------
def press(key_name: str, down: bool):
    import carb.input
    import omni.appwindow

    keyboard = omni.appwindow.get_default_app_window().get_keyboard()
    event = carb.input.KeyboardEventType.KEY_PRESS if down else carb.input.KeyboardEventType.KEY_RELEASE
    carb.input.acquire_input_provider().buffer_keyboard_key_event(keyboard, event, getattr(carb.input.KeyboardInput, key_name), 0)


script = [(1.0, "W", True), (4.0, "W", False), (4.5, "A", True), (6.5, "A", False)]  # 3 s forward, 2 s turn left
start = robot.get_world_poses()[0].numpy()[0, :2].copy()
log, steps = {}, 0
while simulation_app.is_running():
    t = steps / 60.0
    if args.test or args.record:
        while script and t >= script[0][0]:
            _, k, down = script.pop(0)
            press(k, down)
        if steps == int(4.4 * 60):
            log["forward"] = float(np.linalg.norm(robot.get_world_poses()[0].numpy()[0, :2] - start))
        if steps == int(6.9 * 60):
            w_, x_, y_, z_ = robot.get_world_poses()[1].numpy()[0]
            log["yaw"] = float(np.degrees(np.arctan2(2 * (w_ * z_ + x_ * y_), 1 - 2 * (y_ * y_ + z_ * z_))))
        if t > 7.5:
            break
    simulation_app.update()
    steps += 1
    if recorder:
        recorder.capture()

ok = True
if args.test:
    checks = {
        f"W held for 3 s: drove {log['forward']:.2f} m (expected ~0.6)": abs(log["forward"] - 0.6) < 0.1,
        f"A held for 2 s: turned left to {log['yaw']:.0f} deg (expected ~90)": 60 < log["yaw"] < 100,
    }
    for text, passed in checks.items():
        print(f"[05][check] {'ok  ' if passed else 'FAIL'} {text}")
    ok = all(checks.values())
    print(f"[05][test] {'PASS' if ok else 'FAIL'}")
if recorder:
    recorder.save()
app_utils.stop()
simulation_app.close(exit_code=0 if ok else 1)
sys.exit(0 if ok else 1)

自動チェック

--test は、Kit の入力システムを通してスクリプトからキーを押します。そのため、グラフ全体(キーボードのノード → … → 車輪)が、実際にキーを打ったときとまったく同じように動きます。

[05][check] ok   W held for 3 s: drove 0.60 m (expected ~0.6)
[05][check] ok   A held for 2 s: turned left to 87 deg (expected ~90)
[05][test] PASS
ノードタイプ ID (Python) GUI での名前
omni.graph.action.OnPlaybackTick On Playback Tick
omni.graph.nodes.ReadKeyboardState Read Keyboard State
omni.graph.nodes.ToDouble / Subtract / Multiply / ConstantDouble To Double / Subtract / Multiply / Constant Double
isaacsim.robot.wheeled_robots.DifferentialController Differential Controller
isaacsim.core.nodes.IsaacArticulationController Articulation Controller

次へ: 06 · ROS 2 で走らせる。グラフは同じですが、速度は /cmd_vel から来ます。