Custom message
ROS message
1. Simply put, the message of ROS is similar to the structure of c language. The structure contains all the standard data types and can be nested between messages. ROS brings a series of messages, such as std_msgs. It can also be user-defined. The data types supported by messages are as follows, and between c++ language and python language. Correspondence.
Message data type | serialize | Corresponding C++ |
---|---|---|
bool | uint8_t | bool |
int8 | int8_t | int |
uint8 | uint8_t | int |
int16 | int16_t | int |
uint16 | uint16_t | int |
int32 | int32_t | int |
uint32 | uint32_t | int |
int64 | int64_t | long |
uint64 | uint64_t | long |
float32 | float | float |
float64 | double | double |
string | std::string | string |
time | ros::Time | rospy.Time |
duration | ros::Duration | rospy.Duration |
Custom message
1. In the first program of ROS, we used the first message of ROS, the String message of std_msgs. So, how do we define a message ourselves?
For example: define person, name, age, gender.
2. In the previous project, we can create a msg folder under the ros_test folder. Note that the case is exactly the same. Create a new person.msg file under the MSG folder.
Create a custom person-type message in the following way. You can define variables or constants.
string name #variable uint8 age #variable uint8 sex #variable uint8 male = 0 #constant uint8 female = 1 #constant
Generate message
Modify package.xml file
Added to the <-package> node of package.xml for automatic message generation.
<!--Custom message type, function package dependency--> <build_depend>message_generation</build_depend> <exec_depend>message_runtime</exec_depend>
Modify the CMakeLists.txt file
1, find_package adds:
find_package(catkin REQUIRED COMPONENTS roscpp std_msgs message_generation #Custom message type dependency }
2, add_message_files, add person.msg file here
add_message_files( FILES person.msg )
3. Add generate_messages.
generate_messages( DEPENDENCIES std_msgs #Custom message dependency )
4. Add catkin_package.
catkin_package( INCLUDE_DIRS include LIBRARIES ros_test CATKIN_DEPENDS roscpp std_msgs message_runtime #Custom dependencies DEPENDS system_lib )
5. Use catkin_make in the terminal of vscode to generate person message. When the generation is completed, you can view the generated person.h file in the devel/include/ros_test folder.
Create Publishers and Subscribers
1. Create under the src folder, personTalker.cpp and personListener.cpp files.
2. Paste the following code in personTalker.cpp.
#include <iostream> #include "ros/ros.h" #include "ros_test/person.h" //Automatic generation int main(int argc, char **argv) { setlocale(LC_ALL, ""); //Chinese support //Ros node initializes the "personTalker" node name, and two are not allowed in ROS at the same time. ros::init(argc,argv,"personTalker"); //Create node handles ros::NodeHandle h; //Create a publisher, topic:personChat, message type ros_test::person ros::Publisher chatter_pub = h.advertise<ros_test::person>("personChat",1000); // "personChat" topic name, message caching //Setting the frequency of a single cycle ros::Rate looprate(10); while (ros::ok()) { ros_test::person per; per.name = "Mu Shi Ning soul"; per.age = 20; per.sex = per.male; chatter_pub.publish(per); //Waiting for callback function ros::spinOnce(); //Follow the previous cycle looprate.sleep(); } }
3. Paste the following code in personTalker.cpp.
#include <ros/ros.h> #include "ros_test/person.h" #include "std_msgs/String.h" void chatterCallBack(const ros_test::person::ConstPtr &msg) { if(msg->sex==msg->male) { ROS_INFO("I get a man : name [%s], age[%d]}",msg->name.c_str(), msg->age); } else { ROS_INFO("I get a woman: name [%s], age[%d]}",msg->name.c_str(), msg->age); } } int main(int argc, char **argv) { setlocale(LC_ALL, ""); //Ros node initializes the "personListener" node name, and two are not allowed in ROS at the same time. ros::init(argc,argv,"personListener"); //Create node handles ros::NodeHandle h; //Create a subscriber, topic:chatter, message type ros_test::person, receive the message, and respond to chatterCallBack ros::Subscriber listener_sub = h.subscribe<ros_test::person>("personChat",1000,chatterCallBack); //Loop waiting callback function ros::spin(); return 0; }
4. For specific explanations, please see the explanations.
5. Modify the CMakeLists.txt file
add_executable(personListener src/personListener.cpp) target_link_libraries(personListener ${catkin_LIBRARIES}) add_dependencies(personListener ${PROJECT_NAME}_gen_cpp) #Contains custom message type customization add_executable(personTalker src/personTalker.cpp) target_link_libraries(personTalker ${catkin_LIBRARIES}) add_dependencies(personTalker ${PROJECT_NAME}_gen_cpp) #Contains custom message type customization
Compile and run
Using catkin_make in the terminal of vscode, compile the whole project, use the shortcut key Ctrl + Shift + T, pop up the system terminal, start the roscore, run the personListener and personTalker nodes, test results:
Epilogue
More ROS-related robots, public-public-number search "robot station".