@@ -73,14 +73,8 @@ def __init__(
7373 self .fig = mujoco .MjvFigure ()
7474 mujoco .mjv_defaultFigure (self .fig )
7575
76- # Points for sampling of sensors... dictates smoothness of graph
77- self ._num_pnts = 100
78- self ._data_graph_line_names = []
79- self ._line_datas = []
80-
81- for n in range (0 , len (self .model .sensor_adr ) * 3 ):
82- for i in range (0 , 300 ):
83- self .fig .linedata [n ][2 * i ] = float (- i )
76+ # Counter for number of lines added to plot
77+ self ._line_counter = 0
8478
8579 self .ctx = mujoco .MjrContext (
8680 self .model , mujoco .mjtFontScale .mjFONTSCALE_150 .value )
@@ -98,9 +92,6 @@ def __init__(
9892 self .fig .flg_extend = 1
9993 self .fig .flg_symmetric = 0
10094
101- # Makes the graph to be in autorange
102- self .axis_autorange ()
103-
10495 # load camera from configuration (if available)
10596 pathlib .Path (
10697 self .CONFIG_PATH .parent ).mkdir (
@@ -146,89 +137,41 @@ def __init__(
146137 self ._overlay = {}
147138 self ._markers = []
148139
149- def set_grid_divisions (self , x_div : int , y_div : int , x_axis_time : float = 0.0 , override = False ):
150- if override is False :
151- assert x_axis_time >= self .model .opt .timestep * 50 , "Set [x_axis_time] >= [self.model.opt.timestep * 50], inorder to get a suitable sampling rate"
152- self .fig .gridsize [0 ] = x_div + 1
153- self .fig .gridsize [1 ] = y_div + 1
154- if x_axis_time != 0.0 :
155- self ._num_pnts = x_axis_time / self .model .opt .timestep
156- print ("self._num_pnts: " , self ._num_pnts )
157- if self ._num_pnts > 300 :
158- self ._num_pnts = 300
159- new_x_axis_time = self .model .opt .timestep * self ._num_pnts
160- print (
161- f"Minimum x_axis_time is: { new_x_axis_time } "
162- + " reduce the x_axis_time"
163- f" OR Maximum time_step is: "
164- + f"{ self .model .opt .timestep * self ._num_pnts } "
165- + " increase the timestep"
166- )
167- # assert x_axis_time ==
168- assert 1 <= self ._num_pnts <= 300 , (
169- "num_pnts should be [10,300], it is currently:" ,
170- f"{ self ._num_pnts } " ,
171- )
172- # self._num_pnts = num_pnts
173- self ._time_per_div = (self .model .opt .timestep * self ._num_pnts ) / (
174- x_div
175- )
176- self .set_x_label (
177- xname = f"time/div: { self ._time_per_div } s"
178- + f" total: { self .model .opt .timestep * self ._num_pnts } "
179- )
180-
181- def axis_autorange (self ):
182- """
183- Call this function to auto-range the graph
184- """
185- self .fig .range [0 ][0 ] = 1.0
186- self .fig .range [0 ][1 ] = - 1.0
187- self .fig .range [1 ][0 ] = 1.0
188- self .fig .range [1 ][1 ] = - 1.0
189-
190- def set_graph_name (self , name : str ):
191- assert type (name ) == str , "name is not a string"
192- self .fig .title = name
193-
194- def show_graph_legend (self , show_legend : bool = True ):
195- if show_legend is True :
196- for i in range (0 , len (self ._data_graph_line_names )):
197- self .fig .linename [i ] = self ._data_graph_line_names [i ]
198- self .fig .flg_legend = True
199-
200- def set_x_label (self , xname : str ):
201- assert type (xname ) == str , "xname is not a string"
202- self .fig .xlabel = xname
203-
204- def add_graph_line (self , line_name , line_data = 0.0 ):
140+ def add_graph_line (self , line_name ):
205141 assert (
206142 type (line_name ) == str
207143 ), f"Line_name is not a string: { type (line_name )} "
208- if line_name in self ._data_graph_line_names :
209- print ("line name already exists" )
210- else :
211- self ._data_graph_line_names .append (line_name )
212- self ._line_datas .append (line_data )
144+ if line_name in []:
145+ raise Exception (
146+ "line name already exists"
147+ )
148+
149+ self .fig .linename [self ._line_counter ] = line_name
150+ for i in range (mujoco .mjMAXLINEPNT ):
151+ self .fig .linedata [self ._line_counter ][2 * i ] = float (i )
152+ self .fig .linepnt [self ._line_counter ] = 0
153+ self ._line_counter += 1
213154
214155 def update_graph_line (self , line_name , line_data ):
215- if line_name in self ._data_graph_line_names :
216- idx = self ._data_graph_line_names .index (line_name )
217- self ._line_datas [idx ] = line_data
218- else :
219- raise NameError (
156+ try :
157+ _line_name = line_name .encode ('utf8' )
158+ linenames = self .fig .linename .tolist ()
159+ line_idx = linenames .index (_line_name )
160+ except ValueError :
161+ raise Exception (
220162 "line name is not valid, add it to list before calling update"
221163 )
222164
223- def sensorupdate (self ):
224- pnt = int (mujoco .mju_min (self ._num_pnts , self .fig .linepnt [0 ] + 1 ))
225- for n in range (0 , len (self ._line_datas )):
226- for i in range (pnt - 1 , 0 , - 1 ):
227- self .fig .linedata [n ][2 * i + 1 ] = self .fig .linedata [n ][
228- 2 * i - 1
229- ]
230- self .fig .linepnt [n ] = pnt
231- self .fig .linedata [n ][1 ] = self ._line_datas [n ]
165+ _x = self .fig .linepnt [line_idx ]
166+
167+ if _x < mujoco .mjMAXLINEPNT :
168+ self .fig .linedata [line_idx ][2 * _x ] = _x
169+ self .fig .linedata [line_idx ][2 * _x + 1 ] = float (line_data )
170+ self .fig .linepnt [line_idx ] += 1
171+ else :
172+ for i in range (mujoco .mjMAXLINEPNT - 1 ):
173+ self .fig .linedata [line_idx ][2 * i + 1 ] = self .fig .linedata [line_idx ][2 * (i + 1 )+ 1 ]
174+ self .fig .linedata [line_idx ][- 1 ] = float (line_data )
232175
233176 def update_graph_size (self , size_div_x = None , size_div_y = None ):
234177 if size_div_x is None and size_div_y is None :
@@ -429,7 +372,6 @@ def read_pixels(self, camid=None, depth=False):
429372 mujoco .mjr_render (self .viewport , self .scn , self .ctx )
430373 shape = glfw .get_framebuffer_size (self .window )
431374
432-
433375 if depth :
434376 rgb_img = np .zeros ((shape [1 ], shape [0 ], 3 ), dtype = np .uint8 )
435377 depth_img = np .zeros ((shape [1 ], shape [0 ], 1 ), dtype = np .float32 )
@@ -491,24 +433,11 @@ def update():
491433 self .ctx )
492434
493435 # Handle graph and pausing interactions
494- if (
495- not self ._paused
496- and not self ._hide_graph
497- ):
498- self .sensorupdate ()
499- self .update_graph_size ()
500- mujoco .mjr_figure (
501- self .graph_viewport , self .fig , self .ctx
502- )
503- elif self ._hide_graph and self ._paused :
504- self .update_graph_size ()
505- elif not self ._hide_graph and self ._paused :
436+ if not self ._hide_graph :
506437 mujoco .mjr_figure (
507438 self .graph_viewport , self .fig , self .ctx
508439 )
509- elif self ._hide_graph and not self ._paused :
510- self .sensorupdate ()
511- self .update_graph_size ()
440+ self .update_graph_size ()
512441
513442 glfw .swap_buffers (self .window )
514443 glfw .poll_events ()
0 commit comments