catkin_make编译错误如下:

Base path: /home/zth/catkin_ws
Source space: /home/zth/catkin_ws/src
Build space: /home/zth/catkin_ws/build
Devel space: /home/zth/catkin_ws/devel
Install space: /home/zth/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/zth/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/zth/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/kinetic
-- This workspace overlays: /opt/ros/kinetic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.12", minimum required is "2") 
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/zth/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/gmock': gtests will be built
-- Found gmock sources under '/usr/src/gmock': gmock will be built
-- Found PythonInterp: /usr/bin/python2 (found version "2.7.12") 
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.20
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
CMake Error at /home/zth/catkin_ws/build/catkin_generated/order_packages.cmake:11 (message):
  Circular dependency in subset of packages:

  geometry_msgs, turtlesim
Call Stack (most recent call first):
  /opt/ros/kinetic/share/catkin/cmake/catkin_workspace.cmake:42 (include)
  CMakeLists.txt:67 (catkin_workspace)


-- Configuring incomplete, errors occurred!
See also "/home/zth/catkin_ws/build/CMakeFiles/CMakeOutput.log".
See also "/home/zth/catkin_ws/build/CMakeFiles/CMakeError.log".
Makefile:528: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1
Invoking "make cmake_check_build_system" failed

解决方法

将catkin_ws文件夹中所有文件删除,按照这篇博文https://blog.csdn.net/qq_44324181/article/details/108150419重新创建工作空间和功能包。
然后

创建功能包

cd ~/catkin_ws/src 
catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim

创建发布者代码(C++)

如何实现一个发布者
  • 初始化ROS节点;
  • 向ROS Master注册节点信息,包括发布的话题名和话题中的消息类型;
  • 创建消息数据;
  • 按照一定频率循环发布消息。
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/

/**
 * 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist
 */
 
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

int main(int argc, char **argv)
{
	// ROS节点初始化
	ros::init(argc, argv, "velocity_publisher");

	// 创建节点句柄
	ros::NodeHandle n;

	// 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
	ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);

	// 设置循环的频率
	ros::Rate loop_rate(10);

	int count = 0;
	while (ros::ok())
	{
	    // 初始化geometry_msgs::Twist类型的消息
		geometry_msgs::Twist vel_msg;
		vel_msg.linear.x = 0.5; // 线速度
		vel_msg.angular.z = 0.2; // 角速度

	    // 发布消息
		turtle_vel_pub.publish(vel_msg);
		ROS_INFO("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", 
				vel_msg.linear.x, vel_msg.angular.z); // 输出信息

	    // 按照循环频率延时
	    loop_rate.sleep();
	}
	return 0;
}

配置发布者代码编译规则

如何配置CMakeLists.txt中的编译规则
  • 设置需要编译的代码和生成的可执行文件;
  • 设置链接库。
// CMakeLists.txt
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})

编译并运行发布者

cd ~/catkin_ws
catkin_make
source devel/setup.bash
roscore
rosrun turtlesim turtlesim_node
rosrun learning_topic velocity_publisher