-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
346 lines (319 loc) · 10.1 KB
/
Copy pathmain.py
File metadata and controls
346 lines (319 loc) · 10.1 KB
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
import numpy as np
from plotting import Plot2D, Plot3D, Vline
from copy import copy
import pandas as pd
from scipy import signal
def dict_from_csv(path: str) -> dict[str, np.ndarray]:
"""
Extract timeseries data from csv as dictionary.
:param path: relative path of file, including extension
:returns: data dictionary of timeseries
"""
df = pd.read_csv(path)
data = {}
for col in df.columns:
first = str(df[col].dropna().iloc[0]).strip()
# check if the data is stored as such: "[1.0 2.0 3.0]"
if first.startswith("[") and first.endswith("]"):
# convert to numpy array
parsed_series = df[col].apply(
lambda x: np.fromstring(str(x).strip("[]"), sep=" ")
)
# stack series of 1D arrays into (N, 3) 2D numpy array
data[col] = np.vstack(parsed_series)
else:
data[col] = df[col].to_numpy()
return data
def filter_fftconvolve(
data: np.ndarray, size: int = 50, p: float = 0.5, sig: int = 2
) -> np.ndarray:
"""
filter data using a convolution.
:param data: input array.
:param size:
Low size = less smoothing, cutoff frequency gets higher.
High size = more smoothing, cutoff frequency gets lower
:param p:
Low p = sharper.
High p = leads to more rectangular but smoother result.
:param sig:
Low sig = less smoothing.
High sig = more smoothing.
:returns: filtered array.
"""
window = signal.windows.general_gaussian(size + 1, p=p, sig=sig)
filtered = signal.fftconvolve(data, window, mode="same")
return (np.average(data) / np.average(filtered)) * filtered
def fig_3() -> None:
"""
Generate figure 3, 3D plot of clearance positions for the lateral hopping experiment
"""
data = dict_from_csv("csv/fig_3_lateral_hop.csv")
plot = Plot3D(name="fig_3", N_subplots=1, title=False)
ax = plot.get_next_subplot(xlab="x (m)", ylab="y (m)", zlab=r"z (m)", labelpad=10)
pos_clear_hist = data["pos_clear_hist"]
# air
pos_clear_air = copy(pos_clear_hist)
pos_clear_air[:, 0] = np.where(pos_clear_air[:, 2] > 0, pos_clear_air[:, 0], np.nan)
pos_clear_air[:, 1] = np.where(pos_clear_air[:, 2] > 0, pos_clear_air[:, 1], np.nan)
pos_clear_air[:, 2] = np.clip(pos_clear_hist[:, 2], 0.0, np.inf)
# contact
pos_clear_contact = copy(pos_clear_hist)
pos_clear_contact[:, 0] = np.where(
pos_clear_contact[:, 2] < 0, pos_clear_contact[:, 0], np.nan
)
pos_clear_contact[:, 1] = np.where(
pos_clear_contact[:, 2] < 0, pos_clear_contact[:, 1], np.nan
)
pos_clear_contact[:, 2] = np.where(
pos_clear_contact[:, 2] < 0, pos_clear_contact[:, 2], np.nan
)
ax.plot(
*data["pos_com_hist"].T,
color="b",
label=r"$p_\text{CoM}$",
zorder=100,
)
ax.plot(
*pos_clear_contact.T,
color="purple",
label=r"$p_\text{clearance}$ in contact",
zorder=40,
)
ax.plot(
*pos_clear_air.T,
color="r",
label=r"$p_\text{clearance}$ in air",
zorder=50,
)
h_clear_hist_filtered = filter_fftconvolve(
pos_clear_hist[:, 2], size=2500, p=50, sig=250
)
# get indices of local minima
hop_indices = signal.argrelextrema(h_clear_hist_filtered, np.less, order=100)
footsteps = pos_clear_hist[hop_indices, :]
heading_vec_hist = data["heading_vec_hist"]
footstep_heading_vecs = heading_vec_hist[hop_indices, :]
ax.quiver(
*footsteps.T,
*footstep_heading_vecs.T,
arrow_length_ratio=0.075,
color="orange",
label="heading",
zorder=0,
capstyle="round",
)
ax.set_xticks(np.arange(-0.5, 2.0, 0.5))
ax.set_zticks(np.arange(0.0, 1.5, 0.5))
ax.view_init(azim=225, elev=15)
plot.set_scale_equal(ax)
ax.set_zlim3d(bottom=0)
ax.legend(
loc=(0.65, 0.65),
framealpha=1.0,
borderpad=0.1,
labelspacing=0.1,
)
plot.save_img(extension=".pdf")
def fig_4a() -> None:
"""
Generate figure 4a, clearance height, and electrical power demand for the table jump
"""
vlines = [
Vline(
x=0.85,
y=0.9,
label="lift-off",
ha="right",
va="top",
),
Vline(
x=1.2,
y=0.1,
label="impact",
ha="left",
va="bottom",
),
Vline(
x=2.95,
y=0.1,
label="drop",
ha="right",
va="bottom",
),
Vline(
x=3.25,
y=0.9,
label="impact",
ha="right",
va="top",
),
]
data = dict_from_csv("csv/fig_4a_table_jump.csv")
plot = Plot2D(
name="fig_4a",
N_subplots=3,
title=False,
gridspec_kw={"height_ratios": [2, 1, 1]},
)
xlab = "Time (s)"
q_stamps = data["timestamps"]
ax = plot.get_next_subplot(xlab=xlab, ylab="Height (m)")
ax.plot(
q_stamps,
data["h_com_hist"],
label=r"$h_\text{CoM}$",
color="blue",
)
ax.plot(
q_stamps,
data["h_clear_hist"],
label=r"$h_\text{clearance}$",
color="red",
)
ax.set_ylim(bottom=0, top=2.5)
ax.legend()
if vlines is not None:
plot.plot_vlines(ax, vlines, show_label=True)
# Current and Voltage
m_stamps = data["motor_timestamps"]
m_current_total = data["motor_current"]
m_voltage_total = data["motor_voltage"]
m_power_total = data["motor_power"]
ax = plot.get_next_subplot(xlab=xlab, ylab="Current (A)")
(p1,) = ax.plot(m_stamps, m_current_total, label="Current", color="tab:blue")
ax2 = ax.twinx()
ax2.set_ylabel("Voltage (V)")
(p2,) = ax2.plot(m_stamps, m_voltage_total, label="Bus Voltage", color="tab:red")
ax2.legend(
handles=[p1, p2],
loc="center right",
handlelength=1.0,
borderpad=0.1,
labelspacing=0.1,
)
if vlines is not None:
plot.plot_vlines(ax, vlines, show_label=False)
# Total Power
ax = plot.get_next_subplot(xlab=xlab, ylab="Power (W)")
ax.plot(m_stamps, m_power_total, label="Power (W)", color="tab:orange")
ax.set_yticks(np.arange(-2500, 7500, 2500))
if vlines is not None:
plot.plot_vlines(ax, vlines, show_label=False)
plot.adjust_hspace(0.1)
plot.save_img(extension=".pdf")
def fig_4b() -> None:
"""
Generate figure 4b, clearance height for the table jump repeatability experiment
"""
data = dict_from_csv("csv/fig_4b_table_jump_repeatability.csv")
plot = Plot3D(name="fig_4b", N_subplots=1, title=False)
ax = plot.get_next_subplot(xlab="x (m)", ylab="y (m)", zlab=r"z (m)")
pos_clear_hist = data["pos_clear_hist"]
# split into individual loops
h_clear_hist = pos_clear_hist[:, 2]
N = len(h_clear_hist)
split_indices = []
for k in range(1, N):
if h_clear_hist[k] > 0.1 and h_clear_hist[k - 1] < 0.1:
# going from below to above a certain height tells us the robot has just started jumping
split_indices.append(int(np.clip(k - 10, 0, N)))
trials = np.split(pos_clear_hist, split_indices)
# the first element isn't a full jump
trials.pop(0)
for trial in trials:
# we only need the first half of each trial, the second half is just driving
N_trial_half = int(len(trial) / 2)
trial = trial[:N_trial_half]
# compute mean
N_trial = int(max([len(trial) for trial in trials]))
dim = np.shape(trials[0])[1]
mean = np.zeros((N_trial, dim))
for k in range(N_trial):
trials_k = [item for item in trials if len(item) > k]
mean[k, :] = np.mean([trial[k] for trial in trials_k], axis=0)
ax.plot(
*mean.T,
color="red",
label="mean",
zorder=50,
)
ax.plot(
*pos_clear_hist.T,
color="lightsalmon",
label="trials",
zorder=49,
)
plot.add_legend(ax)
plot.save_img(extension=".pdf")
def fig_5() -> None:
"""
Generate figure 5, whole-body inertia Iyy and angular velocity during the front flip
"""
vlines = [
Vline(
x=0.45,
y=0.6,
label="lift-off",
ha="right",
va="top",
),
Vline(
x=0.9,
y=0.98,
label="impact",
ha="right",
va="top",
),
]
data = dict_from_csv("csv/fig_5_flip.csv")
timestamps = data["timestamps"]
I_hist = data["I_hist"]
h_com_hist = data["h_com_hist"]
plot = Plot2D(
name="fig_5",
N_subplots=3,
title=False,
gridspec_kw={"height_ratios": [2, 1, 1]},
)
xlab = "Time (s)"
ax = plot.get_next_subplot(xlab=xlab, ylab=r"$h_\text{CoM}$ (m)")
ax.plot(
timestamps,
h_com_hist,
label=r"$h_\text{CoM}$",
color="blue",
)
ax.set_ylim(bottom=0, top=1.5)
if vlines is not None:
plot.plot_vlines(ax, vlines, show_label=False)
ax = plot.get_next_subplot(xlab=xlab, ylab="Inertia (kg m²)")
ax.plot(timestamps, I_hist[:, 0], label=r"$I_\text{xx}$")
ax.plot(timestamps, I_hist[:, 1], label=r"$I_\text{yy}$")
ax.plot(timestamps, I_hist[:, 2], label=r"$I_\text{zz}$")
ax.legend(
loc="upper right",
handlelength=1.0,
borderpad=0.1,
labelspacing=0.1,
)
if vlines is not None:
plot.plot_vlines(ax, vlines, show_label=True)
ax = plot.get_next_subplot(xlab=xlab, ylab="Ang. Vel. (rad/s)")
ax.plot(timestamps, data["angvel_base_hist"][:, 0], label=r"$\omega_x$")
ax.plot(timestamps, data["angvel_base_hist"][:, 1], label=r"$\omega_y$")
ax.plot(timestamps, data["angvel_base_hist"][:, 2], label=r"$\omega_z$")
ax.set_yticks(np.arange(-5.0, 25.0, 5.0))
ax.legend(
handlelength=1.0,
borderpad=0.1,
labelspacing=0.1,
)
if vlines is not None:
plot.plot_vlines(ax, vlines, show_label=False)
plot.save_img(extension=".pdf")
if __name__ == "__main__":
fig_3()
fig_4a()
fig_4b()
fig_5()