-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory_calibration_tool.py
More file actions
1891 lines (1600 loc) · 73.1 KB
/
factory_calibration_tool.py
File metadata and controls
1891 lines (1600 loc) · 73.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
EZ Tool - 简化版双串口工厂舵机标定工具
基于原始工具,只增加一个中间值校准按钮
"""
import sys
import time
import threading
import subprocess
import os
from typing import List
from queue import Queue
# 添加必要的路径
sys.path.append('.')
sys.path.append('./scservo_sdk')
from PySide6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QPushButton, QLabel, QTextEdit, QGridLayout, QGroupBox,
QMessageBox, QFrame, QStatusBar, QSplitter, QComboBox
)
from PySide6.QtCore import QTimer, Signal, QObject, Qt
from PySide6.QtGui import QFont, QPalette, QColor
from scservo_sdk.port_handler import PortHandler
from scservo_sdk.sms_sts import sms_sts
from scservo_sdk.scservo_def import COMM_SUCCESS
# 引入端口工具
try:
from src.port_utils import get_default_port, get_available_ports
PORT_UTILS_AVAILABLE = True
except ImportError:
PORT_UTILS_AVAILABLE = False
print("Warning: port_utils not found, using fallback port detection")
class RemoteControlWorker(QObject):
"""遥控操作后台工作线程"""
status_updated = Signal(str) # 状态更新信号
log_message = Signal(str) # 日志消息信号
control_started = Signal() # 遥控启动信号
control_stopped = Signal() # 遥控停止信号
def __init__(self, read_port=None, control_port=None):
super().__init__()
self.remote_process = None
self.running = False
self.project_root = os.path.abspath(os.path.dirname(__file__))
self.read_port = read_port
self.control_port = control_port
def start_remote_control(self):
"""启动遥控操作"""
if self.running:
return False, "遥控操作已在运行"
try:
# 使用 -m 模块方式运行,确保能找到 scservo_sdk
command = [sys.executable, '-m', 'src.tools.servo_remote_control']
if self.read_port:
command.extend(['--read-port', self.read_port])
if self.control_port:
command.extend(['--control-port', self.control_port])
# 启动子进程运行遥控脚本
self.remote_process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
bufsize=1,
cwd=self.project_root
)
self.running = True
self.log_message.emit("🚀 遥控操作已启动 (10ms更新间隔)")
self.control_started.emit()
# 启动监控线程
threading.Thread(target=self._monitor_process, daemon=True).start()
return True, "遥控操作启动成功"
except Exception as e:
return False, f"启动遥控操作失败: {e}"
def stop_remote_control(self):
"""停止遥控操作"""
if not self.running:
return False, "遥控操作未运行"
try:
if self.remote_process:
self.remote_process.terminate()
# 等待进程正常退出
try:
self.remote_process.wait(timeout=5)
except subprocess.TimeoutExpired:
# 如果5秒内没有退出,强制杀死
self.remote_process.kill()
self.remote_process.wait()
self.running = False
self.remote_process = None
self.log_message.emit("⏹️ 遥控操作已停止")
self.control_stopped.emit()
return True, "遥控操作停止成功"
except Exception as e:
return False, f"停止遥控操作失败: {e}"
def _monitor_process(self):
"""监控遥控进程的输出"""
if not self.remote_process:
return
try:
while self.running and self.remote_process.poll() is None:
line = self.remote_process.stdout.readline()
if line:
line = line.strip()
if line:
self.log_message.emit(f"遥控: {line}")
time.sleep(0.1)
# 进程结束
if self.remote_process.poll() is not None:
self.running = False
self.remote_process = None
self.log_message.emit("🔚 遥控进程已结束")
self.control_stopped.emit()
except Exception as e:
self.log_message.emit(f"监控遥控进程异常: {e}")
self.running = False
self.control_stopped.emit()
class ServoWorker(QObject):
"""单个舵机控制工作线程"""
status_updated = Signal(list, bool, str) # 舵机列表, 连接状态, 端口标识
id_changed = Signal(int, int, bool, str, str) # old_id, new_id, success, message, 端口标识
log_message = Signal(str, str) # 日志消息, 端口标识
def __init__(self, port_name: str, port_id: str):
super().__init__()
self.port_name = port_name
self.port_id = port_id # 端口标识 (left/right)
self.port_handler = None
self.servo_handler = None
self.is_connected = False
self.current_servos = []
self.running = False
# 连接配置
self.baud_rate = 1000000
# ID修改队列
self.id_change_queue = Queue()
self.id_change_thread = None
self.id_change_running = False
# 扫描控制
self.pause_scanning = False # 是否暂停扫描
def connect_servo(self) -> bool:
"""连接舵机控制器"""
try:
print(f"[DEBUG] {self.port_id}: Attempting to connect to {self.port_name}")
self.log_message.emit(f"正在连接舵机控制器: {self.port_name}", self.port_id)
self.port_handler = PortHandler(self.port_name)
if not self.port_handler.openPort():
print(f"[DEBUG] {self.port_id}: Failed to open port {self.port_name}")
self.log_message.emit(f"❌ 无法打开串口: {self.port_name}", self.port_id)
return False
if not self.port_handler.setBaudRate(self.baud_rate):
print(f"[DEBUG] {self.port_id}: Failed to set baud rate {self.baud_rate}")
self.log_message.emit(f"❌ 无法设置波特率: {self.baud_rate}", self.port_id)
self.port_handler.closePort()
return False
self.servo_handler = sms_sts(self.port_handler)
self.is_connected = True
print(f"[DEBUG] {self.port_id}: Successfully connected to {self.port_name}")
self.log_message.emit("✅ 舵机控制器连接成功", self.port_id)
return True
except Exception as e:
print(f"[DEBUG] {self.port_id}: Connection exception: {e}")
self.log_message.emit(f"❌ 连接失败: {e}", self.port_id)
return False
def disconnect_servo(self):
"""断开舵机连接"""
try:
if self.port_handler:
self.port_handler.closePort()
self.is_connected = False
self.log_message.emit("🔌 舵机控制器已断开", self.port_id)
except:
pass
def ping_servo(self, servo_id: int) -> bool:
"""检测舵机是否存在"""
try:
model_number, result, error = self.servo_handler.ping(servo_id)
if result == COMM_SUCCESS:
print(f"[DEBUG] {self.port_id}: 舵机 {servo_id} 型号: {model_number}")
return True
else:
print(f"[DEBUG] {self.port_id}: Ping 舵机 {servo_id} 失败: result={result}, error={error}")
return False
except Exception as e:
print(f"[DEBUG] {self.port_id}: Ping 舵机 {servo_id} 异常: {e}")
return False
def scan_servos(self) -> List[int]:
"""扫描所有舵机"""
if not self.is_connected:
return []
found_servos = []
for servo_id in range(1, 10): # 扫描所有可能的ID
if self.ping_servo(servo_id):
found_servos.append(servo_id)
return found_servos
def change_servo_id(self, old_id: int, new_id: int) -> (bool, str):
"""修改舵机ID(队列版本)"""
# 将请求加入队列
self.queue_id_change(old_id, new_id)
return True, "ID修改请求已加入队列"
def queue_id_change(self, old_id: int, new_id: int):
"""将ID修改请求加入队列"""
print(f"[DEBUG] {self.port_id}: ID修改请求入队: {old_id} -> {new_id}")
self.log_message.emit(f"📝 ID修改请求已排队: {old_id} -> {new_id}", self.port_id)
self.id_change_queue.put((old_id, new_id, time.time()))
# 启动ID修改线程(如果还没启动)
if not self.id_change_running:
self.start_id_change_processor()
def start_id_change_processor(self):
"""启动ID修改处理线程"""
if not self.id_change_running:
self.id_change_running = True
self.id_change_thread = threading.Thread(target=self.process_id_changes, daemon=True)
self.id_change_thread.start()
print(f"[DEBUG] {self.port_id}: ID修改处理线程已启动")
def process_id_changes(self):
"""处理ID修改队列"""
print(f"[DEBUG] {self.port_id}: 开始处理ID修改队列")
while self.id_change_running or not self.id_change_queue.empty():
try:
if not self.id_change_queue.empty():
old_id, new_id, request_time = self.id_change_queue.get(timeout=1)
# 暂停扫描,避免总线冲突
self.pause_scanning = True
print(f"[DEBUG] {self.port_id}: 暂停扫描,准备执行ID修改: {old_id} -> {new_id}")
self.log_message.emit(f"⏸️ 暂停扫描,执行ID修改: {old_id} -> {new_id}", self.port_id)
# 等待一下确保扫描完全停止
time.sleep(0.5)
# 执行ID修改
success, message = self.execute_id_change(old_id, new_id)
# 恢复扫描
self.pause_scanning = False
print(f"[DEBUG] {self.port_id}: 恢复扫描")
self.log_message.emit(f"▶️ 恢复扫描", self.port_id)
# 发送结果
self.id_changed.emit(old_id, new_id, success, message, self.port_id)
else:
time.sleep(0.1) # 短暂休眠避免CPU占用
except Exception as e:
print(f"[DEBUG] {self.port_id}: ID修改处理异常: {e}")
self.log_message.emit(f"❌ ID修改处理异常: {e}", self.port_id)
# 确保扫描被恢复
self.pause_scanning = False
print(f"[DEBUG] {self.port_id}: ID修改处理线程结束")
self.id_change_running = False
self.pause_scanning = False
def execute_id_change(self, old_id: int, new_id: int) -> (bool, str):
"""执行实际的ID修改操作"""
try:
if not self.is_connected:
return False, "未连接舵机控制器"
self.log_message.emit(f"🔧 执行SMS_STS ID修改: {old_id} -> {new_id}", self.port_id)
print(f"[DEBUG] {self.port_id}: 执行ID修改: {old_id} -> {new_id}")
# 首先读取舵机信息(此时扫描已暂停,不会冲突)
try:
model_number, result, error = self.servo_handler.ping(old_id)
if result == COMM_SUCCESS:
print(f"[DEBUG] {self.port_id}: SMS_STS 舵机型号: {model_number}")
self.log_message.emit(f"📋 舵机型号: {model_number}", self.port_id)
else:
print(f"[DEBUG] {self.port_id}: 无法读取舵机信息: {error}")
return False, f"无法读取舵机信息: {error}"
except Exception as e:
return False, f"读取舵机信息异常: {e}"
# SMS_STS EEPROM解锁流程
print(f"[DEBUG] {self.port_id}: SMS_STS 解锁EEPROM...")
result, error = self.servo_handler.unLockEprom(old_id)
if result != COMM_SUCCESS:
print(f"[DEBUG] {self.port_id}: EEPROM解锁失败: result={result}, error={error}")
return False, f"EEPROM解锁失败: {error}"
print(f"[DEBUG] {self.port_id}: EEPROM解锁成功")
time.sleep(0.1)
# 修改ID (使用SMS_STS_ID地址)
print(f"[DEBUG] {self.port_id}: 写入新ID: {new_id}")
result, error = self.servo_handler.write1ByteTxRx(old_id, 5, new_id) # SMS_STS_ID = 5
if result != COMM_SUCCESS:
print(f"[DEBUG] {self.port_id}: ID写入失败: result={result}, error={error}")
return False, f"ID写入失败: {error}"
print(f"[DEBUG] {self.port_id}: ID写入成功")
time.sleep(0.3)
# 验证新ID(此时扫描仍暂停,ping不会冲突)
print(f"[DEBUG] {self.port_id}: 验证新ID: {new_id}")
if not self.ping_servo(new_id):
print(f"[DEBUG] {self.port_id}: 新ID验证失败")
return False, f"验证失败,无法ping通新ID: {new_id}"
print(f"[DEBUG] {self.port_id}: 新ID验证成功")
# 重新锁定EEPROM
print(f"[DEBUG] {self.port_id}: 重新锁定EEPROM...")
result, error = self.servo_handler.LockEprom(new_id)
if result != COMM_SUCCESS:
print(f"[DEBUG] {self.port_id}: 重新锁定失败: {error}")
self.log_message.emit(f"⚠️ 重新锁定EEPROM失败: {error}", self.port_id)
else:
print(f"[DEBUG] {self.port_id}: 重新锁定成功")
self.log_message.emit(f"✅ SMS_STS ID修改成功: {old_id} -> {new_id}", self.port_id)
print(f"[DEBUG] {self.port_id}: ID修改完成: {old_id} -> {new_id}")
return True, ""
except Exception as e:
error_msg = f"修改ID异常: {e}"
print(f"[DEBUG] {self.port_id}: 修改ID异常: {e}")
self.log_message.emit(f"❌ {error_msg}", self.port_id)
return False, error_msg
def run_scanner(self):
"""运行扫描循环"""
scan_count = 0
self.running = True
consecutive_failures = 0
max_failures = 3
self.log_message.emit("🚀 扫描线程启动", self.port_id)
print(f"[DEBUG] {self.port_id}: Scanner thread started")
# 首次连接
if not self.is_connected:
self.connect_servo()
while self.running:
try:
scan_count += 1
# 如果未连接,尝试重新连接
if not self.is_connected:
if consecutive_failures < max_failures:
self.log_message.emit(f"🔄 尝试重新连接... (第{consecutive_failures + 1}次)", self.port_id)
time.sleep(2) # 等待2秒再重试
if self.connect_servo():
consecutive_failures = 0 # 重置失败计数
else:
consecutive_failures += 1
continue
else:
# 失败次数过多,延长等待时间
self.log_message.emit(f"⚠️ 连续失败{max_failures}次,等待10秒后重试...", self.port_id)
time.sleep(10)
consecutive_failures = 0 # 重置计数
continue
# 检查是否暂停扫描(ID修改期间)
if self.pause_scanning:
print(f"[DEBUG] {self.port_id}: 扫描已暂停(ID修改中)")
time.sleep(0.5) # 短暂休眠,减少CPU占用
continue
# 扫描舵机
new_servos = self.scan_servos()
print(f"[DEBUG] {self.port_id}: Scan result: {new_servos}, current: {self.current_servos}")
# 如果扫描成功,重置失败计数
if new_servos is not None:
consecutive_failures = 0
# 如果舵机列表有变化
if new_servos != self.current_servos:
old_servos = self.current_servos.copy() if self.current_servos else []
self.current_servos = new_servos
if new_servos:
if not old_servos:
self.log_message.emit(f"📡 发现舵机: {new_servos}", self.port_id)
else:
added = set(new_servos) - set(old_servos)
removed = set(old_servos) - set(new_servos)
changes = []
if added:
changes.append(f"新增: {list(added)}")
if removed:
changes.append(f"移除: {list(removed)}")
self.log_message.emit(f"📡 舵机变化: {', '.join(changes)}", self.port_id)
else:
if old_servos:
self.log_message.emit("📡 所有舵机已断开", self.port_id)
print(f"[DEBUG] {self.port_id}: Emitting status_updated: servos={new_servos}, connected={self.is_connected}")
self.status_updated.emit(self.current_servos, self.is_connected, self.port_id)
# 每30次扫描显示一次状态(减少日志频率)
if scan_count % 30 == 0:
if self.current_servos:
self.log_message.emit(f"📊 当前舵机ID: {self.current_servos}", self.port_id)
else:
self.log_message.emit("📊 当前无舵机", self.port_id)
time.sleep(1) # 扫描间隔
except Exception as e:
consecutive_failures += 1
self.log_message.emit(f"❌ 扫描异常: {e} (失败次数: {consecutive_failures})", self.port_id)
# 不要立即断开连接,给下次重试机会
time.sleep(1)
def start(self):
"""启动工作线程"""
if not self.running:
self.running = True
threading.Thread(target=self.run_scanner, daemon=True).start()
def stop(self):
"""停止工作线程"""
self.running = False
self.id_change_running = False
self.disconnect_servo()
# 等待ID修改线程结束
if self.id_change_thread and self.id_change_thread.is_alive():
self.id_change_thread.join(timeout=2)
class ServoPanel(QWidget):
"""单个舵机控制面板"""
def __init__(self, port_name: str, port_id: str):
super().__init__()
self.port_name = port_name
self.port_id = port_id
self.worker = ServoWorker(port_name, port_id)
self.init_ui()
self.init_connections()
self.worker.start()
def init_ui(self):
"""初始化界面"""
layout = QVBoxLayout(self)
layout.setSpacing(10)
# 标题
self.title_label = QLabel(f"🏭 {self.port_name} - 舵机标定")
self.title_label.setAlignment(Qt.AlignCenter)
self.title_label.setStyleSheet("font-size: 18px; font-weight: bold; color: #2c3e50; margin: 5px;")
layout.addWidget(self.title_label)
# 状态面板
self.create_status_panel(layout)
# 舵机状态面板
self.create_servo_panel(layout)
# 标定面板
self.create_calibration_panel(layout)
# 日志面板
self.create_log_panel(layout)
# 设置整体样式
self.setStyleSheet(f"""
QWidget#{self.port_id} {{
border: 2px solid #dee2e6;
border-radius: 10px;
padding: 10px;
background-color: #ffffff;
}}
QGroupBox {{
font-weight: bold;
border: 1px solid #dee2e6;
border-radius: 8px;
margin-top: 5px;
padding-top: 10px;
}}
QGroupBox::title {{
subcontrol-origin: margin;
left: 8px;
padding: 0 3px 0 3px;
}}
QPushButton {{
background-color: #007bff;
color: white;
border: none;
padding: 12px;
border-radius: 6px;
font-size: 14px;
font-weight: bold;
}}
QPushButton:hover {{
background-color: #0056b3;
}}
QPushButton:pressed {{
background-color: #004085;
}}
QPushButton:disabled {{
background-color: #6c757d;
}}
QTextEdit {{
background-color: #f8f9fa;
color: #212529;
border: 1px solid #ced4da;
border-radius: 4px;
font-family: 'Consolas', monospace;
font-size: 11px;
}}
QLabel {{
color: #495057;
}}
""")
self.setObjectName(self.port_id)
def create_status_panel(self, layout):
"""创建状态面板"""
status_group = QGroupBox("📡 系统状态")
status_layout = QHBoxLayout()
status_group.setLayout(status_layout)
# 连接状态
self.connection_status = QLabel("🔴 未连接")
self.connection_status.setStyleSheet("font-size: 12px; font-weight: bold;")
status_layout.addWidget(self.connection_status)
status_layout.addStretch()
# 当前舵机
self.current_servos_label = QLabel("当前舵机: 扫描中...")
self.current_servos_label.setStyleSheet("font-size: 12px;")
status_layout.addWidget(self.current_servos_label)
layout.addWidget(status_group)
def create_servo_panel(self, layout):
"""创建舵机状态面板"""
servo_group = QGroupBox("📡 舵机状态")
servo_layout = QVBoxLayout()
servo_group.setLayout(servo_layout)
# 舵机列表
self.servo_list = QTextEdit()
self.servo_list.setReadOnly(True)
self.servo_list.setMaximumHeight(150)
self.servo_list.setPlainText("正在扫描舵机...")
servo_layout.addWidget(self.servo_list)
layout.addWidget(servo_group)
def create_calibration_panel(self, layout):
"""创建标定面板"""
calibration_group = QGroupBox("🎯 ID标定")
calibration_layout = QVBoxLayout()
calibration_group.setLayout(calibration_layout)
# 说明文字
info_label = QLabel("📋 点击目标ID执行修改\n⏸️ 自动暂停扫描确保成功")
info_label.setStyleSheet("background-color: #e3f2fd; border: 1px solid #bbdefb; padding: 8px; border-radius: 4px; color: #1565c0; font-size: 11px;")
calibration_layout.addWidget(info_label)
# ID按钮网格
self.id_buttons = []
button_layout = QGridLayout()
for i in range(6):
row = i // 3
col = i % 3
btn = QPushButton(str(i + 1))
btn.setMinimumHeight(60)
btn.setMinimumWidth(80)
btn.setStyleSheet("font-size: 24px;")
btn.clicked.connect(lambda checked, id_val=i+1: self.change_servo_id(id_val))
btn.setEnabled(False)
self.id_buttons.append(btn)
button_layout.addWidget(btn, row, col)
calibration_layout.addLayout(button_layout)
layout.addWidget(calibration_group)
def create_log_panel(self, layout):
"""创建日志面板"""
log_group = QGroupBox("📋 操作日志")
log_layout = QVBoxLayout()
log_group.setLayout(log_layout)
# 日志文本框
self.log_text = QTextEdit()
self.log_text.setReadOnly(True)
self.log_text.setMaximumHeight(120)
self.log_text.setPlainText("系统启动...")
log_layout.addWidget(self.log_text)
# 清空日志按钮
clear_btn = QPushButton("清空日志")
clear_btn.setMaximumWidth(80)
clear_btn.setStyleSheet("font-size: 11px;")
clear_btn.clicked.connect(self.log_text.clear)
log_layout.addWidget(clear_btn)
layout.addWidget(log_group)
def init_connections(self):
"""初始化信号连接"""
self.worker.status_updated.connect(self.update_status)
self.worker.id_changed.connect(self.on_id_changed)
self.worker.log_message.connect(self.add_log)
# 添加初始连接日志
self.add_log("🔄 信号连接已建立", self.port_id)
self.add_log("📡 开始扫描舵机...", self.port_id)
def update_status(self, servos, connected, port_id):
"""更新状态显示"""
if port_id != self.port_id:
return
print(f"[DEBUG] {port_id} update_status called: servos={servos}, connected={connected}")
if connected:
self.connection_status.setText("🟢 已连接")
self.connection_status.setStyleSheet("color: #28a745; font-size: 12px; font-weight: bold;")
else:
self.connection_status.setText("🔴 未连接")
self.connection_status.setStyleSheet("color: #dc3545; font-size: 12px; font-weight: bold;")
if servos:
self.current_servos_label.setText(f"当前舵机: {', '.join(map(str, servos))}")
self.servo_list.setPlainText("📡 发现的舵机:\n\n" + "\n".join([f"• 舵机 ID: {servo_id}" for servo_id in servos]))
else:
self.current_servos_label.setText("当前舵机: 无")
self.servo_list.setPlainText("📡 未发现舵机\n\n请检查:\n1. 舵机控制器是否连接\n2. 舵机是否通电\n3. 串口配置是否正确")
# 更新按钮状态
self.update_button_states(servos, connected)
def update_button_states(self, servos, connected):
"""更新按钮状态"""
has_servos = connected and len(servos) > 0
for i, btn in enumerate(self.id_buttons):
target_id = i + 1
is_assigned = target_id in servos
btn.setEnabled(has_servos and not is_assigned)
if is_assigned:
btn.setStyleSheet("""
QPushButton {
background-color: #6c757d;
color: white;
font-size: 24px;
}
""")
else:
btn.setStyleSheet("""
QPushButton {
background-color: #007bff;
color: white;
font-size: 24px;
}
QPushButton:hover {
background-color: #0056b3;
}
""")
def change_servo_id(self, target_id):
"""修改舵机ID"""
if not self.worker.current_servos:
QMessageBox.warning(self, "警告", "没有可用的舵机进行ID修改")
return
# 优先使用第一个可用的舵机
old_id = self.worker.current_servos[0]
# 确认对话框
reply = QMessageBox.question(
self,
f"确认修改ID ({self.port_name})",
f"确定要将舵机 ID {old_id} 修改为 ID {target_id} 吗?\n\n系统将自动暂停扫描确保修改成功。",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No
)
if reply == QMessageBox.Yes:
self.add_log(f"🎯 提交ID修改请求: {old_id} -> {target_id}", self.port_id)
# 将请求加入队列(立即返回)
success, message = self.worker.change_servo_id(old_id, target_id)
if success:
self.add_log(f"✅ {message}", self.port_id)
# 禁用按钮,防止重复提交
for btn in self.id_buttons:
if btn.text() == str(target_id):
btn.setEnabled(False)
btn.setStyleSheet("background-color: #ffc107; color: black; font-size: 24px;")
break
else:
self.add_log(f"❌ {message}", self.port_id)
def on_id_changed(self, old_id, new_id, success, message, port_id):
"""处理ID修改结果"""
if port_id != self.port_id:
return
print(f"[DEBUG] {port_id} on_id_changed called: {old_id} -> {new_id}, success={success}, message={message}")
# 恢复按钮状态
for btn in self.id_buttons:
if btn.text() == str(new_id):
btn.setEnabled(True)
break
if success:
QMessageBox.information(self, f"修改成功 ({self.port_name})", f"ID修改成功!\n{old_id} -> {new_id}")
# 强制重新扫描舵机列表
self.add_log(f"🔄 ID修改成功,重新扫描舵机...", self.port_id)
# 给舵机一点时间响应新ID
time.sleep(0.5)
# 更新内部的舵机列表
if old_id in self.worker.current_servos:
self.worker.current_servos.remove(old_id)
if new_id not in self.worker.current_servos:
self.worker.current_servos.append(new_id)
self.worker.current_servos.sort()
# 手动触发状态更新
self.update_status(self.worker.current_servos, self.worker.is_connected, self.port_id)
else:
QMessageBox.critical(self, f"修改失败 ({self.port_name})", f"ID修改失败!\n{message}")
self.add_log(f"❌ 队列中ID修改失败: {old_id} -> {new_id}", self.port_id)
def add_log(self, message, port_id):
"""添加日志消息"""
if port_id != self.port_id:
return
timestamp = time.strftime("%H:%M:%S")
log_entry = f"[{timestamp}] {message}"
# Remove emojis for console output
clean_message = message.encode('ascii', 'ignore').decode('ascii')
clean_log_entry = f"[{timestamp}] {clean_message}"
print(f"[DEBUG {port_id}] {clean_log_entry}")
self.log_text.append(log_entry)
# 自动滚动到底部
scrollbar = self.log_text.verticalScrollBar()
scrollbar.setValue(scrollbar.maximum())
# 限制日志行数 - 修复Qt API错误
document = self.log_text.document()
if document.blockCount() > 500:
cursor = self.log_text.textCursor()
cursor.movePosition(QTextCursor.Start)
cursor.select(QTextCursor.LineUnderCursor)
cursor.removeSelectedText()
def update_port_name(self, new_port_name: str):
"""更新端口名称和标题"""
self.port_name = new_port_name
self.title_label.setText(f"🏭 {self.port_name} - 舵机标定")
def stop(self):
"""停止工作线程"""
self.worker.stop()
class EZToolUI(QMainWindow):
"""EZ Tool - 简化版双串口工厂舵机标定工具"""
def __init__(self, left_port: str = None, right_port: str = None):
# Auto-detect default ports using port_utils
if PORT_UTILS_AVAILABLE:
if left_port is None:
left_port = get_default_port(0)
if right_port is None:
right_port = get_default_port(1)
else:
# Fallback to platform-based detection
import platform
if left_port is None:
left_port = "COM1" if platform.system() == "Windows" else "/dev/ttyUSB0"
if right_port is None:
right_port = "COM2" if platform.system() == "Windows" else "/dev/ttyUSB1"
super().__init__()
self.left_port = left_port
self.right_port = right_port
# 遥控工作线程
self.remote_worker = None
# 可用串口列表
self.available_ports = []
self.init_ui()
self.init_connections()
self.refresh_ports()
def init_ui(self):
"""初始化界面"""
self.setWindowTitle("🏭 双串口工厂舵机标定工具")
self.setGeometry(50, 50, 1600, 900)
# 设置字体
font = QFont("Microsoft YaHei", 10)
self.setFont(font)
# 创建中央部件
central_widget = QWidget()
self.setCentralWidget(central_widget)
# 主布局
main_layout = QVBoxLayout(central_widget)
main_layout.setSpacing(15)
# 创建顶部标题栏(包含串口选择、遥控按钮和中间值校准按钮)
header_layout = QHBoxLayout()
# 左侧标题
title_label = QLabel("🏭 双串口工厂舵机标定工具")
title_label.setStyleSheet("font-size: 28px; font-weight: bold; color: #2c3e50;")
header_layout.addWidget(title_label)
# 串口选择区域
port_selection_group = QGroupBox("串口选择")
port_selection_group.setStyleSheet("""
QGroupBox {
font-size: 12px;
font-weight: bold;
border: 2px solid #dee2e6;
border-radius: 8px;
margin-top: 5px;
padding-top: 10px;
background-color: #f8f9fa;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 8px;
padding: 0 3px 0 3px;
color: #495057;
}
""")
port_selection_layout = QHBoxLayout(port_selection_group)
port_selection_layout.setSpacing(10)
# 左串口选择
left_port_layout = QVBoxLayout()
left_port_layout.setSpacing(2)
left_label = QLabel("串口1:")
left_label.setStyleSheet("font-size: 11px; font-weight: bold; color: #495057;")
left_port_layout.addWidget(left_label)
self.left_port_combo = QComboBox()
self.left_port_combo.setMinimumWidth(80)
self.left_port_combo.setMaximumWidth(120)
self.left_port_combo.setStyleSheet("""
QComboBox {
font-size: 11px;
padding: 3px;
border: 1px solid #ced4da;
border-radius: 4px;
background-color: white;
}
QComboBox:hover {
border: 1px solid #80bdff;
}
""")
self.left_port_combo.currentTextChanged.connect(self.on_left_port_changed)
left_port_layout.addWidget(self.left_port_combo)
port_selection_layout.addLayout(left_port_layout)
# 右串口选择
right_port_layout = QVBoxLayout()
right_port_layout.setSpacing(2)
right_label = QLabel("串口2:")
right_label.setStyleSheet("font-size: 11px; font-weight: bold; color: #495057;")
right_port_layout.addWidget(right_label)
self.right_port_combo = QComboBox()
self.right_port_combo.setMinimumWidth(80)
self.right_port_combo.setMaximumWidth(120)
self.right_port_combo.setStyleSheet("""
QComboBox {
font-size: 11px;
padding: 3px;
border: 1px solid #ced4da;
border-radius: 4px;
background-color: white;
}
QComboBox:hover {
border: 1px solid #80bdff;
}
""")
self.right_port_combo.currentTextChanged.connect(self.on_right_port_changed)
right_port_layout.addWidget(self.right_port_combo)
port_selection_layout.addLayout(right_port_layout)
# 刷新按钮
refresh_btn = QPushButton("🔄")
refresh_btn.setFixedSize(30, 30)
refresh_btn.setStyleSheet("""
QPushButton {
background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
stop:0 #17a2b8, stop:1 #138496);
color: white;
border: none;
border-radius: 4px;
font-size: 14px;
font-weight: bold;
}
QPushButton:hover {
background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
stop:0 #138496, stop:1 #117a8b);
}
QPushButton:pressed {
background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
stop:0 #117a8b, stop:1 #0c5460);
}
""")
refresh_btn.clicked.connect(self.refresh_ports)
refresh_btn.setToolTip("刷新串口列表")
port_selection_layout.addWidget(refresh_btn)
header_layout.addWidget(port_selection_group)
# 添加6个按钮水平排列区域
buttons_layout = QHBoxLayout()
buttons_layout.setSpacing(8)
# 创建水平排列的6个按钮容器
buttons_container = QWidget()
buttons_container_layout = QVBoxLayout(buttons_container)
buttons_container_layout.setSpacing(3)
buttons_container_layout.setContentsMargins(0, 0, 0, 0)
# 按钮行布局 - 6个按钮水平排开
buttons_row = QHBoxLayout()
buttons_row.setSpacing(8)
# 串口1中位校准按钮
self.left_calib_btn = QPushButton("串口1中位校准")
self.left_calib_btn.setFixedSize(100, 35)
self.left_calib_btn.setStyleSheet("""
QPushButton {
background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
stop:0 #4caf50, stop:1 #45a049);
color: white;
border: none;
border-radius: 6px;
font-size: 11px;
font-weight: bold;
}
QPushButton:hover {
background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
stop:0 #059669, stop:1 #047857);
}