File size: 3,675 Bytes
d6b3397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause

"""
Script to view ARL Robot 1.

Launch Isaac Sim Simulator first.
"""

# Create argparser
import argparse

from isaaclab.app import AppLauncher

parser = argparse.ArgumentParser(description="View ARL Robot 1 with Lee Position Controller.")
# append AppLauncher cli args
AppLauncher.add_app_launcher_args(parser)
args_cli = parser.parse_args()

# launch omniverse app
app_launcher = AppLauncher(args_cli)
simulation_app = app_launcher.app

"""Rest everything follows."""

import torch

import omni.usd
from pxr import Gf, UsdLux

import isaaclab.sim as sim_utils
from isaaclab.sim import SimulationContext

from isaaclab_contrib.assets import Multirotor
from isaaclab_contrib.controllers.lee_position_control import LeePosController
from isaaclab_contrib.controllers.lee_position_control_cfg import LeePosControllerCfg

from isaaclab_assets.robots.arl_robot_1 import ARL_ROBOT_1_CFG


def main():
    """Main function to spawn arl_robot_1."""

    # Create simulation context
    sim_cfg = sim_utils.SimulationCfg(dt=0.01)
    sim = SimulationContext(sim_cfg)

    # Create a dome light with light blue color
    stage = omni.usd.get_context().get_stage()
    dome_light = UsdLux.DomeLight.Define(stage, "/World/DomeLight")
    dome_light.CreateColorAttr(Gf.Vec3f(0.53, 0.81, 0.92))  # Light blue
    dome_light.CreateIntensityAttr(1000.0)

    # Spawn ground plane
    cfg = sim_utils.GroundPlaneCfg()
    cfg.func("/World/defaultGroundPlane", cfg)

    # Spawn robot
    robot_cfg = ARL_ROBOT_1_CFG.replace(prim_path="/World/Robot")
    robot_cfg.actuators["thrusters"].dt = sim_cfg.dt
    robot = Multirotor(robot_cfg)

    # Play the simulator
    sim.reset()

    # Create Lee position controller
    controller_cfg = LeePosControllerCfg(
        K_pos_range=((2.5, 2.5, 1.5), (3.5, 3.5, 2.0)),
        K_vel_range=((2.5, 2.5, 1.5), (3.5, 3.5, 2.0)),
        K_rot_range=((1.6, 1.6, 0.25), (1.85, 1.85, 0.4)),
        K_angvel_range=((0.4, 0.4, 0.075), (0.5, 0.5, 0.09)),
        max_inclination_angle_rad=1.0471975511965976,
        max_yaw_rate=1.0471975511965976,
    )
    controller = LeePosController(controller_cfg, robot, num_envs=1, device=str(sim.device))

    # Get allocation matrix and compute pseudoinverse
    allocation_matrix = torch.tensor(robot_cfg.allocation_matrix, device=sim.device, dtype=torch.float32)
    # allocation_matrix is (6, num_thrusters), we need pseudoinverse for wrench -> thrust
    alloc_pinv = torch.linalg.pinv(allocation_matrix)  # Shape: (num_thrusters, 6)

    # Position command: hover in place (zero position, zero yaw)
    pos_command = torch.zeros((1, 4), device=sim.device)  # [x, y, z, yaw]
    pos_command[0, 2] = 1.0  # Hover at 1 meter height

    # Simulation loop
    print("[INFO] Starting demo with Lee Position Controller. Press Ctrl+C to stop.")

    while simulation_app.is_running():
        # Compute wrench from velocity controller
        wrench = controller.compute(pos_command)  # Shape: (1, 6)

        # Allocate wrench to thrusters: thrust = pinv(A) @ wrench
        thrust_cmd = torch.matmul(wrench, alloc_pinv.T)  # Shape: (1, num_thrusters)
        thrust_cmd = thrust_cmd.clamp(min=0.0)  # Ensure non-negative thrust

        # Apply thrust
        robot.set_thrust_target(thrust_cmd)

        # Step simulation
        robot.write_data_to_sim()
        sim.step()

        # Update robot
        robot.update(sim_cfg.dt)

    # Cleanup
    simulation_app.close()


if __name__ == "__main__":
    main()