-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadCellMath_old.py
More file actions
143 lines (111 loc) · 5.34 KB
/
Copy pathLoadCellMath_old.py
File metadata and controls
143 lines (111 loc) · 5.34 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
import pandas as pd
import numpy as np
from scipy.stats import zscore
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import simpledialog, messagebox, filedialog
import os
class Load_cell_math:
def __init__(self, lowerthrust: float, upperthrust: float, maxthrust_precent: float, spacing: int):
self.lowerthrust = lowerthrust
self.upperthrust = upperthrust
self.maxthrust_precent = maxthrust_precent
self.spacing = spacing
def calculations(self):
# Load CSV data file
filepath = filedialog.askopenfilename(
initialdir="/",
title="Select a file",
filetypes=(
("CSV Files", "*.csv"), ('All', '*.*')
)
)
try:
# Read the data from the CSV file
dataTable = pd.read_csv(filepath, skiprows=1) # Skip the first row if it contains headers
except FileNotFoundError:
messagebox.showerror("Error", "File Not Found")
return
except Exception as e:
messagebox.showerror("Error", "Error Loading File")
return
# Getting user input Variables
root = tk.Tk()
root.withdraw() # Hide the main window
MaxThrust = float(simpledialog.askstring("Input", "What is the expected max thrust (in Newtons):", initialvalue="0"))
if MaxThrust is None:
return None, None, None, None, None, None, None
try:
# Convert the user input to the desired variable type
MaxThrustInput = MaxThrust
TooLarge = (MaxThrustInput) * float(self.maxthrust_precent) #1000000
except ValueError:
messagebox.showerror("Error", "Invalid input for max thrust")
return None, None, None, None, None, None, None
# Getting Data
# Extract time (first column), thrust (third column), and pressure (fifth column) data
time_sec = dataTable.iloc[:, 0].values
thrust_N = dataTable.iloc[:, 2].values
pressure_psi = dataTable.iloc[:, 4].values
L = len(thrust_N)
time_ms = time_sec / 1000
# Find indices where thrust is greater than upperthrust and drops below lowerthrust
#indices_above_20N = np.argwhere(thrust_N > self.upperthrust)
#indices_below_20N = np.argwhere(thrust_N < self.lowerthrust)
indices_above_20N = np.array([i for i in thrust_N if i > self.upperthrust],dtype=int)
indices_below_20N = np.array([i for i in thrust_N if i < self.lowerthrust],dtype=int)
print(f"Above 20: {thrust_N[indices_above_20N]}")
print(f"Below 20: {thrust_N[indices_below_20N]}")
IAL = len(indices_above_20N)
IBL = len(indices_below_20N)
# Process indices to remove outliers (not a perfect method but the original filter was not working)
indices_above_20Nfixed = []
#array of z scores for each data point
z_scores = zscore(thrust_N)
#filters out points whose z score is large
indices_above_20Nfixed = np.argwhere(np.array(thrust_N[np.abs(z_scores) <= 3]))
'''for i in range(IAL): old outlier filter
k = indices_above_20N[i]
# Make sure we don't go out of bounds
start_idx = max(0, k - self.spacing)
end_idx = min(L, k + self.spacing + 1)
iThrustData = thrust_N[start_idx:end_idx]
DumbBig = thrust_N[k]
if DumbBig > TooLarge:
continue
# Calculate differences
diffthrust = np.diff(iThrustData)
if len(diffthrust) > 0 and np.max(diffthrust) > 1000:
continue
# Calculate average
iAvg = np.mean(iThrustData)
if iAvg > 20:
indices_above_20Nfixed.append(indices_above_20N[i])
indices_above_20Nfixed = np.array(indices_above_20Nfixed) '''
if len(indices_above_20Nfixed) == 0:
messagebox.showwarning("warning",'There were not enough data points to gather data now displaying the max pressure and thrust recorded')
if len(indices_above_20N) > 0:
max_idx = indices_above_20N[-1]
messagebox.showinfo(f'Max Thrust: {thrust_N[max_idx]:.2f} N, Max Pressure: {pressure_psi[max_idx]:.2f} psi') #this displays all the info in the title of the window so it might get cut off
return
# Check if the thrust drops below lowerthrust in the data
if len(indices_below_20N) > 0:
# Select points around when thrust exceeds upperthrust
start_A20N = max(indices_above_20Nfixed[0] - self.spacing, 0)
end_index_A20N = min(indices_above_20Nfixed[0] + self.spacing, L - 1)
# Process end indices
LDB2 = indices_above_20Nfixed[-1]
BeginNew2 = LDB2 + 1
TimeofBurn = time_ms[indices_above_20Nfixed[-1]] - time_ms[indices_above_20Nfixed[0]]
end_A20N = min(LDB2 + 200, len(thrust_N) - 1)
# Filtered data based on conditions
filtered_time_above_20N = time_ms[start_A20N:end_A20N+1]
filtered_thrust_above_20N = thrust_N[start_A20N:end_A20N+1]
filtered_pressure_above_20N = pressure_psi[start_A20N:end_A20N+1]
# Calculate Impulse (Riemann sum of thrust)
if len(filtered_time_above_20N) > 1:
dt = np.diff(filtered_time_above_20N) # Calculate time intervals
impulse = np.sum(filtered_thrust_above_20N[:-1] * dt) # Riemann sum calculation
else:
impulse = 0
return filtered_pressure_above_20N, filtered_thrust_above_20N, filtered_time_above_20N, impulse, time_ms, end_A20N, start_A20N