Skip to content

Commit

Permalink
Merge branch 'feature/support_tackle_PR196' into 'master'
Browse files Browse the repository at this point in the history
feat(tools): logging corrupted CSI data and other serial output

See merge request esp-components/esp-csi-ext!57
  • Loading branch information
zhanzhaocheng committed Aug 8, 2024
2 parents ec766fe + da24a81 commit 5514403
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
14 changes: 7 additions & 7 deletions examples/esp-radar/console_test/tools/esp_csi_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"waveform_wander", "wander_average", "waveform_wander_threshold", "someone_status",
"waveform_jitter", "jitter_midean", "waveform_jitter_threshold", "move_status"]

g_csi_phase_array = np.zeros(
g_csi_amplitude_array = np.zeros(
[CSI_DATA_INDEX, CSI_DATA_COLUMNS], dtype=np.int32)
g_rssi_array = np.zeros(CSI_DATA_INDEX, dtype=np.int8)
g_radio_header_pd = pd.DataFrame(np.zeros([10, len(
Expand Down Expand Up @@ -259,7 +259,7 @@ def __init__(self, serial_queue_write, parent=None):

for i in range(CSI_DATA_COLUMNS):
curve = self.graphicsView_subcarrier.plot(
g_csi_phase_array[:, i], name=str(i), pen=csi_vaid_subcarrier_color[i])
g_csi_amplitude_array[:, i], name=str(i), pen=csi_vaid_subcarrier_color[i])
self.curve_subcarrier.append(curve)

self.curve_rssi = self.graphicsView_rssi.plot(
Expand Down Expand Up @@ -525,10 +525,10 @@ def show_curve_subcarrier(self):
b, a = signal.butter(8, wn, 'lowpass')

if self.wave_filtering_flag:
self.median_filtering(g_csi_phase_array)
csi_filtfilt_data = signal.filtfilt(b, a, g_csi_phase_array.T).T
self.median_filtering(g_csi_amplitude_array)
csi_filtfilt_data = signal.filtfilt(b, a, g_csi_amplitude_array.T).T
else:
csi_filtfilt_data = g_csi_phase_array
csi_filtfilt_data = g_csi_amplitude_array

if self.curve_subcarrier_range[0] > csi_filtfilt_data.min() or self.curve_subcarrier_range[1] < csi_filtfilt_data.max():
if csi_filtfilt_data.min() > 0 and self.curve_subcarrier_range[0] > csi_filtfilt_data.min():
Expand Down Expand Up @@ -909,15 +909,15 @@ def closeEvent(self, event):

def csi_data_handle(self, data):
# Rotate data
g_csi_phase_array[:-1] = g_csi_phase_array[1:]
g_csi_amplitude_array[:-1] = g_csi_amplitude_array[1:]
g_rssi_array[:-1] = g_rssi_array[1:]
g_radio_header_pd.iloc[1:] = g_radio_header_pd.iloc[:-1]

csi_raw_data = data['data']
for i in range(CSI_DATA_COLUMNS):
data_complex = complex(csi_raw_data[csi_vaid_subcarrier_index[i] * 2],
csi_raw_data[csi_vaid_subcarrier_index[i] * 2 - 1])
g_csi_phase_array[-1][i] = np.abs(data_complex)
g_csi_amplitude_array[-1][i] = np.abs(data_complex)

g_rssi_array[-1] = data['rssi']
g_radio_header_pd.loc[0] = data[1:len(CSI_DATA_COLUMNS_NAMES)-1]
Expand Down
37 changes: 28 additions & 9 deletions examples/get-started/tools/csi_data_read_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,28 @@ def __init__(self):
self.plotWidget_ted.setYRange(-20, 100)
self.plotWidget_ted.addLegend()

self.csi_phase_array = np.abs(csi_data_array)
self.csi_amplitude_array = np.abs(csi_data_array)
self.curve_list = []

# print(f"csi_vaid_subcarrier_color, len: {len(csi_vaid_subcarrier_color)}, {csi_vaid_subcarrier_color}")

for i in range(CSI_DATA_COLUMNS):
curve = self.plotWidget_ted.plot(
self.csi_phase_array[:, i], name=str(i), pen=csi_vaid_subcarrier_color[i])
self.csi_amplitude_array[:, i], name=str(i), pen=csi_vaid_subcarrier_color[i])
self.curve_list.append(curve)

self.timer = pq.QtCore.QTimer()
self.timer.timeout.connect(self.update_data)
self.timer.start(100)

def update_data(self):
self.csi_phase_array = np.abs(csi_data_array)
self.csi_amplitude_array = np.abs(csi_data_array)

for i in range(CSI_DATA_COLUMNS):
self.curve_list[i].setData(self.csi_phase_array[:, i])
self.curve_list[i].setData(self.csi_amplitude_array[:, i])


def csi_data_read_parse(port: str, csv_writer):
def csi_data_read_parse(port: str, csv_writer, log_file_fd):
ser = serial.Serial(port=port, baudrate=921600,
bytesize=8, parity='N', stopbits=1)
if ser.isOpen():
Expand All @@ -121,23 +121,37 @@ def csi_data_read_parse(port: str, csv_writer):
index = strings.find('CSI_DATA')

if index == -1:
# Save serial output other than CSI data
log_file_fd.write(strings + '\n')
log_file_fd.flush()
continue

csv_reader = csv.reader(StringIO(strings))
csi_data = next(csv_reader)

if len(csi_data) != len(DATA_COLUMNS_NAMES):
print("element number is not equal")
log_file_fd.write("element number is not equal\n")
log_file_fd.write(strings + '\n')
log_file_fd.flush()
continue

try:
csi_raw_data = json.loads(csi_data[-1])
except json.JSONDecodeError:
print(f"data is incomplete")
print("data is incomplete")
log_file_fd.write("data is incomplete\n")
log_file_fd.write(strings + '\n')
log_file_fd.flush()
continue

# Reference on the length of CSI data and usable subcarriers
# https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/wifi.html#wi-fi-channel-state-information
if len(csi_raw_data) != 128 and len(csi_raw_data) != 256 and len(csi_raw_data) != 384:
print(f"element number is not equal: {len(csi_raw_data)}")
log_file_fd.write(f"element number is not equal: {len(csi_raw_data)}\n")
log_file_fd.write(strings + '\n')
log_file_fd.flush()
continue

csv_writer.writerow(csi_data)
Expand All @@ -159,19 +173,21 @@ def csi_data_read_parse(port: str, csv_writer):


class SubThread (QThread):
def __init__(self, serial_port, save_file_name):
def __init__(self, serial_port, save_file_name, log_file_name):
super().__init__()
self.serial_port = serial_port

save_file_fd = open(save_file_name, 'w')
self.log_file_fd = open(log_file_name, 'w')
self.csv_writer = csv.writer(save_file_fd)
self.csv_writer.writerow(DATA_COLUMNS_NAMES)

def run(self):
csi_data_read_parse(self.serial_port, self.csv_writer)
csi_data_read_parse(self.serial_port, self.csv_writer, self.log_file_fd)

def __del__(self):
self.wait()
self.log_file_fd.close()


if __name__ == '__main__':
Expand All @@ -185,14 +201,17 @@ def __del__(self):
help="Serial port number of csv_recv device")
parser.add_argument('-s', '--store', dest='store_file', action='store', default='./csi_data.csv',
help="Save the data printed by the serial port to a file")
parser.add_argument('-l', '--log', dest="log_file", action="store", default="./csi_data_log.txt",
help="Save other serial data the bad CSI data to a log file")

args = parser.parse_args()
serial_port = args.port
file_name = args.store_file
log_file_name = args.log_file

app = QApplication(sys.argv)

subthread = SubThread(serial_port, file_name)
subthread = SubThread(serial_port, file_name, log_file_name)
subthread.start()

window = csi_data_graphical_window()
Expand Down

0 comments on commit 5514403

Please sign in to comment.