Skip to content

Commit

Permalink
try true??
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Nov 30, 2024
1 parent 587e3d7 commit dfb4a58
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
3 changes: 3 additions & 0 deletions config.dev.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ initial_duty_cycle=15
pwm_hz=200
use_rpm=True
duration_between_updates_seconds=23
post_delay_duration=0.25
pre_delay_duration=2.0
enable_dodging_od=True


[stirring.pid]
Expand Down
16 changes: 9 additions & 7 deletions pioreactor/background_jobs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,13 @@ def sneak_in(ads_interval, post_delay, pre_delay) -> None:

with catchtime() as timer:
self._action_to_do_after_od_reading()
sleep(ads_interval - self.OD_READING_DURATION - (post_delay + pre_delay) - timer())
action_after_duration = timer()
if ads_interval - self.OD_READING_DURATION - (post_delay + pre_delay) - action_after_duration < 0:
raise ValueError(
"samples_per_second is too high, or post_delay is too high, or pre_delay is too high, or action_to_do_after_od_reading takes too long."
)

sleep(ads_interval - self.OD_READING_DURATION - (post_delay + pre_delay) - action_after_duration)
self._action_to_do_before_od_reading()

# this could fail in the following way:
Expand All @@ -1150,14 +1156,10 @@ def sneak_in(ads_interval, post_delay, pre_delay) -> None:
sneak_in,
job_name=self.job_name,
args=(ads_interval, post_delay, pre_delay),
run_immediately=False,
run_immediately=True,
run_after=ads_interval - ((time() - ads_start_time) % ads_interval),
logger=self.logger,
)

# TODO: shouldn't I just use run_after in `RepeatedTimer` instead of this?
time_to_next_ads_reading = ads_interval - ((time() - ads_start_time) % ads_interval)

sleep(time_to_next_ads_reading + (post_delay + self.OD_READING_DURATION))
self.sneak_in_timer.start()

def on_sleeping(self) -> None:
Expand Down
9 changes: 5 additions & 4 deletions pioreactor/background_jobs/stirring.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,12 @@ def action_to_do_after_od_reading(self):
self.poll_and_update_dc(2)

def initialize_dodging_operation(self):
if config.getfloat("od_reading.config", "samples_per_second") >= 0.2:
if config.getfloat("od_reading.config", "samples_per_second") > 0.12:
self.logger.warning(
"Recommended to decrease `samples_per_second` to ensure there is time to start/stop stirring. Try 0.15 or less."
"Recommended to decrease `samples_per_second` to ensure there is time to start/stop stirring. Try 0.12 or less."
)

self.set_duty_cycle(0)
self.stop_stirring()
self.rpm_check_repeated_thread = RepeatedTimer(
1_000,
lambda *args: None,
Expand Down Expand Up @@ -545,7 +545,8 @@ def start_stirring(
experiment=experiment,
rpm_calculator=rpm_calculator,
)
stirrer.start_stirring()
if not stirrer.currently_dodging_od:
stirrer.start_stirring()
return stirrer


Expand Down
25 changes: 15 additions & 10 deletions pioreactor/tests/test_stirring.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pioreactor.background_jobs.stirring import RpmFromFrequency
from pioreactor.background_jobs.stirring import start_stirring
from pioreactor.background_jobs.stirring import Stirrer
from pioreactor.config import config
from pioreactor.config import temporary_config_change
from pioreactor.pubsub import publish
from pioreactor.pubsub import subscribe
from pioreactor.utils import local_persistant_storage
Expand Down Expand Up @@ -179,15 +181,18 @@ def test_stirring_will_try_to_restart_and_dodge_od_reading() -> None:
from pioreactor.background_jobs.od_reading import start_od_reading

exp = "test_stirring_will_try_to_restart_and_dodge_od_reading"
rpm_calculator = RpmCalculator()
rpm_calculator.setup()
with start_od_reading(
"90", interval=5.0, unit=unit, experiment=exp, fake_data=True, use_calibration=False
):
with Stirrer(500, unit, exp, rpm_calculator=rpm_calculator) as st: # type: ignore
st.start_stirring()

pause(20)
# rpm_calculator = RpmCalculator()
# rpm_calculator.setup()
with temporary_config_change(config, "stirring.config", "enable_dodging_od", "true"):
with start_od_reading(
"90", interval=10.0, unit=unit, experiment=exp, fake_data=True, use_calibration=False
):
with start_stirring(500, unit, exp, use_rpm=True) as st:
assert st.duty_cycle == 0
assert st._estimate_duty_cycle > 0
assert st.currently_dodging_od
assert st.enable_dodging_od
pause(5)


def test_block_until_rpm_is_close_to_target_will_timeout() -> None:
Expand All @@ -203,7 +208,7 @@ def test_block_until_rpm_is_close_to_target_will_timeout() -> None:


def test_block_until_rpm_is_close_will_exit() -> None:
exp = "test_block_until_rpm_is_close_to_target_will_timeout"
exp = "test_block_until_rpm_isf_close_to_target_will_timeout"
rpm_calculator = MockRpmCalculator()
rpm_calculator.setup()
with Stirrer(
Expand Down

0 comments on commit dfb4a58

Please sign in to comment.