src.acoustools.Levitator
1from ctypes import CDLL, POINTER 2import ctypes 3 4import torch, os 5 6from acoustools.Utilities import get_convert_indexes 7from torch import Tensor 8 9 10class LevitatorController(): 11 ''' 12 Class to enable the manipulation of an OpenMPD style acoustic levitator from python. 13 ''' 14 15 def __init__(self, bin_path:str|None = None, ids:tuple[int] = (1000,999), matBoardToWorld:list[int]|None=None, 16 print_lines:bool=False, force_create_pat=False, pat_name=None): 17 ''' 18 Creates the controller - reccomended to use in a `with` block\n 19 ```python 20 from acoustools.Levitator import LevitatorController 21 from acoustools.Utilities import create_points, add_lev_sig, propagate_abs 22 from acoustools.Solvers import wgs 23 24 p = create_points(1,1,x=0,y=0,z=0) 25 x = wgs(p) 26 print(propagate_abs(x,p)) 27 print(x.shape) 28 x = add_lev_sig(x) 29 30 with LevitatorController(ids=-1) as lev: 31 32 lev.levitate(x) 33 print('Levitating...') 34 input() 35 print('Stopping...') 36 37 ``` 38 THIS CHANGES THE CURRENT WORKING DIRECTORY AND THEN CHANGES IT BACK \n 39 :param bin_path: The path to the binary files needed. If `None` will use files contained in AcousTools. Default: None. 40 :param ids: IDs of boards. Default `(1000,999)`. For two board setup will be (Top, Bottom) if `-1` then all messages will be ignored. Use when testing code but no device is conncted 41 :param matBoardToWorld: Matric defining the mapping between simulated and real boards. When `None` uses a default setting. Default `None`. 42 :param print_lines: If False supresses some print messages 43 ''' 44 45 self.mode = 1 46 ''' 47 @private 48 ''' 49 if type(ids) == int: 50 ids = (ids,) 51 52 if ids[0] == -1: 53 self.mode = 0 54 print('Virtual Levitator mode - no messages will be sent') 55 else: 56 if bin_path is None: 57 self.bin_path = os.path.dirname(__file__)+"/../../bin/x64/" 58 59 cwd = os.getcwd() 60 os.chdir(self.bin_path) 61 files = os.listdir() 62 63 for id in ids: 64 if force_create_pat or 'board_'+str(id)+'.pat' not in files: 65 data_file = open('board_master.pat','r') 66 data = data_file.read() 67 68 file = open('board_'+str(id)+'.pat','w') 69 if pat_name is None: 70 data_id = data.replace('<XXXXXXXX>',str(id)) 71 else: 72 data_id = data.replace('<XXXXXXXX>',str(pat_name)) 73 file.write(data_id) 74 file.close() 75 data_file.close() 76 77 78 print(os.getcwd()) 79 self.levitatorLib = CDLL(self.bin_path+'Levitator.dll') 80 81 self.board_number = len(ids) 82 self.ids = (ctypes.c_int * self.board_number)(*ids) 83 84 if matBoardToWorld is None: 85 if self.board_number == 2: 86 self.matBoardToWorld = (ctypes.c_float * (16*self.board_number)) ( 87 1, 0, 0, 0, 88 0, 1, 0, 0, 89 0, 0, 1, 0, 90 0, 0, 0, 1, 91 92 1, 0, 0, 0, 93 0, 1, 0, 0, 94 0, 0, 1, 0, 95 0, 0, 0, 1 96 97 98 ) 99 elif self.board_number == 1: 100 self.matBoardToWorld = (ctypes.c_float * (16*self.board_number)) ( 101 1, 0, 0, 0, 102 0, 1, 0, 0, 103 0, 0, 1, 0, 104 0, 0, 0, 1 105 ) 106 else: 107 raise ValueError('For number of boards > 2, matBoardToWorld shouldnt be None') 108 109 else: 110 self.matBoardToWorld = (ctypes.c_float * (16*self.board_number))(*matBoardToWorld) 111 112 113 self.levitatorLib.connect_to_levitator.argtypes = [POINTER(ctypes.c_int), POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_bool] 114 self.levitatorLib.connect_to_levitator.restype = ctypes.c_void_p 115 self.controller = self.levitatorLib.connect_to_levitator(self.ids,self.matBoardToWorld,self.board_number,print_lines) 116 117 os.chdir(cwd) 118 119 self.IDX = get_convert_indexes(256*self.board_number).cpu().detach() 120 121 def __enter__(self): 122 return self 123 124 def __exit__(self, exc_type, exc_value, traceback) -> None: 125 self.disconnect() 126 127 128 def send_message(self, phases, amplitudes=None, relative_amplitude=1, num_geometries = 1, sleep_ms = 0, loop=False, num_loops = 0): 129 ''' 130 RECCOMENDED NOT TO USE - USE `levitate` INSTEAD\\ 131 @private 132 sends messages to levitator 133 ''' 134 if self.mode: 135 self.levitatorLib.send_message.argtypes = [ctypes.c_void_p,POINTER(ctypes.c_float), POINTER(ctypes.c_float), ctypes.c_float, ctypes.c_int, ctypes.c_int, ctypes.c_int] 136 self.levitatorLib.send_message(self.controller,phases,amplitudes,relative_amplitude,num_geometries, sleep_ms, loop, num_loops) 137 138 def disconnect(self): 139 ''' 140 Disconnects the levitator 141 ''' 142 if self.mode: 143 self.levitatorLib.disconnect.argtypes = [ctypes.c_void_p] 144 self.levitatorLib.disconnect(self.controller) 145 146 def turn_off(self): 147 ''' 148 Turns of all transducers 149 ''' 150 if self.mode: 151 self.levitatorLib.turn_off.argtypes = [ctypes.c_void_p] 152 self.levitatorLib.turn_off(self.controller) 153 154 def set_frame_rate(self, frame_rate:int): 155 ''' 156 Set a new framerate 157 :param frame_rate: The new frame rate to use. Note OpenMPD cannot use framerates below 157Hz 158 ''' 159 if self.mode: 160 self.levitatorLib.set_new_frame_rate.argtypes = [ctypes.c_void_p, ctypes.c_int] 161 new_frame_rate = self.levitatorLib.set_new_frame_rate(self.controller, frame_rate) 162 163 def levitate(self, hologram:list[Tensor]|Tensor, relative_amplitude:int=-1, permute:bool=True, sleep_ms:float = 0, loop:bool=False, num_loops:int=0): 164 ''' 165 Send a single phase map to the levitator - This is the recomended function to use as will deal with dtype conversions etc 166 :param hologram: `Torch.Tensor` of phases or list of `Torch.Tensor` of phases, expects a batched dimension in dim 0. If phases is complex then ` phases = torch.angle(hologram)` will be run for phase and ` amp = torch.abs(hologram)` for amplitude, else phases left as is 167 :param relative_amplitude: Single value [0,1] or -1 to set amplitude to. If -1 will ignore Default -1 168 :param permute: Convert between acoustools transducer order and OpenMPD. Default True. 169 :param sleep_ms: Time to wait between frames in ms. 170 :param loop: If True will restart from the start of phases, default False 171 :param num_loops: A set number of times to repeat the phases 172 ''' 173 174 175 if self.mode: 176 to_output = [] 177 to_output_amplitudes = [] 178 179 if type(hologram) is Tensor and hologram.shape[0] > 1: 180 holos = [] 181 for h in hologram: 182 holos.append(h.unsqueeze(0).cpu().detach()) 183 hologram = holos 184 185 186 if type(hologram) is list: 187 #chunk this up - blocks of 32.... 188 num_geometries = len(hologram) 189 for phases_elem in hologram: 190 phases_elem = phases_elem.cpu().detach() 191 192 if permute: 193 phases_elem = phases_elem[:,self.IDX] 194 195 if torch.is_complex(phases_elem): 196 amp_elem = torch.abs(phases_elem) 197 phases_elem = torch.angle(phases_elem) 198 199 else: 200 amp_elem = torch.ones_like(phases_elem) 201 202 to_output = to_output + phases_elem.squeeze().tolist() 203 to_output_amplitudes = to_output_amplitudes + amp_elem.squeeze().tolist() 204 else: 205 num_geometries = 1 206 if permute: 207 hologram = hologram.cpu().detach() 208 hologram = hologram[:,self.IDX] 209 210 if torch.is_complex(hologram): 211 amp = torch.abs(hologram) 212 hologram = torch.angle(hologram) 213 else: 214 amp = torch.ones_like(hologram) 215 to_output = hologram[0].squeeze().tolist() 216 to_output_amplitudes = amp[0].squeeze().tolist() 217 218 219 phases = (ctypes.c_float * (256*self.board_number *num_geometries))(*to_output) 220 221 222 if relative_amplitude == -1: 223 amplitudes = (ctypes.c_float * (256*self.board_number*num_geometries))(*to_output_amplitudes) 224 else: 225 amplitudes = None 226 relative_amplitude = ctypes.c_float(relative_amplitude) 227 228 229 self.send_message(phases, amplitudes, relative_amplitude, num_geometries,sleep_ms=sleep_ms,loop=loop,num_loops=num_loops)
class
LevitatorController:
11class LevitatorController(): 12 ''' 13 Class to enable the manipulation of an OpenMPD style acoustic levitator from python. 14 ''' 15 16 def __init__(self, bin_path:str|None = None, ids:tuple[int] = (1000,999), matBoardToWorld:list[int]|None=None, 17 print_lines:bool=False, force_create_pat=False, pat_name=None): 18 ''' 19 Creates the controller - reccomended to use in a `with` block\n 20 ```python 21 from acoustools.Levitator import LevitatorController 22 from acoustools.Utilities import create_points, add_lev_sig, propagate_abs 23 from acoustools.Solvers import wgs 24 25 p = create_points(1,1,x=0,y=0,z=0) 26 x = wgs(p) 27 print(propagate_abs(x,p)) 28 print(x.shape) 29 x = add_lev_sig(x) 30 31 with LevitatorController(ids=-1) as lev: 32 33 lev.levitate(x) 34 print('Levitating...') 35 input() 36 print('Stopping...') 37 38 ``` 39 THIS CHANGES THE CURRENT WORKING DIRECTORY AND THEN CHANGES IT BACK \n 40 :param bin_path: The path to the binary files needed. If `None` will use files contained in AcousTools. Default: None. 41 :param ids: IDs of boards. Default `(1000,999)`. For two board setup will be (Top, Bottom) if `-1` then all messages will be ignored. Use when testing code but no device is conncted 42 :param matBoardToWorld: Matric defining the mapping between simulated and real boards. When `None` uses a default setting. Default `None`. 43 :param print_lines: If False supresses some print messages 44 ''' 45 46 self.mode = 1 47 ''' 48 @private 49 ''' 50 if type(ids) == int: 51 ids = (ids,) 52 53 if ids[0] == -1: 54 self.mode = 0 55 print('Virtual Levitator mode - no messages will be sent') 56 else: 57 if bin_path is None: 58 self.bin_path = os.path.dirname(__file__)+"/../../bin/x64/" 59 60 cwd = os.getcwd() 61 os.chdir(self.bin_path) 62 files = os.listdir() 63 64 for id in ids: 65 if force_create_pat or 'board_'+str(id)+'.pat' not in files: 66 data_file = open('board_master.pat','r') 67 data = data_file.read() 68 69 file = open('board_'+str(id)+'.pat','w') 70 if pat_name is None: 71 data_id = data.replace('<XXXXXXXX>',str(id)) 72 else: 73 data_id = data.replace('<XXXXXXXX>',str(pat_name)) 74 file.write(data_id) 75 file.close() 76 data_file.close() 77 78 79 print(os.getcwd()) 80 self.levitatorLib = CDLL(self.bin_path+'Levitator.dll') 81 82 self.board_number = len(ids) 83 self.ids = (ctypes.c_int * self.board_number)(*ids) 84 85 if matBoardToWorld is None: 86 if self.board_number == 2: 87 self.matBoardToWorld = (ctypes.c_float * (16*self.board_number)) ( 88 1, 0, 0, 0, 89 0, 1, 0, 0, 90 0, 0, 1, 0, 91 0, 0, 0, 1, 92 93 1, 0, 0, 0, 94 0, 1, 0, 0, 95 0, 0, 1, 0, 96 0, 0, 0, 1 97 98 99 ) 100 elif self.board_number == 1: 101 self.matBoardToWorld = (ctypes.c_float * (16*self.board_number)) ( 102 1, 0, 0, 0, 103 0, 1, 0, 0, 104 0, 0, 1, 0, 105 0, 0, 0, 1 106 ) 107 else: 108 raise ValueError('For number of boards > 2, matBoardToWorld shouldnt be None') 109 110 else: 111 self.matBoardToWorld = (ctypes.c_float * (16*self.board_number))(*matBoardToWorld) 112 113 114 self.levitatorLib.connect_to_levitator.argtypes = [POINTER(ctypes.c_int), POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_bool] 115 self.levitatorLib.connect_to_levitator.restype = ctypes.c_void_p 116 self.controller = self.levitatorLib.connect_to_levitator(self.ids,self.matBoardToWorld,self.board_number,print_lines) 117 118 os.chdir(cwd) 119 120 self.IDX = get_convert_indexes(256*self.board_number).cpu().detach() 121 122 def __enter__(self): 123 return self 124 125 def __exit__(self, exc_type, exc_value, traceback) -> None: 126 self.disconnect() 127 128 129 def send_message(self, phases, amplitudes=None, relative_amplitude=1, num_geometries = 1, sleep_ms = 0, loop=False, num_loops = 0): 130 ''' 131 RECCOMENDED NOT TO USE - USE `levitate` INSTEAD\\ 132 @private 133 sends messages to levitator 134 ''' 135 if self.mode: 136 self.levitatorLib.send_message.argtypes = [ctypes.c_void_p,POINTER(ctypes.c_float), POINTER(ctypes.c_float), ctypes.c_float, ctypes.c_int, ctypes.c_int, ctypes.c_int] 137 self.levitatorLib.send_message(self.controller,phases,amplitudes,relative_amplitude,num_geometries, sleep_ms, loop, num_loops) 138 139 def disconnect(self): 140 ''' 141 Disconnects the levitator 142 ''' 143 if self.mode: 144 self.levitatorLib.disconnect.argtypes = [ctypes.c_void_p] 145 self.levitatorLib.disconnect(self.controller) 146 147 def turn_off(self): 148 ''' 149 Turns of all transducers 150 ''' 151 if self.mode: 152 self.levitatorLib.turn_off.argtypes = [ctypes.c_void_p] 153 self.levitatorLib.turn_off(self.controller) 154 155 def set_frame_rate(self, frame_rate:int): 156 ''' 157 Set a new framerate 158 :param frame_rate: The new frame rate to use. Note OpenMPD cannot use framerates below 157Hz 159 ''' 160 if self.mode: 161 self.levitatorLib.set_new_frame_rate.argtypes = [ctypes.c_void_p, ctypes.c_int] 162 new_frame_rate = self.levitatorLib.set_new_frame_rate(self.controller, frame_rate) 163 164 def levitate(self, hologram:list[Tensor]|Tensor, relative_amplitude:int=-1, permute:bool=True, sleep_ms:float = 0, loop:bool=False, num_loops:int=0): 165 ''' 166 Send a single phase map to the levitator - This is the recomended function to use as will deal with dtype conversions etc 167 :param hologram: `Torch.Tensor` of phases or list of `Torch.Tensor` of phases, expects a batched dimension in dim 0. If phases is complex then ` phases = torch.angle(hologram)` will be run for phase and ` amp = torch.abs(hologram)` for amplitude, else phases left as is 168 :param relative_amplitude: Single value [0,1] or -1 to set amplitude to. If -1 will ignore Default -1 169 :param permute: Convert between acoustools transducer order and OpenMPD. Default True. 170 :param sleep_ms: Time to wait between frames in ms. 171 :param loop: If True will restart from the start of phases, default False 172 :param num_loops: A set number of times to repeat the phases 173 ''' 174 175 176 if self.mode: 177 to_output = [] 178 to_output_amplitudes = [] 179 180 if type(hologram) is Tensor and hologram.shape[0] > 1: 181 holos = [] 182 for h in hologram: 183 holos.append(h.unsqueeze(0).cpu().detach()) 184 hologram = holos 185 186 187 if type(hologram) is list: 188 #chunk this up - blocks of 32.... 189 num_geometries = len(hologram) 190 for phases_elem in hologram: 191 phases_elem = phases_elem.cpu().detach() 192 193 if permute: 194 phases_elem = phases_elem[:,self.IDX] 195 196 if torch.is_complex(phases_elem): 197 amp_elem = torch.abs(phases_elem) 198 phases_elem = torch.angle(phases_elem) 199 200 else: 201 amp_elem = torch.ones_like(phases_elem) 202 203 to_output = to_output + phases_elem.squeeze().tolist() 204 to_output_amplitudes = to_output_amplitudes + amp_elem.squeeze().tolist() 205 else: 206 num_geometries = 1 207 if permute: 208 hologram = hologram.cpu().detach() 209 hologram = hologram[:,self.IDX] 210 211 if torch.is_complex(hologram): 212 amp = torch.abs(hologram) 213 hologram = torch.angle(hologram) 214 else: 215 amp = torch.ones_like(hologram) 216 to_output = hologram[0].squeeze().tolist() 217 to_output_amplitudes = amp[0].squeeze().tolist() 218 219 220 phases = (ctypes.c_float * (256*self.board_number *num_geometries))(*to_output) 221 222 223 if relative_amplitude == -1: 224 amplitudes = (ctypes.c_float * (256*self.board_number*num_geometries))(*to_output_amplitudes) 225 else: 226 amplitudes = None 227 relative_amplitude = ctypes.c_float(relative_amplitude) 228 229 230 self.send_message(phases, amplitudes, relative_amplitude, num_geometries,sleep_ms=sleep_ms,loop=loop,num_loops=num_loops)
Class to enable the manipulation of an OpenMPD style acoustic levitator from python.
LevitatorController( bin_path: str | None = None, ids: tuple[int] = (1000, 999), matBoardToWorld: list[int] | None = None, print_lines: bool = False, force_create_pat=False, pat_name=None)
16 def __init__(self, bin_path:str|None = None, ids:tuple[int] = (1000,999), matBoardToWorld:list[int]|None=None, 17 print_lines:bool=False, force_create_pat=False, pat_name=None): 18 ''' 19 Creates the controller - reccomended to use in a `with` block\n 20 ```python 21 from acoustools.Levitator import LevitatorController 22 from acoustools.Utilities import create_points, add_lev_sig, propagate_abs 23 from acoustools.Solvers import wgs 24 25 p = create_points(1,1,x=0,y=0,z=0) 26 x = wgs(p) 27 print(propagate_abs(x,p)) 28 print(x.shape) 29 x = add_lev_sig(x) 30 31 with LevitatorController(ids=-1) as lev: 32 33 lev.levitate(x) 34 print('Levitating...') 35 input() 36 print('Stopping...') 37 38 ``` 39 THIS CHANGES THE CURRENT WORKING DIRECTORY AND THEN CHANGES IT BACK \n 40 :param bin_path: The path to the binary files needed. If `None` will use files contained in AcousTools. Default: None. 41 :param ids: IDs of boards. Default `(1000,999)`. For two board setup will be (Top, Bottom) if `-1` then all messages will be ignored. Use when testing code but no device is conncted 42 :param matBoardToWorld: Matric defining the mapping between simulated and real boards. When `None` uses a default setting. Default `None`. 43 :param print_lines: If False supresses some print messages 44 ''' 45 46 self.mode = 1 47 ''' 48 @private 49 ''' 50 if type(ids) == int: 51 ids = (ids,) 52 53 if ids[0] == -1: 54 self.mode = 0 55 print('Virtual Levitator mode - no messages will be sent') 56 else: 57 if bin_path is None: 58 self.bin_path = os.path.dirname(__file__)+"/../../bin/x64/" 59 60 cwd = os.getcwd() 61 os.chdir(self.bin_path) 62 files = os.listdir() 63 64 for id in ids: 65 if force_create_pat or 'board_'+str(id)+'.pat' not in files: 66 data_file = open('board_master.pat','r') 67 data = data_file.read() 68 69 file = open('board_'+str(id)+'.pat','w') 70 if pat_name is None: 71 data_id = data.replace('<XXXXXXXX>',str(id)) 72 else: 73 data_id = data.replace('<XXXXXXXX>',str(pat_name)) 74 file.write(data_id) 75 file.close() 76 data_file.close() 77 78 79 print(os.getcwd()) 80 self.levitatorLib = CDLL(self.bin_path+'Levitator.dll') 81 82 self.board_number = len(ids) 83 self.ids = (ctypes.c_int * self.board_number)(*ids) 84 85 if matBoardToWorld is None: 86 if self.board_number == 2: 87 self.matBoardToWorld = (ctypes.c_float * (16*self.board_number)) ( 88 1, 0, 0, 0, 89 0, 1, 0, 0, 90 0, 0, 1, 0, 91 0, 0, 0, 1, 92 93 1, 0, 0, 0, 94 0, 1, 0, 0, 95 0, 0, 1, 0, 96 0, 0, 0, 1 97 98 99 ) 100 elif self.board_number == 1: 101 self.matBoardToWorld = (ctypes.c_float * (16*self.board_number)) ( 102 1, 0, 0, 0, 103 0, 1, 0, 0, 104 0, 0, 1, 0, 105 0, 0, 0, 1 106 ) 107 else: 108 raise ValueError('For number of boards > 2, matBoardToWorld shouldnt be None') 109 110 else: 111 self.matBoardToWorld = (ctypes.c_float * (16*self.board_number))(*matBoardToWorld) 112 113 114 self.levitatorLib.connect_to_levitator.argtypes = [POINTER(ctypes.c_int), POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_bool] 115 self.levitatorLib.connect_to_levitator.restype = ctypes.c_void_p 116 self.controller = self.levitatorLib.connect_to_levitator(self.ids,self.matBoardToWorld,self.board_number,print_lines) 117 118 os.chdir(cwd) 119 120 self.IDX = get_convert_indexes(256*self.board_number).cpu().detach()
Creates the controller - reccomended to use in a with block
from acoustools.Levitator import LevitatorController
from acoustools.Utilities import create_points, add_lev_sig, propagate_abs
from acoustools.Solvers import wgs
p = create_points(1,1,x=0,y=0,z=0)
x = wgs(p)
print(propagate_abs(x,p))
print(x.shape)
x = add_lev_sig(x)
with LevitatorController(ids=-1) as lev:
lev.levitate(x)
print('Levitating...')
input()
print('Stopping...')
THIS CHANGES THE CURRENT WORKING DIRECTORY AND THEN CHANGES IT BACK
Parameters
- bin_path: The path to the binary files needed. If
Nonewill use files contained in AcousTools. Default: None. - ids: IDs of boards. Default
(1000,999). For two board setup will be (Top, Bottom) if-1then all messages will be ignored. Use when testing code but no device is conncted - matBoardToWorld: Matric defining the mapping between simulated and real boards. When
Noneuses a default setting. DefaultNone. - print_lines: If False supresses some print messages
def
disconnect(self):
139 def disconnect(self): 140 ''' 141 Disconnects the levitator 142 ''' 143 if self.mode: 144 self.levitatorLib.disconnect.argtypes = [ctypes.c_void_p] 145 self.levitatorLib.disconnect(self.controller)
Disconnects the levitator
def
turn_off(self):
147 def turn_off(self): 148 ''' 149 Turns of all transducers 150 ''' 151 if self.mode: 152 self.levitatorLib.turn_off.argtypes = [ctypes.c_void_p] 153 self.levitatorLib.turn_off(self.controller)
Turns of all transducers
def
set_frame_rate(self, frame_rate: int):
155 def set_frame_rate(self, frame_rate:int): 156 ''' 157 Set a new framerate 158 :param frame_rate: The new frame rate to use. Note OpenMPD cannot use framerates below 157Hz 159 ''' 160 if self.mode: 161 self.levitatorLib.set_new_frame_rate.argtypes = [ctypes.c_void_p, ctypes.c_int] 162 new_frame_rate = self.levitatorLib.set_new_frame_rate(self.controller, frame_rate)
Set a new framerate
Parameters
- frame_rate: The new frame rate to use. Note OpenMPD cannot use framerates below 157Hz
def
levitate( self, hologram: list[torch.Tensor] | torch.Tensor, relative_amplitude: int = -1, permute: bool = True, sleep_ms: float = 0, loop: bool = False, num_loops: int = 0):
164 def levitate(self, hologram:list[Tensor]|Tensor, relative_amplitude:int=-1, permute:bool=True, sleep_ms:float = 0, loop:bool=False, num_loops:int=0): 165 ''' 166 Send a single phase map to the levitator - This is the recomended function to use as will deal with dtype conversions etc 167 :param hologram: `Torch.Tensor` of phases or list of `Torch.Tensor` of phases, expects a batched dimension in dim 0. If phases is complex then ` phases = torch.angle(hologram)` will be run for phase and ` amp = torch.abs(hologram)` for amplitude, else phases left as is 168 :param relative_amplitude: Single value [0,1] or -1 to set amplitude to. If -1 will ignore Default -1 169 :param permute: Convert between acoustools transducer order and OpenMPD. Default True. 170 :param sleep_ms: Time to wait between frames in ms. 171 :param loop: If True will restart from the start of phases, default False 172 :param num_loops: A set number of times to repeat the phases 173 ''' 174 175 176 if self.mode: 177 to_output = [] 178 to_output_amplitudes = [] 179 180 if type(hologram) is Tensor and hologram.shape[0] > 1: 181 holos = [] 182 for h in hologram: 183 holos.append(h.unsqueeze(0).cpu().detach()) 184 hologram = holos 185 186 187 if type(hologram) is list: 188 #chunk this up - blocks of 32.... 189 num_geometries = len(hologram) 190 for phases_elem in hologram: 191 phases_elem = phases_elem.cpu().detach() 192 193 if permute: 194 phases_elem = phases_elem[:,self.IDX] 195 196 if torch.is_complex(phases_elem): 197 amp_elem = torch.abs(phases_elem) 198 phases_elem = torch.angle(phases_elem) 199 200 else: 201 amp_elem = torch.ones_like(phases_elem) 202 203 to_output = to_output + phases_elem.squeeze().tolist() 204 to_output_amplitudes = to_output_amplitudes + amp_elem.squeeze().tolist() 205 else: 206 num_geometries = 1 207 if permute: 208 hologram = hologram.cpu().detach() 209 hologram = hologram[:,self.IDX] 210 211 if torch.is_complex(hologram): 212 amp = torch.abs(hologram) 213 hologram = torch.angle(hologram) 214 else: 215 amp = torch.ones_like(hologram) 216 to_output = hologram[0].squeeze().tolist() 217 to_output_amplitudes = amp[0].squeeze().tolist() 218 219 220 phases = (ctypes.c_float * (256*self.board_number *num_geometries))(*to_output) 221 222 223 if relative_amplitude == -1: 224 amplitudes = (ctypes.c_float * (256*self.board_number*num_geometries))(*to_output_amplitudes) 225 else: 226 amplitudes = None 227 relative_amplitude = ctypes.c_float(relative_amplitude) 228 229 230 self.send_message(phases, amplitudes, relative_amplitude, num_geometries,sleep_ms=sleep_ms,loop=loop,num_loops=num_loops)
Send a single phase map to the levitator - This is the recomended function to use as will deal with dtype conversions etc
Parameters
- hologram:
Torch.Tensorof phases or list ofTorch.Tensorof phases, expects a batched dimension in dim 0. If phases is complex thenphases = torch.angle(hologram)will be run for phase andamp = torch.abs(hologram)for amplitude, else phases left as is - relative_amplitude: Single value [0,1] or -1 to set amplitude to. If -1 will ignore Default -1
- permute: Convert between acoustools transducer order and OpenMPD. Default True.
- sleep_ms: Time to wait between frames in ms.
- loop: If True will restart from the start of phases, default False
- num_loops: A set number of times to repeat the phases