51 from typing
import Callable
55 """ This class implements generic supervisor to handle compass simulation
57 Attributes inherited from GenericSupervisor:
58 context : (CarmaContext) : a CarmaContext instance
60 config : (config) : Parameters structure
62 is_init : (bool) : Flag equals to True if the supervisor has already been initialized
64 iter : (int) : Frame counter
67 rtc : (RtcComponent) : A Rtc component instance
69 cacao : (bool) : CACAO features enabled in the RTC
71 basis : (ModalBasis) : a ModalBasis instance (optimizer)
73 calibration : (Calibration) : a Calibration instance (optimizer)
76 def __init__(self, config_file: str =
None, cacao: bool =
False):
77 """ Init the COMPASS wih the config_file
80 config_file : (str) : path to the configuration file. Default is None
82 cacao : (bool) : Flag to use cacao rtc. Default is False
86 self.
frameframe =
None
87 self.
cacaocacao = cacao
90 config = load_config_from_file(config_file)
92 GenericSupervisor.__init__(self, config)
95 """Initialize the rtc component of the supervisor as a RtcCompass
121 if self.
configconfigconfig.p_wfss[wfs].type == WFSType.SH:
123 if self.
configconfigconfig.p_wfss[wfs]._validsubsx
is None or \
124 self.
configconfigconfig.p_wfss[wfs]._validsubsy
is None:
126 from hraa.tools.doit
import makessp
127 roiTab = makessp(self.
configconfigconfig.p_wfss[wfs].nxsub, obs=0., rmax=0.98)
128 self.
configconfigconfig.p_wfss[wfs]._nvalid = roiTab[0].size
130 wfs]._validsubsx = roiTab[0] * self.
configconfigconfig.p_wfss[wfs].npix
132 wfs]._validsubsy = roiTab[1] * self.
configconfigconfig.p_wfss[wfs].npix
135 wfs]._validsubsx.size
138 np.array([self.
configconfigconfig.p_wfss[wfs]._nvalid], dtype=np.int32))
140 centroider_type.append(self.
configconfigconfig.p_centroiders[wfs].type)
141 delay.append(self.
configconfigconfig.p_controllers[0].delay)
142 offset.append((self.
configconfigconfig.p_wfss[wfs].npix - 1) / 2)
145 cmat_size.append(2 * nvalid[wfs][0])
148 wfs].type == WFSType.PYRLR:
152 centroider_type.append(self.
configconfigconfig.p_centroiders[wfs].type)
153 delay.append(self.
configconfigconfig.p_controllers[0].delay)
157 cmat_size.append(self.
configconfigconfig.p_wfss[wfs].nPupils * nvalid[wfs][0])
160 raise ValueError(
'WFS type not supported')
164 nact, centroider_type, delay, offset, scale,
165 cacao=self.
cacaocacao)
168 [wfs.nslopes
for wfs
in self.
rtcrtc._rtc.d_centro])
172 self.
rtcrtc._rtc.d_centro[wfs].load_validpos(
176 if self.
configconfigconfig.p_centroiders[wfs].type
is CentroiderType.BPCOG:
177 self.
rtcrtc._rtc.d_centro[wfs].set_nmax(self.
configconfigconfig.p_centroiders[wfs].nmax)
178 self.
rtcrtc._rtc.d_centro[wfs].set_npix(npix[wfs])
180 self.
configconfigconfig.p_centroiders[wfs]._nslope = self.
rtcrtc._rtc.d_centro[wfs].nslopes
181 print(
"wfs ", wfs,
" set as ", centroider_type[wfs])
182 size = sum(cmat_size)
183 cMat = np.zeros((nact, size), dtype=np.float32)
184 print(
"Size of cMat:", cMat.shape)
187 self.
rtcrtc._rtc.d_control[0].set_cmat(cMat)
188 self.
rtcrtc._rtc.d_control[0].set_decayFactor(
189 np.ones(nact, dtype=np.float32) * (gain[0] - 1))
190 self.
rtcrtc._rtc.d_control[0].set_matE(np.identity(nact, dtype=np.float32))
191 self.
rtcrtc._rtc.d_control[0].set_modal_gains(
192 np.ones(nact, dtype=np.float32) * -gain[0])
194 print(
"RTC initialized")
197 def _init_components(self) -> None:
198 """ Initialize all the components
203 GenericSupervisor._init_components(self)
217 def next(self) -> None:
218 """ Performs a single loop iteration
224 self.
set_commandset_command(0, np.array(self.
rtcrtc._rtc.d_control[0].d_voltage))
226 self.
rtcrtc.publish()
229 def get_tar_image(self, tar_index, expo_type: str =
"se") -> np.ndarray:
232 raise NotImplementedError(
"Not implemented")
235 """ Immediately sets provided command to DMs - does not affect integrator
238 nctrl : (int) : Controller index (unused)
240 command : (np.ndarray) : Command vector to send
248 """ Get command from DM
251 command : (np.ndarray) : Command vector
269 s =
'--- BenchSupervisor ---\nRTC: ' + repr(self.
rtcrtc)
270 if hasattr(self,
'_cam'):
271 s +=
'\nCAM: ' + repr(cam)
272 if hasattr(self,
'_dm'):
273 s +=
'\nDM: ' + repr(dm)
277 """ Acquire a new WFS frame and load it
280 centro_index : (int) : Index of the centroider where to load the frame
283 if (type(self.
frameframe)
is tuple):
284 centro_index = len(self.
frameframe)
285 for i
in range(centro_index):
286 self.
rtcrtc._rtc.d_centro[i].load_img(self.
frameframe[i], self.
frameframe[i].shape[0],
287 self.
frameframe[i].shape[1], -1)
289 self.
rtcrtc._rtc.d_centro[centro_index].load_img(self.
frameframe,
290 self.
frameframe.shape[0],
291 self.
frameframe.shape[1], -1)
294 """ Compute the WFS frame: calibrate, centroid, commands.
297 for centro
in self.
rtcrtc._rtc.d_centro:
298 centro.calibrate_img()
299 self.
rtcrtc.do_centroids(0)
300 self.
rtcrtc.do_control(0)
301 self.
rtcrtc.do_clipping(0)
302 self.
rtcrtc._rtc.comp_voltage(0)
304 def set_one_actu(self, nctrl: int, nactu: int, *, ampli: float = 1,
305 reset: bool =
True) ->
None:
306 """ Push the selected actuator
309 nctrl : (int) : controller index
311 nactu : (int) : actuator index to push
314 ampli : (float) : amplitude to apply. Default is 1 volt
316 reset : (bool) : reset the previous command vector. Default is True
321 command[nactu] = ampli
325 """ Active all the GPU devices specified in the parameters file
326 Required for using with widgets, due to multithreaded init
327 and in case GPU 0 is not used by the simu
338 if hasattr(self,
'_dm'):
342 """ Reset the nctrl Controller command buffer, reset all controllers if nctrl == -1
345 nctrl : (int) : Controller index. If -1 (default), all controllers commands are reset
348 for control
in self.
rtcrtc._rtc.d_control:
349 control.d_com.reset()
351 self.
rtcrtc._rtc.d_control[nctrl].d_com.reset()
353 def load_config(self, config_file: str =
None) ->
None:
354 """ Init the COMPASS with the config_file
357 config_file : (str) : path to the configuration file
363 """ Set the externally defined function that allows to grab frames
366 cam_callback : (Callable) : function that allows to grab frames
370 def set_dm_callback(self, dm_get_callback: Callable, dm_set_callback: Callable):
371 """ Set the externally defined function that allows to communicate with the DM
374 dm_get_callback : (Callable) : function that allows to retrieve commands
375 dm_set_callback : (Callable) : function that allows to set commands
381 """ Re-centre the centroiding boxes around the spots, and loads
382 the new box coordinates in the slopes computation supervisor
386 init_config : (bool): Flag to reset to the default positions of boxes. Default is False
388 centro_index : (int) : centroider index
393 nsubap = ij_subap.shape[1]
394 self.
rtcrtc._rtc.d_centro[centro_index].load_validpos(
395 ij_subap[0], ij_subap[1], nsubap)
400 for i
in range(nslopes):
403 s = s + self.get_slopes()[self.
slopes_indexslopes_index[centro_index]:self.
404 slopes_index[centro_index + 1]]
408 i_subap = np.array(self.
rtcrtc._rtc.d_centro[centro_index].d_validx)
409 j_subap = np.array(self.
rtcrtc._rtc.d_centro[centro_index].d_validy)
411 nsubap = i_subap.shape[0]
413 s = np.resize(s, (2, nsubap))
415 new_i_subap = (i_subap + s[0, :].round()).astype(int)
416 new_j_subap = (j_subap + s[1, :].round()).astype(int)
418 self.
rtcrtc._rtc.d_centro[centro_index].load_validpos(
419 new_i_subap, new_j_subap, nsubap)
422 """ Returns the currently used subapertures positions
425 centro_index : (int) : Index of the centroider
428 current_pos : (tuple) : (i_subap, j_subap)
430 i_subap = np.array(self.
rtcrtc._rtc.d_centro[centro_index].d_validx)
431 j_subap = np.array(self.
rtcrtc._rtc.d_centro[centro_index].d_validy)
432 return i_subap, j_subap
435 """ Return the index of the first position of each WFS slopes vector
436 inside the global RTC slopes vector
439 slopes_index : (np.ndarray) : Slopes index
445 Extract and convert compass supervisor configuration parameters
446 into 2 dictionnaries containing relevant AO parameters
449 root: (object), COMPASS supervisor object to be parsed
451 Returns : 2 dictionnaries
#define set_active_device(new_device, silent)
#define set_active_device_force(new_device, silent)
This class implements generic supervisor to handle compass simulation.
None load_new_wfs_frame(self, int centro_index=0)
Acquire a new WFS frame and load it.
def get_slopes_index(self)
Return the index of the first position of each WFS slopes vector inside the global RTC slopes vector.
def get_current_windows_pos(self, int centro_index=0)
Returns the currently used subapertures positions.
def __init__(self, str config_file=None, bool cacao=False)
Init the COMPASS wih the config_file.
def set_cam_callback(self, Callable cam_callback)
Set the externally defined function that allows to grab frames.
None next(self)
Performs a single loop iteration.
None force_context(self)
Active all the GPU devices specified in the parameters file Required for using with widgets,...
None set_command(self, int nctrl, np.ndarray command)
Immediately sets provided command to DMs - does not affect integrator.
rtc
(RtcComponent) : A Rtc component instance
np.ndarray get_tar_image(self, tar_index, str expo_type="se")
NOT IMPLEMENTED.
None set_one_actu(self, int nctrl, int nactu, *float ampli=1, bool reset=True)
Push the selected actuator.
None load_config(self, str config_file=None)
Init the COMPASS with the config_file.
None reset_command(self, int nctrl=-1)
Reset the nctrl Controller command buffer, reset all controllers if nctrl == -1.
cacao
(bool) : CACAO features enabled in the RTC
None reset_dm(self)
Reset the DM.
def compute_wfs_frame(self)
Compute the WFS frame: calibrate, centroid, commands.
def set_dm_callback(self, Callable dm_get_callback, Callable dm_set_callback)
Set the externally defined function that allows to communicate with the DM.
np.ndarray get_command(self)
Get command from DM.
def export_config(self)
Extract and convert compass supervisor configuration parameters into 2 dictionnaries containing relev...
def adaptive_windows(self, init_config=False, int centro_index=0)
Re-centre the centroiding boxes around the spots, and loads the new box coordinates in the slopes com...
This class defines generic methods and behavior of a supervisor It is not intended to be instantiated...
iter
(int) : Frame counter
context
(CarmaContext) : a CarmaContext instance
is_init
(bool) : Flag equals to True if the supervisor has already been initialized
config
(config) : Parameters structure
Parameter classes for COMPASS.
Numerical constants for shesha and config enumerations for safe-typing.
Initialization of a Dms object.
Initialization of a Rtc object.
Basic utilities function.