How to live graph in python [mjvFigure]? #298
-
I am trying to build a graphing tool built on top of mujoco-python-viewer for pid tuning ,etc. Intending to use it like this: model = mujoco.MjModel.from_xml_path("path_to_file")
data = mujoco.MjData(model)
while True:
viewer.graph_1(self.data.qpos[0])
viewer.graph_1(actuation_signal) # actuation_signal is a data stream of float values (ex: from a loop)
viewer.render() I have looked at
|
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 8 replies
-
It's probably easier and more pythonic to simply use a Python library to do the plotting. For example we use animated |
Beta Was this translation helpful? Give feedback.
-
I agree with @JeanElsner's answer, but if you do want to use mjvFigure, it should be possible. I'd follow what happens in simulate.cc to
|
Beta Was this translation helpful? Give feedback.
-
Thanks for your answers! @nimrod-gileadi This is the code for the graph I have used(I can provide the entire code, but most of it might not be necessary I think): def __init__(self, model, data):
self.fig = mujoco.MjvFigure()
# Manual
self.fig.range[0][0] = -5
self.fig.range[0][1] = 5
self.fig.range[1][0] = -5
self.fig.range[1][1] = 5
self.fig.flg_extend = 0
self.fig.flg_symmetric = 0
self.pid_viewport = mujoco.MjrRect(0, 0, 800, 200)
def sensorupdate(self):
num_sensors = self.model.nsensor
sensor_data = self.data.qpos
adr = self.model.sensor_adr
print("num_sensors: ", num_sensors, sensor_data, adr)
p = self.fig.linepnt[0]
pnt = mujoco.mju_min(201, self.fig.linepnt[0] + 1)
# print(f"pnt: {pnt}")
for n in range(0, 3):
# shift data
for i in range(1, 0):
self.fig.linedata[n][2 * i + 1] = self.fig.linedata[n][2 * i - 1]
print(f"n:{n} i:{i}")
# assign new
self.fig.linepnt[n] = pnt
self.fig.linedata[n][1] = sensor_data[n]
print(self.fig.linedata)
# show sensor figure
def sensorshow(self, rect):
mujoco.mjr_figure(self.pid_viewport, self.fig, self.callbacks.ctx)
def render(self): #This is called in each loop
self.sensorupdate()
self.sensorshow(rect=self.pid_viewport)
mujoco.mjr_overlay(
mujoco.mjtFontScale.mjFONTSCALE_100,
mujoco.mjtGridPos.mjGRID_BOTTOMLEFT,
self.pid_viewport,
"t1",
"t2",
self.callbacks.ctx,
) mjvFigure-problem.mp4 |
Beta Was this translation helpful? Give feedback.
-
@nimrod-gileadi @yuvaltassa Thanks for the support! I was able to figure out how it works.
Will update the link to the repo. Preview:
mujoco_graph_working.mp4 |
Beta Was this translation helpful? Give feedback.
-
Excellent! Very happy you figured this out, and apologies again for the lacking documentation. |
Beta Was this translation helpful? Give feedback.
-
@yuvaltassa no worries 😅 .Mujoco with python is still new. I'm sure it will take time to fully mature. This branch has a rudimentary example in examples/graphs.py |
Beta Was this translation helpful? Give feedback.
I agree with @JeanElsner's answer, but if you do want to use mjvFigure, it should be possible.
Please share the code you used to try to replicate
profiler_update
.I'd follow what happens in simulate.cc to
figtimer
, and check that your code does all of it:figtimer.linepnt
andfigtimer.linedata
are set.profilershow
is called, which callsmjr_figure
with a particular viewport and the render context. This would bemujoco.mjr_figure
in python.