ROS探究(二)自定义msg并使用Clion编译


1 建立msg文件

tzy@ubuntu:~/ROS_Example/catkin_ws$ source devel/setup.bash tzy@ubuntu:~/ROS_Example/catkin_ws$ roscd beginner_tutorials/
tzy@ubuntu:~/ROS_Example/catkin_ws/src/beginner_tutorials$ vim msg/Area.msg

Area.msg内容为:
loat64 triangleArea
float64 squareAresa

2 修改package.xml和CMakeLists.txt

两个文件均处在~/ROS_Example/catkin_ws/src/beginner_tutorials
package.xml中确保包含

message_generation
message_runtime

CMakeLists.txt中确保包含

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation  需要使用msg生成包
)

 add_message_files(
    FILES
    Area.msg  添加自定义消息
 )

generate_messages(
  DEPENDENCIES
  std_msgs  自定义消息Area依赖std_msgs
)

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES beginner_tutorials
  CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
#  DEPENDS system_lib
)

add_executable(msg_talker src/msg_talker.cpp)  添加编译对象
target_link_libraries(msg_talker ${catkin_LIBRARIES})
add_dependencies(msg_talker ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

add_executable(msg_listener src/msg_listener.cpp)  添加编译对象
target_link_libraries(msg_listener ${catkin_LIBRARIES})
add_dependencies(msg_listener ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

3 添加源代码

msg_talker.cpp

#include "ros/ros.h"
#include "beginner_tutorials/Area.h"


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

    ros::init(argc, argv, "talker");
    ros::NodeHandle n;
    ros::Publisher chatter_pub = n.advertise("chatter", 5);
    ros::Rate loop_rate(1);


    int count = 0;
    while (ros::ok())
    {
        beginner_tutorials::Area msg;
        std::stringstream ss;

        msg.squareAresa = count;
        msg.triangleArea = count*2;

        chatter_pub.publish(msg);

        ros::spinOnce();

        loop_rate.sleep();
        ++count;

    }

    return 0;
}

msg_listener.cpp

#include "ros/ros.h"
#include "beginner_tutorials/Area.h"

void chatterCallback(const beginner_tutorials::Area::ConstPtr& msg)
{
    ROS_INFO("I heard: [%f] [%f]", msg->squareAresa, msg->triangleArea);
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "listener");
    ros::NodeHandle n;
    ros::Subscriber sub = n.subscribe("chatter", 5, chatterCallback);
    
    ros::spin();

    return 0;
}

4 编译

tzy@ubuntu:~/ROS_Example/catkin_ws$ catkin_make
Base path: /home/tzy/ROS_Example/catkin_ws
Source space: /home/tzy/ROS_Example/catkin_ws/src
Build space: /home/tzy/ROS_Example/catkin_ws/build
Devel space: /home/tzy/ROS_Example/catkin_ws/devel
Install space: /home/tzy/ROS_Example/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/tzy/ROS_Example/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/tzy/ROS_Example/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/tzy/ROS_Example/catkin_ws/devel;/opt/ros/kinetic
-- This workspace overlays: /home/tzy/ROS_Example/catkin_ws/devel;/opt/ros/kinetic
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/tzy/ROS_Example/catkin_ws/build/test_results
-- Found gmock sources under '/usr/src/gmock': gmock will be built
-- Found gtest sources under '/usr/src/gmock': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.18
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 2 packages in topological order:
-- ~~  - beginner_tutorials
-- ~~  - pluginlib_tutorials_
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'beginner_tutorials'
-- ==> add_subdirectory(beginner_tutorials)
-- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy
-- beginner_tutorials: 1 messages, 0 services
-- +++ processing catkin package: 'pluginlib_tutorials_'
-- ==> add_subdirectory(pluginlib_tutorials_)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tzy/ROS_Example/catkin_ws/build
####
#### Running command: "make -j4 -l4" in "/home/tzy/ROS_Example/catkin_ws/build"
####
[  0%] Built target std_msgs_generate_messages_py
[  0%] Built target std_msgs_generate_messages_lisp
[  0%] Built target roscpp_generate_messages_py
[  0%] Built target roscpp_generate_messages_cpp
[  0%] Built target rosgraph_msgs_generate_messages_nodejs
[  0%] Built target rosgraph_msgs_generate_messages_eus
[  0%] Built target _beginner_tutorials_generate_messages_check_deps_Area
[  0%] Built target std_msgs_generate_messages_cpp
[  0%] Built target rosgraph_msgs_generate_messages_cpp
[  0%] Built target rosgraph_msgs_generate_messages_py
[  0%] Built target roscpp_generate_messages_eus
[  0%] Built target rosgraph_msgs_generate_messages_lisp
[  0%] Built target roscpp_generate_messages_nodejs
[  0%] Built target std_msgs_generate_messages_nodejs
[  0%] Built target roscpp_generate_messages_lisp
[  0%] Built target std_msgs_generate_messages_eus
[  5%] Built target beginner_tutorials_generate_messages_cpp
[ 15%] Built target listener
[ 26%] Built target talker
[ 36%] Built target polygon_plugins
[ 47%] Built target beginner_tutorials_generate_messages_py
[ 52%] Built target beginner_tutorials_generate_messages_lisp
[ 57%] Built target beginner_tutorials_generate_messages_nodejs
[ 68%] Built target polygon_loader
[ 78%] Built target beginner_tutorials_generate_messages_eus
Scanning dependencies of target msg_listener
Scanning dependencies of target msg_talker
[ 78%] Built target beginner_tutorials_generate_messages
[ 89%] Building CXX object beginner_tutorials/CMakeFiles/msg_listener.dir/src/msg_listener.cpp.o
[ 89%] Building CXX object beginner_tutorials/CMakeFiles/msg_talker.dir/src/msg_talker.cpp.o
[100%] Linking CXX executable /home/tzy/ROS_Example/catkin_ws/devel/lib/beginner_tutorials/msg_listener
[100%] Linking CXX executable /home/tzy/ROS_Example/catkin_ws/devel/lib/beginner_tutorials/msg_talker
[100%] Built target msg_talker
[100%] Built target msg_listener

catkin_make编译成功后Area.h头文件生成在如下目录

tzy@ubuntu:~/ROS_Example/catkin_ws$ find ./ -type f | grep Area.h
./devel/include/beginner_tutorials/Area.h

5 Clion中运行项目

打开项目如下

虽然命令行中catkin_make可以编译成功,但是Clion中会存在Area.h头文件无法识别的情况,所以先要在clion中执行generate_msg编译操作

Area.h 生成在如下路径

 此时可直接编译msg_talker.cpp成功。