Repository navigation
Expand file tree
/
Copy pathplot_pwm_thrust_map_3d.py
More file actions
82 lines (68 loc) · 2.92 KB
/
Copy pathplot_pwm_thrust_map_3d.py
File metadata and controls
82 lines (68 loc) · 2.92 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
#!/usr/bin/env python3
"""3D plot of thrust/torque vs (a_cmd_us, b_cmd_us) from pwm_thrust_grid_merge.py output."""
from __future__ import annotations
import argparse
import csv
from pathlib import Path
from typing import Dict, List
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 (registers 3d projection)
def read_merged_csv(path: Path) -> Dict[str, np.ndarray]:
with path.open(newline="") as handle:
reader = csv.DictReader(handle)
cols: Dict[str, List[float]] = {
"a_cmd_us": [], "b_cmd_us": [], "voltage_v_mean": [],
"thrust_mag_n": [], "torque_mag_nm": [], "fz_mean": [], "tz_mean": [],
}
for row in reader:
for key in cols:
cols[key].append(float(row[key]))
return {k: np.array(v) for k, v in cols.items()}
def plot_surface(ax, x, y, z, c, title, zlabel, cmap="viridis"):
unique_x, unique_y = np.unique(x), np.unique(y)
if len(unique_x) >= 3 and len(unique_y) >= 3:
try:
surf = ax.plot_trisurf(x, y, z, cmap=cmap, linewidth=0.2, antialiased=True)
fig = ax.get_figure()
fig.colorbar(surf, ax=ax, shrink=0.6, label=zlabel)
except Exception:
sc = ax.scatter(x, y, z, c=c, cmap=cmap, s=30)
ax.get_figure().colorbar(sc, ax=ax, shrink=0.6, label="voltage_v_mean")
else:
sc = ax.scatter(x, y, z, c=c, cmap=cmap, s=30)
ax.get_figure().colorbar(sc, ax=ax, shrink=0.6, label="voltage_v_mean")
ax.set_xlabel("a_cmd_us (pwm1)")
ax.set_ylabel("b_cmd_us (pwm2)")
ax.set_zlabel(zlabel)
ax.set_title(title)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("merged_csv", type=Path)
parser.add_argument("--force-axis", choices=["Fz", "Fmag"], default="Fmag")
parser.add_argument("--torque-axis", choices=["Tz", "Tmag"], default="Tmag")
parser.add_argument("--save", type=Path, default=None)
parser.add_argument("--dpi", type=int, default=200)
args = parser.parse_args()
data = read_merged_csv(args.merged_csv)
thrust = data["fz_mean"] if args.force_axis == "Fz" else data["thrust_mag_n"]
torque = data["tz_mean"] if args.torque_axis == "Tz" else data["torque_mag_nm"]
fig = plt.figure(figsize=(13, 6))
ax1 = fig.add_subplot(121, projection="3d")
plot_surface(
ax1, data["a_cmd_us"], data["b_cmd_us"], thrust, data["voltage_v_mean"],
f"Thrust ({args.force_axis}) vs PWM", args.force_axis,
)
ax2 = fig.add_subplot(122, projection="3d")
plot_surface(
ax2, data["a_cmd_us"], data["b_cmd_us"], torque, data["voltage_v_mean"],
f"Torque ({args.torque_axis}) vs PWM", args.torque_axis,
)
fig.tight_layout()
if args.save:
fig.savefig(args.save, dpi=args.dpi)
print(f"saved -> {args.save}")
else:
plt.show()
if __name__ == "__main__":
main()