noptica2-sdr/dmi.py

76 lines
2.5 KiB
Python
Raw Normal View History

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-08-10 19:22:58 +08:00
block_size = 4096
2020-01-11 16:06:54 +08:00
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-08-11 14:59:52 +08:00
stabilizer = Stabilizer(freq_sample, block_size, 1088.1e6 - 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-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
throttle_factor = 8
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-08-11 17:48:11 +08:00
position = position_tracker.input(samples_ref, samples_meas)
2020-08-11 18:35:30 +08:00
position_acc += np.sum(position)/len(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()