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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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((int(record_length.get()), 19))
  18. record = True
  19. print("-----Started recording...")
  20. def save_array():
  21. global save
  22. save = True
  23. def calculate_positions():
  24. localization.scale_factor = float(scale_factor.get())
  25. localization.k_nearest = k_nearest.get()
  26. localization.localize_all_samples(filename_localize_in.get(), filename_localize_out.get()) # NORMAL LOCALIZATION
  27. #localization.localize_averaged_samples(filename_localize_in.get(), filename_localize_out.get()) #AVERAGED LOCALIZATION
  28. def plot_antennas():
  29. temp_ant_list = ""
  30. if check_var_all_frames.get() and not check_var_all_mains.get():
  31. temp_ant_list = "f1, f2, f3, f4, f5, f6, f7, f8"
  32. if check_var_all_mains.get() and not check_var_all_frames.get():
  33. temp_ant_list = "m1, m2, m3, m4, m5, m6, m7, m8"
  34. if check_var_all_frames.get() and check_var_all_mains.get():
  35. temp_ant_list = "f1, f2, f3, f4, f5, f6, f7, f8, m1, m2, m3, m4, m5, m6, m7, m8"
  36. if not check_var_all_frames.get() and not check_var_all_mains.get():
  37. temp_ant_list = antenna_list.get()
  38. temp_ant_list = temp_ant_list.split(', ')
  39. print("Antenna plots were made from : recorded_data/", filename_record_out.get(), "| it uses filename_record_out")
  40. plotting.plot_antennas(filename_record_out.get(), temp_ant_list, float(scale_factor.get()))
  41. def plot_positions():
  42. plotting.plot_positions(filename_plot_in.get())
  43. # Set up Labelframes
  44. root = Tk()
  45. record_labelframe = LabelFrame(root, text="Record")
  46. record_labelframe.grid(row=0, column=0, sticky=N+E+S+W)
  47. localize = LabelFrame(root, text="Localize")
  48. localize.grid(row=0, column=1, sticky=N+E+S+W)
  49. fingerprint = LabelFrame(root, text="ART")
  50. fingerprint.grid(row=1, column=0, sticky=N+E+S+W)
  51. plot = LabelFrame(root, text="Plot")
  52. plot.grid(row=1, column=1, sticky=N+E+S+W)
  53. # record_labelframeLabelframe
  54. filename_record_out = StringVar(record_labelframe)
  55. filename_record_out.set('current_recording')
  56. record_length = IntVar(record_labelframe)
  57. record_length.set(1000)
  58. label_record_length = Label(record_labelframe, text="Number of samples:")
  59. label_record_length.grid(row=0, column=0, sticky=W)
  60. entry_record_length = Entry(record_labelframe, textvariable=record_length)
  61. entry_record_length.grid(row=0, column=1, sticky=N + E + S + W)
  62. label_filename_record_out = Label(record_labelframe, text="Filename (Out):")
  63. label_filename_record_out.grid(row=1, column=0, sticky=W)
  64. entry_filename_record_out = Entry(record_labelframe, textvariable=filename_record_out)
  65. entry_filename_record_out.grid(row=1, column=1, sticky=N + E + S + W)
  66. button_calibrate = Button(record_labelframe, text="Calibrate", command=calibrate)
  67. button_calibrate.grid(row=2, column=0, sticky=N + E + S + W)
  68. button_record = Button(record_labelframe, text="Record", command=start_recording)
  69. button_record.grid(row=2, column=1, sticky=N + E + S + W)
  70. # Localize Labelframe
  71. filename_localize_in = StringVar(root)
  72. filename_localize_in.set('ART Recordings/Recording_1')
  73. scale_factor = StringVar(plot)
  74. scale_factor.set("0.001")
  75. k_nearest = IntVar(plot)
  76. k_nearest.set(2)
  77. filename_localize_out = StringVar(root)
  78. filename_localize_out.set('current_positions')
  79. label_filename_localize_in = Label(localize, text="Filename (In):")
  80. label_filename_localize_in.grid(row=0, column=0, sticky=W)
  81. emtry_filename_localize_in = Entry(localize, textvariable=filename_localize_in)
  82. emtry_filename_localize_in.grid(row=0, column=1, sticky=N + E + S + W)
  83. label_scale_factor = Label(localize, text="Scale factor:")
  84. label_scale_factor.grid(row=1, column=0, sticky=W)
  85. entry_scale_factor = Entry(localize, textvariable=scale_factor)
  86. entry_scale_factor.grid(row=1, column=1, sticky=N + E + S + W)
  87. label_k_nearest = Label(localize, text="k_nearest:")
  88. label_k_nearest.grid(row=2, column=0, sticky=W)
  89. entry_k_nearest = Entry(localize, textvariable=k_nearest)
  90. entry_k_nearest.grid(row=2, column=1, sticky=N + E + S + W)
  91. label_filename_localize_out = Label(localize, text="Filename (Out):")
  92. label_filename_localize_out.grid(row=3, column=0, sticky=W)
  93. entry_filename_localize_out = Entry(localize, textvariable=filename_localize_out)
  94. entry_filename_localize_out.grid(row=3, column=1, sticky=N + E + S + W)
  95. button_calculate_positions = Button(localize, text="Calculate positions", command=calculate_positions)
  96. button_calculate_positions.grid(row=4, column=0)
  97. # Plot Labelframe
  98. filename_plot_in = StringVar(plot)
  99. filename_plot_in.set('current_positions')
  100. antenna_list = StringVar(plot)
  101. antenna_list.set("f1, f2, m1, m2")
  102. check_var_all_frames = IntVar()
  103. check_var_all_mains = IntVar()
  104. label_filename_plot_in = Label(plot, text="Filename (In):")
  105. label_filename_plot_in.grid(row=0, column=0, sticky=W)
  106. entry_filename_plot_in = Entry(plot, textvariable=filename_plot_in)
  107. entry_filename_plot_in.grid(row=0, column=1, sticky=N + E + S + W)
  108. label_antennas = Label(plot, text="Specific Antenna(s):")
  109. label_antennas.grid(row=1, column=0, sticky = W)
  110. entry_antennas = Entry(plot, textvariable=antenna_list)
  111. entry_antennas.grid(row=1, column=1)
  112. checkbox_all_frames = Checkbutton(plot, variable=check_var_all_frames, text="All Frames", onvalue=1, offvalue=0)
  113. checkbox_all_frames.grid(row=2, column=0)
  114. checkbox_all_mains = Checkbutton(plot, variable=check_var_all_mains, text="All Mains", onvalue=1, offvalue=0)
  115. checkbox_all_mains.grid(row=2, column=1)
  116. button_plot_antennas = Button(plot, text="Plot Antennas", command=plot_antennas)
  117. button_plot_antennas.grid(row=2, column=2)
  118. button_plot_positons = Button(plot, text="Plot Positions", command=plot_positions)
  119. button_plot_positons.grid(row=3, column=0)
  120. # ART Labelframe
  121. """ --------------------- Initializing Parameters--------------------------------------------------------------------"""
  122. # Localization Parameters
  123. moving_average_length = 100
  124. moving_avg = MovingAverage(moving_average_length)
  125. localization_method = 'k-nearest' # full_table
  126. timer_stream_positions_to_mqtt_client = time.time()
  127. # Recording parameters
  128. recording_index = 0
  129. record = False
  130. recording_array = np.zeros((1000, 19))
  131. #recording_array = []
  132. counter = 0
  133. startTime = time.time()
  134. samples_counter = 1
  135. socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  136. fingerprinting_table = np.loadtxt("fingerprinting_tables/Julian_1cm_precision_corrected_antenas.csv", delimiter=';')
  137. np.save("fingerprinting_tables/Julian_1cm_precision_corrected_antenas", fingerprinting_table)
  138. print(fingerprinting_table)
  139. def set_reader():
  140. config = {
  141. "samples_block_size": 10,
  142. "resistance": .55,
  143. "inductance": 20e-6,
  144. "loadFactor": 1.0,
  145. "channels": [
  146. (119000, 0.35),
  147. (61500, 0.0),
  148. (50000, 0.0),
  149. (142000, 0.0)],
  150. # Channel permutation
  151. # Raw channels will be arranged in the samples as in this list.
  152. # Channels correspond to connectors in consecutive pairs (0/1 = A, 2/3 = B, ...).
  153. # In each pair the lower (even) channel is frame and the higher (odd) channel is main.
  154. # If you switch antenna connections between ports or have main/frame switched on one
  155. # port, you will have to reverse that change here to keep the same antenna names in software.
  156. # THIS PERMUTATION MUST BE 21 ELEMENTS LONG (21 channels in each sample)
  157. "channelPermutation" :[1, 0, 3, 2, 5, 4, 7, 6, 9, 8, #config for hmi shelf
  158. 11, 10, 13, 12, 15, 14, 17, 16, 18, 19, # in my setup with 8 Antennas, aynthing higher than 15 doesnt matter right?
  159. 20],
  160. "numAntennas": 8
  161. }
  162. reader = ReaderInterface(resistance=config["resistance"],
  163. inductance=config["inductance"],
  164. load_factor=config["loadFactor"],
  165. num_antennas=config["numAntennas"],
  166. channel_permutation=config["channelPermutation"])
  167. for idx, c in enumerate(config["channels"]):
  168. reader.setFrequency(idx, c[0])
  169. if c[1] > 0: # CHANGED c[1] -> c[0]
  170. reader.setExciterCurrent(idx, c[1]) # CHANGED
  171. reader.setExciterEnabled(idx, True)
  172. reader.setChannel20Switch(3) # Set channel 20 to current feedback
  173. reader.enableConfiguration()
  174. return reader
  175. def process_sample(samples):
  176. global record, recording_index, recording_array, record_length, filename, counter, \
  177. startTime, samples_counter, timer_stream_positions_to_mqtt_client
  178. if time.time() - startTime > 1:
  179. counter = 0
  180. startTime = time.time()
  181. else:
  182. counter += 1
  183. for sample in samples:
  184. #if record:
  185. #print(len(recording_array))
  186. rawdata = sample.getRawData()
  187. rawdata = rawdata[0, :]
  188. calibrated_sample = calibration.process_sample(rawdata)
  189. #recording_array.append(rawdata)
  190. if calibrated_sample is not None:
  191. averaged_sample = moving_avg.processSample(calibrated_sample)
  192. #before:
  193. #frame_antennas = np.abs(np.imag(averaged_sample[0:16:2])) #* -1
  194. #main_antennas = np.abs(np.imag(averaged_sample[1:16:2])) * -1
  195. #after:
  196. frame_antennas = np.imag(averaged_sample[0:16:2])
  197. main_antennas = np.imag(averaged_sample[1:16:2])
  198. frames_list = list(frame_antennas)
  199. mains_list = list(main_antennas)
  200. # Fixing the polarity of each antenna
  201. frames_list[0] = frames_list[0] * -1 # frame1
  202. frames_list[4] = frames_list[4] * -1 # frame5
  203. #mains_list[2] = mains_list[2] * -1 # main3
  204. mains_list[4] = mains_list[4] * -1 # main5
  205. mains_list[5] = mains_list[5] * -1 # main6
  206. #fv = np.append(frames_list, mains_list)
  207. pos = [0, 0, 0]
  208. fv = []
  209. for s in frames_list:
  210. fv.append(s)
  211. for s in mains_list:
  212. fv.append(s)
  213. if record:
  214. if recording_index < (record_length.get()):
  215. # pos = localize(fv)
  216. recording_array[recording_index, 0:3] = pos
  217. recording_array[recording_index, 3:] = fv
  218. recording_index += 1
  219. print("recording progress=", recording_index)
  220. # print("Recording progress : ", round((recording_index / record_length.get()) * 100, 3), "%")
  221. else:
  222. np.save("recorded_data/" + entry_filename_record_out.get(), recording_array)
  223. print('-----Finished recording. Saved in: recorded_data/' + entry_filename_record_out.get() + ".npy (shape=",
  224. np.shape(recording_array), ")")
  225. recording_index = 0
  226. record = False
  227. #else:
  228. #samples_counter += 1
  229. """ ------------------------ Setting up reader and starting request Data Thread-----------------------------------"""
  230. calibration = Calibration()
  231. reader = set_reader()
  232. request = reader.requestData(process_sample, blockSize=100, blocks=-1)
  233. localization = Localization()
  234. plotting = Plotting()
  235. root.mainloop()