Compare commits

..

No commits in common. "cb1f1c047eb8eae2042d350c5cdba18b762dc1da" and "d4812e1f83247f0edf7ca58eef08e5dcb1f5ec25" have entirely different histories.

4 changed files with 44 additions and 70 deletions

28
dmi.py
View File

@ -20,42 +20,34 @@ def main():
induction.start()
try:
def stabilizer_cb(spectrum, peak_freq, locked, tuning):
gui.update_ref(spectrum, peak_freq, locked)
gui.update_beat_spectrum(spectrum, peak_freq, locked)
induction.set(tuning)
stabilizer = Stabilizer(freq_sample, block_size, 1088.1e6 - freq_base, stabilizer_cb)
position_tracker = PositionTracker()
position_tracker = PositionTracker(int(0.1*freq_sample/block_size))
sdr = SoapySDR.Device()
for channel in range(2):
sdr.setSampleRate(SoapySDR.SOAPY_SDR_RX, channel, freq_sample)
sdr.setFrequency(SoapySDR.SOAPY_SDR_RX, channel, freq_base)
buf_sdr = BufferedSDR(sdr, [0, 1], block_size, 32)
buf_sdr = BufferedSDR(sdr, [0, 1], block_size, 256)
buf_sdr.start()
try:
throttle = 0
stabilizer_throttle = 0
while True:
buffers = buf_sdr.get()
try:
samples_ref, samples_meas = buffers
# Throttle certain things to avoid overflows due to the limited speed of
# the MHS5200A serial interface and GUI plotting.
throttle += 1
if throttle == 8:
throttle = 0
if throttle == 0:
# We can't update faster than the MHS5200A serial interface
stabilizer_throttle += 1
if stabilizer_throttle == 8:
stabilizer.input(samples_ref)
gui.update_meas(samples_meas)
stabilizer_throttle = 0
if stabilizer.locked():
position = position_tracker.input(samples_ref, samples_meas)
if throttle == 0:
gui.update_position(np.sum(position)/len(position))
else:
position_tracker.reset()
#position, leakage = position_tracker.input(samples_ref, samples_meas)
#print(np.sum(position)/len(position), leakage)
finally:
buf_sdr.dispose(buffers)
finally:

12
gui.py
View File

@ -54,16 +54,8 @@ class GUI:
os.path.join(os.path.dirname(os.path.abspath(__file__)), "gui_impl.py"),
str(freq_sample), str(freq_base), str(block_size)])
def update_ref(self, block, peak_freq, locked):
obj = {"action": "update_ref", "block": block, "peak_freq": peak_freq, "locked": locked}
self.impl.write_pyon(obj)
def update_meas(self, block):
obj = {"action": "update_meas", "block": block}
self.impl.write_pyon(obj)
def update_position(self, position):
obj = {"action": "update_position", "position": position}
def update_beat_spectrum(self, block, peak_freq, locked):
obj = {"action": "update_beat_spectrum", "block": block, "peak_freq": peak_freq, "locked": locked}
self.impl.write_pyon(obj)
def close(self):

View File

@ -4,7 +4,6 @@ import sys
import logging
import numpy as np
from scipy.signal import blackmanharris
from quamash import QEventLoop, QtWidgets, QtCore
import pyqtgraph as pg
from sipyco.pipe_ipc import AsyncioChildComm
@ -16,15 +15,22 @@ class SpectrogramItem(pg.ImageItem):
pg.ImageItem.__init__(self)
depth = 100
self.setOpts(axisOrder="row-major")
self.img_array = np.zeros((depth, block_size))
self.setImage(self.img_array, autoLevels=True, autoDownsample=True)
self.setRect(QtCore.QRectF((freq_base-freq_sample/2)/1e6, -float(depth), freq_sample/1e6, float(depth)))
self.setImage(self.img_array, autoLevels=True)
pos = np.array([0., 1., 0.5, 0.25, 0.75])
color = np.array([[0,255,255,255], [255,255,0,255], [0,0,0,255], (0, 0, 255, 255), (255, 0, 0, 255)], dtype=np.ubyte)
cmap = pg.ColorMap(pos, color)
lut = cmap.getLookupTable(0.0, 1.0, 256)
self.setLookupTable(lut)
self.setRect(QtCore.QRectF(0.0, (freq_base-freq_sample/2)/1e6, float(depth), freq_sample/1e6))
def add_block(self, block):
self.img_array = np.roll(self.img_array, -1, 0)
self.img_array[-1:] = np.fft.fftshift(block)
self.setImage(self.img_array, autoLevels=True, autoDownsample=True)
self.img_array[-1:] = np.fft.fftshift(block)
self.setImage(self.img_array, autoLevels=True)
class MainWindow(pg.GraphicsLayoutWidget):
@ -34,28 +40,18 @@ class MainWindow(pg.GraphicsLayoutWidget):
self.freq_sample = freq_sample
self.freq_base = freq_base
self.block_size = block_size
self.text_ref = pg.LabelItem(size="24pt")
self.addItem(self.text_ref, row=0, col=0)
self.text_locked = pg.LabelItem(size="24pt")
self.addItem(self.text_locked, row=0, col=1)
self.update_ref(None, None, False)
self.update_params(None, False)
p1 = self.addPlot(row=1, col=0, colspan=2)
self.ref_spectrum = SpectrogramItem(freq_sample, freq_base, block_size)
p1.addItem(self.ref_spectrum)
self.beat_spectrum = SpectrogramItem(freq_sample, freq_base, block_size)
p1.addItem(self.beat_spectrum)
p2 = self.addPlot(row=2, col=0, colspan=2)
self.meas_spectrum = SpectrogramItem(freq_sample, freq_base, block_size)
p2.addItem(self.meas_spectrum)
self.position = self.addPlot(row=3, col=0, colspan=2)
self.position_history = np.zeros(300)
def update_ref(self, block, peak_freq, locked):
if block is not None:
self.ref_spectrum.add_block(block)
def update_params(self, peak_freq, locked):
if peak_freq is None:
self.text_ref.setText("REF: NO SIGNAL")
else:
@ -65,16 +61,6 @@ class MainWindow(pg.GraphicsLayoutWidget):
else:
self.text_locked.setText("REF LASER UNLOCKED", color="FF0000")
def update_meas(self, block):
assert len(block) == self.block_size
spectrum = np.abs(np.fft.fft(block*blackmanharris(self.block_size)))
self.meas_spectrum.add_block(spectrum)
def update_position(self, position):
self.position_history = np.roll(self.position_history, -1)
self.position_history[-1] = position
self.position.clear()
self.position.plot(self.position_history)
class IPCClient(AsyncioChildComm):
@ -90,12 +76,12 @@ class IPCClient(AsyncioChildComm):
obj = await self.read_pyon()
try:
action = obj["action"]
del obj["action"]
if action == "update_beat_spectrum":
main_window.beat_spectrum.add_block(obj["block"])
main_window.update_params(obj["peak_freq"], obj["locked"])
if action == "terminate":
self.close_cb()
return
else:
getattr(main_window, action)(**obj)
except:
logging.error("error processing parent message",
exc_info=True)

View File

@ -116,11 +116,11 @@ class Stabilizer:
self.wiggle_direction = 1
self.amp_threshold = 80.0
self.k = 30.0e-6
self.k = 50.0e-6
self.tolerance = 10e3
self.lock_counter_threshold = 60
self.unlock_counter_threshold = 500
self.wiggle = 0.15
self.unlock_counter_threshold = 600
self.wiggle = 0.1
def input(self, samples):
spectrum = np.abs(np.fft.fft(samples*blackmanharris(len(samples))))
@ -169,17 +169,21 @@ def continuous_unwrap(last_phase, last_phase_unwrapped, p):
class PositionTracker:
def __init__(self):
self.reset()
def reset(self):
def __init__(self, leakage_avg):
self.last_phase = 0.0
self.last_position = 0.0
self.leakage = np.zeros(leakage_avg)
self.leakage_ptr = 0
def input(self, ref, meas):
demod = np.conjugate(ref)*meas
phase = np.angle(demod)
self.leakage[self.leakage_ptr] = np.real(np.sum(demod)/len(demod))
self.leakage_ptr = (self.leakage_ptr + 1) % len(self.leakage)
leakage = np.sum(self.leakage)/len(self.leakage)
phase = np.angle(demod - leakage)
position = continuous_unwrap(self.last_phase, self.last_position, phase)/(2.0*np.pi)
self.last_phase = phase[-1]
self.last_position = position[-1]
return position
return position, leakage