Skip to content

Commit

Permalink
First pass at adding cancel button and close button handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
slactjohnson committed Jul 30, 2024
1 parent 1bb69e0 commit 04bbf5d
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions btms_ui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,20 +475,36 @@ def __init__(

self.n_actions = 0 # progress counter for homing
self.total_actions = 5 # total number of homing steps
self.cancel = False
self.positioners = positioners
self.status_text.setText('Ready')
self.progress_bar.setVisible(False)
self.progress_bar.setValue(0.0)
self.cancel_button.clicked.connect(self._cancel_button_press)
self.home_button.clicked.connect(self._home_button_press)

def closeEvent(self, event):
print("Got close event!")
# Cancel the homing routine if it's running
self._cancel_button_press()
event.accept()

def _cancel_button_press(self):
self.close()
self.cancel = True
self._cancel_handler()

def _cancel_handler(self):
self._append_status_text('\nGot cancel request!')
for positioner in self.positioners:
dev = positioner.device
if dev.moving:
self._append_status_text(f'\n{dev} is moving, stopping...')
dev.stop()
return

def _append_status_text(self, new_text):
txt = self.status_text.text()
self.status_text.setText(txt + new_text)
self.status_text.setText(txt + new_text)

def _increment_progress(self):
"""
Expand Down Expand Up @@ -521,7 +537,11 @@ def _home_button_press(self):
loop_counter = ntries
forward = True
lin_pos = []
if self.cancel:
return False
while loop_counter > 0:
if self.cancel:
return False
linear.velocity.put(0.0) # Use maximum closed loop freq
if forward:
linear.home_forward()
Expand All @@ -530,6 +550,8 @@ def _home_button_press(self):
time.sleep(1) # Wait a bit to allow motor to start moving
while linear.moving: # TODO: Add timeout here?
time.sleep(1)
if self.cancel:
return False
pos = linear.user_readback.get()
if linear.homed:
loop_counter -= 1
Expand Down Expand Up @@ -575,13 +597,17 @@ def _home_button_press(self):
rotary.velocity.put(0.0) # Use maximum closed loop freq
forward = True
for i in range(2):
if self.cancel:
return False
if forward:
rotary.home_forward()
else:
rotary.home_reverse()
time.sleep(1)
while rotary.moving: # TODO: Add timeout?
time.sleep(1)
if self.cancel:
return False
pos = rotary.user_readback.get()
if rotary.homed and not self._rotary_pos_comp(pos):
self._append_status_text('\nRotary homing succeeded')
Expand All @@ -601,6 +627,8 @@ def _home_button_press(self):
goniometer.home_forward()
while goniometer.moving: # TODO: Add timeout?
time.sleep(1)
if self.cancel:
return False
if goniometer.homed:
self._append_status_text('\nGoniometer homing succeeded')
self._increment_progress()
Expand Down

0 comments on commit 04bbf5d

Please sign in to comment.