11from __future__ import annotations
22
3+ import sys
34import threading
45import time
56
2526log_interval = 1 # seconds; you can adjust this variable as needed
2627stop_logging = False # flag to signal the logging thread to stop
2728
28- def log_temperature ():
29- # Create a file with the current timestamp in the name
30- timestamp = time .strftime ("%Y%m%d_%H%M%S" )
31- filename = f"{ timestamp } _temp.log"
32- with open (filename , "w" ) as logfile :
33- while not stop_logging :
34- temperature = interface .txdevice .get_temperature ()
35- a_temp = interface .txdevice .get_ambient_temperature ()
36- current_time = time .strftime ("%Y-%m-%d %H:%M:%S" )
37- log_line = f"{ current_time } : Temperature: { temperature } , Ambient Temperature: { a_temp } \n "
38- logfile .write (log_line )
39- logfile .flush () # Ensure the data is written immediately
40- time .sleep (log_interval )
29+ # set focus
30+ xInput = 0
31+ yInput = 0
32+ zInput = 50
4133
34+ frequency = 405e3
35+ voltage = 50.0
36+ duration = 2e-4
37+
38+ json_trigger_data = {
39+ "TriggerFrequencyHz" : 5 ,
40+ "TriggerMode" : 1 ,
41+ "TriggerPulseCount" : 0 ,
42+ "TriggerPulseWidthUsec" : 20000
43+ }
4244
4345print ("Starting LIFU Test Script..." )
4446interface = LIFUInterface ()
4547tx_connected , hv_connected = interface .is_device_connected ()
48+
49+ if not tx_connected :
50+ print ("TX device not connected. Attempting to turn on 12V..." )
51+ interface .hvcontroller .turn_12v_on ()
52+
53+ # Give time for the TX device to power up and enumerate over USB
54+ time .sleep (2 )
55+
56+ # Cleanup and recreate interface to reinitialize USB devices
57+ interface .stop_monitoring ()
58+ del interface
59+ time .sleep (1 ) # Short delay before recreating
60+
61+ print ("Reinitializing LIFU interface after powering 12V..." )
62+ interface = LIFUInterface ()
63+
64+ # Re-check connection
65+ tx_connected , hv_connected = interface .is_device_connected ()
66+
4667if tx_connected and hv_connected :
47- print ("LIFU Device Fully connected." )
68+ print ("✅ LIFU Device fully connected." )
4869else :
49- print (f'LIFU Device NOT Fully Connected. TX: { tx_connected } , HV: { hv_connected } ' )
70+ print ("❌ LIFU Device NOT fully connected." )
71+ print (f" TX Connected: { tx_connected } " )
72+ print (f" HV Connected: { hv_connected } " )
73+ sys .exit (1 )
5074
5175# Ask the user if they want to log temperature
5276log_choice = input ("Do you want to log temperature before starting trigger? (y/n): " ).strip ().lower ()
5377log_temp = (log_choice == "y" )
5478
55- print ("Ping the device" )
56- interface .txdevice .ping ()
79+ def log_temperature ():
80+ # Create a file with the current timestamp in the name
81+ timestamp = time .strftime ("%Y%m%d_%H%M%S" )
82+ filename = f"{ timestamp } _temp.csv"
83+ with open (filename , "w" ) as logfile :
84+ while not stop_logging :
85+ print ("Retrieving Console temperature..." )
86+ con_temp = interface .hvcontroller .get_temperature1 ()
87+ print ("Retrieving TX temperature..." )
88+ tx_temp = interface .txdevice .get_temperature ()
89+ print ("Retrieving TX Amb temperature..." )
90+ amb_temp = interface .txdevice .get_ambient_temperature ()
91+ current_time = time .strftime ("%Y-%m-%d %H:%M:%S" )
92+ log_line = f"{ current_time } ,{ frequency } ,{ duration } ,{ voltage } ,{ con_temp } ,{ tx_temp } ,{ amb_temp } \n "
93+ logfile .write (log_line )
94+ logfile .flush () # Ensure the data is written immediately
95+ time .sleep (log_interval )
96+
97+ # Verify communication with the devices
98+ if not interface .txdevice .ping ():
99+ print ("Failed to ping the transmitter device." )
100+ sys .exit (1 )
101+
102+ if not interface .hvcontroller .ping ():
103+ print ("Failed to ping the console devie." )
104+ sys .exit (1 )
57105
58106print ("Enumerate TX7332 chips" )
59107num_tx_devices = interface .txdevice .enum_tx7332_devices ()
@@ -62,21 +110,27 @@ def log_temperature():
62110else :
63111 raise Exception ("No TX7332 devices found." )
64112
65- # set focus
66- xInput = 0
67- yInput = 0
68- zInput = 50
113+ print ("Set Trigger" )
114+ trigger_setting = interface .txdevice .set_trigger_json (data = json_trigger_data )
115+ if trigger_setting :
116+ print (f"Trigger Setting: { trigger_setting } " )
117+ else :
118+ print ("Failed to set trigger setting." )
119+ sys .exit (1 )
69120
70- frequency = 405e3
71- voltage = 12.0
72- duration = 2e-5
121+ print ("Set High Voltage" )
122+ if interface .hvcontroller .set_voltage (voltage ):
123+ print ("High Voltage set successfully." )
124+ else :
125+ print ("Failed to set High Voltage." )
126+ sys .exit (1 )
73127
74128pulse = Pulse (frequency = frequency , amplitude = voltage , duration = duration )
75129pt = Point (position = (xInput ,yInput ,zInput ), units = "mm" )
76130
77131#arr = Transducer.from_file(r"C:\Users\Neuromod2\Documents\OpenLIFU-python\OpenLIFU_2x.json")
78132# arr = Transducer.from_file(R"..\M4_flex.json")
79- arr = Transducer .from_file (R"E:\CURRENT-WORK\openwater\OpenLIFU-python \notebooks\pinmap.json" )
133+ arr = Transducer .from_file (R". \notebooks\pinmap.json" )
80134
81135focus = pt .get_position (units = "mm" )
82136#arr.elements = np.array(arr.elements)[np.argsort([el.pin for el in arr.elements])].tolist()
@@ -85,9 +139,7 @@ def log_temperature():
85139delays = tof .max () - tof
86140apodizations = np .ones (arr .numelements ())
87141
88-
89-
90- # tURN only single element ON
142+ # Turn only single element ON
91143#active_element = 25
92144
93145#delays = delays*0.0
@@ -123,17 +175,9 @@ def log_temperature():
123175 profile_increment = profile_increment
124176)
125177
126- print ("Get Trigger" )
127- trigger_setting = interface .txdevice .get_trigger_json ()
128- if trigger_setting :
129- print (f"Trigger Setting: { trigger_setting } " )
130- else :
131- print ("Failed to get trigger setting." )
132-
133178# If logging is enabled, start the logging thread
134179if log_temp :
135180 t = threading .Thread (target = log_temperature )
136- t .start ()
137181else :
138182 print ("Get Temperature" )
139183 temperature = interface .txdevice .get_temperature ()
@@ -145,12 +189,24 @@ def log_temperature():
145189
146190print ("Press enter to START trigger:" )
147191input () # Wait for the user to press Enter
192+
193+ print ("Enable High Voltage" )
194+ if not interface .hvcontroller .turn_hv_on ():
195+ print ("Failed to turn on High Voltage." )
196+ sys .exit (1 )
197+
148198print ("Starting Trigger..." )
149199if interface .txdevice .start_trigger ():
200+ if log_temp :
201+ t .start () # Start the logging thread
202+ else :
203+ print ("Trigger started without logging." )
204+
150205 print ("Trigger Running Press enter to STOP:" )
151206 input () # Wait for the user to press Enter
207+ stop_logging = True
208+ time .sleep (1 ) # Give the logging thread time to finish
152209 if interface .txdevice .stop_trigger ():
153- stop_logging = True
154210 print ("Trigger stopped successfully." )
155211 else :
156212 print ("Failed to stop trigger." )
0 commit comments