cpp data recorder

This commit is contained in:
2025-05-23 06:18:23 -05:00
parent fcb291590b
commit 7f2dd0103e
12 changed files with 788 additions and 203 deletions

View File

@@ -45,13 +45,16 @@ def main():
# CPI Parameters (timing values are in clk ticks)
num_pulses = 128
# Should be multiple of udp packet size, currently 4096 bytes, or 1024 samples
num_samples = 5000
num_samples = 16384
start_sample = 2000
tx_num_samples = 1024
tx_start_sample = start_sample
pri = int(.0004 * clk)
prf = 8000
pri = int(1/prf * clk)
pri -= (pri % 3)
# pri = int(.0001 * clk)
print(pri)
inter_cpi = 50
inter_cpi = 2000
tx_lo_offset = 10e6
rx_lo_offset = 0
@@ -63,9 +66,9 @@ def main():
recorder0 = DataRecorder("192.168.2.128", 1234, packet_size=radar.packet_size)
recorder1 = DataRecorder("192.168.3.128", 1235, packet_size=radar.packet_size)
# recorder1 = DataRecorder("192.168.3.128", 1235, packet_size=radar.packet_size)
recorder0.start_recording('test0.bin', True)
recorder1.start_recording('test1.bin', True)
# recorder1.start_recording('test1.bin', True)
radar.configure_cpi(pri, inter_cpi, num_pulses, num_samples, start_sample,
tx_num_samples, tx_start_sample, rx_lo_offset, tx_lo_offset)
@@ -73,83 +76,83 @@ def main():
print('Start Running')
radar.start_running()
# Let it run for a bit
time.sleep(2)
time.sleep(60)
# Stop running
radar.stop_running()
# Stop the data recorder
recorder0.stop_recording()
recorder1.stop_recording()
# Parse some data
# Find header, recording buffer could have wrapped depending on data rate and how long we ran for
recorders = [recorder0, recorder1]
for recorder in recorders:
headers = []
offset = 0
plot_recorder = recorder
hdr_sync = False
while not hdr_sync:
data = plot_recorder.buffer[offset:offset + 4]
sync_word = np.frombuffer(data, dtype=np.uint32)[0]
if sync_word == 0xAABBCCDD:
hdr_sync = True
print('Header found at offset', offset)
else:
offset += 4
num_cpi = 1
for i in range(num_cpi):
# Get Header
data = plot_recorder.buffer[offset:offset + ctypes.sizeof(data_structures.CpiHeader)]
offset += ctypes.sizeof(data_structures.CpiHeader)
headers.append(data_structures.CpiHeader.from_buffer_copy(data))
num_pulses = headers[i].num_pulses
num_samples = headers[i].num_samples
# Get CPI
data_size = num_pulses * num_samples * 4
data = plot_recorder.buffer[offset:offset + data_size]
offset += data_size
# Check some header fields
cpi_times = np.array([x.system_time for x in headers]) / 187.5e6
pps_frac = np.array([x.pps_frac_sec for x in headers]) / 187.5e6
pps_sec = np.array([x.pps_sec for x in headers])
utc_time = pps_sec + pps_frac
print(pri, inter_cpi, num_pulses * pri + inter_cpi)
print(cpi_times - cpi_times[0])
print(pps_frac)
print(pps_sec - pps_sec[0])
# Plot last CPI
data2 = np.frombuffer(data, dtype=np.int16)
i = data2[0::2]
q = data2[1::2]
iq = i + 1j * q
iq = iq.reshape(-1, num_samples)
iq = iq + 1e-15
vmin = -60
vmax = 0
fid, axs = plt.subplots(3)
axs[0].plot(iq.T.real, '-')
axs[0].plot(iq.T.imag, '--')
axs[0].grid()
# axs[1].imshow(db20n(iq), aspect='auto', interpolation='nearest', vmin=vmin, vmax=vmax)
axs[1].imshow(iq.real, aspect='auto', interpolation='nearest')
axs[1].set_ylabel('Pulse Count')
axs[1].set_xlabel('Sample Count')
iq_freq = np.fft.fftshift(np.fft.fft(iq, axis=1), axes=1)
freq_axis = (np.arange(num_samples)/num_samples - 0.5) * radar_manager.BASEBAND_SAMPLE_RATE / 1e6
axs[2].plot(freq_axis, db20n(iq_freq.T))
axs[2].grid()
plt.show()
# recorder1.stop_recording()
#
# # Parse some data
#
# # Find header, recording buffer could have wrapped depending on data rate and how long we ran for
# recorders = [recorder0, recorder1]
# for recorder in recorders:
# headers = []
# offset = 0
# plot_recorder = recorder
# hdr_sync = False
# while not hdr_sync:
# data = plot_recorder.buffer[offset:offset + 4]
# sync_word = np.frombuffer(data, dtype=np.uint32)[0]
# if sync_word == 0xAABBCCDD:
# hdr_sync = True
# print('Header found at offset', offset)
# else:
# offset += 4
#
# num_cpi = 1
# for i in range(num_cpi):
# # Get Header
# data = plot_recorder.buffer[offset:offset + ctypes.sizeof(data_structures.CpiHeader)]
# offset += ctypes.sizeof(data_structures.CpiHeader)
# headers.append(data_structures.CpiHeader.from_buffer_copy(data))
# num_pulses = headers[i].num_pulses
# num_samples = headers[i].num_samples
#
# # Get CPI
# data_size = num_pulses * num_samples * 4
# data = plot_recorder.buffer[offset:offset + data_size]
# offset += data_size
#
# # Check some header fields
# cpi_times = np.array([x.system_time for x in headers]) / 187.5e6
# pps_frac = np.array([x.pps_frac_sec for x in headers]) / 187.5e6
# pps_sec = np.array([x.pps_sec for x in headers])
# utc_time = pps_sec + pps_frac
# print(pri, inter_cpi, num_pulses * pri + inter_cpi)
# print(cpi_times - cpi_times[0])
# print(pps_frac)
# print(pps_sec - pps_sec[0])
#
# # Plot last CPI
# data2 = np.frombuffer(data, dtype=np.int16)
# i = data2[0::2]
# q = data2[1::2]
# iq = i + 1j * q
# iq = iq.reshape(-1, num_samples)
# iq = iq + 1e-15
#
# vmin = -60
# vmax = 0
#
# fid, axs = plt.subplots(3)
# axs[0].plot(iq.T.real, '-')
# axs[0].plot(iq.T.imag, '--')
# axs[0].grid()
#
# # axs[1].imshow(db20n(iq), aspect='auto', interpolation='nearest', vmin=vmin, vmax=vmax)
# axs[1].imshow(iq.real, aspect='auto', interpolation='nearest')
# axs[1].set_ylabel('Pulse Count')
# axs[1].set_xlabel('Sample Count')
#
# iq_freq = np.fft.fftshift(np.fft.fft(iq, axis=1), axes=1)
# freq_axis = (np.arange(num_samples)/num_samples - 0.5) * radar_manager.BASEBAND_SAMPLE_RATE / 1e6
# axs[2].plot(freq_axis, db20n(iq_freq.T))
# axs[2].grid()
#
#
# plt.show()
if __name__ == '__main__':