From ac475e1b812dc52f5df44ae9b593756cac711874 Mon Sep 17 00:00:00 2001 From: Markus Koller Date: Mon, 20 Feb 2023 18:27:14 +0100 Subject: [PATCH] Includes rotation rates to plots --- src/csv_threading.py | 30 ++++++++++++++++++++---------- src/user_interface.py | 15 ++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/csv_threading.py b/src/csv_threading.py index 59e9864..bd3dd75 100644 --- a/src/csv_threading.py +++ b/src/csv_threading.py @@ -156,24 +156,24 @@ def plot_field_sequence(array, width, height): # create plot of fixed size (pix figure = plt.Figure(figsize=(width*px, height*px), dpi=fig_dpi) # create figure with correct size # noinspection PyTypeChecker,SpellCheckingInspection - axes = figure.subplots(3, sharex=True, sharey=True, gridspec_kw={'hspace': 0.4}) # create subplots with shared axes + axes = figure.subplots(4, sharex=True, sharey=False, gridspec_kw={'hspace': 0.4}) # create subplots with shared axes figure.suptitle("Magnetic Field Sequence") # set figure title # modify data to show instantaneous jumps in field to reflect test bench operation - new_array = np.array([[0, 0, 0, 0]], dtype=float) # initialize modified array, zeros to show start from no fields + new_array = np.array([[0, 0, 0, 0, 0]], dtype=float) # initialize modified array, zeros to show start from no fields - last_values = [0, 0, 0] # [x,y,z] field values from last data point (zero here), used to create step in data - for row in array[:, 0:4]: # go through each row in the original array + last_values = [0, 0, 0, 0] # [x,y,z, rr] field values / rot rate from last data point (zero here) + for row in array[:, 0:5]: # go through each row in the original array # create extra datapoint at current timestamp, with field values from last to create a "step" in the plot: new_array = np.append(new_array, [[row[0], *last_values]], axis=0) new_array = np.append(new_array, [row], axis=0) # add actual datapoint for current timestamp - last_values = row[1:4] # save values from current timestamp for next - new_array = np.append(new_array, [[new_array[-1, 0], 0, 0, 0]], axis=0) # append last datapoint with 0 fields + last_values = row[1:5] # save values from current timestamp for next + new_array = np.append(new_array, [[new_array[-1, 0], 0, 0, 0, 0]], axis=0) # append last datapoint with 0 fields - # extract data and plot: + # extract data and plot magnetic fields: t = new_array[:, 0] # extract time column - for i in [0, 1, 2]: # go through all three axes + for i in [0, 1, 2]: # go through all three axes plus rotation rate data = new_array[:, i + 1] * 1e6 # extract field column of this axis and convert to microtesla min_val, max_val = g.CAGE_DEVICE.axes[i].max_comp_field * 1e6 # get limits of achievable field plot = axes[i] # get appropriate subplot @@ -189,9 +189,19 @@ def plot_field_sequence(array, width, height): # create plot of fixed size (pix plot.text(t[-1], min_val, "min", horizontalalignment='center', color='r') plot.set_title(g.AXIS_NAMES[i], size=10) # set subplot title (e.g. "X-Axis") + # plot rotation rate + plot = axes[3] + plot.plot(t, new_array[:, 4], linestyle='solid', marker='.') # plot data + # set ylim of magnetic field axis to same value + ylim_mag = ([min(axes[0].get_ylim()), max(axes[0].get_ylim()), + min(axes[1].get_ylim()), max(axes[1].get_ylim()), + min(axes[2].get_ylim()), max(axes[2].get_ylim())]) + for i in range(3): axes[0].set_ylim(min(ylim_mag),max(ylim_mag)) + # set shared axis labels: - axes[2].set_xlabel("Time (s)") - axes[1].set_ylabel("Magnetic Field (\u03BCT)") + axes[2].set_xlabel("Time [s])") + axes[1].set_ylabel("Magnetic Field [\u03BCT]") + axes[3].set_ylabel("Rate [°/s]") return figure # return the created figure to be inserted somewhere diff --git a/src/user_interface.py b/src/user_interface.py index 8caa5bd..50a5c49 100644 --- a/src/user_interface.py +++ b/src/user_interface.py @@ -700,14 +700,14 @@ class ExecuteCSVMode(Frame): def generate_load_csv_sequence(self): # Generate and load csv sequence # Generate rotation data - t, x, y, z = self.generate_csv_sequence() + t, x, y, z, rr = self.generate_csv_sequence() # Write data into array - temp_array = np.ndarray((len(t), 4)) + temp_array = np.ndarray((len(t), 5)) for i in range(len(t)): - temp_array[i] = [t[i], x[i], y[i], z[i]] + temp_array[i] = [t[i], x[i], y[i], z[i], rr[i]] self.sequence_array = temp_array try: # try to check the values and display the plot - csv_threading.check_array_ok(self.sequence_array) # check for values exceeding limits + csv_threading.check_array_ok(self.sequence_array[:, 0:4]) # check for values exceeding limits self.sequence_array_ok = True # Has nothing to do with limits. Just means the data was parsed self.display_plot() # plot data and display except Exception as e: # something went wrong @@ -824,16 +824,17 @@ class ExecuteCSVMode(Frame): x = np.zeros(arr_len) # [T] y = np.zeros(arr_len) # [T] z = np.zeros(arr_len) # [T] + rot_rate = np.zeros(arr_len) # [T] # Calculate vectors for i in range(arr_len): t[i] = i * rot_time_step - rot_rate = func(t[i]) - th = th + rot_rate * np.pi / 180 * rot_time_step + rot_rate[i] = func(t[i]) + th = th + rot_rate[i] * np.pi / 180 * rot_time_step x[i] = (rot_center[0] + rot_mag * np.cos(th) * a[0] + rot_mag * np.sin(th) * b[0]) * 1e-6 y[i] = (rot_center[1] + rot_mag * np.cos(th) * a[1] + rot_mag * np.sin(th) * b[1]) * 1e-6 z[i] = (rot_center[2] + rot_mag * np.cos(th) * a[2] + rot_mag * np.sin(th) * b[2]) * 1e-6 # Return vectors - return t, x, y, z + return t, x, y, z, rot_rate class CalibrateAmbientField(Frame):