noptica2-sdr/dmi.py

54 lines
1.8 KiB
Python
Raw Normal View History

import SoapySDR
import numpy as np
2020-01-05 19:31:07 +08:00
2020-01-11 18:41:18 +08:00
from noptica import InductionHeater, Stabilizer, PositionTracker
2020-01-05 19:31:07 +08:00
def main():
2020-01-11 16:06:54 +08:00
freq_sample = 5e6
freq_base = 1086e6
2020-01-11 18:41:18 +08:00
bufsize = 4096
2020-01-11 16:06:54 +08:00
2020-01-25 15:26:43 +08:00
induction = InductionHeater("/dev/ttyUSB0", 350e3, 445e3)
2020-01-05 19:31:07 +08:00
induction.start()
try:
2020-01-25 15:26:43 +08:00
stabilizer = Stabilizer(freq_sample, 0.4, 1088.1e6 - freq_base, 50e-6, induction)
2020-01-11 18:41:18 +08:00
position_tracker = PositionTracker(int(0.1*freq_sample/bufsize))
2020-01-05 19:31:07 +08:00
2020-01-11 16:06:54 +08:00
sdr = SoapySDR.Device()
2020-01-11 18:41:18 +08:00
for channel in range(2):
sdr.setSampleRate(SoapySDR.SOAPY_SDR_RX, channel, freq_sample)
sdr.setFrequency(SoapySDR.SOAPY_SDR_RX, channel, freq_base)
2020-01-05 19:31:07 +08:00
2020-01-11 18:41:18 +08:00
samples_ref = np.array([0]*bufsize, np.complex64)
samples_meas = np.array([0]*bufsize, np.complex64)
rx_stream = sdr.setupStream(SoapySDR.SOAPY_SDR_RX, SoapySDR.SOAPY_SDR_CF32, [0, 1])
2020-01-05 19:31:07 +08:00
try:
2020-01-11 16:06:54 +08:00
sdr.activateStream(rx_stream)
try:
stabilizer_throttle = 0
while True:
2020-01-11 18:41:18 +08:00
sr = sdr.readStream(rx_stream, [samples_ref, samples_meas], bufsize)
if sr.ret != bufsize:
2020-01-11 16:06:54 +08:00
print("SDR sampling error")
return
2020-01-11 18:41:18 +08:00
# We can't update faster than the MHS5200A serial interface
2020-01-11 16:06:54 +08:00
stabilizer_throttle += 1
if stabilizer_throttle == 30:
2020-01-11 18:41:18 +08:00
stabilizer.input(samples_ref)
2020-01-11 16:06:54 +08:00
stabilizer_throttle = 0
2020-01-11 18:41:18 +08:00
position, leakage = position_tracker.input(samples_ref, samples_meas)
print(np.sum(position)/len(position), leakage)
2020-01-11 16:06:54 +08:00
finally:
sdr.deactivateStream(rx_stream)
2020-01-05 19:31:07 +08:00
finally:
2020-01-11 16:06:54 +08:00
sdr.closeStream(rx_stream)
2020-01-05 19:31:07 +08:00
finally:
induction.stop()
if __name__ == "__main__":
main()