We've seen how to install ROS in Windows 10 in this article and how to write the Hello World in this article.
Today lets go further and lets write some advanced and very basic programming in ROS, Publisher and Subscriber nodes in Windows 10.
A transmitter and a recipient can be compared to the publishers and subscribers used in ROS message communication. The transmitter is called publisher in ROS, and the recipient is called subscriber. This section is intended to establish a simple message file, and to build and run nodes for Publisher and Subscriber.
First we have to create a package with dependencies inside the source folder in catkin workspace.
To create a new message the 'message generation' package will be required. 'Std msgs' is the standard ROS message package and the client library to use C/C++ in ROS is 'roscpp'. While creating the package, these dependent packages can be included. They can also be added after creating the 'package.xml' in the package folder.
You can ensure that the required files are created or not by the following code.
The ‘package.xml’ file, one of the required ROS configuration files, is an XML file containing the package information such as the package name, author, license, and dependent packages. Let’s open the file using an editor (such as gedit, vim, emacs, etc.) with the following command and modify it for the current node.
Use the exact file or you can edit as required.
Catkin, which is the build system of ROS, uses CMake. Therefore, the build environment is described in the ‘CMakeLists.txt’ file in the package folder. This file configures executable file creation, dependency package priority build, link creation, and so on.
Use the exact file or you can edit as required, but don't miss the necessary lines.
Use the exact file or you can edit as required, but don't miss the necessary lines.
Open another terminal and execute catkin_make in catkin workspace like this after executing rosack profile (this is otional).
catkin_make must be executed without an error and must end like this.
Now, open a new terminal and execute roscore.
Now let’s run the publisher. The following is a command to run the ‘ros_tutorial_msg_publisher’ node of the ‘ros_tutorials_topic’ package using the ‘rosrun’ command.
Let’s use the ‘rostopic’ command to receive the topic published by ‘topic_publisher’. First, list up the topics currently running on the ROS. Use ‘rostopic list’ command to verify that the ‘ros_ tutorial_msg’ topic is running.
The following is the command to run the ‘topic_subscriber’ node of ‘ros_tutorials_topic’ package using the ‘rosrun’ command to run the subscriber.
In windows, better run the setup.bat in devel folder inside the catkin workspace.
Next, let’s check the communication status of executed nodes using ‘rqt’. You can use either ‘rqt_graph’ or ‘rqt’ command as shown below.
We have done Publisher and Subscriber nodes using ROS in Windows successfully.
Source codes for this entire package can be found at (https://github.com/iamnickson/ros_tutorials_topic.git)
Today lets go further and lets write some advanced and very basic programming in ROS, Publisher and Subscriber nodes in Windows 10.
A transmitter and a recipient can be compared to the publishers and subscribers used in ROS message communication. The transmitter is called publisher in ROS, and the recipient is called subscriber. This section is intended to establish a simple message file, and to build and run nodes for Publisher and Subscriber.
First we have to create a package with dependencies inside the source folder in catkin workspace.
To create a new message the 'message generation' package will be required. 'Std msgs' is the standard ROS message package and the client library to use C/C++ in ROS is 'roscpp'. While creating the package, these dependent packages can be included. They can also be added after creating the 'package.xml' in the package folder.
You can ensure that the required files are created or not by the following code.
The ‘package.xml’ file, one of the required ROS configuration files, is an XML file containing the package information such as the package name, author, license, and dependent packages. Let’s open the file using an editor (such as gedit, vim, emacs, etc.) with the following command and modify it for the current node.
Use the exact file or you can edit as required.
Catkin, which is the build system of ROS, uses CMake. Therefore, the build environment is described in the ‘CMakeLists.txt’ file in the package folder. This file configures executable file creation, dependency package priority build, link creation, and so on.
Use the exact file or you can edit as required, but don't miss the necessary lines.
add_message_files(FILES MsgTutorial.msg)The above option indicates to include the message file ‘MsgTutorial.msg’, which will be used in this example node, when building the package. As ‘MsgTutorial.msg’ has not been created yet, let’s create the file in the following order:
The content is relatively clear in the message format. The message includes both 'stamp' type of time and 'int32' type of data variables. Other than these two types, the following types are also available: simple message types such as 'bool,' 'int8,' 'int16,' 'float32,' 'number,' 'length,' 'duration,' and 'common msgs', a list of commonly used ROS messages. We use time, and int32 in this simple example.
The following option was previously configured in the ‘CMakeLists.txt’ file to create an executable file:
add_executable(topic_publisher src/topic_publisher.cpp)
That is, the ‘topic_publisher.cpp’ file is built in the src’ folder to create the ‘topic_publisher’ executable file. Let’s create a code that performs publisher node functions in the following order:
Use the exact file or you can edit as required, but don't miss the necessary lines.
The following is an option in the ‘CMakeLists.txt’ file to generate the executable file.
add_executable(topic_subscriber src/topic_subscriber.cpp)
This mean that the ‘topic_publisher.cpp’ file is built to create the ‘topic_subscriber’ executable file. Let’s write a code that performs subscriber node functions in the following order:
Use the exact file or you can edit as required, but don't miss the necessary lines.
Open another terminal and execute catkin_make in catkin workspace like this after executing rosack profile (this is otional).
catkin_make must be executed without an error and must end like this.
Now, open a new terminal and execute roscore.
Now let’s run the publisher. The following is a command to run the ‘ros_tutorial_msg_publisher’ node of the ‘ros_tutorials_topic’ package using the ‘rosrun’ command.
However, the string displayed on the screen is the data in the publisher using the ROS_INFO() function, which is similar to the printf() function used in common programming languages. In order to actually publish the message on topic, we must use a command that acts as a subscriber node, such as a subscriber node or rostopic.
Let’s use the ‘rostopic’ command to receive the topic published by ‘topic_publisher’. First, list up the topics currently running on the ROS. Use ‘rostopic list’ command to verify that the ‘ros_ tutorial_msg’ topic is running.
Next, let’s verify the message being published from the publisher node. In other words, read the message on the ‘ros_tutorial_msg’ topic.
There will be an error. If you see this, it is because you have not source the setup.bat yet. So source it first.
Next, let’s check the communication status of executed nodes using ‘rqt’. You can use either ‘rqt_graph’ or ‘rqt’ command as shown below.
We have done Publisher and Subscriber nodes using ROS in Windows successfully.
Source codes for this entire package can be found at (https://github.com/iamnickson/ros_tutorials_topic.git)
Comments
Post a Comment