| | """ |
| | Test the linear interpolator on the Lift task with Sawyer arm environment as a test case. |
| | |
| | The linear interpolator is meant to increase the stability and overall safety of a robot arm's trajectory when reaching |
| | a setpoint, "ramping up" the actual action command sent to a given controller from zero to the actual inputted action |
| | over a fraction of the timesteps in betwteen each high-level input action (the "ramp ratio"). As a result, the |
| | resulting trajectory should be smoother, proportional to the interpolator's ramp ratio setting. |
| | |
| | This test verifies that the linear interpolator works correctly on both the IK and OSC controller for both position and |
| | orientation, and proceeds as follows: |
| | |
| | 1. Given a constant delta position action, and with the interpolator disabled, we will measure the sum of absolute |
| | changes in joint torques between individual simulation timesteps |
| | |
| | 2. We will repeat Step 1, but this time with the interpolator enabled and with a ramp ratio of 1.0 (max value) |
| | |
| | 3. We expect the interpolated trajectories to experience a smaller overall magnitude of changes in torques, due to |
| | the setpoints between controller timesteps being smoothed out over the ramp ratio. |
| | |
| | Note: As this is a qualitative test, it is up to the user to evaluate the output and determine the expected behavior of |
| | the tested controllers. |
| | """ |
| |
|
| | import argparse |
| | import json |
| | import os |
| |
|
| | import numpy as np |
| |
|
| | import robosuite as suite |
| | import robosuite.controllers.composite.composite_controller_factory as composite_controller_factory |
| | import robosuite.utils.transform_utils as T |
| |
|
| | |
| |
|
| | |
| | pos_y_threshold = 0.1 |
| | delta_pos_y = 0.01 |
| | pos_action_osc = [0, delta_pos_y * 40, 0] |
| | pos_action_ik = [0, delta_pos_y, 0] |
| |
|
| | |
| | rot_r_threshold = np.pi / 2 |
| | delta_rot_r = 0.01 |
| | rot_action_osc = [delta_rot_r * 40, 0, 0] |
| | rot_action_ik = [delta_rot_r * 5, 0, 0] |
| |
|
| | |
| | thresholds = [pos_y_threshold, rot_r_threshold] |
| | indexes = [1, 0] |
| |
|
| | |
| | min_ratio = 1.10 |
| |
|
| | |
| | parser = argparse.ArgumentParser() |
| | parser.add_argument("--render", action="store_true", help="Whether to render tests or run headless") |
| | args = parser.parse_args() |
| |
|
| | |
| | np.set_printoptions(formatter={"float": lambda x: "{0:0.3f}".format(x)}) |
| |
|
| |
|
| | |
| | def step(env, action, current_torques): |
| | env.timestep += 1 |
| | policy_step = True |
| | summed_abs_delta_torques = np.zeros(7) |
| |
|
| | for i in range(int(env.control_timestep / env.model_timestep)): |
| | env.sim.forward() |
| | env._pre_action(action, policy_step) |
| | last_torques = current_torques |
| | current_torques = env.robots[0].composite_controller.part_controllers["right"].torques |
| | summed_abs_delta_torques += np.abs(current_torques - last_torques) |
| | env.sim.step() |
| | policy_step = False |
| |
|
| | env.cur_time += env.control_timestep |
| | out = env._post_action(action) |
| | return out, summed_abs_delta_torques, current_torques |
| |
|
| |
|
| | |
| | def test_linear_interpolator(): |
| |
|
| | for controller_name in [None, "BASIC"]: |
| |
|
| | for traj in ["pos", "ori"]: |
| |
|
| | |
| | timesteps = [0, 0] |
| | summed_abs_delta_torques = [np.zeros(7), np.zeros(7)] |
| |
|
| | for interpolator in [None, "linear"]: |
| | |
| | np.random.seed(3) |
| |
|
| | |
| | controller_config = composite_controller_factory.load_composite_controller_config( |
| | controller=controller_name, |
| | robot="Sawyer", |
| | ) |
| |
|
| | |
| | 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=controller_config, |
| | ) |
| |
|
| | |
| | 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) |
| | env.robots[0].composite_controller.part_controllers["right"].reset_goal() |
| |
|
| | |
| | print( |
| | "\nTesting controller {} with trajectory {} and interpolator={}...".format( |
| | controller_name, traj, interpolator |
| | ) |
| | ) |
| |
|
| | |
| | if args.render: |
| | env.viewer.set_camera(camera_id=0) |
| |
|
| | |
| | current_torques = np.zeros(7) |
| | initial_state = [env.robots[0]._hand_pos["right"], T.mat2quat(env.robots[0]._hand_orn["right"])] |
| | dstate = [ |
| | env.robots[0]._hand_pos["right"] - initial_state[0], |
| | T.mat2euler( |
| | T.quat2mat(T.quat_distance(T.mat2quat(env.robots[0]._hand_orn["right"]), initial_state[1])) |
| | ), |
| | ] |
| |
|
| | |
| | if traj == "pos": |
| | pos_act = pos_action_osc |
| | rot_act = np.zeros(3) |
| | else: |
| | pos_act = np.zeros(3) |
| | rot_act = rot_action_osc |
| |
|
| | |
| | action = np.concatenate([pos_act, rot_act, [0]]) |
| |
|
| | |
| | k = 0 if traj == "pos" else 1 |
| | j = 0 if not interpolator else 1 |
| |
|
| | |
| | while abs(dstate[k][indexes[k]]) < abs(thresholds[k]): |
| | _, summed_torques, current_torques = step(env, action, current_torques) |
| | if args.render: |
| | env.render() |
| |
|
| | |
| | summed_abs_delta_torques[j] += summed_torques |
| | timesteps[j] += 1 |
| | dstate = [ |
| | env.robots[0]._hand_pos["right"] - initial_state[0], |
| | T.mat2euler( |
| | T.quat2mat(T.quat_distance(T.mat2quat(env.robots[0]._hand_orn["right"]), initial_state[1])) |
| | ), |
| | ] |
| |
|
| | |
| | print( |
| | "Completed trajectory. Avg per-step absolute delta torques: {}".format( |
| | summed_abs_delta_torques[j] / timesteps[j] |
| | ) |
| | ) |
| |
|
| | |
| | env.close() |
| |
|
| | |
| | print() |
| | print("-" * 80) |
| | print("All linear interpolator testing completed.\n") |
| |
|
| |
|
| | if __name__ == "__main__": |
| | test_linear_interpolator() |
| |
|