Skip to content

Commit

Permalink
changing rp2040 firmware address; adding lock to stirring pwm
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Aug 19, 2024
1 parent 307bdb2 commit f17895f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#### Breaking changes

- the RP2040 firmware is now on i2c channel 0x2C (previously 0x30). This is to solve an annoying `i2cdetect` issue were it would lock up.
- the web server now writes its logs to the same location as the app: `/var/log/pioreactor.log`. Those wishing to keep the old location can use a new configuration parameter `ui_log_file` to `[logging]` section and set it to `/var/log/pioreactorui.log`.
- removed `psutil` and `zeroconf` Python packages from new images. We replaced their functionality with built-in routines.
- in config.ini, the section `od_config` renamed to `od_reading.config`, and `stirring` is `stirring.config`. When you update, a script will run to automatically update these names in your config.inis.
Expand Down
39 changes: 24 additions & 15 deletions pioreactor/background_jobs/stirring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import json
from contextlib import suppress
from threading import Lock
from time import perf_counter
from time import sleep
from time import time
Expand Down Expand Up @@ -232,6 +233,7 @@ def __init__(
pin, config.getfloat("stirring.config", "pwm_hz"), unit=self.unit, experiment=self.experiment
)
self.pwm.lock()
self.duty_cycle_lock = Lock()

if target_rpm is not None and self.rpm_calculator is not None:
self.target_rpm: Optional[float] = float(target_rpm)
Expand Down Expand Up @@ -292,7 +294,8 @@ def on_disconnected(self) -> None:
with suppress(AttributeError):
self.rpm_check_repeated_thread.cancel()
with suppress(AttributeError):
self.pwm.clean_up()
with self.duty_cycle_lock:
self.pwm.clean_up()
with suppress(AttributeError):
if self.rpm_calculator:
self.rpm_calculator.clean_up()
Expand All @@ -301,22 +304,26 @@ def start_stirring(self) -> None:
self.logger.debug(
f"Starting stirring with {'no' if self.target_rpm is None else self.target_rpm} RPM."
)
self.pwm.start(100) # get momentum to start
sleep(0.35)
self.set_duty_cycle(self.duty_cycle)

with self.duty_cycle_lock:
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:
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%.
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%.

def kick_stirring_but_avoid_od_reading(self) -> None:
"""
Expand Down Expand Up @@ -397,12 +404,14 @@ 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()
self.set_duty_cycle(0.0)
with self.duty_cycle_lock:
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()
self.start_stirring()
with self.duty_cycle_lock:
self.start_stirring()

def set_duty_cycle(self, value: float) -> None:
self._previous_duty_cycle = self.duty_cycle
Expand Down
4 changes: 2 additions & 2 deletions pioreactor/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@


# I2C channels used
ADC = 0x48 if (0, 0) < hardware_version_info <= (1, 0) else 0x30
DAC = 0x49 if (0, 0) < hardware_version_info <= (1, 0) else 0x30
ADC = 0x48 if (0, 0) < hardware_version_info <= (1, 0) else 0x2C
DAC = 0x49 if (0, 0) < hardware_version_info <= (1, 0) else 0x2C
TEMP = 0x4F


Expand Down

0 comments on commit f17895f

Please sign in to comment.