In this video we are going to see how to create a simple ROS Publisher in C++.
* This is a video based on the question at the ROS Answers Forum
Step 1. Create a project in ROS Development Studio(ROSDS)
ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here. Let’s create a new project and call it abc_qa.
Step 2. Create a package
Let’s create a new package called abc_qa with dependency for the code.
cd catkin_ws/src catkin_create_pkg abc_qa roscpp
Then we create a source file called abc_code.cpp under the abc_qa/src directory with the following content
int main() { //Initialize and start the node ros::Publisher pub; //Define and create some messages abc; while(ros : ok) { ros_time = ros::time::now(); pub.publish(abc); ros::spinOnce(); } }
In order to compile the code, we have to change the following BUILD part in the CMakeLists.txt.
... ## Declare a C++ executable ## With catkin_make all packages are built within a single CMake context ## The recommended prefix ensures that target names across packages don't collide add_executable(abc_node src/abc_code.cpp) ## Rename C++ executable without prefix ## The above recommended prefix causes long target names, the following renames the ## target back to the shorter version for ease of user use ## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node" # set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "") ## Add cmake target dependencies of the executable ## same as for the library above add_dependencies(abc_node ${abc_node_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) ## Specify libraries to link a library or executable target against target_link_libraries(abc_node ${catkin_LIBRARIES} ) ...
Then we can try to compile it with the following command
cd ~/catkin_ws catkin_make
You should get several compilation errors. Let’s fix the code. It should look like this after changing.
#include <ros/ros.h> #include <std_msgs/Int32.h> int main(int argc, char ** argv) { //Initialize and start the node ros::init(argc, argv, "abc"); ros::NodeHandle nh; ros::Publisher pub = nh.advertise<std_msgs::Int32>("abc_topic", 1000); //Define and create some messages std_msgs::Int32 abc; abc.data = 0; ros::Rate(200); while(ros::ok) { pub.publish(abc); ros::spinOnce(); } }
It should work now. Try to compile and run it again with the following command
cd ~/catkin_ws catkin_make source devel/setup.bash rosrun abc_qa abc_node
Now we got error said that it’s missing master now. You can either run whatever simulation from Simulations or run roscore in a new shell to solve this problem. After launching the master, execute the code again. Although it shows nothing, it is actually starting to publish to topic /abc_topic you can see it with the command rostopic echo /abc_topic. You can also try to increment the counter in the while loop.
Related Course
Feedback
Did you like this video? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other. If you want to learn about other ROS topics, please let us know in the comments area and we will do a video about it.
Thank you for being there , It helps alot
what type is the ROS topic?