import pathlib
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams.update({"text.usetex": True, "font.family": "Times"})
def main():
crate_root = pathlib.Path(__file__).parent / ".."
output_dir = crate_root / "output"
df = pd.read_csv(output_dir / "step_response.csv")
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(df["time"], df["p"], "b", label="Measurement")
ax.plot(df["time"], df["sp"], "--r", label="Setpoint")
ax.set_xlabel("Time (s)", fontsize=13)
ax.set_ylabel("Position (m)", fontsize=13)
FINAL_VALUE = 1
MAG = 1
ax.fill_between(
df["time"],
(FINAL_VALUE + MAG * 0.02),
(FINAL_VALUE - MAG * 0.02),
color="g",
alpha=0.5,
label=r"Envelope for 2\% settling time",
)
ax.axhline(
FINAL_VALUE + MAG * 0.2,
color="k",
label=r"20\% Overshoot Goal",
)
ax.text(
0,
1.75,
r"System: $\ddot{x} + 2\zeta\omega_n\dot{x} +\omega_n^2x = f$",
fontsize=12,
alpha=0.5,
color="k",
)
ax.set_title("Step Response of Mass Spring Damper under PID Control", fontsize=15)
ax.set_ylim(top=2.0)
ax.legend(loc="upper right")
ax.grid()
plt.show()
fig.savefig(output_dir / "step_response.png")
if __name__ == "__main__":
main()