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.

main.py 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. from reader_interface import ReaderInterface
  2. import numpy as np
  3. from calibration import Calibration
  4. from localization import Localization
  5. from plot_results import Plotting
  6. from moving_average import MovingAverage
  7. from tkinter import *
  8. import time
  9. import socket
  10. """-----------------------------------GUI Code-------------------------------------------------------------------- """
  11. def calibrate():
  12. global calibration
  13. print("-----Started calibration...")
  14. calibration.new_calibration()
  15. def start_recording():
  16. global record, recording_array
  17. recording_array = np.zeros((record_length.get(), 19), dtype=np.float)
  18. record = True
  19. print("-----Started recording...")
  20. def calculate_positions():
  21. localization.scale_factor = float(scale_factor.get())
  22. localization.k_nearest = k_nearest.get()
  23. localization.localize_all_samples(filename_localize_in.get(), filename_localize_out.get())
  24. def plot_antennas():
  25. antenna_list_formatted = antenna_list.get().split(', ')
  26. print("Antenna plots were made from : recorded_data/", filename_record_out.get(), "| it uses filename_record_out")
  27. plotting.plot_antennas(filename_record_out.get(), antenna_list_formatted)
  28. def plot_positions():
  29. plotting.plot_positions(filename_plot_in.get())
  30. # Set up Labelframes
  31. root = Tk()
  32. record = LabelFrame(root, text="Record")
  33. record.grid(row=0, column=0, sticky=N+E+S+W)
  34. localize = LabelFrame(root, text="Localize")
  35. localize.grid(row=0, column=1, sticky=N+E+S+W)
  36. plot = LabelFrame(root, text="Plot")
  37. plot.grid(row=1, column=1)
  38. # Record Labelframe
  39. filename_record_out = StringVar(record)
  40. filename_record_out.set('current_recording')
  41. record_length = IntVar(record)
  42. record_length.set(1000)
  43. label_record_length = Label(record, text="Number of samples:")
  44. label_record_length.grid(row=0, column=0, sticky=W)
  45. entry_record_length = Entry(record, textvariable=record_length)
  46. entry_record_length.grid(row=0, column=1, sticky=N + E + S + W)
  47. label_filename_record_out = Label(record, text="Filename (Out):")
  48. label_filename_record_out.grid(row=1, column=0, sticky=W)
  49. entry_filename_record_out = Entry(record, textvariable=filename_record_out)
  50. entry_filename_record_out.grid(row=1, column=1, sticky=N + E + S + W)
  51. button_calibrate = Button(record, text="Calibrate", command=calibrate)
  52. button_calibrate.grid(row=2, column=0, sticky=N + E + S + W)
  53. button_record = Button(record, text="Record", command=start_recording)
  54. button_record.grid(row=2, column=1, sticky=N + E + S + W)
  55. # Localize Labelframe
  56. filename_localize_in = StringVar(root)
  57. filename_localize_in.set('current_recording')
  58. scale_factor = StringVar(plot)
  59. scale_factor.set("0.003")
  60. k_nearest = IntVar(plot)
  61. k_nearest.set(10)
  62. filename_localize_out = StringVar(root)
  63. filename_localize_out.set('current_positions')
  64. label_filename_localize_in = Label(localize, text="Filename (In):")
  65. label_filename_localize_in.grid(row=0, column=0, sticky=W)
  66. emtry_filename_localize_in = Entry(localize, textvariable=filename_localize_in)
  67. emtry_filename_localize_in.grid(row=0, column=1, sticky=N + E + S + W)
  68. label_scale_factor = Label(localize, text="Scale factor:")
  69. label_scale_factor.grid(row=1, column=0, sticky=W)
  70. entry_scale_factor = Entry(localize, textvariable=scale_factor)
  71. entry_scale_factor.grid(row=1, column=1, sticky=N + E + S + W)
  72. label_k_nearest = Label(localize, text="k_nearest:")
  73. label_k_nearest.grid(row=2, column=0, sticky=W)
  74. entry_k_nearest = Entry(localize, textvariable=k_nearest)
  75. entry_k_nearest.grid(row=2, column=1, sticky=N + E + S + W)
  76. label_filename_localize_out = Label(localize, text="Filename (Out):")
  77. label_filename_localize_out.grid(row=3, column=0, sticky=W)
  78. entry_filename_localize_out = Entry(localize, textvariable=filename_localize_out)
  79. entry_filename_localize_out.grid(row=3, column=1, sticky=N + E + S + W)
  80. button_calculate_positions = Button(localize, text="Calculate positions", command=calculate_positions)
  81. button_calculate_positions.grid(row=4, column=0)
  82. # Plot Labelframe
  83. filename_plot_in = StringVar(plot)
  84. filename_plot_in.set('current_positions')
  85. antenna_list = StringVar(plot)
  86. antenna_list.set("f1, f2, f3, f4")
  87. label_filename_plot_in = Label(plot, text="Filename (In):")
  88. label_filename_plot_in.grid(row=0, column=0, sticky=W)
  89. entry_filename_plot_in = Entry(plot, textvariable=filename_plot_in)
  90. entry_filename_plot_in.grid(row=0, column=1, sticky=N + E + S + W)
  91. label_antennas = Label(plot, text="Antenna(s): [f1, f2, m1, m3, ...]")
  92. label_antennas.grid(row=1, column=0)
  93. entry_antennas = Entry(plot, textvariable=antenna_list)
  94. entry_antennas.grid(row=1, column=1)
  95. button_plot_antennas = Button(plot, text="Plot Antennas", command=plot_antennas)
  96. button_plot_antennas.grid(row=1, column=2)
  97. button_plot_positons = Button(plot, text="Plot Positions", command=plot_positions)
  98. button_plot_positons.grid(row=2, column=0)
  99. """ --------------------- Initializing Parameters--------------------------------------------------------------------"""
  100. # Localization Parameters
  101. moving_average_length = 100
  102. moving_avg = MovingAverage(moving_average_length)
  103. localization_method = 'k-nearest' # full_table
  104. #scale_factor = 1
  105. table = np.load('fingerprinting_tables/Julian_BThesis_table2.npy') # load fingerprinting table
  106. #k_nearest = 15
  107. timer_stream_positions_to_mqtt_client = time.time()
  108. # Recording parameters
  109. recording_index = 0
  110. record = False
  111. recording_array = recording_array = np.zeros((1000, 19))
  112. counter = 0
  113. startTime = time.time()
  114. samples_counter = 1
  115. socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  116. # fingerprinting_table = np.loadtxt("fingerprinting_tables/Julian_BThesis_table2.csv", delimiter=';')
  117. # np.save("fingerprinting_tables/Julian_BThesis_table2", fingerprinting_table)
  118. def set_reader():
  119. config = {
  120. "samples_block_size": 10,
  121. "resistance": .55,
  122. "inductance": 10e-6,
  123. "loadFactor": 1.0,
  124. "channels": [
  125. (119000, 1.1),
  126. (61500, 0.0),
  127. (50000, 0.0),
  128. (142000, 0.0)],
  129. # "channelPermutation": [0, 1, 3, 2, 4, 5, 6, 7, 8, 9,
  130. # 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  131. # 20],
  132. "channelPermutation" :[1, 0, 3, 2, 4, 5, 7, 6, 9, 8, #config for hmi shelf
  133. 11, 10, 13, 12, 15, 14, 17, 16, 18, 19,
  134. 20],
  135. "numAntennas": 8
  136. }
  137. reader = ReaderInterface(resistance=config["resistance"],
  138. inductance=config["inductance"],
  139. load_factor=config["loadFactor"],
  140. num_antennas=config["numAntennas"],
  141. channel_permutation=config["channelPermutation"])
  142. for idx, c in enumerate(config["channels"]):
  143. reader.setFrequency(idx, c[0])
  144. if c[1] > 0: # CHANGED c[1] -> c[0]
  145. reader.setExciterCurrent(idx, c[1]) # CHANGED
  146. reader.setExciterEnabled(idx, True)
  147. reader.setChannel20Switch(3) # Set channel 20 to current feedback
  148. reader.enableConfiguration()
  149. return reader
  150. def process_sample(samples):
  151. global record, recording_index, recording_array, record_length, filename, counter, \
  152. startTime, samples_counter, timer_stream_positions_to_mqtt_client
  153. if time.time() - startTime > 1:
  154. counter = 0
  155. startTime = time.time()
  156. else:
  157. counter += 1
  158. for sample in samples:
  159. rawdata = sample.getRawData()
  160. rawdata = rawdata[0, :]
  161. calibrated_sample = calibration.process_sample(rawdata)
  162. if calibrated_sample is not None:
  163. averaged_sample = moving_avg.processSample(calibrated_sample)
  164. frame_antennas = np.imag(averaged_sample[0:16:2])
  165. main_antennas = np.imag(averaged_sample[1:16:2])
  166. frames_list = list(frame_antennas)
  167. mains_list = list(main_antennas)
  168. fv = np.append(frame_antennas, main_antennas)
  169. pos = [0, 0, 0]
  170. if record:
  171. if recording_index < (record_length.get()):
  172. # pos = localize(fv)
  173. recording_array[recording_index, 0:3] = pos
  174. recording_array[recording_index, 3:] = fv
  175. recording_index += 1
  176. print("recording progress=", recording_index)
  177. # print("Recording progress : ", round((recording_index / record_length.get()) * 100, 3), "%")
  178. else:
  179. np.save("recorded_data/" + entry_filename_record_out.get(), recording_array)
  180. print('-----Finished recording. Saved in: recorded_data/' + entry_filename_record_out.get() + ".npy ( shape=",
  181. np.shape(recording_array), ")")
  182. recording_index = 0
  183. record = False
  184. else:
  185. samples_counter += 1
  186. """ ------------------------ Setting up reader and starting request Data Thread-----------------------------------"""
  187. calibration = Calibration()
  188. reader = set_reader()
  189. request = reader.requestData(process_sample, blockSize=100, blocks=-1)
  190. localization = Localization()
  191. plotting = Plotting()
  192. root.mainloop()