Skip to main content

Basic ROS programming on Windows 10 - Publisher and Subscriber nodes

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.


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.

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)

Comments

Popular posts from this blog

Hello World project in ROS on Windows 10

We've seen how to install ROS in Windows 10 in this article, this is the time to start programming. What is the very first thing we do once we setup a new Programming Language? Hello World !!! Lets see how to write Hello World in ROS on Windows machine. Before going into the programming, make sure you've installed Gedit on your Windows machine. You can get it from here. Also, setup the path for Gedit and restart your machine to ensure the path definition works. First open the ROS terminal and check for the working directory. We have to be in the catkin workspace. If you've already created it, go to that directory. Or else, create it like this. Create a src folder inside catkin_ws. Inside that newly created src folder, lets create a new package in it. The command to create a ROS package is as follows. >catkin_create_pkg [PACKAGE_NAME] [DEPENDENT_PACKAGE_1] ....[DEPENDENT_PACKAGE_N] ‘std_msgs’ and ‘roscpp’ were added as optional dependent packages...

SQL Joins

You’ll nearly always need to connect many tables if you want to extract anything useful out of data. A join clause in SQL joins columns from one or more tables into a new table, similar to a join operation in relational algebra. In this article, let’s see how the joins work in SQL. We are going to explore the following SQL Joins.   ∘ 1 — (INNER) JOIN   ∘ 2 — LEFT (OUTER) JOIN   ∘ 3 — RIGHT (OUTER) JOIN   ∘ 4 — FULL (OUTER) JOIN AND UNION   ∘ When to use it? Also, I’m going to use the following tables for the above-mentioned SQL Joins. 1 — (INNER) JOIN Returns records with values in both tables that are the same. Image from Author As long as the condition is met, the INNER JOIN keyword selects all rows from both tables. This keyword will produce a result-set by merging all rows from both tables that satisfy the criteria, i.e. the common field’s value will be the same. Syntax: SELECT table1.column1,table1.column2,table2.column1,…. FROM table1 INNER JOIN ...

Collection Framework in Java

Data Structure As a definition, the data structure is a specific method of organizing data in a computer so that it may be used efficiently. In this article, we are going to see about the Collection Framework which enables us to implement some important Data Structures available on Java. Topics covered:   ∘ Data Structure   ∘ Collection Framework   ∘ Class vs Interface   ∘ Lists   ∘ Set   ∘ Map   ∘ Difference between List, Set, and Map interface in Java Collection Framework A Java collection is a grouping of separate objects that are represented as a single entity. Java collections, like data, provide all actions such as searching, sorting, insertion, modification, deletion, and so on. Java Collections is a fairly broad topic, and as a newbie, it might be tough to find your way around. We’ve covered all you need to know to get started with Java Collections. Image from  https://www.geeksforgeeks.org/how-to-learn-java-collections-a-complete-guide/ W...