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

Hyperlinked Images in Gmail Signature

Instead of Texts, You Can Now Add Images. Here's How! If you use Google Apps for Business, you've probably set up a standard signature in Gmail (don't panic if you haven't). You'll be able to do it after this). You may even have a fancy signature that includes a picture of your company logo or other branding elements. But what if you could make a useful hyperlink out of an otherwise just beautiful logo? Fortunately, it's not only doable but also less difficult than you would imagine! Let's assume you want to change your signature to include a picture of your company logo, with a link to your corporate site when someone clicks on the logo. Here's how to go about it: Go to Gear > Settings > General > Signature from your Gmail inbox.  After you've finished writing your signature, click the Insert Image option to add the logo. Image by Author In the conventional sense, Gmail does not allow you to upload photos for signatures. As a result, you

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 table2 ON table1.matching_column =