From 301b97e6abe88d65adae2eada39096f9e3c61025 Mon Sep 17 00:00:00 2001 From: Nathaniel Hargrave Date: Tue, 4 Aug 2026 00:29:37 -0400 Subject: [PATCH] Polar changes --- .../science_python/make_graph.py | 20 ++++++++++--------- .../science_sensors/include/science_node.hpp | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/HW-Devices/science_python/science_python/make_graph.py b/src/HW-Devices/science_python/science_python/make_graph.py index 78ac307c..8f023de5 100755 --- a/src/HW-Devices/science_python/science_python/make_graph.py +++ b/src/HW-Devices/science_python/science_python/make_graph.py @@ -8,21 +8,23 @@ def plot_set(ax: plt.Axes, x_deg: list[int], readings: list[int], title: str): - x_deg = list(range(len(readings))) x_rad = np.deg2rad(x_deg) # --- Model: A cos^2(x - phi) + C --- - def cos2_model(x, A, phi, C): - return A * np.cos(x - phi) ** 2 + C + def cos2_model(x, A, B, phi, C): + return A * np.cos(B * x - phi) ** 2 + C # --- Initial parameter guesses --- A_guess = np.max(readings) - np.min(readings) + B_guess = 1 phi_guess = np.deg2rad(50) C_guess = np.min(readings) - popt, _ = curve_fit(cos2_model, x_rad, readings, p0=[A_guess, phi_guess, C_guess]) + popt, _ = curve_fit( + cos2_model, x_rad, readings, p0=[A_guess, B_guess, phi_guess, C_guess] + ) - A_fit, phi_fit, C_fit = popt + A_fit, B_fit, phi_fit, C_fit = popt # Convert phase to degrees phase_deg = np.rad2deg(phi_fit) @@ -30,11 +32,11 @@ def cos2_model(x, A, phi, C): # --- Generate smooth fit curve --- x_fit_deg = np.linspace(np.min(x_deg), np.max(x_deg), 1000) x_fit_rad = np.deg2rad(x_fit_deg) - y_fit = cos2_model(x_fit_rad, A_fit, phi_fit, C_fit) + y_fit = cos2_model(x_fit_rad, A_fit, B_fit, phi_fit, C_fit) # --- Plot --- - ax.scatter(x_deg, readings, label="Data") - ax.plot(x_fit_deg, y_fit, label="Cos² Fit") + ax.scatter([i * B_fit for i in x_deg], readings, label="Data") + ax.plot([i * B_fit for i in x_fit_deg], y_fit, label="Cos² Fit") ax.axvline(phase_deg, linestyle="--", label=f"Phase Offset = {phase_deg:.2f}°") ax.set_xlabel("Angle (degrees)") @@ -67,7 +69,7 @@ def plot_file(ax: plt.Axes, filename: str, title: str): ax[0].text( 0, -0.2, - f"Phase difference: {math.fabs(phase1 - phase2):.2f} deg\nConcentration: {conc:.2f} g/mL\nSpecific rotation: {math.fabs(phase1 - phase2) * 100 / (40 * conc):.4f} deg*cm^2/g", + f"Phase difference: {math.fabs(phase1 - phase2):.2f} deg\nConcentration: {conc:.2f} g/mL\nSpecific rotation: {math.fabs(phase1 - phase2) * 100 / (43.3 * conc):.4f} deg*cm^2/g", transform=ax[0].transAxes, verticalalignment="top", bbox=props, diff --git a/src/HW-Devices/science_sensors/include/science_node.hpp b/src/HW-Devices/science_sensors/include/science_node.hpp index a4c80504..6fbd0e6c 100644 --- a/src/HW-Devices/science_sensors/include/science_node.hpp +++ b/src/HW-Devices/science_sensors/include/science_node.hpp @@ -15,7 +15,7 @@ #include "interfaces/srv/run_polarimeter.hpp" #include "socket_can.hpp" -#define SCAN_STEPS 48 +#define SCAN_STEPS 96 class ScienceNode : public rclcpp::Node { public: