## For instruction on writing tutorials ## http://www.ros.org/wiki/WritingTutorials #################################### ##FILL ME IN #################################### ## for the canned note of "This tutorial assumes that you have completed the previous tutorials:" just add the links ## note.0= [[ROS/Tutorials/InstallingandConfiguringROSEnvironment| installing ROS]] ## descriptive title for the tutorial ## title = Writing a Simple Publisher and Subscriber (C++) (plain cmake) ## multi-line description to be displayed in search ## description = This tutorial covers how to write a publisher and subscriber node in C++ using plain cmake (i.e., not [[catkin]]). ## the next tutorial description (optional) ## next = ## links to next tutorial (optional) ## next.0.link= ## next.1.link= ## what level user is this tutorial for ## level= BeginnerCategory ## keywords = #################################### <> <> {{{#!wiki blue/solid '''Note:''' Be sure that you have already [[ROS/Installation|installed ROS]] and remember that you must [[ROS/Tutorials/InstallingandConfiguringROSEnvironment#Managing_Your_Environment|source the appropriate setup file]] in every shell you create. }}} == Making a directory in which to work == To help keep yourself organized, make a directory in which to work. You can use any name; we'll just call it `pubsub`: {{{ mkdir -p pubsub cd pubsub }}} == Writing the Publisher Node == <> === The Code === Create a file named `talker.cpp` within the `pubsub` directory and paste the following inside it: <> == Writing the Subscriber Node == === The Code === Create a file named `listener.cpp` within the `pubsub` directory and paste the following inside it: <> == Building your nodes == You need to tell `cmake` how to build (compile and link) your C++ code. Create a file named `CMakeLists.txt` in the `pubsub` directory and paste the following inside it: <> Build your nodes in a subdirectory of `pubsub` named `build` using the standard `cmake` sequence: {{{ mkdir -p build cd build cmake .. make }}} <> Now everything is set to run talker/listener. Open a new shell, go to your `pubsub` directory and type: {{{ ./build/talker }}} Now in the original shell type: {{{ ./build/listener }}} <> == Using roslaunch/rosrun == In order for roslaunch to find launch files associated with this node, you'll need to have a package.xml file located next to a `launch` folder which contains launch files, and you'll need to make sure that the path to that package.xml file can be found in the environment variable ROS_PACKAGE_PATH. The package.xml file just needs a valid name field, i.e. it can be as simple as {{{ my_pkg_name }}} In order for rosrun to be able to detect this executable, you'll need to install your executable into a folder structure that look like this: * The root folder contains an empty .catkin file * The root folder appears in the CMAKE_PREFIX_PATH environment variable * The root folder contains either a `libexec` or `share` folder which contains a folder named after the package, which then contains your executable That last one was a little wordy. Here's what the directory will look like: {{{ /path/to/installation_folder ├── .catkin <- empty file └── libexec <- could be 'share' instead of 'libexec', both are valid └── my_pkg_name └── my_executable }}} And /path/to/installation_folder needs to be appear in CMAKE_PREFIX_PATH. You may find it helpful to create your own setup.sh file to autopopulate the ROS_PACKAGE_PATH and CMAKE_PREFIX_PATH variables. ## AUTOGENERATED DO NOT DELETE ## TutorialCategory ## FILL IN THE STACK TUTORIAL CATEGORY HERE