LoginSignup
1
0

More than 1 year has passed since last update.

Qt Creatorで作成したGUIのROSノード化

Last updated at Posted at 2021-12-10

・Qt Creatorで生成したGUIでROSメッセージを扱いたく,まずROSノード化をしてみました

内容

1.Qt Creatorでプロジェクトを作成(ビルドシステムはcmakeを選択)

・qtcreatorでプロジェクトを作成します(※qtcreatorの基本的な使い方は割愛させていただきます)
・ビルドシステムの選択でcmakeを選択

2.qt creatorのcmake設定

qtcreatorのRUNボタン実行時にはCMAKE_PREFIX_PATHへrosパッケージのパスが通っていないみたいなので以下のように,プロジェクトのビルド設定画面で/opt/ros/noeticみたいにパスを追加してあげます.

cmake_setting2.png

そしてfind_packageroscppを探索できるようにします.

Untitled4.png

するとプロジェクトのcmakeパス設定にroscpp関係のパッケージパスが追加されます.

cmake_setting3.png

これでrosパッケージへのパスが通るようになりました

3.メインプログラムをROSノード化

次にメインプログラムをノード化するように変更します

以下3点を追加すればOKです

#include <ros/ros.h>
ros::init(argc, argv, "aaa");
ros::NodeHandle nh = ros::NodeHandle();

追加後のプログラムは以下のようになります

main.cpp
#include "mainwindow.h"

#include <QApplication>
#include <ros/ros.h> // 追加


int main(int argc, char *argv[])
{

    ros::init(argc, argv, "aaa"); // 追加

    ros::NodeHandle nh = ros::NodeHandle(); // 追加

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

4.CMakeLists.txtroscppパッケージを読み込む

roscppへのリンク設定などを行います.

以下3点を追加すればOK

find_package( roscpp )を追加(※手順2ですでに追加済み)
include_directories( ${roscpp_INCLUDE_DIRS} )を追加
target_link_libraries(untitled26 PRIVATE Qt5::Widgets ${roscpp_LIBRARIES})${roscpp_LIBRARIES}を追加

追加後のCMakeLists.txtは以下のようになります

CMakeLists.txt
cmake_minimum_required(VERSION 3.5)

project(untitled26 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.

#if(ANDROID)
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
#    if (ANDROID_ABI STREQUAL "armeabi-v7a")
#        set(ANDROID_EXTRA_LIBS
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
#    endif()
#endif()

find_package(Qt5 COMPONENTS Widgets REQUIRED)

# 追加 ここから
find_package( roscpp )

include_directories(
 ${roscpp_INCLUDE_DIRS}
)
# 追加 ここまで

if(ANDROID)
  add_library(untitled26 SHARED
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
  )
else()
  add_executable(untitled26
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
  )
endif()

target_link_libraries(untitled26 PRIVATE Qt5::Widgets ${roscpp_LIBRARIES}) # 追加

5.実行

違うブラウザでroscore実行

roscore

qt creatorのrunボタンで実行でGUIが表示されます

result.png

GUIがノードになったか確認

$ rosnode list
/aaa
/rosout

OKです.

まとめ

qt creatorで作成したプロジェクトのROSノード化ができました

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0