summaryrefslogtreecommitdiff
path: root/examples/pybullet/gym/pybullet_envs/minitaur/envs_v2/locomotion_gym_env.py
blob: 0c30e5b8ea6172aeb6c2f4a353fdb0e8a4c39cf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# Lint as: python3
"""This file implements the locomotion gym env."""

import atexit
import collections
import time
from typing import Any, Callable, Sequence, Text, Union
import gin
import gym
import numpy as np

from pybullet_envs.minitaur.envs_v2 import base_client
from pybullet_utils import bullet_client
import pybullet_data
import pybullet
from pybullet_envs.minitaur.envs import minitaur_logging
from pybullet_envs.minitaur.envs import minitaur_logging_pb2
#from pybullet_envs.minitaur.envs import minitaur_logging
#from pybullet_envs.minitaur.envs import minitaur_logging_pb2
from pybullet_envs.minitaur.envs_v2.evaluation import metric_logger
from pybullet_envs.minitaur.envs_v2.scenes import scene_base
from pybullet_envs.minitaur.envs_v2.sensors import sensor
from pybullet_envs.minitaur.envs_v2.sensors import space_utils
from pybullet_envs.minitaur.envs_v2.utilities import rendering_utils
from pybullet_envs.minitaur.robots import autonomous_object
from pybullet_envs.minitaur.robots import robot_base

_ACTION_EPS = 0.01
_NUM_SIMULATION_ITERATION_STEPS = 300
_LOG_BUFFER_LENGTH = 5000

SIM_CLOCK = 'SIM_CLOCK'

# Exports this symbol so we can use it in the config file.
gin.constant('locomotion_gym_env.SIM_CLOCK', SIM_CLOCK)

# This allows us to bind @time.time in the gin configuration.
gin.external_configurable(time.time, module='time')


# TODO(b/122048194): Enable position/torque/hybrid control mode.
@gin.configurable
class LocomotionGymEnv(gym.Env):
  """The gym environment for the locomotion tasks."""
  metadata = {
      'render.modes': ['human', 'rgb_array', 'topdown'],
      'video.frames_per_second': 100
  }

  def __init__(self,
               gym_config,
               clock: Union[Callable[..., float], Text] = 'SIM_CLOCK',
               robot_class: Any = None,
               scene: scene_base.SceneBase = None,
               sensors: Sequence[sensor.Sensor] = None,
               task: Any = None,
               env_randomizers: Any = None):
    """Initializes the locomotion gym environment.

    Args:
      gym_config: An instance of LocomotionGymConfig.
      clock: The clock source to be used for the gym env. The clock should
        return a timestamp in seconds. Setting clock == "SIM_CLOCK" will enable
        the built-in simulation clock. For real robot experiments, we can use
        time.time or other clock wall clock sources.
      robot_class: A class of a robot. We provide a class rather than an
        instance due to hard_reset functionality. Parameters are expected to be
        configured with gin.
      scene: An object for managing the robot's surroundings.
      sensors: A list of environmental sensors for observation.
      task: A callable function/class to calculate the reward and termination
        condition. Takes the gym env as the argument when calling.
      env_randomizers: 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().
      client_factory: A function to create a simulation client, it can be a
        pybullet client.

    Raises:
      ValueError: If the num_action_repeat is less than 1.

    """
    self._pybullet_client = None
    self._metric_logger = metric_logger.MetricLogger()
    # TODO(sehoonha) split observation and full-state sensors (b/129858214)

    # Makes sure that close() is always called to flush out the logs to the
    # disk.
    atexit.register(self.close)
    self.seed()
    self._gym_config = gym_config
    if robot_class is None:
      raise ValueError('robot_class cannot be None.')
    self._robot_class = robot_class
    if issubclass(self._robot_class, robot_base.RobotBase):
      self._use_new_robot_class = True
    else:
      self._use_new_robot_class = False
    self._robot = None

    self._scene = scene or scene_base.SceneBase()

    # TODO(sehoonha) change the data structure to dictionary
    self._env_sensors = list(sensors) if sensors is not None else list()

    # TODO(b/152161457): Make logging a standalone module.
    self._log_path = gym_config.log_path
    self._logging = minitaur_logging.MinitaurLogging(self._log_path)
    self._episode_proto = minitaur_logging_pb2.MinitaurEpisode()
    self._data_dir = gym_config.data_dir

    self._task = task

    self._env_randomizers = env_randomizers if env_randomizers else []

    # Simulation related parameters.
    self._num_action_repeat = gym_config.simulation_parameters.num_action_repeat
    self._on_rack = gym_config.simulation_parameters.robot_on_rack
    if self._num_action_repeat < 1:
      raise ValueError('number of action repeats should be at least 1.')
    # TODO(b/73829334): Fix the value of self._num_bullet_solver_iterations.
    self._num_bullet_solver_iterations = int(_NUM_SIMULATION_ITERATION_STEPS /
                                             self._num_action_repeat)

    self._sim_time_step = gym_config.simulation_parameters.sim_time_step_s
    # The sim step counter is an internal varialbe to count the number of
    # pybullet stepSimulation() has been called since last reset.
    self._sim_step_counter = 0

    self._env_time_step = self._num_action_repeat * self._sim_time_step
    # The env step counter accounts for how many times env.step has been
    # called since reset.
    self._env_step_counter = 0

    if clock == SIM_CLOCK:
      self._clock = self._get_sim_time
    else:
      self._clock = clock

    # Creates the bullet client.
    self._is_render = gym_config.simulation_parameters.enable_rendering
    # The wall-clock time at which the last frame is rendered.
    self._last_frame_time = 0.0
    
    if gym_config.simulation_parameters.enable_rendering:
      self._pybullet_client = bullet_client.BulletClient(connection_mode=pybullet.GUI)
      self._pybullet_client.configureDebugVisualizer(
        pybullet.COV_ENABLE_GUI,
        gym_config.simulation_parameters.enable_rendering_gui)
    else:
      self._pybullet_client = bullet_client.BulletClient()
    if gym_config.simulation_parameters.egl_rendering:
      self._pybullet_client.loadPlugin('eglRendererPlugin')

    self._pybullet_client.setAdditionalSearchPath(
      pybullet_data.getDataPath())

    # If enabled, save the performance profile to profiling_path
    # Use Google Chrome about://tracing to open the file
    if gym_config.profiling_path is not None:
      self._profiling_slot = self._pybullet_client.startStateLogging(
          self._pybullet_client.STATE_LOGGING_PROFILE_TIMINGS,
          gym_config.profiling_path)
      self._profiling_counter = 10
    else:
      self._profiling_slot = -1

    # Set the default render options. TODO(b/152161124): Make rendering a
    # standalone module.
    self._camera_target = gym_config.simulation_parameters.camera_target
    self._camera_dist = gym_config.simulation_parameters.camera_distance
    self._camera_yaw = gym_config.simulation_parameters.camera_yaw
    self._camera_pitch = gym_config.simulation_parameters.camera_pitch
    self._render_width = gym_config.simulation_parameters.render_width
    self._render_height = gym_config.simulation_parameters.render_height

    # Loads the environment and robot. Actions space will be created as well.
    self._hard_reset = True
    self._observation_dict = {}
    self.reset()
    self._hard_reset = gym_config.simulation_parameters.enable_hard_reset

    # Construct the observation space from the list of sensors.
    self.observation_space = (
        space_utils.convert_sensors_to_gym_space_dictionary([
            sensor for sensor in self.all_sensors()
            if sensor.get_name() not in self._gym_config.ignored_sensor_list
        ]))

  def __del__(self):
    self.close()

  def _load_old_robot_class(self):
    self._robot = self._robot_class(
        pybullet_client=self._pybullet_client, on_rack=self._on_rack)
    self._action_list = []
    action_upper_bound = []
    action_lower_bound = []
    for action in self._gym_config.actions:
      self._action_list.append(action.name)
      action_upper_bound.append(action.upper_bound)
      action_lower_bound.append(action.lower_bound)
    self.action_space = gym.spaces.Box(
        np.array(action_lower_bound),
        np.array(action_upper_bound),
        dtype=np.float32)

  def _load_new_robot_class(self):
    self._robot = self._robot_class(
        pybullet_client=self._pybullet_client, clock=self._clock)
    self.action_space = self._robot.action_space

  def _load(self):
    self._pybullet_client.resetSimulation()
    self._pybullet_client.setPhysicsEngineParameter(
        numSolverIterations=self._num_bullet_solver_iterations)
    self._pybullet_client.setTimeStep(self._sim_time_step)
    self._pybullet_client.setGravity(0, 0, -10)
    self._pybullet_client.setPhysicsEngineParameter(enableConeFriction=0)

    # Disable rendering during scene loading will speed up simulation.
    if self._is_render:
      self._pybullet_client.configureDebugVisualizer(
          self._pybullet_client.COV_ENABLE_RENDERING, 0)

    # Rebuild the scene.
    self._scene.build_scene(self._pybullet_client)

    # TODO(b/151975607): Deprecate old robot support.
    if self._use_new_robot_class:
      self._load_new_robot_class()
    else:
      self._load_old_robot_class()

    # Check action space.
    if (isinstance(self.action_space, gym.spaces.Box) and
        not np.all(self.action_space.low < self.action_space.high)):
      raise ValueError(f'Action space contains invalid dimensions, '
                       f'action space low = {self.action_space.low}, '
                       f'action space high = {self.action_space.high}')

    for an_object in self._dynamic_objects():
      an_object.set_clock(self._clock)

    # Enable rendering after loading finishes.
    if self._is_render:
      self._pybullet_client.configureDebugVisualizer(
          self._pybullet_client.COV_ENABLE_RENDERING, 1)

  def close(self):
    atexit.unregister(self.close)
    #if self._pybullet_client:
    
    if self._log_path is not None:
      self._logging.save_episode(self._episode_proto)
    for sensor_ in self.all_sensors():
      sensor_.on_terminate(self)
    if self._robot:
      if self._use_new_robot_class:
        self._robot.terminate()
      else:
        self._robot.Terminate()
    if self._pybullet_client:
      self._pybullet_client.disconnect()
      self._pybullet_client = None

  def seed(self, seed=None):
    self.np_random, self.np_random_seed = gym.utils.seeding.np_random(seed)
    return [self.np_random_seed]

  def _dynamic_objects(self):
    """Returns the python objects controlling moving obstacles."""
    if self._scene:
      return self._scene.dynamic_objects
    else:
      return []

  def all_sensors(self):
    """Returns all robot, environmental and dynamic objects sensors."""
    if self._use_new_robot_class:
      all_sensors = list(self._env_sensors)
      if self._robot:
        all_sensors.extend(list(self._robot.sensors))
      for obj in self._dynamic_objects():
        all_sensors.extend(obj.sensors)

      # The new way of adding task specific sensors to the sensor lists.
      if hasattr(self._task, 'sensors'):
        all_sensors.extend(self._task.sensors)
      return all_sensors
    else:
      # This is a workaround due to the issue in b/130128505#comment5
      task_sensor = ([self._task]
                     if isinstance(self._task, sensor.Sensor) else [])
      robot_sensors = []
      if self._robot:
        robot_sensors = self._robot.GetAllSensors()
      return robot_sensors + self._env_sensors + task_sensor

  def sensor_by_name(self, name):
    """Returns the sensor with the given name, or None if not exist."""
    # TODO(b/154162104): Store sensors as dictionary.
    for sensor_ in self.all_sensors():
      if sensor_.get_name() == name:
        return sensor_
    return None

  @gin.configurable('locomotion_gym_env.LocomotionGymEnv.reset')
  def reset(
      self,
      initial_motor_angles=None,
      reset_duration=1.0,
      reset_visualization_camera=True,
  ):
    """Resets the robot's position in the world or rebuild the sim world.

    The simulation world will be rebuilt if self._hard_reset is True.

    Args:
      initial_motor_angles: A list of Floats. The desired joint angles after
        reset. If None, the robot will use its built-in value.
      reset_duration: Float. The time (in seconds) needed to rotate all motors
        to the desired initial values.
      reset_visualization_camera: Whether to reset debug visualization camera on
        reset.

    Returns:
      A numpy array contains the initial observation after reset.
    """
    

    self._env_step_counter = 0
    self._sim_step_counter = 0
    self._last_reset_time = self._clock()
    self._metric_logger.reset_episode()

    # Clear the simulation world and rebuild the robot interface.
    if self._hard_reset:
      self._load()

    # Resets the scene
    self._scene.reset()

    # Resets the robot with the provided init parameters.
    if self._use_new_robot_class:
      self._robot.reset()
    else:
      self._robot.Reset(
          reload_urdf=False,
          default_motor_angles=initial_motor_angles,
          reset_time=reset_duration)

    # Flush the logs to disc and reinitialize the logging system.
    if self._log_path is not None:
      self._logging.save_episode(self._episode_proto)
      self._episode_proto = minitaur_logging_pb2.MinitaurEpisode()
      minitaur_logging.preallocate_episode_proto(self._episode_proto,
                                                 _LOG_BUFFER_LENGTH,
                                                 self._robot)

    # TODO(b/152161124): Move this part to the renderer module.
    if reset_visualization_camera:
      self._pybullet_client.resetDebugVisualizerCamera(self._camera_dist,
                                                       self._camera_yaw,
                                                       self._camera_pitch,
                                                       [0, 0, 0])

    # Create an example last action based on the type of action space.
    self._last_action = space_utils.create_constant_action(self.action_space)

    for s in self.all_sensors():
      s.on_reset(self)

    if self._task and hasattr(self._task, 'reset'):
      self._task.reset(self)

    # Loop over all env randomizers.
    for env_randomizer in self._env_randomizers:
      env_randomizer.randomize_env(self)

    for obj in self._dynamic_objects():
      obj.reset()

    # Initialize the robot base position.
    if self._use_new_robot_class:
      self._last_base_position = self._robot.base_position
    else:
      self._last_base_position = self._robot.GetBasePosition()

    # Resets the observations again, since randomizers might change the env.
    for s in self.all_sensors():
      s.on_reset(self)

    
    self._last_reset_time = self._clock()
    return self._get_observation()

  def _wait_for_rendering(self):
    # Sleep, otherwise the computation takes less time than real time,
    # which will make the visualization like a fast-forward video.
    time_spent = time.time() - self._last_frame_time
    self._last_frame_time = time.time()
    time_to_sleep = self._env_time_step - time_spent
    if time_to_sleep > 0:
      time.sleep(time_to_sleep)

    # Also keep the previous orientation of the camera set by the user.
    [yaw, pitch, dist] = self._pybullet_client.getDebugVisualizerCamera()[8:11]
    self._pybullet_client.resetDebugVisualizerCamera(dist, yaw, pitch,
                                                     self._last_base_position)

  def _step_old_robot_class(self, action):
    self._last_base_position = self._robot.GetBasePosition()
    self._last_action = action

    if self._is_render:
      self._wait_for_rendering()

    for env_randomizer in self._env_randomizers:
      env_randomizer.randomize_step(self)

    self._robot.Step(action)

    if self._profiling_slot >= 0:
      self._profiling_counter -= 1
      if self._profiling_counter == 0:
        self._pybullet_client.stopStateLogging(self._profiling_slot)

    if self._log_path is not None:
      minitaur_logging.update_episode_proto(self._episode_proto, self._robot,
                                            action, self._env_step_counter)
    reward = self._reward()

    for s in self.all_sensors():
      s.on_step(self)

    if self._task and hasattr(self._task, 'update'):
      self._task.update(self)  # TODO(b/154635313): resolve API mismatch

    done = self._termination()
    self._env_step_counter += 1
    # TODO(b/161941829): terminate removed for now, change terminate to other
    # names.
    return self._get_observation(), reward, done, {}

  def _step_new_robot_class(self, action):
    self._last_base_position = self._robot.base_position
    self._last_action = action

    if self._is_render:
      self._wait_for_rendering()

    for env_randomizer in self._env_randomizers:
      env_randomizer.randomize_step(self)

    action = self._robot.pre_control_step(action, self._env_time_step)
    for obj in self._dynamic_objects():
      obj.pre_control_step(autonomous_object.AUTONOMOUS_ACTION)
    for _ in range(self._num_action_repeat):
      self._robot.apply_action(action)
      for obj in self._dynamic_objects():
        obj.update(self.get_time_since_reset(), self._observation_dict)
        obj.apply_action(autonomous_object.AUTONOMOUS_ACTION)

      self._pybullet_client.stepSimulation()
      self._sim_step_counter += 1

      self._robot.receive_observation()
      for obj in self._dynamic_objects():
        obj.receive_observation()

      for s in self.all_sensors():
        s.on_new_observation()

    self._robot.post_control_step()
    for obj in self._dynamic_objects():
      obj.post_control_step()

    if self._profiling_slot >= 0:
      self._profiling_counter -= 1
      if self._profiling_counter == 0:
        self._pybullet_client.stopStateLogging(self._profiling_slot)

    if self._log_path is not None:
      minitaur_logging.update_episode_proto(self._episode_proto, self._robot,
                                            action, self._env_step_counter)
    reward = self._reward()

    for s in self.all_sensors():
      s.on_step(self)

    if self._task and hasattr(self._task, 'update'):
      self._task.update(self)  # TODO(b/154635313): resolve API mismatch

    done = self._termination()
    self._env_step_counter += 1
    # TODO(b/161941829): terminate removed for now, change terminate to other
    # names.
    return self._get_observation(), reward, done, {}

  def step(self, action):
    """Step forward the simulation, given the action.

    Args:
      action: Can be a list of desired motor angles for all motors when the
        robot is in position control mode; A list of desired motor torques. Or a
        list of tuples (q, qdot, kp, kd, tau) for hybrid control mode. The
        action must be compatible with the robot's motor control mode. Also, we
        are not going to use the leg space (swing/extension) definition at the
        gym level, since they are specific to Minitaur.

    Returns:
      observations: The observation dictionary. The keys are the sensor names
        and the values are the sensor readings.
      reward: The reward for the current state-action pair.
      done: Whether the episode has ended.
      info: A dictionary that stores diagnostic information.

    Raises:
      ValueError: The action dimension is not the same as the number of motors.
      ValueError: The magnitude of actions is out of bounds.
    """
    # TODO(b/151975607): Finish the migration and remove old robot class
    # support.
    if self._use_new_robot_class:
      return self._step_new_robot_class(action)
    else:
      return self._step_old_robot_class(action)

  @gin.configurable('locomotion_gym_env.LocomotionGymEnv.render')
  def render(self, mode='rgb_array'):
    
    if mode == 'topdown':
      # Provide ground height if we know it. Otherwise leave it as gin
      # configurable.
      if hasattr(self.scene, 'ground_height'):
        return rendering_utils.render_topdown(
            self._pybullet_client, ground_height=self.scene.ground_height)
      else:
        return rendering_utils.render_topdown(self._pybullet_client)

    if mode != 'rgb_array':
      raise ValueError('Unsupported render mode:{}'.format(mode))

    if self._camera_target is not None:
      target_position = self._camera_target
    else:
      target_position = self._last_base_position
    view_matrix = self._pybullet_client.computeViewMatrixFromYawPitchRoll(
        cameraTargetPosition=target_position,
        distance=self._camera_dist,
        yaw=self._camera_yaw,
        pitch=self._camera_pitch,
        roll=0,
        upAxisIndex=2)
    proj_matrix = self._pybullet_client.computeProjectionMatrixFOV(
        fov=60,
        aspect=float(self._render_width) / self._render_height,
        nearVal=0.1,
        farVal=100.0)
    return rendering_utils.render_image(self._pybullet_client,
                                        self._render_width, self._render_height,
                                        view_matrix, proj_matrix)

  @property
  def scene(self):
    return self._scene

  @property
  def rendering_enabled(self):
    return self._is_render

  @property
  def env_randomizers(self):
    return self._env_randomizers

  @property
  def last_base_position(self):
    return self._last_base_position

  @property
  def gym_config(self):
    return self._gym_config

  def _termination(self):
    if not self._robot.is_safe:
      return True

    if self._task and hasattr(self._task, 'done'):
      return self._task.done(self)  # TODO(b/154635313): resolve API mismatch

    return False

  def _reward(self):
    if self._task:
      return self._task.reward(self)  # TODO(b/154635313): resolve API mismatch
    return 0

  def _get_observation(self):
    """Get observation of this environment from a list of sensors.

    Returns:
      observations: dictionary of sensory observation with sensor name as key
        and corresponding observation in numpy array as value.
    """
    sensors_dict = {}
    for s in self.all_sensors():
      if s.get_name() in self._gym_config.ignored_sensor_list:
        continue

      obs = s.get_observation()
      if isinstance(obs, dict):
        sensors_dict.update(obs)
      else:
        sensors_dict[s.get_name()] = obs

    self._observation_dict = collections.OrderedDict(
        sorted(sensors_dict.items()))
    return self._observation_dict

  def set_time_step(self, num_action_repeat, sim_step=0.001):
    """Sets the time step of the environment.

    Args:
      num_action_repeat: The number of simulation steps/action repeats to be
        executed when calling env.step().
      sim_step: The simulation time step in PyBullet. By default, the simulation
        step is 0.001s, which is a good trade-off between simulation speed and
        accuracy.

    Raises:
      ValueError: If the num_action_repeat is less than 1.
    """
    if num_action_repeat < 1:
      raise ValueError('number of action repeats should be at least 1.')
    self._sim_time_step = sim_step
    self._num_action_repeat = num_action_repeat
    self._env_time_step = sim_step * num_action_repeat
    self._num_bullet_solver_iterations = int(
        _NUM_SIMULATION_ITERATION_STEPS / self._num_action_repeat)
    self._pybullet_client.setPhysicsEngineParameter(
        numSolverIterations=self._num_bullet_solver_iterations)
    self._pybullet_client.setTimeStep(self._sim_time_step)
    if not self._use_new_robot_class:
      self._robot.SetTimeSteps(self._num_action_repeat, self._sim_time_step)

  def _get_sim_time(self):
    """Returns the simulation time since the sim resets."""
    return self._sim_step_counter * self._sim_time_step

  def get_time_since_reset(self):
    """Get the time passed (in seconds) since the last reset.

    Returns:
      Time in seconds since the last reset.
    """
    if self._use_new_robot_class:
      return self._clock() - self._last_reset_time
    else:
      return self._robot.GetTimeSinceReset()

  def get_time(self):
    """Gets the time reading from the clock source."""
    return self._clock()

  @property
  def observation(self):
    return self._observation_dict

  @property
  def pybullet_client(self):
    return self._pybullet_client

  @property
  def robot(self):
    return self._robot

  @property
  def num_action_repeat(self):
    return self._num_action_repeat

  @property
  def sim_time_step(self):
    return self._sim_time_step

  @property
  def env_step_counter(self):
    return self._env_step_counter

  @property
  def hard_reset(self):
    return self._hard_reset

  @property
  def last_action(self):
    return self._last_action

  @property
  def env_time_step(self):
    return self._env_time_step

  @property
  def data_dir(self):
    return self._data_dir

  @property
  def task(self):
    return self._task

  @property
  def robot_class(self):
    return self._robot_class

  @property
  def action_names(self):
    """Name of each action in the action space.

    By default this returns the actions the robot executes (e.g.
    "VELOCITY_elbow_joint"), but env wrappers may override this if they change
    the action space (e.g. if they convert twist to motor commands).

    Returns:
      Tuple of strings, the action names.
    """
    if self._use_new_robot_class:
      return self._robot.action_names
    return self._action_list

  @property
  def metric_logger(self):
    return self._metric_logger