[ROS in 5 mins] 046 – What is ROS Namespace

[ROS in 5 mins] 046 - What is ROS Namespace

Written by Ruben Alves

08/10/2018

Hello Everyone

In today’s video we’re going to see what is ROS Namespace and how it works.

Step 0. Create a project in Robot Ignite Academy(RIA)

We have the best online ROS course available in RIA. It helps you learn ROS in the easiest way without setting up ROS environment locally. The only thing you need is a browser! Create an account here and start to browse the trial course for free now! We’ll use the ROS Basics in 5 Days course unit0 as an example today.

Step 1. Create a package for the code

At first, let’s create a ROS package with dependencies for our code and a source file called robot.py under the src folder in the pakcage with the following command

cd ~/catkin_ws/src
catkin_create_pkg tutorial rospy geometry_msgs
cd tutorial/src
touch robot.py

In robot.py, please create the following content

#! /usr/bin/env python
import rospy 
from geometry_msgs.msg import Pose

rospy.init_node('robot', anonymous=True)
pub = rospy.Publisher('my_position', Pose, queue_size=1)
rate = rospy.Rate(1) # 1hz

while not rospy.is_shutdown():
pub.publish(Pose())
rate.sleep()

You can then give the permission and run the code with the following commands.

chmod +x robot.py
source ~/catkin_ws/devel/setup.bash
rosrun tutorial robot.py

It will publish the robot pose in my_position topic which you can check with

rostopic list | grep my_pos

Let’s imagine you have two different robot and you want to reuse the code. How can you distinguish between this two topics?

The answer is that you can use the namespace. To specify the different namespace, you can run the following command

rosrun tutorial robot.py __ns:=robot1
rosrun tutorial robot.py __ns:=robot2

Now if you see into the topic list, you should see two different topics called

/robot1/my_position

and

/robot2/my_position

Want to learn more?

If you are interested in this kind of tutorial, I highly recommend you to take any of the following courses on Robot Ignite Academy:

ROS Basics In 5 Days (Python)
ROS Basics In 5 Days (C++)

We love feedback, so, whether you like the video or not, please share your thoughts on the comments section below.

Thanks for watching.

 

 

Edited by: Tony Huang

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This