自启动管理

#!/usr/bin/env python3
'''
Author: CYUN && cyun@tju.enu.cn
Date: 2024-07-24 14:34:29
LastEditors: CYUN && cyun@tju.enu.cn
LastEditTime: 2024-12-11 19:07:30
FilePath: /undefined/home/nvidia/mapping_ws/src/auto_start/ui/auto_mapping.py
Description: 自动建图功能按钮

Copyright (c) 2024 by Tianjin University, All Rights Reserved. 
'''
import sys
import os
import rospy
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QGridLayout, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
import rospkg


def run_command(command):
    os.system(f'terminator -e  "{command}" &')

def run_command_restart(command):
    os.system(f'gnome-terminal -e  "{command}" &')

def stop_command(command_name):
    os.system(f'pkill -f "{command_name}"')

def start1(button):
    run_command(f"bash -c '{WORKSPACE_PATH}/src/auto_start/sh/start_map_1.sh'")
    button.setStyleSheet('background-color: #5e35b1; color: white;')

def stop1(button):
    stop_command("lidar_start.launch")
    stop_command("rosbag")
    button.setStyleSheet('')

def start2(button):
    run_command(f"bash -c '{WORKSPACE_PATH}/src/auto_start/sh/start_map_2.sh'")
    button.setStyleSheet('background-color: #5e35b1; color: white;')

def stop2(button):
    stop_command("run.launch")
    button.setStyleSheet('')

def start3(button):
    run_command(f"bash -c '{WORKSPACE_PATH}/src/auto_start/sh/start_map_3.sh'")
    button.setStyleSheet('background-color: #5e35b1; color: white;')

def stop3(button):
    stop_command("run.launch")
    button.setStyleSheet('')

def start4(button):
    run_command(f"bash -c '{WORKSPACE_PATH}/src/auto_start/sh/start_map_4.sh'")
    button.setStyleSheet('background-color: #5e35b1; color: white;')

def stop4(button):
    stop_command("ndt_localizer_rviz.launch")
    stop_command("sim.launch")
    stop_command("lidar_start_2.launch")
    button.setStyleSheet('')

def start5(button):
    run_command(f"bash -c '{WORKSPACE_PATH}/src/auto_start/sh/start_map_5.sh'")
    button.setStyleSheet('background-color: #5e35b1; color: white;')

def stop5(button):
    stop_command("rosbag")
    button.setStyleSheet('')

def start6(button):
    run_command(f"bash -c '{WORKSPACE_PATH}/src/auto_start/sh/start_map_6.sh'")
    button.setStyleSheet('background-color: #5e35b1; color: white;')

def stop6(button):
    stop_command("lidar_gps_fusion.py")
    button.setStyleSheet('')


class RuntimeManager(QWidget):
    def __init__(self):
        super().__init__()
        # 读取传递的参数
        global WORKSPACE_PATH

        # self.workspace_path = rospy.get_param('workspace_path', '/home/nvidia/10.11_ws')
        # WORKSPACE_PATH = self.workspace_path

        # # 使用rospkg获取当前功能包的路径 !!!
        rp = rospkg.RosPack()
        package_name = "auto_start"
        self.package_path = rp.get_path(package_name)

        WORKSPACE_PATH = self.package_path

        self.initUI()
        rospy.init_node('runtime_manager', anonymous=True)

    def initUI(self):
        self.setWindowTitle('Auto Mapping')
        self.setWindowIcon(QIcon(f'{WORKSPACE_PATH}/src/auto_start/png/icon.png'))

        # Styling
        button_style = 'font-size: 12pt; padding: 10px; min-width: 60px; min-height: 20px;'
        active_button_style = 'background-color: #5e35b1; color: white; font-size: 12pt; padding: 10px; min-width: 80px; min-height: 20px;'

        # Layouts
        main_layout = QVBoxLayout()
        grid_layout = QGridLayout()

        commands = [
            ("建图1", start1, stop1, "启动雷达sdk并且进行rviz显示,然后绕着场地跑动,最好完成一个闭环"),
            ("建图2", start2, stop2, "启动lego_loam,然后播放ros包,保存3维建图结果"),
            ("建图3", start3, stop3, "启动pcd2pgm,将三维地图转为二维并保存到指定目录"),
            ("建图4", start4, stop4, "开启定位,并且rviz显示,请在建图起点附件进行,或者用rviz给出初始位姿"),
            ("建图5", start5, stop5, "记录map_pose和gps到指定文件夹中"),
            ("建图6", start6, stop6, "对记录的数据进行转换,生成yaml文件到指定文件夹中")
        ]

        total_commands = len(commands)
        left_count = (total_commands + 1) // 2
        right_count = total_commands // 2

        # Fill left column
        for index in range(left_count):
            title, start_command, stop_command, description = commands[index]
            label = QLabel(title)
            label.setAlignment(Qt.AlignCenter)
            label.setStyleSheet('font-size: 14pt;')

            start_button = QPushButton('Start')
            start_button.setStyleSheet(button_style)
            start_button.setFixedSize(150, 60)
            start_button.clicked.connect(lambda _, b=start_button, cmd=start_command: cmd(b))
            start_button.setToolTip(description)  # 设置鼠标悬停提示信息,显示功能描述

            stop_button = QPushButton('Stop')
            stop_button.setStyleSheet(button_style)
            stop_button.setFixedSize(150, 60)
            stop_button.clicked.connect(lambda _, b=start_button, cmd=stop_command: cmd(b))

            grid_layout.addWidget(label, index, 0)
            grid_layout.addWidget(start_button, index, 1)
            grid_layout.addWidget(stop_button, index, 2)

        # Fill right column
        for index in range(right_count):
            title, start_command, stop_command, description = commands[left_count + index]
            label = QLabel(title)
            label.setAlignment(Qt.AlignCenter)
            label.setStyleSheet('font-size: 14pt;')

            start_button = QPushButton('Start')
            start_button.setStyleSheet(button_style)
            start_button.setFixedSize(150, 60)
            start_button.clicked.connect(lambda _, b=start_button, cmd=start_command: cmd(b))
            start_button.setToolTip(description)  # 设置鼠标悬停提示信息,显示功能描述

            stop_button = QPushButton('Stop')
            stop_button.setStyleSheet(button_style)
            stop_button.setFixedSize(150, 60)
            stop_button.clicked.connect(lambda _, b=start_button, cmd=stop_command: cmd(b))

            grid_layout.addWidget(label, index, 3)
            grid_layout.addWidget(start_button, index, 4)
            grid_layout.addWidget(stop_button, index, 5)

        start_button_style = 'background-color: #08f311; color: black; font-size: 12pt; padding: 10px; min-width: 80px; min-height: 20px;'
        restart_button_style = 'background-color: #0074D9; color: black; font-size: 12pt; padding: 10px; min-width: 80px; min-height: 20px;'
        # 添加编译功能:
        catkin_button_style = 'background-color: #0074D9; color: black; font-size: 12pt; padding: 10px; min-width: 80px; min-height: 20px;'
        shutdown_button_style = 'background-color: #C40000; color: black; font-size: 12pt; padding: 10px; min-width: 80px; min-height: 20px;'
        horizontal_layout = QHBoxLayout()
        buttons = [
            ("Catkin-Make", catkin_button_style, self.catkin_make),
            ("关闭", shutdown_button_style, self.shutdown)
        ]

        for title, style, command in buttons:
            button = QPushButton(title)
            button.setStyleSheet(style)
            button.setFixedSize(150, 60)
            button.clicked.connect(command)

            horizontal_layout.addWidget(button)

        main_layout.addLayout(horizontal_layout)
        main_layout.addLayout(grid_layout)

        # Add author information
        author_label = QLabel('Contributed by CYUN')
        author_label.setAlignment(Qt.AlignCenter)
        author_label.setStyleSheet('font-size: 12pt; color: black;')
        main_layout.addWidget(author_label)
        self.setLayout(main_layout)

    def start_all(self):
        run_command_restart(f"bash -c '{WORKSPACE_PATH}/src/auto_start/sh/start_all.sh'")

    def restart_all(self):
        run_command(f"bash -c 'source {WORKSPACE_PATH}/devel/setup.bash;rosrun smach_fork cli.py'")

    def catkin_make(self):
        # 进入工作空间编译
        run_command(f"bash -c 'source {WORKSPACE_PATH}/devel/setup.bash;cd {WORKSPACE_PATH};catkin_make;exec bash;'")

    def shutdown(self):
        # 关机
        stop_command("real_clamp.launch")
        stop_command("sim.launch")
        stop_command("camera_node.launch")
        stop_command("lidar_camera_det.launch")
        stop_command("vehicle_control.launch")
        stop_command("location_ndt_gps.launch")
        stop_command("ndt_localizer.launch")
        stop_command("smach_fork.launch")
        stop_command("run_hybrid_a_star.launch")
        stop_command("htop")
        stop_command("agv_status_report.py")
        stop_command("sub_cloud.py")
        stop_command("auto_start.launch")
        stop_command("v2n.launch")
        stop_command("fusion_pointclouds.launch")
        stop_command("forklift_start.py")
        QApplication.quit()


if __name__ == '__main__':
    global WORKSPACE_PATH
    app = QApplication(sys.argv)
    ex = RuntimeManager()
    ex.show()
    sys.exit(app.exec_())

按键自启动的方案从现在开始彻底换成UI启动式的界面开发:
请添加图片描述
当然,这个UI风格并不是最好的,其他美化现在就先不考虑了

posted @ 2024-12-12 16:58  白云千载尽  阅读(4)  评论(0)    收藏  举报  来源