noptica2-sdr/dmi.py

85 行
2.9 KiB
Python

from math import ceil
import SoapySDR
import numpy as np
2020-01-27 17:24:59 +08:00
import gc
2020-01-05 19:31:07 +08:00
2020-01-27 17:24:59 +08:00
from noptica import *
2020-08-10 19:22:58 +08:00
from gui import GUI
2020-01-05 19:31:07 +08:00
def main():
2020-01-27 17:24:59 +08:00
# For this PoC code, a small memory leak is less harmful than random overflows.
gc.disable()
2020-08-10 14:29:55 +08:00
freq_sample = 1e6
freq_base = 1088230e3
2020-09-27 14:03:13 +08:00
freq_ref = 1088100e3
2020-08-10 19:22:58 +08:00
block_size = 4096
throttle_factor = ceil(freq_sample/125e3)
2020-01-11 16:06:54 +08:00
2020-09-27 14:03:13 +08:00
# note: image band still goes through
freq_ref_abs = np.abs(freq_ref-freq_base)
ref_filter = LinearPhaseFilter(127, [freq_ref_abs-15e3, freq_ref_abs+15e3], pass_zero=False, fs=freq_sample)
meas_delay = Delay(block_size, ref_filter.delay())
2020-08-10 19:55:34 +08:00
gui = GUI(freq_sample, freq_base, block_size)
2020-01-05 19:31:07 +08:00
try:
2020-08-10 19:22:58 +08:00
induction = InductionHeater("/dev/ttyUSB0", 350e3, 445e3)
induction.start()
2020-01-05 19:31:07 +08:00
try:
def stabilizer_cb(spectrum, peak_freq, locked, tuning):
2020-08-11 16:09:24 +08:00
gui.update_ref(spectrum, peak_freq, locked)
2020-08-10 19:22:58 +08:00
induction.set(tuning)
2020-09-27 14:03:13 +08:00
stabilizer = Stabilizer(freq_sample, block_size, freq_ref-freq_base, stabilizer_cb)
2020-08-11 17:48:11 +08:00
position_tracker = PositionTracker()
2020-08-10 19:22:58 +08:00
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)
2020-08-16 17:45:51 +08:00
sdr.setGain(SoapySDR.SOAPY_SDR_RX, channel, 55.0)
2020-08-10 19:22:58 +08:00
2020-08-11 17:48:11 +08:00
buf_sdr = BufferedSDR(sdr, [0, 1], block_size, 32)
2020-08-10 19:22:58 +08:00
buf_sdr.start()
try:
2020-08-11 17:48:11 +08:00
throttle = 0
2020-08-11 18:35:30 +08:00
position_acc = 0.0
2020-08-10 19:22:58 +08:00
while True:
buffers = buf_sdr.get()
try:
samples_ref, samples_meas = buffers
2020-08-11 17:48:11 +08:00
# Throttle certain things to avoid overflows due to the limited speed of
# the MHS5200A serial interface and GUI plotting.
throttle += 1
2020-08-11 18:35:30 +08:00
if throttle == throttle_factor:
2020-08-11 17:48:11 +08:00
throttle = 0
2020-08-10 19:22:58 +08:00
2020-08-11 17:48:11 +08:00
if throttle == 0:
stabilizer.input(samples_ref)
2020-08-11 16:09:24 +08:00
gui.update_meas(samples_meas)
2020-08-11 16:24:50 +08:00
if stabilizer.locked():
2020-09-27 14:03:13 +08:00
position = position_tracker.input(ref_filter.input(samples_ref), meas_delay.input(samples_meas))
2020-08-16 17:45:57 +08:00
position_acc += np.mean(position)
2020-08-11 17:48:11 +08:00
if throttle == 0:
2020-08-11 18:35:30 +08:00
gui.update_position(position_acc/throttle_factor)
position_acc = 0.0
2020-08-11 16:24:50 +08:00
else:
position_tracker.reset()
2020-08-11 18:35:30 +08:00
position_acc = 0.0
2020-08-10 19:22:58 +08:00
finally:
buf_sdr.dispose(buffers)
finally:
buf_sdr.stop()
2020-01-05 19:31:07 +08:00
finally:
2020-08-10 19:22:58 +08:00
induction.stop()
2020-01-05 19:31:07 +08:00
finally:
2020-08-10 19:22:58 +08:00
gui.close()
2020-01-05 19:31:07 +08:00
if __name__ == "__main__":
main()