| | """ |
| | Test the variable impedance feature of impedance-based controllers (OSC, Joint Position) on the Lift task with |
| | Sawyer arm environment as a test case. |
| | |
| | The variable impedance feature allows per-action fine-grained control over the specific impedance gains when executing |
| | impedance control (namely, "kp" and "damping" ratios). This allows a given controller to execute more complex and |
| | potentially interactive trajectories by varying the net impedance of the controlled actuators over time. |
| | |
| | This (qualitative) test verifies that the variable impedance works correctly on both the OSC Pose / Position and |
| | Joint Position controllers, and proceeds as follows: |
| | |
| | 1. Given a constant delta position action, and with the the kp values set to critically-damped, we will ramp up |
| | the kp values to its max and then ramp down the values. We qualitatively expect the arm to accelerate as the kp |
| | values are ramped, and then slow down as they are decreased. |
| | |
| | 2. The environment will then be reset. Given a constant delta position action, and with kp values set to its |
| | default value, we will ramp up the damping values to its max and then ramp down the values. We qualitatively |
| | expect the arm to slow down as the damping values are ramped, and then increase in speed as they are decreased. |
| | |
| | 3. We will repeat Step 1 and 2 for each of the tested controllers. |
| | |
| | Periodic prijntouts should verify the above patterns; conversely, running the script with the "--render" argument will |
| | render the trajectories to allow for visual analysis of gains |
| | """ |
| |
|
| | import argparse |
| |
|
| | import numpy as np |
| |
|
| | import robosuite as suite |
| | from robosuite.controllers.composite.composite_controller_factory import load_composite_controller_config |
| |
|
| | |
| | num_timesteps_per_change = 10 |
| | percent_increase = 0.05 |
| |
|
| | |
| | d = 0.05 |
| |
|
| | |
| | kp_default = 150 |
| | damping_default = 1 |
| |
|
| | |
| | parser = argparse.ArgumentParser() |
| | parser.add_argument("--render", action="store_true", help="Whether to render tests or run headless") |
| | args = parser.parse_args() |
| |
|
| |
|
| | |
| | def test_variable_impedance(): |
| |
|
| | for controller_name in ["OSC_POSE", "OSC_POSITION", "JOINT_POSITION"]: |
| |
|
| | |
| | np.random.seed(3) |
| |
|
| | composite_controller_config = load_composite_controller_config(controller=None, robot="Sawyer") |
| | controller_config = composite_controller_config["body_parts"]["right"] |
| | controller_config["type"] = controller_name |
| | |
| | controller_config["impedance_mode"] = "variable" |
| | controller_config["kp_limits"] = [0, 300] |
| | controller_config["damping_limits"] = [0, 10] |
| |
|
| | if controller_name == "OSC_POSITION": |
| | controller_config["output_min"] = [0.05, 0.05, 0.05] |
| | controller_config["output_max"] = [-0.05, -0.05, -0.05] |
| | elif controller_name == "JOINT_POSITION": |
| | controller_config["output_min"] = -1 |
| | controller_config["output_max"] = 1 |
| |
|
| | |
| | env = suite.make( |
| | "Lift", |
| | robots="Sawyer", |
| | has_renderer=args.render, |
| | has_offscreen_renderer=False, |
| | use_camera_obs=False, |
| | horizon=10000, |
| | control_freq=20, |
| | controller_configs=composite_controller_config, |
| | ) |
| |
|
| | |
| | np.set_printoptions(formatter={"float": lambda x: "{0:0.3f}".format(x)}) |
| |
|
| | |
| | |
| | control_dim = 6 if "OSC" in controller_name else 7 |
| | low, high = env.action_spec |
| | damping_low, kp_low = low[:control_dim], low[control_dim : 2 * control_dim] |
| | damping_high, kp_high = high[:control_dim], high[control_dim : 2 * control_dim] |
| | damping_range = damping_high - damping_low |
| | kp_range = kp_high - kp_low |
| |
|
| | |
| | if controller_name == "OSC_POSE": |
| | delta = np.array([0, d, 0, 0, 0, 0]) |
| | elif controller_name == "OSC_POSITION": |
| | delta = np.array([0, d, 0]) |
| | else: |
| | delta = np.array([d, 0, 0, 0, 0, 0, 0]) |
| |
|
| | |
| | total_steps = num_timesteps_per_change / percent_increase * 2 |
| |
|
| | |
| | gains = ["kp", "damping"] |
| |
|
| | for gain in gains: |
| |
|
| | |
| | env.reset() |
| |
|
| | |
| | init_qpos = [-0.5538, -0.8208, 0.4155, 1.8409, -0.4955, 0.6482, 1.9628] |
| | env.robots[0].set_robot_joint_positions(init_qpos) |
| | env.robots[0].composite_controller.part_controllers["right"].update_initial_joints(init_qpos) |
| |
|
| | |
| | print("\nTesting controller {} while sweeping {}...".format(controller_name, gain)) |
| |
|
| | |
| | if args.render: |
| | env.viewer.set_camera(camera_id=0) |
| |
|
| | |
| | last_pos = env.robots[0]._hand_pos["right"] |
| |
|
| | |
| | if gain == "kp": |
| | kp = kp_low |
| | damping = damping_default * np.ones(control_dim) |
| | gain_val = kp |
| | gain_range = kp_range |
| | else: |
| | kp = kp_default * np.ones(control_dim) |
| | damping = damping_low |
| | gain_val = damping |
| | gain_range = damping_range |
| |
|
| | |
| | i = 0 |
| | sign = 1.0 |
| |
|
| | |
| | while i < total_steps: |
| | |
| | action = np.concatenate([damping, kp, sign * delta, [0]]) |
| |
|
| | |
| | env.step(action) |
| | if args.render: |
| | env.render() |
| |
|
| | |
| | cur_pos = env.robots[0]._hand_pos["right"] |
| |
|
| | |
| | if i == int(num_timesteps_per_change / percent_increase): |
| | sign *= -1.0 |
| |
|
| | |
| | if i % num_timesteps_per_change == 0: |
| | |
| | delta_pos = np.linalg.norm(cur_pos - last_pos) |
| | print(" Magnitude eef distance change with {} = {}: {:.5f}".format(gain, gain_val[0], delta_pos)) |
| | last_pos = cur_pos |
| | |
| | gain_val += percent_increase * gain_range * sign |
| |
|
| | |
| | i += 1 |
| |
|
| | |
| | print("Completed trajectory.") |
| |
|
| | |
| | env.close() |
| |
|
| | |
| | print() |
| | print("-" * 80) |
| | print("All variable impedance testing completed.\n") |
| |
|
| |
|
| | if __name__ == "__main__": |
| | test_variable_impedance() |
| |
|