Skip to content

Commit

Permalink
Create PWM loop to manage backlight
Browse files Browse the repository at this point in the history
  • Loading branch information
callumforrester committed Feb 6, 2025
1 parent 9f047bf commit 336cdd7
Showing 1 changed file with 49 additions and 15 deletions.
64 changes: 49 additions & 15 deletions src/htss_rig_bluesky/plans/backlight.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bluesky.plan_stubs as bps
from bluesky.utils import MsgGenerator
from ophyd_async.fastcs.panda import HDFPanda
from ophyd_async.fastcs.panda import HDFPanda, TimeUnits


def set_backlight_intensity(
Expand All @@ -10,38 +10,72 @@ def set_backlight_intensity(
group: str | None = None,
) -> MsgGenerator[None]:
if 0.0 <= intensity <= 1.0:
duty_cycle = int(round(intensity * 100.0))
group = group or set_backlight_intensity.__name__
duty_cycle = int(round(intensity * 100))

if duty_cycle == 0:
port = "ZERO"
elif duty_cycle == 100:
port = "ONE"
else:
port = yield from create_pwm(panda, duty_cycle, wait, group)
port = yield from create_pwm(panda, intensity, wait=False, group=group)

Check warning on line 21 in src/htss_rig_bluesky/plans/backlight.py

View check run for this annotation

Codecov / codecov/patch

src/htss_rig_bluesky/plans/backlight.py#L21

Added line #L21 was not covered by tests

yield from set_backlight_control_port(panda, port, wait=False, group=group)

yield from set_backlight_control_port(panda, port, wait, group)
if wait:
yield from bps.wait(group=group)
else:
raise ValueError(f"Given intensity {intensity} should be between 0.0 and 1.0")

Check warning on line 28 in src/htss_rig_bluesky/plans/backlight.py

View check run for this annotation

Codecov / codecov/patch

src/htss_rig_bluesky/plans/backlight.py#L28

Added line #L28 was not covered by tests


def create_pwm(
panda: HDFPanda,
duty_cycle: float,
period: float = 100.0,
wait: bool = True,
group: str | None = None,
) -> MsgGenerator[str]:
live_time = period * duty_cycle
dead_time = period - live_time

Check warning on line 39 in src/htss_rig_bluesky/plans/backlight.py

View check run for this annotation

Codecov / codecov/patch

src/htss_rig_bluesky/plans/backlight.py#L38-L39

Added lines #L38 - L39 were not covered by tests

clock = panda.clock[2]
pulse = panda.pulse[2]

Check warning on line 42 in src/htss_rig_bluesky/plans/backlight.py

View check run for this annotation

Codecov / codecov/patch

src/htss_rig_bluesky/plans/backlight.py#L41-L42

Added lines #L41 - L42 were not covered by tests

group = group or create_pwm.__name__

Check warning on line 44 in src/htss_rig_bluesky/plans/backlight.py

View check run for this annotation

Codecov / codecov/patch

src/htss_rig_bluesky/plans/backlight.py#L44

Added line #L44 was not covered by tests

for signal, value in {

Check warning on line 46 in src/htss_rig_bluesky/plans/backlight.py

View check run for this annotation

Codecov / codecov/patch

src/htss_rig_bluesky/plans/backlight.py#L46

Added line #L46 was not covered by tests
# Set units first
clock.period_units: TimeUnits.US,
pulse.delay_units: TimeUnits.US,
pulse.width_units: TimeUnits.US,
# Then set all other values
clock.enable: "ONE",
clock.enable_delay: 0.0,
clock.period: period,
pulse.enable: "ONE",
pulse.trig: "CLOCK2.OUT",
pulse.delay: dead_time,
pulse.width: live_time,
pulse.trig_edge: "Rising",
}.items():
yield from bps.abs_set(signal, value, wait=False, group=group)

Check warning on line 61 in src/htss_rig_bluesky/plans/backlight.py

View check run for this annotation

Codecov / codecov/patch

src/htss_rig_bluesky/plans/backlight.py#L61

Added line #L61 was not covered by tests

if wait:
yield from bps.wait(group)

Check warning on line 64 in src/htss_rig_bluesky/plans/backlight.py

View check run for this annotation

Codecov / codecov/patch

src/htss_rig_bluesky/plans/backlight.py#L63-L64

Added lines #L63 - L64 were not covered by tests

return "PULSE2.OUT"

Check warning on line 66 in src/htss_rig_bluesky/plans/backlight.py

View check run for this annotation

Codecov / codecov/patch

src/htss_rig_bluesky/plans/backlight.py#L66

Added line #L66 was not covered by tests


def set_backlight_control_port(
panda: HDFPanda,
port: str,
wait: bool = True,
group: str | None = None,
) -> MsgGenerator[None]:
ttl_out_2 = panda.ttlout[2]
output_port = panda.ttlout[2]
yield from bps.abs_set(
ttl_out_2.val,
output_port.val,
port,
wait=wait,
group=group,
)


def create_pwm(
panda: HDFPanda,
duty_cycle: int,
wait: bool = True,
group: str | None = None,
) -> MsgGenerator[str]:
return NotImplemented

0 comments on commit 336cdd7

Please sign in to comment.