feat(devkit): 每帧打印接收数据,持续输出到CSV文件
This commit is contained in:
@@ -231,31 +231,55 @@ def _append_analysis_log(source_csv: str, stats: dict):
|
||||
class SensorPushServicer(sensor_stream_pb2_grpc.SensorPushServicer):
|
||||
"""接收实时传感器帧(streaming)"""
|
||||
|
||||
_csv_path = None # 类变量,记录当前 CSV 路径
|
||||
|
||||
def __init__(self):
|
||||
self.frame_count = 0
|
||||
self.last_report_time = time.time()
|
||||
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):
|
||||
print("[SensorPush] Client connected, waiting for frames...")
|
||||
reset_baseline()
|
||||
self.last_angle = None
|
||||
self.frame_count = 0
|
||||
self._open_csv()
|
||||
|
||||
for frame in request_iterator:
|
||||
self.frame_count += 1
|
||||
angle = 0.0
|
||||
magnitude = 0.0
|
||||
state = 0
|
||||
ok = True
|
||||
message = "OK"
|
||||
if len(frame.matrix) == SENSOR_ROWS * SENSOR_COLS:
|
||||
try:
|
||||
angle, magnitude, state = get_pzt_angle(frame.matrix)
|
||||
self.last_angle = angle
|
||||
if self.frame_count <= 10 or self.frame_count % 30 == 0:
|
||||
print(
|
||||
f"[SensorPush] PZT angle frame #{frame.seq} "
|
||||
f"dts={frame.dts_ms} angle={angle:.2f} "
|
||||
f"mag={magnitude:.3f} state={state}"
|
||||
)
|
||||
# 打印接收到的数据
|
||||
print(f"[Recv #{frame.seq}] seq={frame.seq} ts={frame.timestamp_ms} dts={frame.dts_ms} "
|
||||
f"data={list(frame.matrix)}")
|
||||
except Exception as e:
|
||||
ok = False
|
||||
message = str(e)
|
||||
@@ -263,6 +287,16 @@ class SensorPushServicer(sensor_stream_pb2_grpc.SensorPushServicer):
|
||||
else:
|
||||
ok = False
|
||||
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(
|
||||
seq=frame.seq,
|
||||
@@ -291,6 +325,7 @@ class SensorPushServicer(sensor_stream_pb2_grpc.SensorPushServicer):
|
||||
f"total={self.frame_count} | ~{fps:.1f} fps"
|
||||
)
|
||||
|
||||
self._close_csv()
|
||||
print(f"[SensorPush] Stream ended. Total: {self.frame_count}")
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user