@@ -176,28 +176,45 @@ def __init__(
176176 time .sleep (5 )
177177
178178 # Stop any running program — RTDE cannot upload its control script
179- # while a program is occupying the Secondary Interface. Only needed
180- # after a boot (a program left running by another client is their problem).
181- if boot_needed :
182- try :
183- self ._stop_program ()
184- time .sleep (2 )
185- except ConnectionError :
186- logger .warning (
187- "[URRobot] Could not stop program via Dashboard. "
188- "Continuing — RTDE connection may fail if a program is running."
189- )
179+ # while a program occupies the Secondary Interface. Always try, since
180+ # a previous session may have left ExternalControl running.
181+ try :
182+ self ._stop_program ()
183+ time .sleep (2 )
184+ except ConnectionError :
185+ logger .warning (
186+ "[URRobot] Could not stop program via Dashboard. "
187+ "Continuing — RTDE connection may fail if a program is running."
188+ )
190189
191- # Connect RTDE — retry after boot, the Secondary Interface may need
192- # extra time to accept connections even after the robot is IDLE.
193- rtde_attempts = 2 if boot_needed else 1
190+ # Connect RTDE — retry, the Secondary Interface may need time to
191+ # release registers after program stop or boot.
192+ from urkit .exceptions import RtdeRegisterConflictError
193+
194+ rtde_attempts = 2
194195 for attempt in range (1 , rtde_attempts + 1 ):
195196 try :
196197 self ._rtde_c , self ._rtde_r , self ._rtde_io = _connect_rtde (
197198 ip ,
198199 frequency = self ._rtde_frequency ,
199200 )
200201 break
202+ except RtdeRegisterConflictError as e :
203+ if attempt < rtde_attempts :
204+ logger .warning (
205+ "[URRobot] RTDE registers in use (attempt %d/%d). "
206+ "Retrying in 5s..." ,
207+ attempt ,
208+ rtde_attempts ,
209+ )
210+ time .sleep (5 )
211+ else :
212+ raise ConnectionError (
213+ f"RTDE registers are locked on robot at { ip } . "
214+ "A previous session crashed without releasing its connection.\n "
215+ "Fix: fully shut down the robot controller "
216+ "(Settings → System → Shutdown), wait 10s, power on."
217+ ) from e
201218 except Exception as e :
202219 if attempt < rtde_attempts :
203220 logger .warning (
0 commit comments