feat(devkit): 每帧打印接收数据,持续输出到CSV文件

This commit is contained in:
lenn
2026-05-22 16:33:20 +08:00
parent 030479a962
commit dff8489c36
2 changed files with 41 additions and 6 deletions

View File

@@ -231,31 +231,55 @@ def _append_analysis_log(source_csv: str, stats: dict):
class SensorPushServicer(sensor_stream_pb2_grpc.SensorPushServicer): class SensorPushServicer(sensor_stream_pb2_grpc.SensorPushServicer):
"""接收实时传感器帧streaming""" """接收实时传感器帧streaming"""
_csv_path = None # 类变量,记录当前 CSV 路径
def __init__(self): def __init__(self):
self.frame_count = 0 self.frame_count = 0
self.last_report_time = time.time() self.last_report_time = time.time()
self.last_angle = None self.last_angle = None
self._csv_file = None
self._csv_writer = None
def _open_csv(self):
"""打开一个新的 CSV 文件用于持续写入"""
ts = time.strftime("%Y%m%d_%H%M%S")
SensorPushServicer._csv_path = os.path.join(os.getcwd(), f"sensor_log_{ts}.csv")
self._csv_file = open(SensorPushServicer._csv_path, "w", newline="", encoding="utf-8-sig")
self._csv_writer = csv.writer(self._csv_file)
header = ["seq", "timestamp_ms", "dts_ms", "angle", "magnitude", "state", "resultant_force"] + [f"ch{i}" for i in range(SENSOR_ROWS * SENSOR_COLS)]
self._csv_writer.writerow(header)
self._csv_file.flush()
print(f"[SensorPush] CSV logging to: {SensorPushServicer._csv_path}")
def _close_csv(self):
"""关闭 CSV 文件"""
if self._csv_file:
self._csv_file.close()
print(f"[SensorPush] CSV saved: {SensorPushServicer._csv_path}")
self._csv_file = None
self._csv_writer = None
def Upload(self, request_iterator, context): def Upload(self, request_iterator, context):
print("[SensorPush] Client connected, waiting for frames...") print("[SensorPush] Client connected, waiting for frames...")
reset_baseline() reset_baseline()
self.last_angle = None self.last_angle = None
self.frame_count = 0
self._open_csv()
for frame in request_iterator: for frame in request_iterator:
self.frame_count += 1 self.frame_count += 1
angle = 0.0 angle = 0.0
magnitude = 0.0
state = 0
ok = True ok = True
message = "OK" message = "OK"
if len(frame.matrix) == SENSOR_ROWS * SENSOR_COLS: if len(frame.matrix) == SENSOR_ROWS * SENSOR_COLS:
try: try:
angle, magnitude, state = get_pzt_angle(frame.matrix) angle, magnitude, state = get_pzt_angle(frame.matrix)
self.last_angle = angle self.last_angle = angle
if self.frame_count <= 10 or self.frame_count % 30 == 0: # 打印接收到的数据
print( print(f"[Recv #{frame.seq}] seq={frame.seq} ts={frame.timestamp_ms} dts={frame.dts_ms} "
f"[SensorPush] PZT angle frame #{frame.seq} " f"data={list(frame.matrix)}")
f"dts={frame.dts_ms} angle={angle:.2f} "
f"mag={magnitude:.3f} state={state}"
)
except Exception as e: except Exception as e:
ok = False ok = False
message = str(e) message = str(e)
@@ -263,6 +287,16 @@ class SensorPushServicer(sensor_stream_pb2_grpc.SensorPushServicer):
else: else:
ok = False ok = False
message = f"Invalid matrix length: {len(frame.matrix)}" message = f"Invalid matrix length: {len(frame.matrix)}"
print(f"[Recv #{frame.seq}] INVALID len={len(frame.matrix)}")
# 持续写入 CSV
if self._csv_writer:
row = [frame.seq, frame.timestamp_ms, frame.dts_ms,
f"{angle:.4f}", f"{magnitude:.4f}", state, frame.resultant_force]
row += list(frame.matrix)
self._csv_writer.writerow(row)
if self.frame_count % 10 == 0:
self._csv_file.flush()
yield sensor_stream_pb2.PztAngleResponse( yield sensor_stream_pb2.PztAngleResponse(
seq=frame.seq, seq=frame.seq,
@@ -291,6 +325,7 @@ class SensorPushServicer(sensor_stream_pb2_grpc.SensorPushServicer):
f"total={self.frame_count} | ~{fps:.1f} fps" f"total={self.frame_count} | ~{fps:.1f} fps"
) )
self._close_csv()
print(f"[SensorPush] Stream ended. Total: {self.frame_count}") print(f"[SensorPush] Stream ended. Total: {self.frame_count}")