Update GUI 2.0.6 components and longCommand
This commit is contained in:
+56
-19
@@ -467,14 +467,13 @@ class DroneMonitor(Node):
|
||||
self.serial_receivers = []
|
||||
|
||||
# ================================================================================
|
||||
# 【新增】初始化 CommandLongClient(持久化,不會每次調用都創建/銷毀)
|
||||
# 【新增】初始化 CommandLongClient 字典(為每個 drone 維護獨立的 client)
|
||||
# ================================================================================
|
||||
self.command_long_client = None
|
||||
try:
|
||||
self.command_long_client = CommandLongClient()
|
||||
except Exception as e:
|
||||
print(f"⚠️ 警告: 無法初始化 CommandLongClient: {e}")
|
||||
self.command_long_client = None
|
||||
# 改為為每個 drone 創建獨立的 client,避免多機並行時的競態條件
|
||||
self.command_long_clients = {} # {drone_id: CommandLongClient}
|
||||
self.client_lock = Lock() # 保護 clients 字典的訪問
|
||||
self.client_counter = 0 # 用於生成唯一的 client 節點名稱
|
||||
self.executor = None # 將在 gui.py 中設置,用於添加新的 clients
|
||||
# ================================================================================
|
||||
|
||||
# 主题检测定时器
|
||||
@@ -501,6 +500,38 @@ class DroneMonitor(Node):
|
||||
|
||||
return self.socket_id_mapping[original_socket_id]
|
||||
|
||||
def get_or_create_client(self, drone_id):
|
||||
"""為每個 drone 獲取或創建獨立的 CommandLongClient,避免競態條件"""
|
||||
with self.client_lock:
|
||||
if drone_id not in self.command_long_clients:
|
||||
try:
|
||||
# 生成唯一的 client 節點名稱
|
||||
self.client_counter += 1
|
||||
unique_name = f"cmd_long_client_{drone_id}_{self.client_counter}"
|
||||
client = CommandLongClient(node_name=unique_name)
|
||||
self.command_long_clients[drone_id] = client
|
||||
print(f" ✓ 為 {drone_id} 創建新的 CommandLongClient (node={unique_name})")
|
||||
|
||||
# 將新 client 添加到主執行器(這樣它的回調才能被處理)
|
||||
if self.executor:
|
||||
self.executor.add_node(client)
|
||||
print(f" ✓ 將 {drone_id} 的 client 添加到主執行器")
|
||||
|
||||
except TypeError:
|
||||
# 舊版 CommandLongClient 不支持 node_name 參數,使用預設
|
||||
client = CommandLongClient()
|
||||
self.command_long_clients[drone_id] = client
|
||||
print(f" ✓ 為 {drone_id} 創建新的 CommandLongClient (使用預設名稱)")
|
||||
|
||||
if self.executor:
|
||||
self.executor.add_node(client)
|
||||
print(f" ✓ 將 {drone_id} 的 client 添加到主執行器")
|
||||
|
||||
except Exception as e:
|
||||
print(f"⚠️ 無法為 {drone_id} 創建 CommandLongClient: {e}")
|
||||
return None
|
||||
return self.command_long_clients[drone_id]
|
||||
|
||||
def scan_topics(self):
|
||||
topics = self.get_topic_names_and_types()
|
||||
drone_pattern = re.compile(r'/fc_network/vehicle/(sys\d+)/(\w+)')
|
||||
@@ -619,17 +650,19 @@ class DroneMonitor(Node):
|
||||
|
||||
print(f"\n📢 [SET_MODE] {drone_id} → {mode_name} (custom_mode={custom_mode})")
|
||||
|
||||
if not self.command_long_client:
|
||||
print(f"❌ [SET_MODE] CommandLongClient 未初始化")
|
||||
# 獲取或創建該 drone 專用的 client(避免多機並行時的競態條件)
|
||||
client = self.get_or_create_client(drone_id)
|
||||
if not client:
|
||||
print(f"❌ [SET_MODE] CommandLongClient 無法初始化")
|
||||
return False
|
||||
|
||||
# 直接調用 async 方法,無需線程池(避免嵌套執行器衝突)
|
||||
result = await self.command_long_client.change_mode_async(
|
||||
result = await client.change_mode_async(
|
||||
target_sysid=sysid,
|
||||
custom_mode=float(custom_mode),
|
||||
target_compid=0,
|
||||
base_mode=1.0,
|
||||
timeout_sec=2.0,
|
||||
timeout_sec=5.0, # 增加超時時間以提高多機操作的可靠性
|
||||
)
|
||||
|
||||
if result and result.success:
|
||||
@@ -656,16 +689,18 @@ class DroneMonitor(Node):
|
||||
action_name = "解鎖" if arm else "上鎖"
|
||||
print(f"\n📢 [ARM] {drone_id} → {action_name}")
|
||||
|
||||
if not self.command_long_client:
|
||||
print(f"❌ [ARM] CommandLongClient 未初始化")
|
||||
# 獲取或創建該 drone 專用的 client(避免多機並行時的競態條件)
|
||||
client = self.get_or_create_client(drone_id)
|
||||
if not client:
|
||||
print(f"❌ [ARM] CommandLongClient 無法初始化")
|
||||
return False
|
||||
|
||||
# 直接調用 async 方法,無需線程池(避免嵌套執行器衝突)
|
||||
result = await self.command_long_client.arm_disarm_async(
|
||||
result = await client.arm_disarm_async(
|
||||
target_sysid=sysid,
|
||||
arm=arm,
|
||||
target_compid=0,
|
||||
timeout_sec=2.0,
|
||||
timeout_sec=5.0, # 增加超時時間以提高多機操作的可靠性
|
||||
)
|
||||
|
||||
if result and result.success:
|
||||
@@ -691,16 +726,18 @@ class DroneMonitor(Node):
|
||||
|
||||
print(f"\n📢 [TAKEOFF] {drone_id} → 起飛 (高度={altitude}m)")
|
||||
|
||||
if not self.command_long_client:
|
||||
print(f"❌ [TAKEOFF] CommandLongClient 未初始化")
|
||||
# 獲取或創建該 drone 專用的 client(避免多機並行時的競態條件)
|
||||
client = self.get_or_create_client(drone_id)
|
||||
if not client:
|
||||
print(f"❌ [TAKEOFF] CommandLongClient 無法初始化")
|
||||
return False
|
||||
|
||||
# 直接調用 async 方法,無需線程池(避免嵌套執行器衝突)
|
||||
result = await self.command_long_client.takeoff_async(
|
||||
result = await client.takeoff_async(
|
||||
target_sysid=sysid,
|
||||
altitude_m=float(altitude),
|
||||
target_compid=0,
|
||||
timeout_sec=2.0,
|
||||
timeout_sec=5.0, # 增加超時時間以提高多機操作的可靠性
|
||||
)
|
||||
|
||||
if result and result.success:
|
||||
|
||||
+24
-6
@@ -52,9 +52,8 @@ class ControlStationUI(QMainWindow):
|
||||
self.executor = rclpy.executors.SingleThreadedExecutor()
|
||||
self.executor.add_node(self.monitor)
|
||||
|
||||
# 添加 CommandLongClient 到執行器(這樣它的回調才能被處理)
|
||||
if self.monitor.command_long_client:
|
||||
self.executor.add_node(self.monitor.command_long_client)
|
||||
# 將執行器註冊到 DroneMonitor,以便動態創建的 CommandLongClient 能被添加
|
||||
self.monitor.executor = self.executor
|
||||
|
||||
# 定时处理ROS事件
|
||||
self.timer = QTimer()
|
||||
@@ -587,6 +586,9 @@ class ControlStationUI(QMainWindow):
|
||||
self.group_tab_widget.setCurrentIndex(idx)
|
||||
self.active_group_id = gid
|
||||
self.statusBar().showMessage(f"已新增 Group {gid}", 2000)
|
||||
|
||||
# 更新刪除按鈕的啟用/禁用狀態
|
||||
self._update_delete_buttons_state()
|
||||
|
||||
def _on_group_tab_changed(self, index):
|
||||
"""Tab 切換時更新 active group 並同步地圖模式"""
|
||||
@@ -925,6 +927,11 @@ class ControlStationUI(QMainWindow):
|
||||
|
||||
def _handle_delete_group(self, group_id):
|
||||
"""刪除一個任務群組"""
|
||||
# 檢查是否只有一個群組,如果是就禁止刪除
|
||||
if len(self.mission_groups) <= 1:
|
||||
self.statusBar().showMessage("⚠️ 至少需要保留一個群組!", 3000)
|
||||
return
|
||||
|
||||
if group_id not in self.mission_groups:
|
||||
self.statusBar().showMessage(f"Group {group_id} 不存在", 3000)
|
||||
return
|
||||
@@ -961,6 +968,17 @@ class ControlStationUI(QMainWindow):
|
||||
self.active_group_id = None
|
||||
|
||||
self.statusBar().showMessage(f"已刪除 Group {group_id}", 3000)
|
||||
|
||||
# 更新刪除按鈕的啟用/禁用狀態
|
||||
self._update_delete_buttons_state()
|
||||
|
||||
def _update_delete_buttons_state(self):
|
||||
"""根據群組數量,更新所有群組的刪除按鈕啟用/禁用狀態"""
|
||||
# 如果只有一個群組,禁用該群組的刪除按鈕
|
||||
# 如果有多個群組,啟用所有刪除按鈕
|
||||
should_enable = len(self.mission_groups) > 1
|
||||
for gid, panel in self.group_panels.items():
|
||||
panel.set_delete_enabled(should_enable)
|
||||
|
||||
def _on_group_mission_completed(self, group_id):
|
||||
"""群組任務完成回呼"""
|
||||
@@ -1723,10 +1741,10 @@ class ControlStationUI(QMainWindow):
|
||||
# Clean up serial receivers
|
||||
for receiver in self.monitor.serial_receivers:
|
||||
receiver.stop()
|
||||
# Clean up CommandLongClient
|
||||
if self.monitor.command_long_client:
|
||||
# Clean up all CommandLongClient instances
|
||||
for drone_id, client in self.monitor.command_long_clients.items():
|
||||
try:
|
||||
self.monitor.command_long_client.destroy_node()
|
||||
client.destroy_node()
|
||||
except:
|
||||
pass
|
||||
self.monitor.destroy_node()
|
||||
|
||||
@@ -344,13 +344,13 @@ class GroupPanel(QWidget):
|
||||
clear_btn.setStyleSheet(BTN.format(bg='#777', fg='white', hover='#888'))
|
||||
clear_btn.clicked.connect(
|
||||
lambda: self.clear_group_requested.emit(self.group.group_id))
|
||||
delete_group_btn = QPushButton("刪除群組")
|
||||
delete_group_btn.setStyleSheet(BTN.format(bg='#EF5350', fg='white', hover='#E53935'))
|
||||
delete_group_btn.clicked.connect(
|
||||
self.delete_group_btn = QPushButton("刪除群組")
|
||||
self.delete_group_btn.setStyleSheet(BTN.format(bg='#EF5350', fg='white', hover='#E53935'))
|
||||
self.delete_group_btn.clicked.connect(
|
||||
lambda: self.delete_group_requested.emit(self.group.group_id))
|
||||
grid_r2.addWidget(assign_btn)
|
||||
grid_r2.addWidget(clear_btn)
|
||||
grid_r2.addWidget(delete_group_btn)
|
||||
grid_r2.addWidget(self.delete_group_btn)
|
||||
right.addLayout(grid_r2)
|
||||
|
||||
right.addStretch()
|
||||
@@ -474,6 +474,10 @@ class GroupPanel(QWidget):
|
||||
def set_all_select_state(self, is_selected):
|
||||
"""外部設置全選狀態(按鈕文本保持「全選/取消」)"""
|
||||
self._is_all_selected = is_selected
|
||||
|
||||
def set_delete_enabled(self, enabled):
|
||||
"""啟用或禁用刪除群組按鈕"""
|
||||
self.delete_group_btn.setEnabled(enabled)
|
||||
|
||||
def _update_param_visibility(self, _=None):
|
||||
"""根據當前任務類型,顯示/隱藏對應的參數列"""
|
||||
|
||||
Reference in New Issue
Block a user