Skip to content

Commit

Permalink
Remove display_frequency, back to visualize
Browse files Browse the repository at this point in the history
- display_frequency may be a more elegant approach overall, but
  it's redundant with the generate function parameter
  'update_display', which everything in the repo uses and is common
  between different interfaces.
    - so, reverting for now. could be changed back to display_frequency
    in a more complete PR
- also the new check for passing invalid joint_names into the interface
  caught a bug in the rover_vision.py example, it is fixed now
  • Loading branch information
studywolf committed Nov 19, 2023
1 parent 9a46713 commit cc35737
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
54 changes: 23 additions & 31 deletions abr_control/interfaces/mujoco.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ class Mujoco(Interface):
such as: number of joints, number of links, mass information etc.
dt: float, optional (Default: 0.001)
simulation time step in seconds
display_frequency: int, optional (Default: 1)
How often to render the frame to display on screen.
EX:
a value of 1 displays every sim frame
a value of 5 displays every 5th frame
a value of 0 runs the simulation offscreen
render_params : dict, optional (Default: None)
visualize: boolean, optional (Default: True)
turns visualization on or off
offscreen_render_params : dict, optional (Default: None)
'cameras' : list
camera ids, the order the camera output is appended in
'resolution' : list
Expand All @@ -38,32 +34,32 @@ def __init__(
self,
robot_config,
dt=0.001,
display_frequency=1,
render_params=None
visualize=True,
offscreen_render_params=None
):
super().__init__(robot_config)

self.robot_config = robot_config
self.dt = dt # time step
self.display_frequency = display_frequency
self.render_params = render_params
self.visualize = visualize
self.offscreen_render_params = offscreen_render_params
self.count = 0 # keep track of how many times send forces is called
self.timestep = 0

self.camera_feedback = np.array([])
if self.render_params:
if self.offscreen_render_params:
# if not empty, create array for storing rendered camera feedback
self.camera_feedback = np.zeros(
(
self.render_params["resolution"][0],
self.render_params["resolution"][1]
* len(self.render_params["cameras"]),
self.offscreen_render_params["resolution"][0],
self.offscreen_render_params["resolution"][1]
* len(self.offscreen_render_params["cameras"]),
3,
)
)
self.subpixels = np.product(self.camera_feedback.shape)
if "frequency" not in self.render_params.keys():
self.render_params["frequency"] = 1
if "frequency" not in self.offscreen_render_params.keys():
self.offscreen_render_params["frequency"] = 1



Expand Down Expand Up @@ -126,7 +122,7 @@ def connect(self, joint_names=None, camera_id=-1):
)

# create the visualizer
if self.display_frequency > 0:
if self.visualize:
self.viewer = mujoco_viewer.MujocoViewer(self.model, self.data)
# set the default display to skip frames to speed things up
self.viewer._render_every_frame = False
Expand All @@ -135,12 +131,12 @@ def connect(self, joint_names=None, camera_id=-1):
self.viewer.cam.fixedcamid = camera_id
self.viewer.cam.type = mujoco.mjtCamera.mjCAMERA_FIXED

if self.render_params is not None:
if self.offscreen_render_params is not None:
self.offscreen = mujoco_viewer.MujocoViewer(
self.model,
self.data,
height=self.render_params["resolution"][1],
width=self.render_params["resolution"][0],
height=self.offscreen_render_params["resolution"][1],
width=self.offscreen_render_params["resolution"][0],
mode="offscreen",
)
# set the default display to skip frames to speed things up
Expand All @@ -151,7 +147,7 @@ def connect(self, joint_names=None, camera_id=-1):

def disconnect(self):
"""Stop and reset the simulation"""
if self.display_frequency > 0:
if self.visualize:
self.viewer.close()

print("MuJoCO session closed...")
Expand Down Expand Up @@ -187,8 +183,6 @@ def send_forces(self, u, update_display=True, use_joint_dyn_addrs=True):
----------
u: np.array
the torques to apply to the robot [Nm]
update_display: boolean, Optional (Default:True)
toggle for updating display
use_joint_dyn_addrs: boolean
set false to update the control signal for all actuators
"""
Expand All @@ -204,9 +198,7 @@ def send_forces(self, u, update_display=True, use_joint_dyn_addrs=True):
self.count += self.dt
self.timestep = int(self.count / self.dt)

freq_display = not self.timestep % self.display_frequency

if freq_display and update_display:
if self.visualize and update_display:
self.viewer.render()

def set_external_force(self, name, u_ext):
Expand Down Expand Up @@ -257,10 +249,10 @@ def get_feedback(self):
self.q = np.copy(self.data.qpos[self.joint_pos_addrs])
self.dq = np.copy(self.data.qvel[self.joint_vel_addrs])

if self.render_params:
res = self.render_params['resolution']
if self.timestep % self.render_params["frequency"] == 0:
for ii, jj in enumerate(self.render_params["cameras"]):
if self.offscreen_render_params:
res = self.offscreen_render_params['resolution']
if self.timestep % self.offscreen_render_params["frequency"] == 0:
for ii, jj in enumerate(self.offscreen_render_params["cameras"]):
glfw.make_context_current(self.offscreen.window)
self.camera_feedback[
:, ii * res[1] : (ii + 1) * res[1]
Expand Down
4 changes: 2 additions & 2 deletions examples/Mujoco/rover_vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class ExitSim(Exception):
interface = Mujoco(
robot_config=rover,
dt=dt,
render_params={
offscreen_render_params={
"cameras": [4, 1, 3, 2], # camera ids and order to render
"resolution": [32, 32],
"frequency": 1, # render images from cameras every time step
},
)
interface.connect(joint_names=["steering_wheel", "rear_differential"])
interface.connect(joint_names=["steering_wheel", "RR_joint"])

# set up the target position
target = np.array([-0.0, 0.0, 0.2])
Expand Down

0 comments on commit cc35737

Please sign in to comment.