88
99from widgets .widget_toolbar_marcos import MarcosToolBar
1010import subprocess
11+ import platform
1112import experiment as ex
1213import numpy as np
1314import shutil
1415import configs .hw_config as hw
15- import autotuning .autotuning as autotuning # Just to use an arduino
16+ import autotuning .autotuning as autotuning # Just to use an arduino
17+ import threading
1618
1719
1820class MarcosController (MarcosToolBar ):
@@ -51,21 +53,69 @@ def __init__(self, *args, **kwargs):
5153 self .action_copybitstream .triggered .connect (self .copyBitStream )
5254 self .action_gpa_init .triggered .connect (self .initgpa )
5355
56+ thread = threading .Thread (target = self .search_sdrlab )
57+ thread .start ()
58+
5459 # Arduino to control the interlock
5560 self .arduino = autotuning .Arduino (baudrate = 19200 , name = "interlock" , serial_number = hw .ard_sn_interlock )
5661 self .arduino .connect ()
5762
63+ def search_sdrlab (self ):
64+ # Get the IP of the SDRLab
65+ if not self .demo :
66+ try :
67+ hw .rp_ip_address = self .get_sdrlab_ip ()[0 ]
68+ except :
69+ print ("No SDRLab found." )
70+ try :
71+ hw .rp_ip_address = self .get_sdrlab_ip ()[0 ]
72+ except :
73+ print ("No communication with SDRLab." )
74+ print ("Try manually." )
75+
76+
77+
78+ @staticmethod
79+ def get_sdrlab_ip ():
80+ print ("\n Searching for SDRLabs..." )
81+
82+ ip_addresses = []
83+ subnet = '192.168.1.'
84+ timeout = 0.1 # Adjust timeout value as needed
85+
86+ for i in range (101 , 132 ): # Scan IP range 192.168.1.101 to 192.168.1.132
87+ ip = subnet + str (i )
88+ try :
89+ if platform .system () == 'Linux' :
90+ subprocess .run (['ping' , '-c' , '1' , ip ], stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL ,
91+ timeout = timeout )
92+ elif platform .system () == 'Windows' :
93+ subprocess .run (['ping' , '-n' , '1' , ip ], stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL ,
94+ timeout = timeout )
95+ ip_addresses .append (ip )
96+ except :
97+ pass
98+
99+ for ip in ip_addresses :
100+ print ("SDRLab found at IP " + ip )
101+
102+ return ip_addresses
103+
58104 def startMaRCoS (self ):
59105 """
60106 Starts the MaRCoS system.
61107
62108 Executes startRP.sh: copy_bitstream.sh & marcos_server.
63109 """
64110 if not self .demo :
65- subprocess .run ([hw .bash_path , "--" , "./communicateRP.sh" , hw .rp_ip_address , "killall marcos_server" ])
66- subprocess .run ([hw .bash_path , "--" , "./startRP.sh" , hw .rp_ip_address , hw .rp_version ])
67- self .initgpa ()
68- print ("\n MaRCoS updated, server connected, gpa initialized." )
111+
112+ try :
113+ subprocess .run ([hw .bash_path , "--" , "./communicateRP.sh" , hw .rp_ip_address , "killall marcos_server" ])
114+ subprocess .run ([hw .bash_path , "--" , "./startRP.sh" , hw .rp_ip_address , hw .rp_version ])
115+ self .initgpa ()
116+ print ("\n MaRCoS updated, server connected, gpa initialized." )
117+ except :
118+ print ("\n ERROR: Server connection not found! Please verify if the blue LED is illuminated on the Red Pitaya." )
69119 else :
70120 print ("\n This is a demo" )
71121 self .action_server .setChecked (True )
@@ -79,16 +129,31 @@ def controlMarcosServer(self):
79129 """
80130 if not self .demo :
81131 if not self .action_server .isChecked ():
132+ subprocess .run ([hw .bash_path , "--" , "./communicateRP.sh" , hw .rp_ip_address , "killall marcos_server" ])
82133 self .action_server .setStatusTip ('Connect to marcos server' )
83134 self .action_server .setToolTip ('Connect to marcos server' )
84- subprocess .run ([hw .bash_path , "--" , "./communicateRP.sh" , hw .rp_ip_address , "killall marcos_server" ])
85135 print ("\n Server disconnected" )
86136 else :
87- self .action_server .setStatusTip ('Kill marcos server' )
88- self .action_server .setToolTip ('Kill marcos server' )
89137 subprocess .run ([hw .bash_path , "--" , "./communicateRP.sh" , hw .rp_ip_address , "killall marcos_server" ])
138+ time .sleep (1.5 )
90139 subprocess .run ([hw .bash_path , "--" , "./communicateRP.sh" , hw .rp_ip_address , "~/marcos_server" ])
91- print ("\n Server connected" )
140+ time .sleep (1.5 )
141+ self .action_server .setStatusTip ('Kill marcos server' )
142+ self .action_server .setToolTip ('Kill marcos server' )
143+
144+ # Run tiny sequence to check connection to the server
145+ try :
146+ expt = ex .Experiment (init_gpa = False )
147+ expt .add_flodict ({
148+ 'grad_vx' : (np .array ([100 ]), np .array ([0 ])),
149+ })
150+ expt .run ()
151+ expt .__del__ ()
152+ print ("\n Server connected!" )
153+ except Exception as e :
154+ print ("\n Server not connected!" )
155+ print ("Try to connect to the server again." )
156+ print ("Error details:" , e )
92157 else :
93158 print ("\n This is a demo" )
94159
@@ -99,9 +164,13 @@ def copyBitStream(self):
99164 Executes copy_bitstream.sh.
100165 """
101166 if not self .demo :
102- subprocess .run ([hw .bash_path , "--" , "./communicateRP.sh" , hw .rp_ip_address , "killall marcos_server" ])
103- subprocess .run ([hw .bash_path , '--' , './copy_bitstream.sh' , '192.168.1.101' , 'rp-122' ])
104- print ("\n MaRCoS updated" )
167+ try :
168+ subprocess .run ([hw .bash_path , "--" , "./communicateRP.sh" , hw .rp_ip_address , "killall marcos_server" ])
169+ subprocess .run ([hw .bash_path , '--' , './copy_bitstream.sh' , hw .rp_ip_address , 'rp-122' ], timeout = 10 )
170+ print ("\n MaRCoS updated" )
171+ except subprocess .TimeoutExpired as e :
172+ print ("\n Timeout error." )
173+ print ("Error details:" , e )
105174 else :
106175 print ("\n This is a demo." )
107176 self .action_server .setChecked (False )
@@ -114,7 +183,7 @@ def initgpa(self):
114183 if self .action_server .isChecked ():
115184 if not self .demo :
116185 link = False
117- while link == False :
186+ while not link :
118187 try :
119188 # Check if GPA available
120189 received_string = self .arduino .send ("GPA_VERB 1;" ).decode ()
@@ -157,7 +226,7 @@ def initgpa(self):
157226 expt .__del__ ()
158227 link = True
159228 print ("\n GPA init done!" )
160-
229+
161230 # Enable power modules
162231 # Enable GPA module
163232 received_string = self .arduino .send ("GPA_ON 1;" ).decode () # Enable power module
0 commit comments