前言
最近在给ROS机器人做上位机界面,因为有点C++的基础,也就选择了绿油油的QT来做。作为刚入门的小白,单单是配环境就折磨了我好几天(半路出家,好多坑得爬),做点笔记,但愿以后别重新踩坑,也希望能对大家有所帮助。本文方法并没有使用 ros_qtc_plugin。
一、QT和ROS的安装
1.下载QT
QT的我装的是5.12.6版本 ,作为LTS的最新版本感觉靠谱一点,下载链接如下。
http://download.qt.io/official_releases/qt/5.12/5.12.6/
2.安装QT
chmod +x qt-opensource-linux-x64-5.12.6.run #给run文件执行权限
./qt-opensource-linux-x64-5.12.6.run #运行安装
然后一路next,skip。
到这步记得勾选 Qt 5.12.6 Desktop gcc 64-bit 组件
要设置相应的环境变量
sudo gedit ~/.bashrc #在末尾添加下面几行
export Qt5_DIR=~/opt/5.12.6/gcc_64 #设置Qt5_DIR 变量
export PATH=~/opt/5.12.6/gcc_64/bin:$PATH #添加Linux 命令行搜索路径
export LD_LIBRARY_PATH=~/opt/5.12.6/gcc_64/lib:$LD_LIBRARY_PATH #添加动态库的搜索路径
二、ROS的安装
我的使用的是安装脚本安装,比较方便。
https://git.lug.ustc.edu.cn/zenvo/ros_install_sh.git
在 installROSgeneral 目录下,下来后运行即可
期间可能会有一些ros的软件包没有安装成功
sudo apt-get update #更新一下源
在运行一次脚本即可,接下来设置环境变量
echo “source /opt/ros/kinetic/setup.bash” >> ~/.bashrc
三、用QT编写ROS程序
1.配置qtcreator
为了加载环境变量,需要终端打开QT。
sh /opt/Tools/QtCreator/bin/qtcreator.sh #这个路径跟你QT安装的位置有关,为了方便用alias给这行命令取个别名
sudo gedit ~/.bashrc #在末尾添加
alias qt=‘sh /opt/Tools/QtCreator/bin/qtcreator.sh’
也可以根据ros官方教程进行配置 http://wiki.ros.org/IDEs
创建一个功能包。在QT界面,File->Open File or Project 打开工作空间/src/CMakeLists.txt,将Project->Build&Run->CMake->Build directory:修改为工作空间的编译空间即可。
2.配置编写CMakeList.txt
cmake_minimum_required(VERSION 2.8.3)
project(qt_app)
## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(SOURCES
src/main.cpp
src/qt_app_node.cpp
src/mainwindow.cpp
src/qnavigationwidget.cpp
)
#find_package catkin
set(CMAKE_PREFIX_PATH /opt/5.12.6/gcc_64)
message(CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH})
message(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~find_package Qt5~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)
find_package(Qt5 5.12.6 REQUIRED COMPONENTS
Widgets
Gui
Core
WebEngineWidgets
)
#find_package catkin
set(CMAKE_PREFIX_PATH /opt/ros/kinetic)
message(CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH})
message(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~find_package catkin~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)
find_package(catkin REQUIRED COMPONENTS
sensor_msgs
cv_bridge
image_transport
roscpp
rospy
std_msgs
)
find_package(OpenCV REQUIRED
)
set(FORMS
src/mainwindow.ui
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES qt_app
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_executable(${PROJECT_NAME}_node ${SOURCES} ${FORMS} include/mainwindow.h include/qt_app_node.h include/qnavigationwidget.h)
target_link_libraries(${PROJECT_NAME}_node
${catkin_LIBRARIES}
${OpenCV_LIBS}
Qt5::Widgets
Qt5::Gui
Qt5::Core
Qt5::WebEngineWidgets
)