Skip to content

Commit

Permalink
??
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Aug 19, 2024
1 parent 3eeba10 commit 35beeba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
42 changes: 19 additions & 23 deletions pioreactor/background_jobs/stirring.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ def on_disconnected(self) -> None:
with suppress(AttributeError):
self.rpm_check_repeated_thread.cancel()
with suppress(AttributeError):
with self.duty_cycle_lock:
self.pwm.clean_up()
self.pwm.clean_up()
with suppress(AttributeError):
if self.rpm_calculator:
self.rpm_calculator.clean_up()
Expand All @@ -307,25 +306,23 @@ def start_stirring(self) -> None:
f"Starting stirring with {'no' if self.target_rpm is None else self.target_rpm} RPM."
)

with self.duty_cycle_lock:
self.pwm.start(100) # get momentum to start
sleep(0.35)
self.set_duty_cycle(self.duty_cycle)
self.pwm.start(100) # get momentum to start
sleep(0.35)
self.set_duty_cycle(self.duty_cycle)

if self.rpm_calculator is not None:
self.rpm_check_repeated_thread.start() # .start is idempotent

def kick_stirring(self) -> None:
with self.duty_cycle_lock:
self.logger.debug("Kicking stirring")
_existing_duty_cycle = self.duty_cycle
self.set_duty_cycle(0)
sleep(0.30)
self.set_duty_cycle(100)
sleep(0.5)
self.set_duty_cycle(
min(1.01 * _existing_duty_cycle, 60)
) # DC should never need to be above 60 - simply not realistic. We want to avoid the death spiral to 100%.
self.logger.debug("Kicking stirring")
_existing_duty_cycle = self.duty_cycle
self.set_duty_cycle(0)
sleep(0.30)
self.set_duty_cycle(100)
sleep(0.5)
self.set_duty_cycle(
min(1.01 * _existing_duty_cycle, 60)
) # DC should never need to be above 60 - simply not realistic. We want to avoid the death spiral to 100%.

def kick_stirring_but_avoid_od_reading(self) -> None:
"""
Expand Down Expand Up @@ -406,19 +403,18 @@ def poll_and_update_dc(self, poll_for_seconds: Optional[float] = None) -> None:

def on_ready_to_sleeping(self) -> None:
self.rpm_check_repeated_thread.pause()
with self.duty_cycle_lock:
self.set_duty_cycle(0.0)
self.set_duty_cycle(0.0)

def on_sleeping_to_ready(self) -> None:
self.duty_cycle = self._previous_duty_cycle
self.rpm_check_repeated_thread.unpause()
with self.duty_cycle_lock:
self.start_stirring()
self.start_stirring()

def set_duty_cycle(self, value: float) -> None:
self._previous_duty_cycle = self.duty_cycle
self.duty_cycle = clamp(0.0, round(value, 5), 100.0)
self.pwm.change_duty_cycle(self.duty_cycle)
with self.duty_cycle_lock:
self._previous_duty_cycle = self.duty_cycle
self.duty_cycle = clamp(0.0, round(value, 5), 100.0)
self.pwm.change_duty_cycle(self.duty_cycle)

def set_target_rpm(self, value: float) -> None:
if self.rpm_calculator is None:
Expand Down
1 change: 1 addition & 0 deletions pioreactor/tests/test_stirring.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def pause(n=1) -> None:

def test_stirring_runs() -> None:
st = start_stirring(target_rpm=500)
assert st.duty_cycle > 0
st.clean_up()


Expand Down

0 comments on commit 35beeba

Please sign in to comment.