2018-02-26 17:35:07 +01:00
|
|
|
import os
|
|
|
|
import copy
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
import gym
|
|
|
|
from gym import error, spaces
|
|
|
|
from gym.utils import seeding
|
|
|
|
from gym.envs.robotics import robot_env
|
|
|
|
|
|
|
|
|
|
|
|
class HandEnv(robot_env.RobotEnv):
|
|
|
|
def __init__(self, model_path, n_substeps, initial_qpos, relative_control):
|
|
|
|
self.relative_control = relative_control
|
|
|
|
|
|
|
|
super(HandEnv, self).__init__(
|
2021-07-29 02:26:34 +02:00
|
|
|
model_path=model_path,
|
|
|
|
n_substeps=n_substeps,
|
|
|
|
n_actions=20,
|
|
|
|
initial_qpos=initial_qpos,
|
|
|
|
)
|
2018-02-26 17:35:07 +01:00
|
|
|
|
|
|
|
# RobotEnv methods
|
|
|
|
# ----------------------------
|
|
|
|
|
|
|
|
def _set_action(self, action):
|
|
|
|
assert action.shape == (20,)
|
|
|
|
|
|
|
|
ctrlrange = self.sim.model.actuator_ctrlrange
|
2021-07-29 02:26:34 +02:00
|
|
|
actuation_range = (ctrlrange[:, 1] - ctrlrange[:, 0]) / 2.0
|
2018-02-26 17:35:07 +01:00
|
|
|
if self.relative_control:
|
|
|
|
actuation_center = np.zeros_like(action)
|
|
|
|
for i in range(self.sim.data.ctrl.shape[0]):
|
2021-07-29 12:42:48 -04:00
|
|
|
actuation_center[i] = self.sim.data.get_joint_qpos(self.sim.model.actuator_names[i].replace(":A_", ":"))
|
2021-07-29 02:26:34 +02:00
|
|
|
for joint_name in ["FF", "MF", "RF", "LF"]:
|
2021-07-29 12:42:48 -04:00
|
|
|
act_idx = self.sim.model.actuator_name2id("robot0:A_{}J1".format(joint_name))
|
|
|
|
actuation_center[act_idx] += self.sim.data.get_joint_qpos("robot0:{}J0".format(joint_name))
|
2018-02-26 17:35:07 +01:00
|
|
|
else:
|
2021-07-29 02:26:34 +02:00
|
|
|
actuation_center = (ctrlrange[:, 1] + ctrlrange[:, 0]) / 2.0
|
2018-02-26 17:35:07 +01:00
|
|
|
self.sim.data.ctrl[:] = actuation_center + action * actuation_range
|
2021-07-29 12:42:48 -04:00
|
|
|
self.sim.data.ctrl[:] = np.clip(self.sim.data.ctrl, ctrlrange[:, 0], ctrlrange[:, 1])
|
2018-02-26 17:35:07 +01:00
|
|
|
|
|
|
|
def _viewer_setup(self):
|
2021-07-29 02:26:34 +02:00
|
|
|
body_id = self.sim.model.body_name2id("robot0:palm")
|
2018-02-26 17:35:07 +01:00
|
|
|
lookat = self.sim.data.body_xpos[body_id]
|
|
|
|
for idx, value in enumerate(lookat):
|
|
|
|
self.viewer.cam.lookat[idx] = value
|
|
|
|
self.viewer.cam.distance = 0.5
|
2021-07-29 02:26:34 +02:00
|
|
|
self.viewer.cam.azimuth = 55.0
|
|
|
|
self.viewer.cam.elevation = -25.0
|
2019-02-15 15:55:51 -08:00
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
def render(self, mode="human", width=500, height=500):
|
2019-02-15 15:55:51 -08:00
|
|
|
return super(HandEnv, self).render(mode, width, height)
|