ROS C++ classes
Two options for a publisher in pose_rviz.cpp
for lab2:
Global publisher
Your publisher needs to be advertised before spinning up the node (spin()
or spinOnce()
in a loop), so it will probably be instantiated in your main()
.
But your callback, turtle_pose_callback()
also needs to access it. If you declare the publisher outside main()
and then instantiate it properly in main()
your subscriber callback can publish something every time it gets a new message.
Publisher in a class
Because global variables are generally bad practice, you can wrap this nicely in a class to allow access to the publisher.
In your CMakeLists.txt you'd have
include_directories(
${PROJECT_SOURCE_DIR}/include
${catkin_INCLUDE_DIRS}
)
...
add_executable(EXECUTABLE_NAME src/file.cpp)
and in your ROS package, typically you'd have:
src/file.cpp
-
src/include/PACKAGE_NAME/file.h
replacingPACKAGE_NAME
with whatever your package is called (inCMakeLists.txt
it should be in here:
project(PACKAGE_NAME)
and replacing EXECUTABLE_NAME
with whatever you want your node executable to be called.
About the class structure itself, it's up to you.
I don't know how familiar you are with C++.
Maybe you can find some examples online?
Just make the necessary ROS includes for the data types you use in your header file, and include your header file in your .cpp
file like:
#include <PACKAGE_NAME/file.h>