1. 程式人生 > >【QT】QT的學習:在QT中如何呼叫ROS庫

【QT】QT的學習:在QT中如何呼叫ROS庫

舉例:目前正在QT開發介面等,但是需要用到ROS的訂閱釋出機制,訂閱別的ROS模組的topic.

(3)特別重要,否則編譯不過:

配置qt的啟動檔案。需要在qt啟動時載入ros的環境變數

sudo gedit ~/.local/share/applications/DigiaQt-qtcreator-community.desktop 

[Desktop Entry] Type=Application Exec=bash -i -c /home/ubuntu/Qt5.10.1/Tools/QtCreator/bin/qtcreator Name=Qt Creator (Community) GenericName=The IDE of choice for Qt development. Icon=QtProject-qtcreator StartupWMClass=qtcreator Terminal=false Categories=Development;IDE;Qt; MimeType=text/x-c++src;text/x-c++hdr;text/x-xsrc;application/x-designer;application/vnd.qt.qmakeprofile;application/vnd.qt.xml.resource;text/x-qml;text/x-qt.qml;text/x-qt.qbs;

中加上:

bash -i -c

(4)在寫的QT工程中加入:即在.pro檔案中新增ros標頭檔案路徑和動態連結庫。

INCLUDEPATH += /opt/ros/kinetic/include
DEPENDPATH += /opt/ros/kinetic/include
LIBS += -L/opt/ros/kinetic/lib -lroscpp -lroslib -lrosconsole -lroscpp_serialization -lrostime

(5)嘗試例子:

(5.1)可以現在本地建立一個簡單的訂閱\釋出的ros例子,網上很多.

見:

這樣就可以實現ros的訂閱或者釋出.

(4.2)在QT的程式上加上ROS函式:這是一個訂閱的程式,但是該程式還是有問題的,因為在ros::spin();處於死迴圈,導致不能執行a.exec();介面沒有顯示.

#include "widget.h"
#include <QApplication>
#include "ros/ros.h"
#include "std_msgs/String.h"

//話題回撥函式
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
    ROS_INFO("node_b is receiving [%s]", msg->data.c_str());
    qDebug("node_b is receiving [%s]", msg->data.c_str());
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    ros::init(argc, argv, "listener");
    ros::NodeHandle node;
    ros::Subscriber sub = node.subscribe("message", 1000, chatterCallback);
    ros::spin();

    return a.exec();
}

在終端執行roscore;

新開終端執行釋出程式:

rosrun test1_tutorials test1_a 執行QT,得到:Qt上可以收到釋出的資訊.

借鑑自: