-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdipole-hall-tuner.py
229 lines (170 loc) · 7.03 KB
/
dipole-hall-tuner.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import numpy as np
from time import sleep
from setup import CycleMagnet, GetMagnet, SetMagnet, GetHall, Dist
from setup import SaveIm, GetBeamPos
import datetime
import random
#when done testing, uncomment all SetMagnet, CycleMagnet and
#make sure current step sizes are multiples of each other
small_step = 0.01
big_step = 0.05
total_steps = 20 #total steps of 0.6A
dHall = 0.0001 #max diff in hall readings when matching
timestring = (datetime.datetime.now()).strftime("%m-%d_%H-%M")
##############################################################
##############################################################
def matchHall():
b0_hall = GetHall(dipole_pair[0])
b1_hall = GetHall(dipole_pair[1])
print("Beginning matching...")
while ( abs(b0_hall - b1_hall) > dHall):
diff = abs(b0_hall - b1_hall)
dI = 0.001
#this takes too long if they're really far so I'm gona add this for now
if (diff >= 0.001):
dI = 0.1
elif (diff > 0.00005):
dI = 0.01
b0_i = GetMagnet(dipole_pair[0])
b1_i = GetMagnet(dipole_pair[1])
if (b0_hall > b1_hall):
# change b0 and compare
SetMagnet(dipole_pair[0], b0_i - dI)
#saving the new actual hall value
sleep(3)
b0_hall = GetHall(dipole_pair[0])
elif (b0_hall < b1_hall):
# change b1 and compare
SetMagnet(dipole_pair[1], b1_i - dI)
#saving the new actual hall value
sleep(3)
b1_hall = GetHall(dipole_pair[1])
print(f"Fields: {b0_hall:.6f}, {b1_hall:.6f}, dHall: {((b0_hall - b1_hall)/b0_hall*100):.5f}%")
print("Done matching Hall values.")
def scanDipoles(b_small, b_big):
#set number of steps (from previous experience scanning although might need adjustments)
bs_stepsize = small_step
bb_stepsize = big_step
#initial nominal quad values
q0_init, q1_init = GetMagnet(quad_pair[0]), GetMagnet(quad_pair[1])
for i in range(total_steps):
print(f"Step {i+1} out of {total_steps} large steps.")
#get initial current of dipoles
bs_init = GetMagnet(b_small)
bb_init = GetMagnet(b_big)
for j in range(0, int(bb_stepsize/bs_stepsize)+1):
#changing quads and taking pictures at 4 tunes
#take picture with all at init values
all_nom_im= SaveIm('allNom', viewer)
#take picture with all at zero
SetMagnet(quad_pair[0], 1.5*q0_init)
SetMagnet(quad_pair[1], 2*q1_init)
pos_1= GetBeamPos(all_nom_im, viewer)
sleep(5) #might need to increase this if the jumps in current are big
all_zero_im= SaveIm('allZero', viewer)
#take picture with first quad at half
SetMagnet(quad_pair[0], q0_init/2)
SetMagnet(quad_pair[1], q1_init)
pos_2= GetBeamPos(all_zero_im, viewer)
sleep(5)
q0_half_im= SaveIm('q0half', viewer)
#take picture with second quad at half
SetMagnet(quad_pair[0], q0_init)
SetMagnet(quad_pair[1], q1_init*2)
pos_3= GetBeamPos(q0_half_im, viewer)
sleep(5)
q01_half_im= SaveIm('q01half', viewer)
#return quads to original values
SetMagnet(quad_pair[0], q0_init)
SetMagnet(quad_pair[1], q1_init)
pos_4= GetBeamPos(q01_half_im, viewer)
#peak/intensity
#pk_1 = pos_1[2:]
#pk_2 = pos_2[2:]
#pk_3 = pos_3[2:]
#pk_4 = pos_4[2:]
#centroid positions
pos_1 = pos_1[0:2]
pos_2 = pos_2[0:2]
pos_3 = pos_3[0:2]
pos_4 = pos_4[0:2]
#get quadratic distance from centroids
print(f"Centroids:\n({pos_1[0]:.2f}, {pos_1[1]:.2f})\n({pos_2[0]:.2f}, {pos_2[1]:.2f})\n({pos_3[0]:.2f}, {pos_3[1]:.2f})\n({pos_4[0]:.2f}, {pos_4[1]:.2f})")
distance= Dist(pos_1, pos_2, pos_3, pos_4)
print(f"Dist= {distance:.5f}")
bs_hall = GetHall(b_small)
bb_hall = GetHall(b_big)
bs_i = GetMagnet(b_small)
bb_i = GetMagnet(b_big)
#save i, hall values and distance to file
f= open(f"{b_small}_{b_big}_Distance_{timestring}.txt", "a+")
f.write(f'{bs_i:.3f}\t{bb_i:.3f}\t{bs_hall:.7f}\t{bb_hall:.7f}\t{distance:.4f}\t{pos_1[0]:.4f}\t{all_nom_im}\n')
f.close()
#ramps down this magnet in small steps multiple of the large step
SetMagnet( b_small, bs_init - bs_stepsize*(j+1))
#one magnet goes down 0.06 A, the other 0.02 A x 3
SetMagnet( b_big, bb_init - bb_stepsize)
#####################
#if (abs(bs_hall - bb_hall) > dHall):
#if they are different, match again
matchHall()
#else:
# print(f"Fields: {bs_hall:.6f}, {bb_hall:.6f}, dHall: {((bs_hall - bb_hall)/bs_hall*100):.5f}%")
##############################################################
##############################################################
while (True):
dipole_pair = input("Enter which dipole pair to tune (i.e. b5, b6): ").lower()
#getting list of dipoles
dipole_pair = [x.lstrip().rstrip() for x in dipole_pair.split(",")]
if (len(dipole_pair) == 2 and dipole_pair[0]!=dipole_pair[1] and 'b1' not in dipole_pair):
break
else:
print("Invalid choice.")
#two dipoles: dipole_pair[0] and dipole_pair[1]
#setting correct devices to use
if ('b3' in dipole_pair):
quad_pair = ['q6', 'q7']
viewer = 'D1638'
elif ('b5' in dipole_pair):
quad_pair = ['q10', 'q11']
viewer = 'D1783'
elif ('b7' in dipole_pair):
quad_pair = ['q14', 'q15']
viewer = 'D1879'
print(f"\nTuning {dipole_pair[0].capitalize()} and {dipole_pair[1].capitalize()}, using {quad_pair[0].capitalize()} and {quad_pair[1].capitalize()} on viewer {viewer}.\n")
#warning to set the correct range
print("Make sure Hall probe ranges are set correctly.")
#cycle
time1 = CycleMagnet(dipole_pair[0])
time2 = CycleMagnet(dipole_pair[1])
sleep(np.max([time1, time2]))
print("Done cycling.")
cont= input("Once magnets have settled, enter 'y' to continue...")
if (cont != 'y'):
print("Exiting...")
exit()
#match Hall probe readings
matchHall()
#get initial values to return two for second scan
b0_init = GetMagnet(dipole_pair[0])
b1_init = GetMagnet(dipole_pair[1])
#'''
scanDipoles(dipole_pair[0], dipole_pair[1])
print(f"Done with {dipole_pair[0]} scan.")
#reset dipoles to original currents and cycle
SetMagnet(dipole_pair[0], b0_init)
SetMagnet(dipole_pair[1], b1_init)
#cycle
time1 = CycleMagnet(dipole_pair[0])
time2 = CycleMagnet(dipole_pair[1])
sleep(np.max([time1, time2]))
print("Done cycling.")
cont= input("Once magnets have settled, enter 'y' to continue...")
if (cont != 'y'):
print("Exiting...")
exit()
#match Hall probe readings
matchHall()
#'''
scanDipoles(dipole_pair[1], dipole_pair[0])
print(f"Done with {dipole_pair[1]} scan.")