前期准备
建立ssh连接及多机通信:
参考我另一篇博客:https://blog.csdn.net/xx970829/article/details/114899706
expect安装:
sudo apt-get install expect
expect文件
roscore.sh
#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set port 22
set password 6
set timeout -1
spawn ssh $user@$host
sleep 3
send "source /opt/ros/melodic/setup.bash\r"
send "roscore\r"
interact
cam.sh
#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set port 22
set password 6
set timeout -1
spawn ssh $user@$host
sleep 3
send "roslaunch usb_cam usb_cam-test.launch\r"
interact
imu.sh
#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set port 22
set password 6
set timeout -1
spawn ssh $user@$host
sleep 3
send "roslaunch sbg_driver sbg_device.launch\r"
interact
velodyne.sh
#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set port 22
set password 6
set timeout -1
spawn ssh $user@$host
sleep 3
send "roslaunch velodyne_pointcloud VLP16_points.launch\r"
interact
record.sh
#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set port 22
set password 6
set timeout -1
spawn ssh $user@$host
sleep 3
send "rosbag record /usb_cam/image_raw /velodyne_points /daheng_camera /sbg/ekf_euler /sbg/ekf_nav /sbg/ekf_quat\r"
interact
number.sh
#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set num [lindex $argv 2]
set port 22
set password 6
set timeout -1
spawn ssh $user@$host
sleep 3
send "rostopic echo $num\r"
interact
shutdown.sh
#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set port 22
set password 6
set timeout -1
spawn ssh $user@$host
sleep 3
send "sudo shutdown -h now\r"
interact
qt程序
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<iostream>
#include<QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit->setText("xx");//本机名xx
ui->lineEdit_2->setText("robot");//工控机名
ui->lineEdit_3->setText("192.168.1.104");//ip
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::getstring()
{
str1=ui->lineEdit->text();//本机名
str2=ui->lineEdit_2->text();//工控机名
str3=ui->lineEdit_3->text();//ip
}
//ros
void MainWindow::on_pushButton_clicked()
{
getstring();
//system("gnome-terminal -- bash -c 'chmod +x /home/xx/roscore.sh;/home/xx/roscore.sh robot 192.168.1.104'&");
QString str4=QString("gnome-terminal -- bash -c 'chmod +x /home/%1/roscore.sh;/home/%2/roscore.sh %3 %4'&").arg(str1).arg(str1).arg(str2).arg(str3);
ui->textEdit->append(str4);
char *n;
QByteArray m=str4.toLatin1();
n=m.data();
system(n);
}
//雷达
void MainWindow::on_pushButton_2_clicked()
{
getstring();
QString str4=QString("gnome-terminal -- bash -c 'chmod +x /home/%1/velodyne.sh;/home/%2/velodyne.sh %3 %4'&").arg(str1).arg(str1).arg(str2).arg(str3);
ui->textEdit->append(str4);
char *n;
QByteArray m=str4.toLatin1();
n=m.data();
system(n);
}
//贯导
void MainWindow::on_pushButton_3_clicked()
{
getstring();
QString str4=QString("gnome-terminal -- bash -c 'chmod +x /home/%1/imu.sh;/home/%2/imu.sh %3 %4'&").arg(str1).arg(str1).arg(str2).arg(str3);
ui->textEdit->append(str4);
char *n;
QByteArray m=str4.toLatin1();
n=m.data();
system(n);
}
//大恒相机
void MainWindow::on_pushButton_4_clicked()
{
getstring();
QString str4=QString("gnome-terminal -- bash -c 'chmod +x /home/%1/daheng.sh;/home/%2/daheng.sh %3 %4'&").arg(str1).arg(str1).arg(str2).arg(str3);
ui->textEdit->append(str4);
char *n;
QByteArray m=str4.toLatin1();
n=m.data();
system(n);
}
//红外相机
void MainWindow::on_pushButton_5_clicked()
{
getstring();
QString str4=QString("gnome-terminal -- bash -c 'chmod +x /home/%1/cam.sh;/home/%2/cam.sh %3 %4'&").arg(str1).arg(str1).arg(str2).arg(str3);
ui->textEdit->append(str4);
char *n;
QByteArray m=str4.toLatin1();
n=m.data();
system(n);
}
//记录ros包
void MainWindow::on_pushButton_6_clicked()
{
getstring();
QString str4=QString("gnome-terminal -- bash -c 'chmod +x /home/%1/record.sh;/home/%2/record.sh %3 %4'&").arg(str1).arg(str1).arg(str2).arg(str3);
ui->textEdit->append(str4);
char *n;
QByteArray m=str4.toLatin1();
n=m.data();
system(n);
}
//可视化
void MainWindow::on_pushButton_7_clicked()
{
system("gnome-terminal -- bash -c 'source /opt/ros/melodic/setup.bash;rqt_image_view'&");
}
//数据
void MainWindow::on_pushButton_8_clicked()
{
getstring();
QString str4=ui->comboBox->currentText();//获取参数
QString str5=QString("gnome-terminal -- bash -c 'chmod +x /home/%1/number.sh;/home/%2/number.sh %3 %4 %5'&").arg(str1).arg(str1).arg(str2).arg(str3).arg(str4);
ui->textEdit->append(str5);
char *n;
QByteArray m=str5.toLatin1();
n=m.data();
system(n);
}
//关闭所有终端
void MainWindow::on_action_triggered()
{
system("gnome-terminal -- bash -c 'killall -9 bash'&");
}
//工控机关机
void MainWindow::on_action_2_triggered()
{
getstring();
QString str4=QString("gnome-terminal -- bash -c 'chmod +x /home/%1/myssh/shutdown.sh;/home/%2/myssh/shutdown.sh %3 %4'&").arg(str1).arg(str1).arg(str2).arg(str3);
ui->textEdit->append(str4);
char *n;
QByteArray m=str4.toLatin1();
n=m.data();
system(n);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void getstring();
private slots:
void on_pushButton_clicked();//ros
void on_pushButton_2_clicked();//雷达
void on_pushButton_3_clicked();//贯导
void on_pushButton_4_clicked();//大恒相机
void on_pushButton_5_clicked();//红外相机
void on_pushButton_6_clicked();//记录ros包
void on_pushButton_7_clicked();//可视化
void on_pushButton_8_clicked();//数据
void on_action_triggered();//关闭所有终端
void on_action_2_triggered();//工控机关机
private:
Ui::MainWindow *ui;
QString str1;
QString str2;
QString str3;
};
#endif // MAINWINDOW_H
pro配置
如果要生成可执行文件,需要在pro中加入一行代码:
QMAKE_LFLAGS += -no-pie
界面搭建
主要界面:
1.菜单
创建两个动作
2.comboBox
创建4个选项
【项目下载链接(已经打包好,可直接运行):https://download.csdn.net/download/xx970829/15992965】