Skip to content

Commit b10f9cf

Browse files
committed
Merge branch 'main' into dev
2 parents a668e70 + 96d22e3 commit b10f9cf

File tree

3 files changed

+84
-59
lines changed

3 files changed

+84
-59
lines changed

.vscode/project-words.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ logfilewarninglist
482482
lognormal
483483
logscale
484484
logseries
485+
logspace
485486
longtable
486487
longtblr
487488
lookat
@@ -536,6 +537,7 @@ modificationdate
536537
modname
537538
moredelim
538539
morekeywords
540+
mpatches
539541
mpimg
540542
MRO
541543
msg
@@ -914,6 +916,7 @@ Weissl
914916
worldbody
915917
WSL
916918
wxyz
919+
xaxis
917920
xbm
918921
xcb
919922
xcolor
@@ -932,6 +935,7 @@ xyaxes
932935
xyflow
933936
xyzw
934937
yarl
938+
yaxis
935939
Yinyang
936940
ylabel
937941
ylim

examples/z_ec_course/A3_plot_function.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@
3232
TARGET_POSITION = [5, 0, 0.5]
3333

3434

35-
def show_xpos_history(history: list[float]) -> None:
35+
def show_xpos_history(
36+
history: list[float],
37+
*,
38+
save: bool = True,
39+
show: bool = True,
40+
) -> None:
3641
# Initialize world to get the background
3742
mj.set_mjcb_control(None)
3843
world = OlympicArena(
@@ -110,33 +115,43 @@ def show_xpos_history(history: list[float]) -> None:
110115
correct_collision_with_floor=False,
111116
)
112117

113-
last_value = None
114-
for i in range(len(pos_data)):
115-
position = pos_data[i]
116-
if last_value is not None:
117-
distance = np.abs(np.array(position - last_value)) / 2
118-
else:
119-
distance = np.array((0.01, 0.01, 0.01))
120-
last_value = position
121-
122-
distance_as_size: str = (
123-
f'"{(distance[0] + 0.01):.2f} 0.05 {(distance[2] + 0.01):.2f}"'
118+
# Draw the path of the robot
119+
for i in range(1, len(pos_data)):
120+
# Get the two points to draw the distance between
121+
pos_i = pos_data[i]
122+
pos_j = pos_data[i - 1]
123+
124+
# Size of the box to represent the distance
125+
distance = pos_i - pos_j
126+
minimum_size = 0.05
127+
geom_size = np.array([
128+
max(abs(distance[0]) / 2, minimum_size),
129+
max(abs(distance[1]) / 2, minimum_size),
130+
max(abs(distance[2]) / 2, minimum_size),
131+
])
132+
geom_size_str: str = f"{geom_size[0]} {geom_size[1]} {geom_size[2]}"
133+
134+
# Position the box in the middle of the two points
135+
half_way_point = (pos_i + pos_j) / 2
136+
geom_pos_str = (
137+
f"{half_way_point[0]} {half_way_point[1]} {half_way_point[2]}"
124138
)
125139

126140
path_box = rf"""
127141
<mujoco>
128142
<worldbody>
129143
<geom name="yellow_sphere"
130144
type="box"
131-
size={distance_as_size}
145+
pos="{geom_pos_str}"
146+
size="{geom_size_str}"
132147
rgba="1 1 0 0.9"
133148
/>
134149
</worldbody>
135150
</mujoco>
136151
"""
137152
world.spawn(
138153
mj.MjSpec.from_string(path_box),
139-
position=position + (adjustment * 1.25),
154+
position=(adjustment * 1.25),
140155
correct_collision_with_floor=False,
141156
)
142157

@@ -188,5 +203,11 @@ def show_xpos_history(history: list[float]) -> None:
188203
# Title
189204
plt.title("Robot Path in XY Plane")
190205

206+
# Save the figure
207+
if save:
208+
fig_path = DATA / "robot_path.png"
209+
plt.savefig(fig_path, bbox_inches="tight", dpi=300)
210+
191211
# Show results
192-
plt.show()
212+
if show:
213+
plt.show()

0 commit comments

Comments
 (0)