def test_static_pose_on_rack(self): pybullet_client = bullet_client.BulletClient() pybullet_client.enable_cns() pybullet_client.resetSimulation() pybullet_client.setPhysicsEngineParameter(numSolverIterations=60) pybullet_client.setTimeStep(TIME_STEP) pybullet_client.setGravity(0, 0, -10) robot = ( mini_cheetah.MiniCheetah( pybullet_client=pybullet_client, action_repeat=5, time_step=0.002, on_rack=True)) robot.Reset( reload_urdf=False, default_motor_angles=INIT_MOTOR_ANGLES, reset_time=0.5) for _ in range(NUM_STEPS): robot.Step(INIT_MOTOR_ANGLES) motor_angles = robot.GetMotorAngles() np.testing.assert_array_almost_equal( motor_angles, INIT_MOTOR_ANGLES, decimal=2)
def reset(self): self.t = 0 if not self._isInitialized: if self.enable_draw: self._pybullet_client = bullet_client.BulletClient( connection_mode=p1.GUI) else: self._pybullet_client = bullet_client.BulletClient() self._pybullet_client.setAdditionalSearchPath( pybullet_data.getDataPath()) z2y = self._pybullet_client.getQuaternionFromEuler( [-math.pi * 0.5, 0, 0]) self._planeId = self._pybullet_client.loadURDF( "plane.urdf", [0, 0, 0], z2y) #print("planeId=",self._planeId) self._pybullet_client.configureDebugVisualizer( self._pybullet_client.COV_ENABLE_Y_AXIS_UP, 1) self._pybullet_client.setGravity(0, -9.8, 0) self._pybullet_client.setPhysicsEngineParameter( numSolverIterations=10) self._pybullet_client.changeDynamics(self._planeId, linkIndex=-1, lateralFriction=0.9) self._mocapData = motion_capture_data.MotionCaptureData() motionPath = pybullet_data.getDataPath( ) + "/motions/humanoid3d_walk.txt" #motionPath = pybullet_data.getDataPath()+"/motions/humanoid3d_backflip.txt" self._mocapData.Load(motionPath) timeStep = 1. / 600 useFixedBase = False self._humanoid = humanoid_stable_pd.HumanoidStablePD( self._pybullet_client, self._mocapData, timeStep, useFixedBase) self._isInitialized = True self._pybullet_client.setTimeStep(timeStep) self._pybullet_client.setPhysicsEngineParameter(numSubSteps=2) selfCheck = False if (selfCheck): curTime = 0 while self._pybullet_client.isConnected(): self._humanoid.setSimTime(curTime) state = self._humanoid.getState() #print("state=",state) pose = self._humanoid.computePose( self._humanoid._frameFraction) for i in range(10): curTime += timeStep #taus = self._humanoid.computePDForces(pose) #self._humanoid.applyPDForces(taus) #self._pybullet_client.stepSimulation() time.sleep(timeStep) #print("numframes = ", self._humanoid._mocap_data.NumFrames()) startTime = random.randint(0, self._humanoid._mocap_data.NumFrames() - 2) rnrange = 1000 rn = random.randint(0, rnrange) startTime = float(rn) / rnrange * self._humanoid.getCycleTime() self._humanoid.setSimTime(startTime) self._humanoid.resetPose() #this clears the contact points. Todo: add API to explicitly clear all contact points? self._pybullet_client.stepSimulation()
def __init__(self, urdf_root=pybullet_data.getDataPath(), urdf_version=None, distance_weight=1.0, energy_weight=0.005, shake_weight=0.0, drift_weight=0.0, distance_limit=float("inf"), observation_noise_stdev=SENSOR_NOISE_STDDEV, self_collision_enabled=True, motor_velocity_limit=np.inf, pd_control_enabled=False, leg_model_enabled=True, accurate_motor_model_enabled=False, remove_default_joint_damping=False, motor_kp=1.0, motor_kd=0.02, control_latency=0.0, pd_latency=0.0, torque_control_enabled=False, motor_overheat_protection=False, hard_reset=True, on_rack=False, render=False, num_steps_to_log=1000, action_repeat=1, control_time_step=None, env_randomizer=None, forward_reward_cap=float("inf"), reflection=True, log_path=None): """Initialize the minitaur gym environment. Args: urdf_root: The path to the urdf data folder. urdf_version: [DEFAULT_URDF_VERSION, DERPY_V0_URDF_VERSION, RAINBOW_DASH_V0_URDF_VERSION] are allowable versions. If None, DEFAULT_URDF_VERSION is used. DERPY_V0_URDF_VERSION is the result of first pass system identification for derpy. We will have a different URDF and related Minitaur class each time we perform system identification. While the majority of the code of the class remains the same, some code changes (e.g. the constraint location might change). __init__() will choose the right Minitaur class from different minitaur modules based on urdf_version. distance_weight: The weight of the distance term in the reward. energy_weight: The weight of the energy term in the reward. shake_weight: The weight of the vertical shakiness term in the reward. drift_weight: The weight of the sideways drift term in the reward. distance_limit: The maximum distance to terminate the episode. observation_noise_stdev: The standard deviation of observation noise. self_collision_enabled: Whether to enable self collision in the sim. motor_velocity_limit: The velocity limit of each motor. pd_control_enabled: Whether to use PD controller for each motor. leg_model_enabled: Whether to use a leg motor to reparameterize the action space. accurate_motor_model_enabled: Whether to use the accurate DC motor model. remove_default_joint_damping: Whether to remove the default joint damping. motor_kp: proportional gain for the accurate motor model. motor_kd: derivative gain for the accurate motor model. control_latency: It is the delay in the controller between when an observation is made at some point, and when that reading is reported back to the Neural Network. pd_latency: latency of the PD controller loop. PD calculates PWM based on the motor angle and velocity. The latency measures the time between when the motor angle and velocity are observed on the microcontroller and when the true state happens on the motor. It is typically (0.001- 0.002s). torque_control_enabled: Whether to use the torque control, if set to False, pose control will be used. motor_overheat_protection: Whether to shutdown the motor that has exerted large torque (OVERHEAT_SHUTDOWN_TORQUE) for an extended amount of time (OVERHEAT_SHUTDOWN_TIME). See ApplyAction() in minitaur.py for more details. hard_reset: Whether to wipe the simulation and load everything when reset is called. If set to false, reset just place the minitaur back to start position and set its pose to initial configuration. on_rack: Whether to place the minitaur on rack. This is only used to debug the walking gait. In this mode, the minitaur's base is hanged midair so that its walking gait is clearer to visualize. render: Whether to render the simulation. num_steps_to_log: The max number of control steps in one episode that will be logged. If the number of steps is more than num_steps_to_log, the environment will still be running, but only first num_steps_to_log will be recorded in logging. action_repeat: The number of simulation steps before actions are applied. control_time_step: The time step between two successive control signals. env_randomizer: An instance (or a list) of EnvRandomizer(s). An EnvRandomizer may randomize the physical property of minitaur, change the terrrain during reset(), or add perturbation forces during step(). forward_reward_cap: The maximum value that forward reward is capped at. Disabled (Inf) by default. log_path: The path to write out logs. For the details of logging, refer to minitaur_logging.proto. Raises: ValueError: If the urdf_version is not supported. """ # Set up logging. self._log_path = log_path self.logging = minitaur_logging.MinitaurLogging(log_path) # PD control needs smaller time step for stability. if control_time_step is not None: self.control_time_step = control_time_step self._action_repeat = action_repeat self._time_step = control_time_step / action_repeat else: # Default values for time step and action repeat if accurate_motor_model_enabled or pd_control_enabled: self._time_step = 0.002 self._action_repeat = 5 else: self._time_step = 0.01 self._action_repeat = 1 self.control_time_step = self._time_step * self._action_repeat # TODO(b/73829334): Fix the value of self._num_bullet_solver_iterations. self._num_bullet_solver_iterations = int( NUM_SIMULATION_ITERATION_STEPS / self._action_repeat) self._urdf_root = urdf_root self._self_collision_enabled = self_collision_enabled self._motor_velocity_limit = motor_velocity_limit self._observation = [] self._true_observation = [] self._objectives = [] self._objective_weights = [ distance_weight, energy_weight, drift_weight, shake_weight ] self._env_step_counter = 0 self._num_steps_to_log = num_steps_to_log self._is_render = render self._last_base_position = [0, 0, 0] self._distance_weight = distance_weight self._energy_weight = energy_weight self._drift_weight = drift_weight self._shake_weight = shake_weight self._distance_limit = distance_limit self._observation_noise_stdev = observation_noise_stdev self._action_bound = 1 self._pd_control_enabled = pd_control_enabled self._leg_model_enabled = leg_model_enabled self._accurate_motor_model_enabled = accurate_motor_model_enabled self._remove_default_joint_damping = remove_default_joint_damping self._motor_kp = motor_kp self._motor_kd = motor_kd self._torque_control_enabled = torque_control_enabled self._motor_overheat_protection = motor_overheat_protection self._on_rack = on_rack self._cam_dist = 1.0 self._cam_yaw = 0 self._cam_pitch = -30 self._forward_reward_cap = forward_reward_cap self._hard_reset = True self._last_frame_time = 0.0 self._control_latency = control_latency self._pd_latency = pd_latency self._urdf_version = urdf_version self._ground_id = None self._reflection = reflection self._env_randomizers = convert_to_list( env_randomizer) if env_randomizer else [] self._episode_proto = minitaur_logging_pb2.MinitaurEpisode() if self._is_render: self._pybullet_client = bullet_client.BulletClient( connection_mode=pybullet.GUI) else: self._pybullet_client = bullet_client.BulletClient() if self._urdf_version is None: self._urdf_version = DEFAULT_URDF_VERSION self._pybullet_client.setPhysicsEngineParameter(enableConeFriction=0) self.seed() self.reset() observation_high = (self._get_observation_upper_bound() + OBSERVATION_EPS) observation_low = (self._get_observation_lower_bound() - OBSERVATION_EPS) action_dim = NUM_MOTORS action_high = np.array([self._action_bound] * action_dim) self.action_space = spaces.Box(-action_high, action_high) self.observation_space = spaces.Box(observation_low, observation_high) self.viewer = None self._hard_reset = hard_reset # This assignment need to be after reset()
def __init__(self, urdf_root=pybullet_data.getDataPath(), urdf_version=None, distance_weight=1.0, energy_weight=0.005, shake_weight=0.0, drift_weight=0.0, distance_limit=float("inf"), observation_noise_stdev=SENSOR_NOISE_STDDEV, self_collision_enabled=True, motor_velocity_limit=np.inf, pd_control_enabled=False, leg_model_enabled=True, accurate_motor_model_enabled=False, remove_default_joint_damping=False, motor_kp=1.0, motor_kd=0.02, control_latency=0.0, pd_latency=0.0, torque_control_enabled=False, motor_overheat_protection=False, hard_reset=True, on_rack=False, render=False, num_steps_to_log=1000, action_repeat=1, control_time_step=None, env_randomizer=None, forward_reward_cap=float("inf"), reflection=True, log_path=None): print( '------------------------------INITIALIZE ENVIRONMENT------------------------------' ) self._log_path = log_path self.logging = minitaur_logging.MinitaurLogging(log_path) if control_time_step is not None: self.control_time_step = control_time_step # 0.006 self._action_repeat = action_repeat # 6 self._time_step = control_time_step / action_repeat # 0.001 else: if accurate_motor_model_enabled or pd_control_enabled: self._time_step = 0.002 self._action_repeat = 5 else: self._time_step = 0.01 self._action_repeat = 1 self.control_time_step = self._time_step * self._action_repeat self._num_bullet_solver_iterations = int( NUM_SIMULATION_ITERATION_STEPS / self._action_repeat) self._urdf_root = urdf_root self._self_collision_enabled = self_collision_enabled self._motor_velocity_limit = motor_velocity_limit self._observation = [] self._true_observation = [] self._objectives = [] self._objective_weights = [ distance_weight, energy_weight, drift_weight, shake_weight ] self._env_step_counter = 0 self._num_steps_to_log = num_steps_to_log self._is_render = render self._last_base_position = [0, 0, 0] self._distance_weight = distance_weight self._energy_weight = energy_weight self._drift_weight = drift_weight self._shake_weight = shake_weight self._distance_limit = distance_limit self._observation_noise_stdev = observation_noise_stdev self._action_bound = 1 self._pd_control_enabled = pd_control_enabled self._leg_model_enabled = leg_model_enabled self._accurate_motor_model_enabled = accurate_motor_model_enabled self._remove_default_joint_damping = remove_default_joint_damping self._motor_kp = motor_kp self._motor_kd = motor_kd self._torque_control_enabled = torque_control_enabled self._motor_overheat_protection = motor_overheat_protection self._on_rack = on_rack self._cam_dist = 1.0 self._cam_yaw = 0 self._cam_pitch = -30 self._forward_reward_cap = forward_reward_cap self._hard_reset = True self._last_frame_time = 0.0 self._control_latency = control_latency self._pd_latency = pd_latency self._urdf_version = urdf_version self._ground_id = None self._reflection = reflection self._env_randomizers = convert_to_list( env_randomizer) if env_randomizer else [] self._episode_proto = minitaur_logging_pb2.MinitaurEpisode() print('----------ENTER BulletClient----------') if self._is_render: self._pybullet_client = BC.BulletClient( connection_mode=pybullet.GUI) print('----------EXIT BulletClient----------') else: self._pybullet_client = BC.BulletClient() print('----------EXIT BulletClient----------') self._pybullet_client.setPhysicsEngineParameter(enableConeFriction=0) self.seed() print('----------ENTER MinitaurGymEnv.reset1-----------') self.reset() print('----------EXIT MinitaurGymEnv.reset1------------') print( '------------------------------ENVIRONMENT INITIALIZED------------------------------' )
def test_init(self): pybullet_client = bullet_client.BulletClient() pybullet_client.enable_cns() robot = mini_cheetah.MiniCheetah( pybullet_client=pybullet_client, time_step=TIME_STEP, on_rack=True) self.assertIsNotNone(robot)