qt creator下创建ros工程,实现小乌龟程序。
程序员文章站
2022-07-14 22:09:23
...
1.安装ros,详细过程可以看我的上一篇博客。
https://blog.csdn.net/yjf20127/article/details/107249552
2.下载qt for ros
安装Qt Creator for ROS(如果已经装了其他版本的qt,最好线卸载)
$ sudo apt install qt59creator
$ sudo apt-get install qt57creator-plugin-ros
$ sudo add-apt-repository ppa:levi-armstrong/qt-libraries-xenial
$ sudo add-apt-repository ppa:levi-armstrong/ppa
$ sudo apt-get update && sudo apt-get install qt57creator-plugin-ros libqtermwidget57-0-dev
3.创建工程
(1)新建工程
(2)创建package
右击工程->Add New->Package
(3)新建cpp文件
右击工程->Add New->Basic Node
(4)最终目录如下:
(5)修改Cmake文件
cmake_minimum_required(VERSION 3.0.2)
project(pkg)
find_package(catkin REQUIRED COMPONENTS roscpp)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES pkg
# CATKIN_DEPENDS roscpp
# DEPENDS system_lib
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(rostest hello.cpp)
target_link_libraries(rostest ${catkin_LIBRARIES})
(6)修改package.xml文件(之前新建pkg文件,只添加了roscpp,需要添加一些其他的依赖)
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp rospy stf_msgs geometry_msgs turtlesim</build_depend>
<build_export_depend>roscpp rospy stf_msgs geometry_msgs turtlesim</build_export_depend>
<exec_depend>roscpp rospy stf_msgs geometry_msgs turtlesim</exec_depend>
(7)编码
#include <ros/ros.h>
#include "turtlesim/Pose.h"
void poseCallBack(const turtlesim::Pose::ConstPtr &msg)
{
ROS_INFO("Turtle pose :x:%0.6f,y%0.6f",msg->x,msg->y);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "hello");
ros::NodeHandle n;
ROS_INFO("Hello world!");
ros::Subscriber pose_sub = n.subscribe("turtle1/pose",10,poseCallBack);
ros::spin();
}
(8)点击qt右下方构建,没有报错的话成功一大半了。
(9)打开一个终端,运行:roscore
(10)打开另一个终端,运行:rosrun pkg rostest
(11)最后效果如下:
最近自学ros,可能会有一些不全面的地方,还请见谅。