飞道的博客

【902项目】qt+bash实现对rosbag的基本操作

230人阅读  评论(0)

qt中运行bash脚本

参考我另一篇博客https://blog.csdn.net/xx970829/article/details/116463167

bash脚本

在home目录下新建一个rosbash文件夹,并将以下sh脚本文件放入文件夹内:
roscore.sh

#!/bin/bash

gnome-terminal -- bash -c 'source /opt/ros/melodic/setup.bash && roscore'&

exit 0

bagplay.sh

#!/bin/bash
gnome-terminal -- bash -c "source /opt/ros/melodic/setup.bash;rosbag play /home/$1/$2"&
#source /opt/ros/melodic/setup.bash
#rosbag play /home/$1/$2
#echo "str"
exit 0

bagrecord.sh

#!/bin/bash
DATE=`date '+%Y-%m-%d-%H-%M-%S'`
bagname=$1
echo "记录的包名:$bagname "
source /opt/ros/melodic/setup.bash
rosbag record -O $bagname /usb_cam/image_raw /velodyne_points /daheng_camera /sbg/ekf_euler /sbg/ekf_nav /sbg/ekf_quat
sleep 5

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.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void readoutput();

    void readerror();

    void on_pushButton_4_clicked();

    void on_pushButton_5_clicked();

private:
    Ui::MainWindow *ui;
    QProcess *process;

};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QProcess>
#include<QDir>
#include<QFileDialog>
#include<QDebug>
#include<QTime>
#include<QMessageBox>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("rosbag");
    ui->lineEdit->setText("xx");//获取本机名
    process = new QProcess(this);
    connect(process , &QProcess::readyReadStandardOutput, this , &MainWindow::readoutput);
    connect(process , &QProcess::readyReadStandardError, this , &MainWindow::readerror);

}

MainWindow::~MainWindow()
{
    delete ui;
    if(process)
    {
          process->close();
          process->waitForFinished();
    }
}
//获取线程输出
void MainWindow::readoutput()
{
    ui->textEdit->append(process->readAllStandardOutput().data());   //将输出信息读取到编辑框
}
void MainWindow::readerror()
{
    QMessageBox::information(0, "Error", process->readAllStandardError().data());   //弹出信息框提示错误信息
}
//启动ros
void MainWindow::on_pushButton_clicked()
{
    QString strget=ui->lineEdit->text();
    QString str=QString("/home/%1/rosbash/roscore.sh").arg(strget);
    char *n;
    QByteArray m=str.toLatin1();
    n=m.data();
    QProcess::startDetached(n);
}
//添加bag列表
void MainWindow::on_pushButton_2_clicked()
{
    ui->comboBox->clear();
    QString strget=ui->lineEdit->text();
    QString pathget=ui->lineEdit_2->text();
    QString Path=QString("/home/%1%2").arg(strget).arg(pathget);//指定文件路径
    QDir dir(Path);
    //QDir dir(QDir::currentPath());//当前文件路径
    QString filtername = "*.bag";
    QStringList filter;
    filter << filtername;
    dir.setNameFilters(filter);
    QStringList Neuronindex = dir.entryList();
    ui->comboBox->addItems(Neuronindex);//把列表加载到comboBox
    QString s = QString::number(Neuronindex.size());//txt文件个数
    ui->textEdit->append(QString("rosbash $: 共有%1个bag文件").arg(s));

}

//运行该bag文件
void MainWindow::on_pushButton_3_clicked()
{
    QString strget=ui->lineEdit->text();
    QString pathget=ui->lineEdit_2->text();
    QString getbag=ui->comboBox->currentText();
    QString str=QString("/home/%1/rosbash/bagplay.sh %2%3 %4 \n").arg(strget).arg(strget).arg(pathget).arg(getbag);
    char *n;
    QByteArray m=str.toLatin1();
    ui->textEdit->append(QString("rosbash $: %1").arg(str));
    n=m.data();
    process->start("bash");      //启动终端(Windows下改为cmd)
    process->waitForStarted();   //等待启动完成
    process->write(n);           //向终端写入命令,注意尾部的“\n”不可省略
}
//删除该bag文件
void MainWindow::on_pushButton_4_clicked()
{
    QString strget=ui->lineEdit->text();
    QString pathget=ui->lineEdit_2->text();
    QString getbag=ui->comboBox->currentText();
    QString path =QString("/home/%1%2/%3").arg(strget).arg(pathget).arg(getbag);//指定文件路径
    QFile fileTemp(path);
    fileTemp.remove();
    ui->textEdit->append(QString("rosbash $: 成功删除%1文件").arg(path));
    on_pushButton_2_clicked();
}
//记录ros包
void MainWindow::on_pushButton_5_clicked()
{
    QString nameget=ui->lineEdit_3->text();
    QDate D;
    D=QDate::currentDate();
    QTime T;
    T=QTime::currentTime();
    QString bagname =QString("%1%2-%3-%4-%5:%6:%7.bag").arg(nameget).arg(D.year()).arg(D.month()).arg(D.day()).arg(T.hour()).arg(T.minute()).arg(T.second());//指定文件路径
    //qDebug() << bagname;
    ui->textEdit->append(QString("rosbash $: 记录的包将被命名为:%1").arg(bagname));
    QString strget=ui->lineEdit->text();
    QString str4=QString("gnome-terminal -- bash -c '/home/%1/rosbash/bagrecord.sh %2'&").arg(strget).arg(bagname);
    ui->textEdit->append(QString("rosbash $: /home/%1/rosbash/bagrecord.sh %2").arg(strget).arg(bagname));
    char *n;
    QByteArray m=str4.toLatin1();
    n=m.data();
    system(n);
}

界面搭建

使用简介

1.文件路径不加时,默认寻找home目录下的bag文件:


2.添加路径:


3.播放bag

4.删除包


5.记录包

6.给记录的ros包名加一个的前缀


转载:https://blog.csdn.net/xx970829/article/details/116535549
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场