< >
Home » ROS与QT语言入门教程 » ROS与QT语言入门教程-按钮调用ROS的librviz

ROS与QT语言入门教程-按钮调用ROS的librviz

ROS与QT语言入门教程-按钮调用ROS的librviz

说明:

  • 在ROS QT GUI模板上,按钮调用ROS的librviz

点击查看完整代码

点击查看librviz的源代码

运行和测试librviz应用程序

  • 安装预编译的Debian软件包
$ sudo apt-get install ros-kinetic-visualization-tutorials
  • 启动 ros master
$ roscore
  • 运行librviz应用程序
$ rosrun librviz_tutorial myviz
  • 弹出下面的界面

请输入图片描述

按钮调用ROS的librviz步骤:

  • 下载librviz源码
$ git clone https://github.com/ros-visualization/visualization_tutorials
  • 创建ros qt包
$ catkin_create_qt_pkg test3 
  • 所有的源代码放在visualization_tutorials/librviz_tutorials/src目录下

    • 把myviz.h复制到test3包的include目录下

    • 把myviz.cpp复制到test3包的src目录下

  • 在qtcreator创建工程文件

  • 编辑main_windows.cppmyviz.cpp,添加头文件

    #include "../include/test3/myviz.h"

  • 编辑main_window.hpp头文件

    把#include <QtGui/QMainWindow> 修改为 #include <QtWidgets/QMainWindow>

  • 编辑main_window.ui文件

    • 拖入一个按钮控件,名为librviz

    • 在按钮空间上右击,选择Go to slot... -> clicked()

  • 编辑main_windows.cpp源文件

namespace test3 {
    void test3::MainWindow::on_pushButton_clicked()
    {
      MyViz* myviz = new MyViz();
      myviz->show();
      myviz->resize(600,400);
      myviz->setWindowTitle("Mapping Show");
    }
}

代码解释:创建对象myviz,并且进行显示,设置它的大小为600 x 400,并且命名标题为Mapping Show

  • 编辑CMakeLists.txt文件
cmake_minimum_required(VERSION 2.8.0)
project(test3)
set(CMAKE_INCLUDE_CURRENT_DIR ON)#added

find_package(catkin REQUIRED COMPONENTS  roscpp rviz)#deleted qt_build added rviz
find_package(Qt5 REQUIRED Core Widgets)#added
set(QT_LIBRARIES Qt5::Widgets)#added
include_directories(${catkin_INCLUDE_DIRS})

catkin_package()

#rosbuild_prepare_qt4(QtCore QtGui) # Add the appropriate components to the component list here

file(GLOB QT_FORMS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ui/*.ui)
file(GLOB QT_RESOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} resources/*.qrc)
file(GLOB_RECURSE QT_MOC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS include/test3/*.hpp *.h)

QT5_ADD_RESOURCES(QT_RESOURCES_CPP ${QT_RESOURCES})
QT5_WRAP_UI(QT_FORMS_HPP ${QT_FORMS})
QT5_WRAP_CPP(QT_MOC_HPP ${QT_MOC})

file(GLOB_RECURSE QT_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS src/*.cpp)

add_executable(test3 ${QT_SOURCES} ${QT_RESOURCES_CPP} ${QT_FORMS_HPP} ${QT_MOC_HPP})
target_link_libraries(test3 ${QT_LIBRARIES} ${catkin_LIBRARIES})
install(TARGETS test3 RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
  • 编辑package.xml
    <build_depend>rviz</build_depend>
    <run_depend>rviz</run_depend>
    <build_depend>qtbase5-dev</build_depend>
    <run_depend>libqt5-core</run_depend>
    <run_depend>libqt5-gui</run_depend>
    <run_depend>libqt5-widgets</run_depend>
  • 编译运行

    • 在终端运行:
$ roscore
  • 运行qt程序

  • 点击按钮 connect -> libviz

  • 拉动Line Thickness滑条,调整线的粗细

  • 拉动Cell Size滑条,调整块的大小

请输入图片描述

参考资料
http://docs.ros.org/fuerte/api/librviz_tutorial/html/
https://github.com/ros-visualization/visualization_tutorials
https://blog.csdn.net/qq_30460905/article/details/79210300

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ros与qt语言入门教程, ros qt