Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random forces in sim2sim #128

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/gpr_0.5s_small.kinfer
Binary file not shown.
5 changes: 3 additions & 2 deletions sim/envs/humanoids/grp_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class rewards:
target_joint_pos_scale = 0.17 # rad
target_feet_height = 0.06 # m

cycle_time = 0.64 # sec
# cycle_time = 0.25 # sec
cycle_time = 0.5
# if true negative total rewards are clipped at zero (avoids early termination problems)
only_positive_rewards = True
# tracking reward = exp(error*sigma)
Expand Down Expand Up @@ -180,7 +181,7 @@ class scales:

# base pos
default_joint_pos = 0.5
orientation = 1.0
orientation = 1.
base_height = 0.2
base_acc = 0.2
# energy
Expand Down
26 changes: 24 additions & 2 deletions sim/sim2sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ def run_mujoco(
hist_obs = np.zeros((model_info["num_observations"]), dtype=np.double)

count_lowlevel = 0
force_application_interval = 1000 # Apply force every 1000 steps (1 second at 1000Hz)
force_magnitude_range = (0, 0) #(-15, 15) # Force range in Newtons
force_duration = 100 # Duration of force application in timesteps
force_timer = 0

input_data = {
"x_vel.1": np.zeros(1).astype(np.float32),
Expand Down Expand Up @@ -276,6 +280,24 @@ def run_mujoco(
tau = np.clip(tau, -tau_limit, tau_limit) # Clamp torques

data.ctrl = tau

# Apply random forces periodically
if count_lowlevel % force_application_interval == 0:
# Generate random force vector
random_force = np.random.uniform(
force_magnitude_range[0],
force_magnitude_range[1],
size=3
)
force_timer = force_duration

# Apply force if timer is active
if force_timer > 0:
data.xfrc_applied[1] = np.concatenate([random_force, np.zeros(3)]) # [force_x, force_y, force_z, torque_x, torque_y, torque_z]
force_timer -= 1
else:
data.xfrc_applied[1] = np.zeros(6)

mujoco.mj_step(model, data)

if render:
Expand Down Expand Up @@ -319,7 +341,7 @@ def run_mujoco(

policy_cfg = ActorCfg(embodiment=args.embodiment)
if args.embodiment == "gpr":
policy_cfg.cycle_time = 0.25
policy_cfg.cycle_time = 0.5
cfg = Sim2simCfg(
sim_duration=10.0,
dt=0.001,
Expand All @@ -337,7 +359,7 @@ def run_mujoco(
cycle_time=policy_cfg.cycle_time,
)

if args.load_model.endswith(".kinfer"):
if args.load_model.endswith(".kinfer") or args.load_model.endswith(".onnx"):
policy = ONNXModel(args.load_model)
else:
actor_model, sim2sim_info, input_tensors = get_actor_policy(args.load_model, policy_cfg)
Expand Down
Loading