from __future__ import annotations
from atomiq.components.primitives import Component, Switchable
from atomiq.components.electronics.rfsource import RFSource
from atomiq.components.electronics.adc import ADCChannel
from atomiq.components.optoelectronics.photodiode import AnalogPhotodiode, CalibratedPhotodiode
from atomiq.components.optoelectronics.lightmodulator import RFLightModulator
from atomiq.components.basics.calibration import InvertableCalibration
from atomiq.components.laser import ContinuouslyStabilizedModulatedLaser
from atomiq.helper import replace_member, isnan, rec_getattr
from artiq.experiment import kernel, portable
from artiq.language.units import us
from artiq.language.core import now_mu, delay, delay_mu, parallel
from artiq.master.worker_db import DummyDevice
import numpy as np
[docs]
class SUServo(Component):
"""Representation for the SUServo gateware
On init, the servo loop in the SUServo is stared automatically. If this is not wanted
configure the :param:start_loop
Args:
suservo_device: The artiq SUServo device from your device_db, e.g. @suservo0.
start_loop: Sets if the servo loop should be started at the prerun stage.
"""
kernel_invariants = {"suservo_device", "start_loop"}
def __init__(self, suservo_device, start_loop: bool = True, *args, **kwargs):
Component.__init__(self, *args, **kwargs)
self.suservo_device = suservo_device
self.servo_on = False
self.start_loop = start_loop
[docs]
@kernel
def _prerun(self):
if self.start_loop:
delay(100 * us)
self.set_servomode(True)
[docs]
@kernel
def set_servomode(self, enabled: bool):
if enabled != self.servo_on:
self.servo_on = enabled
self.suservo_device.set_config(1 if enabled else 0)
[docs]
@kernel
def get_adc_value(self, channel: np.int32) -> float:
return self.suservo_device.get_adc(channel)
[docs]
@kernel
def set_adc_gain(self, channel: np.int32, gain: np.int32 = 0):
if self.debug_output:
self.experiment.log.info(self.identifier + ": Setting gain for channel {0} to {1}", [channel, gain])
self.suservo_device.set_pgia_mu(channel, gain)
[docs]
class SUServoChannel(RFSource, Switchable):
"""A Urukul DDS channel in SUServo configuration as RF Source
If not in servo mode, this can be used just like a normal :class:UrukulChannel
Args:
suservo: The atomiq SUServo component this channel belongs to
suservo_channel: The artiq SUServo channel from your device_db, e.g. @suservo0_ch2.
default_profile: Which of the 32 profiles of the SUServo should be used? (default 0)
servo_divider: Todo: Why do we need this? (default 10.0)
"""
kernel_invariants = {"suservo", "suservo_channel", "cpld"}
def __init__(
self,
suservo: SUServo,
suservo_channel,
default_attenuation=19.0,
default_profile=0,
servo_divider: float = 10.0,
*args,
**kwargs,
):
RFSource.__init__(self, *args, **kwargs)
Switchable.__init__(self, ["RF_on"])
self.suservo = suservo
self.suservo_channel = suservo_channel
if not isinstance(self.suservo.suservo_device, DummyDevice):
self.cpld = self.suservo.suservo_device.cplds[self.suservo_channel.servo_channel // 4]
self.device = self.suservo.suservo_device.ddses[self.suservo_channel.servo_channel // 4]
self.profile = default_profile
self.attenuation = default_attenuation
self.servo_on = False
self.servo_divider = servo_divider
self.next_on = np.int64(0)
self.next_off = np.int64(0)
[docs]
@kernel
def _prerun(self):
self.off()
# attenuation
self.set_att(self.attenuation)
# DDS
self.set(frequency=self.frequency, phase=0.0, amplitude=self.amplitude, profile=self.profile)
[docs]
@kernel
def set(
self,
frequency: float = float("nan"),
amplitude: float = float("nan"),
phase: float = 0.0,
profile: np.int32 = -1,
):
"""Set the frequency and amplitude of the DDS channel
Frequency/amplitude are set to the last known value if ``nan`` is passed (default).
Args:
frequency: Frequency in Hz (``nan`` to use previous value)
amplitude: If servomode is off, this is the DDS amplitude (0..1). If servomode is on, this
is the target voltage on the photodiode. (``nan`` to use previous value)
phase: Phase in rad to be set (default 0.0)
profile: DDS Profile (``-1`` to keep current profile)
"""
if profile < 0:
profile = self.profile
if not isnan(frequency):
self.frequency = frequency
if not isnan(amplitude):
self.amplitude = amplitude
if self.debug_output:
self.experiment.log.info(self.identifier + ": Setting frequency to {0:.2f} MHz", [self.frequency * 1e-6])
self.experiment.log.info(self.identifier + ": Setting amplitude to {0:.2f}", [self.amplitude])
self.suservo_channel.set_dds(profile, self.frequency, -1 * self.amplitude / self.servo_divider, phase)
# only touch the IIR state (the DDS amplitude in open loop) when the amplitude is explicitly given. Otherwise
# calls like set_phase() on a beam that holds a calibrated sample and hold state (stabilize(False)) would
# clobber the held amplitude.
if not self.servo_on and not isnan(amplitude):
self.suservo_channel.set_y(profile, y=self.amplitude)
[docs]
@kernel
def set_att(self, attenuation: float):
"""
Set the hardware attenuation for this urukul channel via cpld.
Args:
attenuation: channel attenuation (0. to 31.0 in 0.5 increments) [dB]
"""
self.cpld.set_att(self.suservo_channel.servo_channel % 4, attenuation)
[docs]
@kernel
def _set_frequency(self, frequency: float):
self.set(frequency=frequency)
[docs]
@kernel
def set_amplitude(self, amplitude):
"""Set the amplitude of the DDS output
Args:
amplitude: If servomode is off, this is the DDS amplitude (0..1). If servomode is on, this
is the target voltage on the photodiode.
"""
if not self.servo_on:
if amplitude <= self.amp_max and amplitude >= self.amp_min:
self._set_amplitude(amplitude)
else:
self.experiment.log.error(
"Requested amplitude {0} outside limits [{1}, {2}] for the RFSource " + self.identifier,
[amplitude, self.amp_min, self.amp_max],
)
else:
self._set_amplitude(amplitude)
[docs]
@kernel
def _set_amplitude(self, amplitude: float):
if self.debug_output:
self.experiment.log.info(self.identifier + ": Setting amplitude to {0}", [amplitude])
if self.servo_on:
self.suservo_channel.set_dds_offset(self.profile, -1 * amplitude / self.servo_divider)
else:
self.suservo_channel.set_y(self.profile, y=amplitude)
self.amplitude = amplitude
[docs]
@kernel
def _set_phase(self, phase: float):
self.set(phase=phase)
[docs]
@kernel
def on(self):
"""Turn on the RF output via the fast switch"""
if self.debug_output:
self.experiment.log.info(self.identifier + ": switching on (next_on = {0})", [self.next_on])
delay_mu(4)
self.suservo_channel.set(1, en_iir=1 if self.servo_on else 0, profile=self.profile)
self.next_on = now_mu()
delay_mu(4)
[docs]
@kernel
def off(self):
"""Turn off the RF output via the fast switch"""
if self.debug_output:
self.experiment.log.info(self.identifier + ": switching off (next_off = {0})", [self.next_off])
delay_mu(4)
self.next_off = now_mu()
self.suservo_channel.set(0, en_iir=1 if self.servo_on else 0, profile=self.profile)
delay_mu(4)
[docs]
@kernel
def set_servomode(self, enabled: bool):
"""Switch servo mode of the SUServo on or off
The servo loop in the SUServo gateware (running on the Kasli) can only be switched on globally for
all channels. However, we can decide for each channel if the output of the servo loop should be given
to the DDS. This function switches the the updating for this channel.
Args:
enabled: Whether the servomode should be enabled
"""
output_on = 1 if self.next_on <= now_mu() and self.next_off > now_mu() else 0
if self.servo_on and not enabled:
# switch servo mode off
if self.debug_output:
self.experiment.log.info(self.identifier + ": Switch off servo (with ouput_on={0})", [output_on])
self.suservo_channel.set(output_on, en_iir=0, profile=self.profile)
if not self.servo_on and enabled:
# switch servo mode on
if self.debug_output:
self.experiment.log.info(self.identifier + ": Switch on servo (with ouput_on={0})", [output_on])
self.suservo.set_servomode(True)
self.suservo_channel.set(output_on, en_iir=1, profile=self.profile)
self.servo_on = enabled
# we need some time for the servo to settle
delay(0.03 * us)
[docs]
@kernel(flags={"fast-math"})
def _ramp(
self,
duration: float,
frequency_start: float,
frequency_end: float,
amplitude_start: float,
amplitude_end: float,
ramp_timestep: float = 200e-6,
):
"""
At some point, we want to replace this by code that uses the digital ramp generator (DRG) on the AD9910 because
this allows for much faster ramps
"""
if ramp_timestep < 1.5 * us:
ramp_timestep = 1.5 * us
self.experiment.log.warning(
self.identifier + ": Ramp timestep {0:.3f} below limit (1.4us). Reducing...", [ramp_timestep * 1e6]
)
n = int(duration / ramp_timestep)
ramp_timestep = duration / float(n)
if n < 2:
self.experiment.log.error(
self.identifier + ": Ramp impossible with duration {0}us and ramp_timestep {1}us",
[duration * 1e6, ramp_timestep * 1e6],
)
freq_step = (frequency_end - frequency_start) / (n - 1)
amp_step = (amplitude_end - amplitude_start) / (n - 1)
delay(-1.31 * us)
for i in range(n):
_freq = frequency_start + i * freq_step
_amp = amplitude_start + i * amp_step
with parallel:
self.set(frequency=_freq, amplitude=_amp)
delay(ramp_timestep)
[docs]
class SUServoADCChannel(ADCChannel):
"""Analog Input of a Sampler in an SUServo configuration
ATTENTION: When default_gain is !=1 is given, this might cause problems in closed loop mode.
It seems SUServo internally uses gain=1. Maybe it can be set differently but I don't see how.
Args:
suservo: The SUServo component that this channel belongs to
default_gain: The default gain in machine units (0: 1, ..., 3: 1000) to set for this channel
on startup
"""
kernel_invariants = {"suservo"}
def __init__(self, suservo: SUServo, default_gain: np.int32 = 0, *args, **kwargs):
kwargs["adc_device"] = suservo
ADCChannel.__init__(self, *args, **kwargs)
self.suservo = suservo
self.gain = default_gain
[docs]
@kernel
def _prerun(self):
self.set_gain(self.gain)
[docs]
@kernel
def set_gain(self, gain: np.int32 = 0):
"""Set the gain for the ADC channel in SUServo configuration
Args:
gain: Gain in machine units (0: 1, ..., 3: 1000)
"""
if gain < 4 and gain >= 0:
self.gain = gain
self.suservo.set_adc_gain(self.channel, self.gain)
else:
self.experiment.log.error(
self.identifier + ": Cannot set gain {0}. Must be one of [0,1,2,3] meaning gain of [1,10,100,1000]",
[gain],
)
[docs]
@kernel
def measure(self, samples: np.int32 = 1, cached: bool = False) -> float:
meas_sum = 0.0
for i in range(samples):
meas_sum += self.suservo.get_adc_value(self.channel)
delay(5 * us)
return meas_sum / samples
[docs]
class SUServoModulatedLaser(ContinuouslyStabilizedModulatedLaser):
"""A Laser with intensity stabilization realized by the SUServo
The SUServo realizes a PI servo controller in hardware with an update time interval of 1.3us, leading to a maximum
analog bandwidth of >500kHz. To achieve this a Sampler (8 analog in) is bundled in the gateware with two Urukuls
(2 x 4 DDS channels).
With this class, the SUServo can be used in open loop as well as in closed loop mode. Use the :func:`stabilize()`
function to switch between open and closed loop mode. When switching from closed to open loop, the last output
power on the DDS channel will be hold.
Args:
modulator: The modulator (most likely an AOM) that is driven by the SUServo. The Modulator given here must be
of a class derived from :class:RFLightModulator.
photodiode: The photodiode monitoring the laser. The photodiode must be of a class derived from
:class:AnalogPhotodiode.
default_kp: default proportional (P-) part for the SUServo loop. Values should be negative.
default_ki: default integral (I-) part for the SUServo loop. Values should be negative. (default 0.0)
default_ki_limit: default limit for I-part of the SUServo loop. 0.0 means unlimited. (default 0.0)
suservo_channel_path: Path (relative to the modulator) at which the :class:SUServoChannel driving the
modulator can be found. By default this is the rfsource of the modulator itself, but
it can be set to reach through an arbitrary RF chain (e.g. a frequency mixer) to the
SUServoChannel at its end, e.g. ``"rfsource.if_source"``. (default ``"rfsource"``)
adc_channel_path: Path (relative to the photodiode) at which the :class:SUServoADCChannel monitoring the
laser can be found. By default this is the adc_channel of the photodiode itself, but it
can be set to reach through an arbitrary component chain to the SUServoADCChannel at
its end. (default ``"adc_channel"``)
"""
kernel_invariants = {"suservo_channel", "adc_channel", "default_kp", "default_ki", "default_ki_limit"}
def __init__(
self,
default_kp: float,
default_ki: float = 0.0,
default_ki_limit: float = 0.0,
suservo_channel_path: str = "rfsource",
adc_channel_path: str = "adc_channel",
*args,
**kwargs,
):
ContinuouslyStabilizedModulatedLaser.__init__(self, *args, **kwargs)
self.default_kp = default_kp
self.default_ki = default_ki
self.default_ki_limit = default_ki_limit
if not isinstance(self.photodiode, AnalogPhotodiode):
self.experiment.log.error(
f"The photodiode {self.photodiode.identifier} is not an AnalogPhotodiode which is"
f"required for {self.identifier}"
)
self.adc_channel = rec_getattr(self.photodiode, adc_channel_path)
if not isinstance(self.adc_channel, SUServoADCChannel):
self.experiment.log.error(
f"The adc channel at '{adc_channel_path}' of the photodiode {self.photodiode.identifier} is not a"
f"SUServoADCChannel which is required for {self.identifier}"
)
if isinstance(self.photodiode, CalibratedPhotodiode) and not isinstance(
self.photodiode.calibration, InvertableCalibration
):
self.experiment.log.error(
f"The photodiode {self.photodiode.identifier} is calibrated by {self.photodiode.calibration.identifier}"
"which is not invertable. But SUServo needs an invertable calibration."
)
if not isinstance(self.modulator, RFLightModulator):
self.experiment.log.error(
f"The modulator {self.modulator.identifier} is not an RFLightModulator which is"
f"required for {self.identifier}"
)
self.suservo_channel = rec_getattr(self.modulator, suservo_channel_path)
if not isinstance(self.suservo_channel, SUServoChannel):
self.experiment.log.error(
f"The rfsource at '{suservo_channel_path}' of the modulator {self.modulator.identifier} is not a"
f"SUServoChannel which is required for {self.identifier}"
)
if isinstance(self.photodiode, CalibratedPhotodiode):
replace_member(self, "_transform_power", "_transform_power_calibration")
else:
replace_member(self, "_transform_power", "_transform_power_dummy")
[docs]
@kernel
def _prerun(self):
# servo mode must be running no matter if we operate in open loop or closed loop
self.suservo_channel.suservo.set_servomode(True)
# set p and i parameter for servo loop
self.set_servo_parameter(self.adc_channel.channel, self.default_kp, self.default_ki, self.default_ki_limit)
self.stabilize(self.stabilized)
[docs]
@kernel
def set_servo_parameter(self, adc_channel, kp: float, ki: float, ki_limit: float, delay: float = 0.0):
"""Set servo loop parameters.
Args:
kp: proportional (P-) part for the SUServo loop. Values should be negative.
ki: integral (I-) part for the SUServo loop. Values should be negative.
ki_limit: limit for I-part of the SUServo loop. 0.0 means unlimited.
delay: delay between switching on the laser an the servo loop becoming active (default: 0.0)
"""
self.suservo_channel.suservo_channel.set_iir(self.suservo_channel.profile, adc_channel, kp, ki, ki_limit, delay)
[docs]
@kernel
def _stabilize(self, enable: bool):
self.stabilized = enable
self.suservo_channel.set_servomode(enable)
[docs]
@portable
def _amplitude_from_power(self, power: float) -> float:
if self.stabilized:
return self._transform_power(power)
else:
return power / self.src_transmission / self.laser_source.get_power()
_docstring_overwrites = {
"set_power": """\
Set the output power of the laser
If the SUServo is in open loop mode, this sets the amplitude of the DDS channel. If the SUServo is in closed
loop mode, it sets the photodiode value the servo loop should target. If the photodiode is calibrated the
`power` argument should be given in the calibrated units.
Args:
power: the power the laser should be set/stabilized to. In open loop mode it takes the DDS amplitude (0..1).
In closed loop mode it takes the target value on the photodiode for the servo loop. If the
photodiode is calibrated the power should be given in the calibrated units (e.g. mW).
"""
}