6 from scipy.ndimage.measurements
import center_of_mass
11 config = load_config_from_file(os.getenv(
"COMPASS_ROOT") +
12 "/shesha/tests/pytest/par/test_sh.py")
13 config.p_controller0.set_delay(0.0)
14 sup = Supervisor(config)
20 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
21 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
False, 0,
23 rtc.add_controller(sup.context, sup.config.p_wfs0._nvalid,
24 sup.config.p_wfs0._nvalid * 2, sup.config.p_controller0.nactu,
25 sup.config.p_controller0.delay, 0,
"generic", idx_centro=np.zeros(1),
27 centro = rtc.d_centro[0]
28 control = rtc.d_control[0]
29 rtc.d_centro[0].set_npix(sup.config.p_wfs0.npix)
30 xvalid = np.array(sup.rtc.rtc.d_centro[0].d_validx)
31 yvalid = np.array(sup.rtc.rtc.d_centro[0].d_validy)
32 rtc.d_centro[0].load_validpos(xvalid, yvalid, xvalid.size)
33 cmat = sup.rtc.get_command_matrix(0)
34 rtc.d_control[0].set_cmat(cmat)
35 rtc.d_control[0].set_gain(sup.config.p_controller0.gain)
36 frame = sup.wfs.get_wfs_image(0)
38 rtc.d_centro[0].load_img(frame, frame.shape[0])
39 rtc.d_centro[0].calibrate_img()
42 slp = ng.array(rtc.d_control[0].d_centroids)
44 com = ng.array(rtc.d_control[0].d_com)
46 dark = np.random.random(frame.shape)
47 flat = np.random.random(frame.shape)
48 centro.set_dark(dark, frame.shape[0])
49 centro.set_flat(flat, frame.shape[0])
53 return np.abs((array1 - array2) / array2.max()).max()
57 assert (centro.nvalid - sup.config.p_wfs0._nvalid < precision)
61 assert (centro.offset - (sup.config.p_wfs0.npix / 2 - 0.5) < precision)
65 assert (centro.scale - sup.config.p_wfs0.pixsize < precision)
69 assert (centro.type ==
"cog")
73 assert (control.nslope - sup.config.p_wfs0._nvalid * 2 < precision)
77 assert (control.nactu - sup.config.p_controller0.nactu < precision)
81 assert (control.type ==
"generic")
85 assert (control.delay - sup.config.p_controller0.delay < precision)
89 assert (centro.npix - sup.config.p_wfs0.npix < precision)
105 assert (control.gain - sup.config.p_controller0.gain < precision)
121 centro.calibrate_img()
122 imgCal = (frame - dark) * flat
127 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
128 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
129 offset = centro.offset
131 for k
in range(sup.config.p_wfs0._nvalid):
132 tmp = center_of_mass(bincube[:, :, k])
133 slopes[k] = (tmp[0] - offset) * scale
134 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
140 slopes = ng.array(control.d_centroids).toarray()
142 cmat = ng.array(control.d_cmat).toarray()
143 commands = cmat.dot(slopes) * gain * (-1)
149 control.set_comRange(-1, 1)
150 assert (control.comRange == (-1, 1))
154 control.set_comRange(-1, 1)
155 C = (np.random.random(sup.config.p_controller0.nactu) - 0.5) * 4
156 control.set_com(C, C.size)
159 C_clipped[np.where(C > 1)] = 1
160 C_clipped[np.where(C < -1)] = -1
166 C = np.random.random(sup.config.p_controller0.nactu)
167 control.add_perturb_voltage(
"test", C, 1)
169 ng.array(control.d_perturb_map[
"test"][0]).toarray(), C) < precision)
173 control.remove_perturb_voltage(
"test")
174 assert (control.d_perturb_map == {})
178 C = np.random.random(sup.config.p_controller0.nactu)
179 control.add_perturb_voltage(
"test", C, 1)
180 com = ng.array(control.d_com_clipped).toarray()
181 control.add_perturb()
187 control.disable_perturb_voltage(
"test")
188 com = ng.array(control.d_com_clipped).toarray()
189 control.add_perturb()
195 control.enable_perturb_voltage(
"test")
196 com = ng.array(control.d_com_clipped).toarray()
197 C = ng.array(control.d_perturb_map[
"test"][0]).toarray()
198 control.add_perturb()
204 control.reset_perturb_voltage()
205 assert (control.d_perturb_map == {})
211 control.set_comRange(volt_min, volt_max)
212 control.comp_voltage()
213 C = np.random.random(sup.config.p_controller0.nactu)
214 control.add_perturb_voltage(
"test", C, 1)
215 control.set_com(C, C.size)
216 com0 = ng.array(control.d_circularComs0).toarray()
217 com1 = ng.array(control.d_circularComs1).toarray()
218 control.comp_voltage()
219 delay = sup.config.p_controller0.delay
220 a = delay - int(delay)
222 commands = a * com0 + b * com1
223 comPertu = commands + C
224 comPertu[np.where(comPertu > volt_max)] = volt_max
225 comPertu[np.where(comPertu < volt_min)] = volt_min
231 rtc.remove_centroider(0)
232 assert (rtc.d_centro == [])
236 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
237 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
240 centro = rtc.d_centro[-1]
242 centro.set_threshold(threshold)
243 centro.set_npix(sup.config.p_wfs0.npix)
244 centro.load_validpos(xvalid, yvalid, xvalid.size)
245 centro.load_img(frame, frame.shape[0])
246 centro.calibrate_img()
248 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
249 bincube /= bincube.max()
250 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
251 offset = centro.offset
253 bincube = bincube - threshold
254 bincube[np.where(bincube < 0)] = 0
255 for k
in range(sup.config.p_wfs0._nvalid):
256 tmp = center_of_mass(bincube[:, :, k])
257 slopes[k] = (tmp[0] - offset) * scale
258 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
264 rtc.remove_centroider(0)
265 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
266 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
269 centro = rtc.d_centro[-1]
272 centro.set_npix(sup.config.p_wfs0.npix)
273 centro.load_validpos(xvalid, yvalid, xvalid.size)
274 centro.load_img(frame, frame.shape[0])
275 centro.calibrate_img()
277 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
278 bincube /= bincube.max()
279 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
280 offset = centro.offset
282 for k
in range(sup.config.p_wfs0._nvalid):
283 imagette = bincube[:, :, k]
284 threshold = np.sort(imagette, axis=
None)[-(bpix + 1)]
285 imagette -= threshold
286 imagette[np.where(imagette < 0)] = 0
287 tmp = center_of_mass(imagette)
288 slopes[k] = (tmp[0] - offset) * scale
289 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale