Template for a ROS Subscriber in Python

A few days ago I wrote a tutorial about a template for a Publisher node in Python. In the same tutorial, I used the template to write a ROS node to generate a random number.

Today, I continue the series of tutorials that ease the work of beginners in ROS, with a template for Subscriber in Python. In addition, in the article, you will find a template implementation with a ROS node that displays the data generated by the Publisher node from the previous tutorial.

Below you will find the template for a ROS Publisher node in Python. All you have to do is copy the text into a ‘. py’ file, delete the extra text and replace the text with capital letters.

Template for a ROS Publisher in Python

Template for a ROS Publisher in Python (image source)

#The ROS Node Subscriber template

#!/usr/bin/env python

#remove or add the library/libraries for ROS
import rospy, time, math, cv2, sys

#remove or add the message type
from std_msgs.msg import String, Float32, Image, LaserScan, Int32

#define function/functions to provide the required functionality
def name_callback(msg):
    make_something_here_with msg.data
    rospy.loginfo("I heard %s", msg.data)

if __name__=='__main__':
    #Add here the name of the ROS. In ROS, names are unique named.
    rospy.init_node('THE_NAME_OF_THE_NODE')
    #subscribe to a topic using rospy.Subscriber class
    sub=rospy.Subscriber('TOPIC_NAME', TOPIC_MESSAGE_TYPE, name_callback)
    rospy.spin()
 

I used the above template to write a ROS node that will display the random numbers sent by the node created to test the Publisher template.

#!/usr/bin/env python
import rospy

from std_msgs.msg import Int32
from random import randint


def random_callback(msg):
    rospy.loginfo("I heard %s", msg.data)

if __name__=='__main__':
    rospy.init_node('rand_subscriber')
   
    sub=rospy.Subscriber('rand_no', Int32, random_callback)
    rospy.spin()

To run the above node, navigate to the .py file and make it executable. The command is:

chmod u+x my_python_file.py

After the file is executable, you can run the node.

Step 1: open a new Terminal and run the command:

roscore

Step 2: open a new Terminal and run the Publisher node with the following command:

rosrun your_package your_ros_node_that_generates_random_number.py

Step 3: open a new Terminal and run the subscriber node with the following command:

rosrun your_package your_ros_node_for_subscriber.py

Template for a ROS Publisher in Python

I do not like to memorize definitions, methods, procedures, etc., but I like to find solutions to help me reach my final goal.

In ROS, the structure of a simple node is repeated almost every time. To write the Publisher nodes as easily as possible, I’ve made a template that you can modify as you needed.

The template below represents a ROS Publisher node written in Python. All you have to do is to copy the template into a ‘. py’ file, delete the extra text and replace the text in capital letters.

Template for a ROS Publisher in Python

Template for a ROS Publisher in Python (image source)

#The ROS Node Publisher template

#!usr/bin/env python

#remove or add the library/libraries for ROS
import rospy, time, math, random

#remove or add the message type
from std_msgs.msg import Int32, Float32, String
from basics.msg import TimerAction, TimerGoal, TimeResult
from time import sleep

#you can define functions to provide the required functionality
def body(arg):
    def_body_here
    return the_value

if __name__=='__main__':
    #add here the node name. In ROS, nodes are unique named.
    rospy.init_node('THE_NAME_OF_THE_NODE')

    #publish messages to a topic using rospy.Publisher class
    name_pub=rospy.Publisher('THE_NAME_OF_THE_TOPIC', THE_TYPE_OF_THE_MESSAGE, queue_size=1)

    #you can define functions to provide the required functionality
    if var1==var2:
        make_something()
        else:
            make_something()

    #set a publishing rate. Below is a publishing rate of 10 times/second
    rate=rospy.Rate(10)

    while not rospy.is_shutdown():
        #some calculation here
        if a1==a2:
            pub.publish(message1)
            else:
                pub.publish(message2)
rate.sleep()

I used the above template to make a ROS node that generates a random number between 0 and 5000.

#!/usr/bin/env python
import rospy

from std_msgs.msg import Int32
from random import randint

#a function to generate the random number
def generate_random_number():
    rnd= randint(0,5000)
    return rnd

if __name__=='__main__':
    rospy.init_node('random_number')
    pub=rospy.Publisher('rand_no', Int32, queue_size=10)
    rate= rospy.Rate(5)

    while not rospy.is_shutdown():
        rnd_gen=generate_random_number()
       
        if rnd_gen<=2500:
         rospy.loginfo("The number generated is lower than 2500: %s", rnd_gen)
         pub.publish(rnd_gen)
        else:
         rospy.loginfo("The number generated is higher than 2500: %s", rnd_gen)
         pub.publish(rnd_gen)

rate.sleep()

To run the above node, navigate to the .py file and make it executable. The command is:

chmod u+x my_python_file.py

After the file is executable, you can run the node.

Step 1: open a new Terminal and run the command:

roscore

Step 2: open a new Terminal and run the node with the following command:

rosrun your_package your_ros_node_file.py

Installing and Setting Up Arduino with ROS Kinetic (Raspberry Pi 3)

This guide will walk you through how to install and setting up an Arduino board to work with Raspberry Pi 3 having in common ROS Kinetic.

To walk through this guide, you must have a Raspberry Pi 3 with ROS Kinetic installed, an Arduino UNO board connected via the USB port to Pi, and some Linux knowledge.

Arduino is an open-source development tool very easy to use both as hardware and software. This development board simplifies the robot construction process and is therefore used together with Raspberry Pi and ROS to control sensors, motors or any other component that can be controlled with a microcontroller.

The Arduino microcontroller can only run one ROS node at a time.

Install the Arduino IDE

Arduino is connected to Raspberry Pi 3 through the USB port. To program Arduino, you need to install the Arduino IDE on the Pi.

To install the Arduino IDE on the Ubuntu Mate operating system, use the following commands in the Linux terminal.

sudo apt-get update
sudo apt-get install arduino arduino-core

Read more →

A Must-See List of TensorFlow Examples

This article was last modified on 24 July 2017.

TensorFlow can become for machine learning what is OpenCV for computer vision and ROS as a robot operating system. Machine learning is getting more and more attractive to the public, and impressive results are not to be expected.

TensorFlow is an open-source library developed by Google and launched in 2015 for the general public. Ever since it was launched to be used by anyone who wants to work with artificial intelligence, the library broke all the records in terms of projects on GitHub.

Returning to Google products, TensorFlow has a special place in the company’s projects, including Google Search, YouTube, Google Translate, Gmail, and more. Since the library is a very important resource of intelligence, in this article I made a list of examples using TensorFlow to do robotics applications. Furthermore, some of these examples are made by amateurs in the field of artificial intelligence.

  • Self-driving RC car using Raspberry Pi 3 and Tensorflow (added on 24.07.2017)
    DeepTesla is a deep neural network designed to drive a car. The neural network is used in this project to recognize a path delimited by line.

    The robot uses a simple webcam to capture images. The images are processed in real-time on a Raspberry Pi 3 at a rate of 30-50ms / frame.

    The artificial intelligence module has been trained on a computer with a GTX 1060 graphics processor.

  • Read more →

ROS Kinetic: Publisher and Subscriber in Python

The first time I started reading about ROS was last year. Also at that time, I made the decision to learn as much about ROS and make as many projects as possible based on it. After reading the first tutorials, I was sure it would be painful until I’ll build the first robot based on ROS. And I was right.

The first step in ROS begins with this tutorial. Or better, everything starts with two nodes, the so-called basic elements. A node to publish data, and a node to read and display the data received. As an autodidact, I know how hard it is to learn things that are not explained in detail. In this tutorial, I put a lot of attention on resources and the steps needed to write and run two nodes: a Publisher node and a Subscriber node.

Note: I have written with capital letters the Publisher and Subscriber to highlight the two ROS concepts.

What you find in this tutorial
  1. Resources needed
  2. A short description of the Publisher and Subscriber nodes
  3. A schema of the nodes
  4. Creating the workspace and the package that will contain the nodes
  5. The Publisher
  6. The Subscriber
  7. Run nodes with an automatic launch file
  8. Helpful commands
  9. Statistics
  10. Conclusion

1. Resources needed

To write the two ROS nodes you need hardware and software resources. Below I made a list of mandatory resources, and in the end, I added an optional resource that can be helpful in developing complex projects.
Read more →

The Best Raspberry Pi 3 Kits for Robotics

Updated on 22.05.2017
Using a robotic kit has the great advantage of having together all of the components you need to build a robot. Moreover, some kits allow the addition of new components or sensors, so using a single platform, you can build different robots.

Whether we are talking about remote controlled robots or autonomous robots, we need to connect to any of them. Raspberry Pi 3 brought a great advantage in the construction of robots – wireless connectivity. Controlling or programming the robot using the Internet or Bluetooth connectivity has become effortless.

In addition to wireless connectivity, let’s not forget that Pi 3 is a computer capable of running Linux distributions, algorithms and a set of useful frameworks such as ROS and OpenCV.

Below are the best kits compatible with Raspberry Pi 3. In addition, these kits can be used for a wide range of applications. From robots capable of detecting and avoiding obstacles using ultrasonic sensors or a webcam, to robot arms that can be programmed to grasp and move objects of different sizes. In addition, all of these kits can be controlled via the Internet or Bluetooth from a smartphone, tablet, or computer. Read more →

How To Build a Remote Controlled Robot From Scratch

The nice part when I build things in robotics is that I can reuse the components from one project to another. Several boxes full of sensors, motor drivers, and a wide range of kits. What is missing here is just an idea and some time to put it together. So, I decided to build something new, something that I have never built before.

I chose to use a remote control with a receiver, a mobile platform, and one of the powerful motor drivers on the market, and at the same time, the best of my collection. The result is a remote controlled robot.

Such a project requires basic knowledge in electronics (something about voltage, ampere, how to use power wires, soldering, etc.). Moreover, this is a simple project that can be finished in a few hours.

In this article, I described the components used, how to assemble the components, and finally, you’ll see how I tested the robot in my own kitchen.

The hardware components used in the project

  • The remote control is a Flysky FS-T6-RB6 2.4GHz FS

    I found the 2.4GHz remote control and the receiver at a good price on eBay, somewhere early this year.

  • The receiver with six radio channels
    it came bundled with the remote control.
  • Read more →

How To install ROS Kinetic on Raspberry Pi 3 (Ubuntu Mate)

The ROS framework is compatible with a short list of Linux distributions. Neither the hardware side is not better. There are just few hardware architectures compatible with ROS. Raspberry Pi is one of the development boards compatible in terms of hardware with ROS.

So, I thought to install ROS Kinetic on the Raspberry Pi 3 running Ubuntu Mate. But only a certain version of Ubuntu Mate is compatible with ROS and Raspberry Pi 3, it is about the Ubuntu MATE for Raspberry Pi 3. This is an OS version released last year and include support for the WiFi and Bluetooth modules integrated into the Pi 3.

The OS version used by me on Raspberry Pi 3 is Ubuntu MATE 16.04.2.

The ROS version that I have installed is Kinetic Kame. Kinetic was released early last year and is compatible with Ubuntu Mate 16.04. I chose this version for two reasons:

  1. it will be officially supported for the next five years;
  2. it is the most complete version after Indigo;

The first step in installing ROS on Raspberry Pi 3 is called Mate. Ubuntu Mate. The operating system is simple to install. I followed the steps on the download page, and within minutes I managed to have a Pi 3 running Ubuntu Mate.
Read more →

How To Install ros-indigo-desktop-full on Linux Ubuntu

One month ago, I wrote a post with all the reasons that should make you use ROS to design and build robots. In this tutorial, I will show you the end-to-end process to have a fresh ROS environment on a Linux Ubuntu machine.

I know you can read this guide and get all the information to install ROS Indigo on a Linux machine without any problem. But this guide may be incomplete for someone who wants to install ROS on a virtual machine and setting up the creation of an ROS workspace.

The tutorial has four parts:

  1. In the first part, I’ll show you how to install and setup Linux Ubuntu under Oracle VM VirtualBox. This part is very useful if you’re using a Windows PC or Mac OS. If you already use a Linux Ubuntu machine, then you can jump directly to the second part of the tutorial.
  2. In the second part of the tutorial, I’ll show you how to install ros-indigo-desktop-full on Linux Ubuntu.
  3. In the third part, you’ll see how to install external packages needed to start working with ROS.
  4. In the last part, I’ll show you how to check your ROS environment.

Read more →

Why and How To Use Switching Voltage Regulators To Maintain a Constant Voltage Level

The sensor inputs are as important as the outputs. So, keeping the input voltage of the sensor at a constant level with a step-up/step-down voltage regulator is mandatory to have accurate measurements.

In another tutorial, I have shown you how to make accurate ADC readings with Arduino. In this post, I continue the series of articles about how to make accurate readings with sensors. Using a switching voltage regulator to keep a constant voltage level at the input of sensors is important for both digital and analog sensors.

A sensor is designed to return accurate measurements at a certain value of the voltage. Most of the sensors used in robotics are in the range of 3.3V and 5V. Of course, some sensors are exceptions. For example, the LIDAR-Lite v3 is designed to work with a power supply between 4.75 and 6V maximum.

For example, if the sensor requires a 5V constant power supply, it will return the most accurate readings at this value. So, we need to make all we can do to have a constant voltage at the input of the sensor. The power supply voltage value is indicated in the datasheet of every sensor.

HC-SR04 and a step-up/step-down voltage regulators

A voltage regulator is designed to provide a fixed voltage level regardless of its input.

There are several types of voltage regulators, but in this article, I will focus on the switching regulators.
Read more →