You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.9KB

  1. from math import ceil
  2. import SoapySDR
  3. import numpy as np
  4. import gc
  5. from noptica import *
  6. from gui import GUI
  7. def main():
  8. # For this PoC code, a small memory leak is less harmful than random overflows.
  9. gc.disable()
  10. freq_sample = 1e6
  11. freq_base = 1088230e3
  12. freq_ref = 1088100e3
  13. block_size = 4096
  14. throttle_factor = ceil(freq_sample/125e3)
  15. # note: image band still goes through
  16. freq_ref_abs = np.abs(freq_ref-freq_base)
  17. ref_filter = LinearPhaseFilter(127, [freq_ref_abs-15e3, freq_ref_abs+15e3], pass_zero=False, fs=freq_sample)
  18. meas_delay = Delay(block_size, ref_filter.delay())
  19. gui = GUI(freq_sample, freq_base, block_size)
  20. try:
  21. induction = InductionHeater("/dev/ttyUSB0", 350e3, 445e3)
  22. induction.start()
  23. try:
  24. def stabilizer_cb(spectrum, peak_freq, locked, tuning):
  25. gui.update_ref(spectrum, peak_freq, locked)
  26. induction.set(tuning)
  27. stabilizer = Stabilizer(freq_sample, block_size, freq_ref-freq_base, stabilizer_cb)
  28. position_tracker = PositionTracker()
  29. sdr = SoapySDR.Device()
  30. for channel in range(2):
  31. sdr.setSampleRate(SoapySDR.SOAPY_SDR_RX, channel, freq_sample)
  32. sdr.setFrequency(SoapySDR.SOAPY_SDR_RX, channel, freq_base)
  33. sdr.setGain(SoapySDR.SOAPY_SDR_RX, channel, 55.0)
  34. buf_sdr = BufferedSDR(sdr, [0, 1], block_size, 32)
  35. buf_sdr.start()
  36. try:
  37. throttle = 0
  38. position_acc = 0.0
  39. while True:
  40. buffers = buf_sdr.get()
  41. try:
  42. samples_ref, samples_meas = buffers
  43. # Throttle certain things to avoid overflows due to the limited speed of
  44. # the MHS5200A serial interface and GUI plotting.
  45. throttle += 1
  46. if throttle == throttle_factor:
  47. throttle = 0
  48. if throttle == 0:
  49. stabilizer.input(samples_ref)
  50. gui.update_meas(samples_meas)
  51. if stabilizer.locked():
  52. position = position_tracker.input(ref_filter.input(samples_ref), meas_delay.input(samples_meas))
  53. position_acc += np.mean(position)
  54. if throttle == 0:
  55. gui.update_position(position_acc/throttle_factor)
  56. position_acc = 0.0
  57. else:
  58. position_tracker.reset()
  59. position_acc = 0.0
  60. finally:
  61. buf_sdr.dispose(buffers)
  62. finally:
  63. buf_sdr.stop()
  64. finally:
  65. induction.stop()
  66. finally:
  67. gui.close()
  68. if __name__ == "__main__":
  69. main()