debug tools v1.1
authorkyz <kongyuzhong@google.com>
Wed, 31 Dec 2025 09:15:03 +0000 (17:15 +0800)
committerkyz <kongyuzhong@google.com>
Wed, 31 Dec 2025 09:15:20 +0000 (17:15 +0800)
修改调试工具时间轴数据

QiDong/debug_tools/main.py

index 826a6a6311a97573e2cf8a87b77c3d58fe58c530..946e93525169056e91f0312fdd46f830d650f261 100644 (file)
@@ -145,11 +145,18 @@ class page_devst( QWidget, ui_devst.Ui_Form):
             mouse_point = self.p_curr.vb.mapSceneToView(pos)
             x = mouse_point.x()
             y = mouse_point.y()
-            
+
             # 找到最接近的x坐标数据点
             num_points = len(self.y_press)  # 使用实际数据点数量
+            num_points = len(self.x_time)
             if num_points > 0:
-                index = int(round(x))
+
+                #从x_time中找到最接近的x坐标数据点,index为该点的索引
+                #使用enumerate(nums)获取每个元素的索引和值, min函数通过key参数定义排序规则
+                #abs(j[1] - target)计算元素值与目标值的绝对差值
+                #-j[0]确保当差值相同时,索引较大的元素被优先选择(因为-j[0]更小)
+                index = min(enumerate(self.x_time), key=lambda j: (abs(j[1] - x), -j[0]))[0]
+
                 if 0 <= index < num_points:
                     # 获取所有曲线在该点的值
                     target_val = self.y_target[index] if index < len(self.y_target) else 0
@@ -159,7 +166,7 @@ class page_devst( QWidget, ui_devst.Ui_Form):
                     thresold_val = self.y_thresold[index] if index < len(self.y_thresold) else 0
                     
                     # 显示信息
-                    info_str = f"点: {index}\n"
+                    info_str = f"时间: {self.x_time[index]:.3f}\n"
                     info_str += f"目标压力: {target_val:.3f}\n"
                     info_str += f"实时压力: {press_val:.3f}\n"
                     info_str += f"输出压力: {output_val:.3f}\n"
@@ -193,6 +200,7 @@ class page_devst( QWidget, ui_devst.Ui_Form):
             self.y_press = []
             self.y_output = []
             self.y_step = []
+            self.x_start_time = time.time()
         else:
             self.ser.write_pkt( 0xF1, struct.pack( '<f', -1.0 ))
             #stop timer
@@ -220,8 +228,10 @@ class page_devst( QWidget, ui_devst.Ui_Form):
             tick, step  = struct.unpack( '>LB', data[:5] )
             target, thresold, curr_press, output_press = struct.unpack( '<fffL', data[5:21] )
             
+            now = time.time() - self.x_start_time
             # 1. 优化日志写入 - 使用缓冲区
-            time_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
+            #time_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
+            time_str = f"{now:.3f}"
             log_entry = [time_str, f"{target:.3f}", f"{curr_press:.3f}", f"{thresold:.3f}", f"{output_press:d}", f"{step:d}"]
             self.log_buffer.append(log_entry)
 
@@ -236,7 +246,8 @@ class page_devst( QWidget, ui_devst.Ui_Form):
 #           print( "F2 data",len(data), data.hex() )
 #           print( "F2, recv: tick:%d step:%d target:%0.3f thresold:%0.3f curr_press:%0.3f output_press:%d"%(tick,step,target,thresold,curr_press,output_press) )
 
-            #self.x_time.append( datetime.now() )
+            #self.x_time.append( time.time() )
+            self.x_time.append( time.time() - self.x_start_time )
             self.y_target.append( target/10.0 *6.25 )
             self.y_thresold.append( thresold )
             self.y_press.append( curr_press )
@@ -260,15 +271,16 @@ class page_devst( QWidget, ui_devst.Ui_Form):
                 # 4. 优化图形绘制 - 避免不必要的NumPy转换
                 # PyQtGraph可以直接使用Python列表,不需要转换为NumPy数组
                 num_points = len(self.y_target)
-                self.curve_target.setData(range(num_points), self.y_target)
-                self.curve_press.setData(range(num_points), self.y_press)
-                self.curve_output.setData(range(num_points), self.y_output)
-                self.curve_step.setData(range(num_points), self.y_step)
-                self.curve_thresold.setData(range(num_points), self.y_thresold)
+                self.curve_target.setData(self.x_time, self.y_target)
+                self.curve_press.setData(self.x_time, self.y_press)
+                self.curve_output.setData(self.x_time, self.y_output)
+                self.curve_step.setData(self.x_time, self.y_step)
+                self.curve_thresold.setData(self.x_time, self.y_thresold)
                 
                 # 重置计数器和时间
                 self.ui_update_counter = 0
                 self.last_update_time = current_time
+                print( "xtime:", self.x_time[-10:] )
     def show_log( self, file ):
         if self.connected:
             self.on_btn_devst_connect()
@@ -282,23 +294,27 @@ class page_devst( QWidget, ui_devst.Ui_Form):
             self.y_press = []
             self.y_output = []
             self.y_step = []
+            self.x_time = []
             for row in reader:
                 i += 1
                 if i<10: 
                     print( i, "--", row )
                 if i == 1:
                     continue
+                # 转换时间为秒
+                now = float(row[0])
+                self.x_time.append( now )
                 self.y_target.append( float(row[1])/10.0 *6.25 )
                 self.y_thresold.append( float(row[3]) )
                 self.y_press.append( float(row[2]) )
                 self.y_output.append( float(row[4])/100.0 )
                 self.y_step.append( int(row[5]) )
             num_points = len(self.y_target)
-            self.curve_target.setData(range(num_points), self.y_target)
-            self.curve_press.setData(range(num_points), self.y_press)
-            self.curve_output.setData(range(num_points), self.y_output)
-            self.curve_step.setData(range(num_points), self.y_step)
-            self.curve_thresold.setData(range(num_points), self.y_thresold)
+            self.curve_target.setData(self.x_time, self.y_target)
+            self.curve_press.setData(self.x_time, self.y_press)
+            self.curve_output.setData(self.x_time, self.y_output)
+            self.curve_step.setData(self.x_time, self.y_step)
+            self.curve_thresold.setData(self.x_time, self.y_thresold)
             self.setEnabled(True )
 
 class App( QDialog, ui_main.Ui_Dialog ):
@@ -306,7 +322,7 @@ class App( QDialog, ui_main.Ui_Dialog ):
     def __init__( self ):
         super().__init__()
         self.setupUi(self)
-        self.setWindowTitle( u"气动调试工具 V1.0" )
+        self.setWindowTitle( u"气动调试工具 V1.1" )
         try:
             # PyInstaller创建的临时文件夹,存储于_MEIPASS中
             base_path = sys._MEIPASS