Publisher
ros::NodeHandle n; == Create a handle to this process' node. The first Nodehandle created will actually do the initialization of the node, and the last one destructed will cleanup and resources the node was using
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000); == Tell the master that we are going to be publishing a message fo type std_msgs/String on the topic chatter. queue size is 1000 which can buffer message up to 1000.
NodeHandle::advertise() returns ros::Publisher object
By default roscpp will install a SIGINT handler which provides Ctrl-C handling which will cause ros::ok() to return false if that happens.
ros::spinOnce(); == to call callbacks we need this
Subscriber
chatterCallback() == this is the callback function that will get called when a new message has arrived on the chatter topic. the message is passed in boost shared_ptr.
ros::Subscriber sub = n.subscribe("chatter" , 1000, chatterCallback); == Subscribe to the chatter topic with the master. ROS will call the chatterCallback() function whenever a enw emssage arrives.
Nodehandle::subscribe() returns a ros::SUbscriber object, that we must hold on to until you wnat to unsubscribe.
ros::spin(); == enters a loop, calling message callbacks as fast as possible. ros::spin() will exit once ros::ok() returns false.
CMakeList.txt to build new files
add_dependencies(talker beginner_tutorials_generate_messages_cpp) == this makes sure message headers of this package are generated before being used.
target_link_libraries(talker ${catkin_LIBRARIES}) == You can invoke executables directly or you can use rosrun to invoke them.
'자율주행 > ROS tutorial C++' 카테고리의 다른 글
Interactive Markers: Getting Started (0) | 2021.02.07 |
---|---|
Markers:Points and Lines (0) | 2021.02.07 |
Markers: Basic shapes (0) | 2021.01.10 |
Writing a Simple Service and Client (0) | 2020.12.30 |