-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_corrected_beam_pos.py
executable file
·125 lines (105 loc) · 5.53 KB
/
plot_corrected_beam_pos.py
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
import argparse
from pathlib import Path
import matplotlib.pyplot as plt
import csv
def read_data(file: Path):
time, current_pos, corrected_pos, current_shift, current_current, new_current = [], [], [], [], [], []
with open(file, 'r') as f:
lines = f.readlines()
for i in range(len(lines)):
if i in range(0, 9):
continue
time.append(float(lines[i].split(',')[0]))
current_pos.append(float(lines[i].split(',')[1]))
corrected_pos.append(float(lines[i].split(',')[2]))
current_shift.append(float(lines[i].split(',')[3]))
current_current.append(float(lines[i].split(',')[4]))
new_current.append(float(lines[i].split(',')[5]))
return time, current_pos, corrected_pos, current_shift, current_current, new_current
def delta_I_to_delta_x(delta_I: float):
# Output of calibration:
# x(I) = -45.796722 * I + 410.90
# y(I) = -0.210579 * I + 342.08
# I(x) = -0.021836 * x + -8.97
# I(y) = -4.748816 * y + -1624.48
return -45.796722 * delta_I
def main(file: Path, cut: bool, begin: int , end: int):
time, current_pos, corrected_pos, current_shift, current_current, new_current = read_data(file)
niveau = 150
not_controlled_pos = []
niveau_diviation_controlled = []
niveau_diviation_not_controlled = []
# The first position is the initial position and is not controlled
first_pos = current_pos[0]
not_controlled_pos.append(first_pos)
for i in range(len(current_shift)):
# Skip the first one because it's the initial position
if i == 0:
continue
# If the current is +- 2.0 A, the shift is not added
if abs(current_current[i]) == 2.0:
x_shift = 0
elif current_current[i] + current_shift[i] > 2.0:
x_shift = delta_I_to_delta_x(2.0 - current_current[i])
elif current_current[i] + current_shift[i] < -2.0:
x_shift = delta_I_to_delta_x(-2.0 - current_current[i])
else:
x_shift = delta_I_to_delta_x(current_shift[i]) #-1
not_controlled_pos.append(not_controlled_pos[i-1] + x_shift)
niveau_diviation_not_controlled.append(abs(niveau - not_controlled_pos[i]))
niveau_diviation_controlled.append(abs(niveau - corrected_pos[i]))
# print length after calculation and cut if necessary
if cut:
corrected_pos = corrected_pos[begin:end]
not_controlled_pos = not_controlled_pos[begin:end]
niveau_diviation_controlled = niveau_diviation_controlled[begin:end]
niveau_diviation_not_controlled = niveau_diviation_not_controlled[begin:end]
time = time[begin:end]
print(f'Length of the controlled position: {len(corrected_pos)}')
print(f'Length of the not controlled position: {len(not_controlled_pos)}')
print(f'Length of the niveau diviation controlled: {len(niveau_diviation_controlled)}')
print(f'Length of the niveau diviation not controlled: {len(niveau_diviation_not_controlled)}')
print('')
avg_niveau_diviation_controlled = sum(niveau_diviation_controlled)/len(niveau_diviation_controlled)
avg_niveau_diviation_not_controlled = sum(niveau_diviation_not_controlled)/len(niveau_diviation_not_controlled)
# TODO: durschnittliche abweichung vom niveau für beide
# TODO: Für den einregelnden fall auch für den schon eingeregelten fall
print(f'Maximum of the controlled position: {max(corrected_pos)}')
print(f'Maximum of the not controlled position: {max(not_controlled_pos)}')
print('')
print(f'Minimum of the controlled position: {min(corrected_pos)}')
print(f'Minimum of the not controlled position: {min(not_controlled_pos)}')
print('')
print(f'Average of the controlled position: {sum(corrected_pos)/len(corrected_pos)}')
print(f'Average of the not controlled position: {sum(not_controlled_pos)/len(not_controlled_pos)}')
print('')
print(f'Average of the controlled position diviation from niveau: {avg_niveau_diviation_controlled}')
print(f'Average of the not controlled position diviation from niveau: {avg_niveau_diviation_not_controlled}')
if (args.dump == True):
with open("dump.csv", 'w') as f:
f.write("time,corrected_pos,not_controlled_pos\n")
for i in range(len(time)):
f.write(str(time[i]))
f.write(",")
f.write(str(corrected_pos[i]))
f.write(",")
f.write(str(not_controlled_pos[i]))
f.write("\n")
f.close()
plt.title("Strahlposition")
plt.axhline(y=niveau, color='r', label='Niveau')
plt.plot(time, not_controlled_pos, label='Ungeregelte Strahlposition')
plt.plot(time, corrected_pos, label='Geregelte Strahlposition')
plt.ylabel('Horizontale Strahlposition x in Pixel')
plt.xlabel('Zeit t in s')
plt.legend()
plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Plot beam pos with and without controller')
parser.add_argument('file', type=Path, help='Path to file')
parser.add_argument('--cut', action='store_true', help='Cut the data with the specified beginning and end')
parser.add_argument('--begin', type=int, help='Beginning of the window to cut the data')
parser.add_argument('--end', type=int, help='End of the window to cut the data')
parser.add_argument('--dump', action='store_true', help='Dump the calculated positions in a .csv file')
args = parser.parse_args()
main(args.file, args.cut, args.begin, args.end)