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
from artiq.experiment import kernel, portable
from artiq.language.types import TFloat, TBool, TInt32
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: TBool = True, *args, **kwargs):
Component.__init__(self, *args, **kwargs)
self.suservo_device = suservo_device
self.servo_on = False
self.start_loop = start_loop
@kernel
def _prerun(self):
if self.start_loop:
delay(100*us)
self.set_servomode(True)
[docs]
@kernel
def set_servomode(self, enabled: TBool):
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: TInt32) -> TFloat:
return self.suservo_device.get_adc(channel)
[docs]
@kernel
def set_adc_gain(self, channel: TInt32, gain: TInt32 = 0):
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: TFloat = 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)
@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: TFloat = -1.0, amplitude: TFloat = -1.0, phase: TFloat = 0.0, profile: TInt32 = -1):
"""Set the frequency and amplitude of the DDS channel
Frequency/amplitude are set to the last known value if -1 is given.
Args:
frequency: frequency [Hz]. (default -1.0)
amplitude: If servomode is off, this is the DDS amplitude (0..1). If servomode is on, this
is the target voltage on the photodiode. (default -1)
phase: Phase in rad to be set (default 0.0)
"""
if profile < 0:
profile = self.profile
self.frequency = self.frequency if frequency < 0 else frequency
self.amplitude = self.amplitude if amplitude < 0 else 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, frequency, -1*self.amplitude/self.servo_divider, phase)
if not self.servo_on:
self.suservo_channel.set_y(profile, y=self.amplitude)
[docs]
@kernel
def set_att(self, attenuation: TFloat):
"""
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)
@kernel
def _set_frequency(self, frequency: TFloat):
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)
@kernel
def _set_amplitude(self, amplitude: TFloat):
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
@kernel
def _set_phase(self, phase: TFloat):
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: TBool):
"""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)
@kernel(flags={"fast-math"})
def _ramp(self, duration: TFloat,
frequency_start: TFloat,
frequency_end: TFloat,
amplitude_start: TFloat,
amplitude_end: TFloat,
ramp_timestep: TFloat = 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
amp_step = (amplitude_end - amplitude_start) / n
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 differntly 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: TInt32 = 0, *args, **kwargs):
kwargs["adc_device"] = suservo
ADCChannel.__init__(self, *args, **kwargs)
self.suservo = suservo
self.gain = default_gain
@kernel
def _prerun(self):
self.set_gain(self.gain)
[docs]
@kernel
def set_gain(self, gain: TInt32 = 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: TInt32 = 1, cached: TBool = False) -> TFloat:
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 inteval of 1.3us, leading to a maximum
analog bandwith 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 and the rfsource of the modulator must be of the
class :class:SUServoChannel.
photodiode: The photodiode monitoring the laser. The photodiode must be of a class derived from
:class:AnalogPhotodiode and the photodiode's :param:adc_channel must be of the class
:class:SUServoADCChannel.
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)
"""
kernel_invariants = {"suservo_channel", "default_kp", "default_ki", "default_ki_limit"}
def __init__(self, default_kp: TFloat,
default_ki: TFloat = 0.0,
default_ki_limit: TFloat = 0.0,
*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)
and isinstance(self.photodiode.adc_channel, SUServoADCChannel)):
self.experiment.log.error(f"The photodiode {self.photodiode.identifier} is not configured to be connected \
to an SUServo where this 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)
and isinstance(self.modulator.rfsource, SUServoChannel)):
self.experiment.log.error(f"The modulator {self.modulator.identifier} is not configured to be connected to \
an SUServo where this is required for {self.identifier}")
self.suservo_channel = self.modulator.rfsource
if isinstance(self.photodiode, CalibratedPhotodiode):
replace_member(self, "_transform_power", "_transform_power_calibration")
else:
replace_member(self, "_transform_power", "_transform_power_dummy")
@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.photodiode.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: TFloat, ki: TFloat, ki_limit: TFloat, delay: TFloat = 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. (default 0.0)
ki_limit: limit for I-part of the SUServo loop. 0.0 means unlimited. (default 0.0)
delay: delay between switching on the laser an the servo loop becoming active
"""
self.suservo_channel.suservo_channel.set_iir(self.suservo_channel.profile, adc_channel, kp, ki, ki_limit, delay)
@kernel
def _stabilize(self, enable: TBool):
self.stabilized = enable
self.suservo_channel.set_servomode(enable)
@portable
def _transform_power_dummy(self, power: TFloat) -> TFloat:
return power
@portable
def _transform_power_calibration(self, power: TFloat) -> TFloat:
if self.stabilized:
return self.photodiode.calibration.transform_inv(power)
else:
return power
@portable
def _amplitude_from_power(self, power: TFloat) -> TFloat:
if self.stabilized:
return self._transform_power(power)
else:
return power / self.src_transmission / self.laser_source.get_power()
[docs]
@kernel
def set_power(self, power: TFloat):
"""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).
"""
self.suservo_channel.set_amplitude(amplitude=self._amplitude_from_power(power))