Use of custom msg for ROS

Keywords: xml Python github

Ros uses custom msg

# Code Hierarchy Diagram:
|- usbacn_ws
    |- build
    |- devel
    |- src
        |- CMakeLists.txt       
        |- usbcan_test
            |- include
                |- .h*
            |- lib
                |- .so*
            |- msg
                |- test.msg
            |- main.cpp
            |- CmakeLists.txt
            |- package.xml

1. Create an empty package to store the msg type separately (of course, you can also customize the msg type in any package). For illustration purposes, create a package named usbcan_test to illustrate the usage of the custom msg type.

$ cd usbacn_ws/src
$ catkin_create_pkg usbcan_test

2. Create a msg folder in usbcan_test and a new message type file named test.msg in the MSG folder.

$ cd usbcan_test
$ mkdir msg
$ gedit test.msg
# The contents are as follows:
std_msgs/Header header
int16 id
int16 len
int32[8] data

3. Modifying package.xml requires message_generation to generate code that C++ or Python can use. The. msg file will be compiled and generated. h file, which requires message_runtime to provide runtime support. So add the following two sentences to package.xml (there are usually generated files, just comment on them).

<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
# Or this sentence
<exec_depend>message_runtime</exec_depend>

If you need ros support, annotate it as well

<build_depend>roscpp</build_depend>
<exec_depend>roscpp</exec_depend>

4. Modify CMakeLists.txt, which is under the function packages. There are several points to note: (1) First, call find_package to find dependent packages. The necessary packages are roscpp, rospy, message_generation. Others are added according to specific types, such as std_msgs/header type used in the msg file above, so it is necessary to call find_package to find dependent packages. Find std_msgs

find_package(catkin REQUIRED COMPONENTS
   roscpp
   rospy
   std_msgs
   message_generation
)

(2) Then add_message_files, specifying the msg file

add_message_files(
    FILES
    test.msg
)

(3) Then generate_messages, specifying the dependencies when generating message files, such as the other message types std_msgs nested above, must be noted.

# Gene_messages must precede catkin_package
generate_messages(
    DEPENDENCIES
    std_msgs  
)

(4) Then the catkin_package setting runtime dependencies

catkin_package(
#    INCLUDE_DIRS include
    LIBRARIES usbcan_test
    CATKIN_DEPENDS roscpp message_runtime
    DEPENDS system_lib
)

The new msg type usbcan_test/test can be used here. Now compile the package and use the rosmsg show instruction to view it.

$ cd catkin_ws
$ catkin_make
$ rosmsg show usbcan_test/test
std_msgs/Header header
  uint32 seq
  time stamp
  string frame_id
int16 id
int16 len
int32[8] data

5. To call a custom msg type, if the usbcan_test/test type is called in a node in the usbcan_test package, you only need to call it in the. cpp file as follows

#include "usbcan_test/test.h"

usbcan_test::test msg;
// (usbcan_test folder): (test.msg) (any name)

Then modify CMakeLists.txt

# add_executable(${PROJECT_NAME}_node src/usbcan_test_node.cpp)
add_executable(cantest /home/fu/usbcan_ws/src/usbcan_test/main.cpp)
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
# This PROJECT_NAME is the node name of rosrun when you arrive
add_dependencies(cantest ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

# Dynamic library dependencies (if required)
target_link_libraries(cantest ${catkin_LIBRARIES}
    /usr/lib/libECanVci.so
    /usr/lib/libusb.so
    /usr/lib/libusb-1.0.so
)

CMakeLists.txt is annotated because the header file in the include folder is also used.

include_directories(
    include
    ${catkin_INCLUDE_DIRS}
)

6. Other package calls custom msg types

If you call the usbcan_test/test type in other packages, you need to modify package.xml and MakeLists. txt. For example, there is also a package named test in the workspace usbacn_ws. We can write a node in this package, using the message type usbcan_test/test we just defined, as follows: (1) Modify package.xml. Make it a habit to keep the list of packages updated so that you can install dependencies before others use your software. Of course, this file does not affect program compilation.

<build_depend>roscpp</build_depend>
<run_depend>roscpp</run_depend>

<build_depend>usbcan_test</build_depend>
<run_depend>usbcan_test</run_depend>

(2) Modify CMakeLists.txt call custom message type mainly in two areas, the following is the focus: first, find_package needs to declare to find the package containing the message type; second, add_dependencies need to indicate the dependency of the message, other places as normal nodes.

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  message_generation
  usbcan_test
)

add_dependencies(test1 usbcan_test_gencpp)#Indicate dependencies when calling custom message types in the same workspace to prevent header file failures

Complete Engineering Reference: https://github.com/FelicxFoster/ROS_learning/tree/master/src/usbcan_test

Posted by ptraffick on Sat, 10 Aug 2019 02:38:40 -0700