Difference between revisions of "Writing a Simple Publisher and Subscriber (C++)"

From Lofaro Lab Wiki
Jump to: navigation, search
(Created page with "==Writing a Simple Publisher and Subscriber (C++)== '''Writing the Publisher Node''' 1) In a terminal, move to your beginner_tutorials folder: <nowiki>cd ~/catkin_ws/src/b...")
 
(Writing a Simple Publisher and Subscriber (C++))
Line 1: Line 1:
 
==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'''
 
'''Writing the Publisher Node'''
  
Line 23: Line 25:
  
 
'''Building Nodes'''
 
'''Building Nodes'''
 +
 +
1) Edit the CMakeList.txt file:
 +
  <nowiki>gedit beginner_tutorials/CMakeLists.txt</nowiki>
 +
Add the following to the end of the file:
 +
  <nowiki>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)</nowiki>
 +
For a completely clean file, you can view the entire CMakeLists.txt file [https://raw.githubusercontent.com/ros/catkin_tutorials/master/create_package_pubsub/catkin_ws/src/beginner_tutorials/CMakeLists.txt here]
 +
 +
Save the file and close the editor. This should create a talker and listener files in your devel folder.

Revision as of 16:43, 21 October 2014

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.