Writing a Simple Publisher and Subscriber (C++)
Writing a Simple Publisher and Subscriber (C++)
Editor's Note: Error in build, bash: syntax error near unexpected token `talker'
Writing the Publisher Node
1) In a terminal, move to your beginner_tutorials folder:
cd ~/catkin_ws/src/beginner_tutorials
2) Create the file talker.cpp:
gedit src/talker.cpp
3) In the gedit editor, paste all from this link. Save the file and close the editor. For information on what is inside this file, see this link. In a nutshell, it initializes the ROS system, advertise what we are doing, and finally loop what we are doing.
Writing the Subscriber Node
1) In a terminal, move to your beginner_tutorials folder:
cd ~/catkin_ws/src/beginner_tutorials
2) Create the file listener.cpp:
gedit src/listener.cpp
3) In the gedit editor, paste all from this link. Save the file and close the editor. For information on what is inside this file, see this link. In a nutshell, it initializes the ROS system, subscribes to the topic 'chatter', spin (wait for messages), when message arrives call the chatterCallback() function.
Building Nodes
1) Edit the CMakeList.txt file:
gedit beginner_tutorials/CMakeLists.txt
Add the following to the end of the file:
include_directories(include ${catkin_INCLUDE_DIRS}) add_executable(talker src/talker.cpp) target_link_libraries(talker ${catkin_LIBRARIES}) add_dependencies(talker beginner_tutorials_generate_messages_cpp) add_executable(listener src/listener.cpp) target_link_libraries(listener ${catkin_LIBRARIES}) add_dependencies(listener beginner_tutorials_generate_messages_cpp)
For a completely clean file, you can view the entire CMakeLists.txt file here
Save the file and close the editor. This should create a talker and listener files in your devel folder.