Skip to the content.

Getting-fast-into-ROS

In robot programming, implementing planning processes that is customized into the specific application is very essential. In this regard, ROS has a big advantage to control robot with user defined process plans. However, it is not easy to find a precise tutorial that helps to devive into ROS in short time.

Let us consider a turtle sim, 2D robot and dive fast into ROS. Assuming you have a complete installation of ROS packages,and turtle sim;

Content

Explained tutorial using Command terminal with Turtle simulation

This tutorial helps to get familiar with ROS environment using Turtle sim using command line.

$ rostopic echo /turtle1/cmd_vel

Publishers: None

Subscribers:

Explained tutorial with Python script

Subscriber function

Save the Subscriber function as “subscriber_tutorial.py” in your src folder in catkin_ws.

import sys
import rospy
from geometry_msgs.msg import Twist
import datetime

def callback_pose(data):
    """Get the ROS message"""   
    print(data)


def listener():
   """Subscribe to ROS topic node"""

    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("/turtle1/cmd_vel", Twist, callback_pose)
    rospy.spin()

if __name__ == '__main__':
    listener()

Publisher function

Save the publisher function as “publisher_tutorial.py” in your src folder in catkin_ws.

import rospy
from geometry_msgs.msg import Twist
import sys

def moveFunction_turtle(linear_velocity, angular_velocity):
    """Move turtle robot with input velocity"""
    rospy.init_node('moveFunction_turtle', anonymous=True)
    pub = rospy.Publisher("/turtle1/cmd_vel", Twist, queue_size=10)
    rate = rospy.Rate(10) # 10hz
    velocity = Twist()
    while not rospy.is_shutdown():
	velocity.linear.x = linear_velocity
	velocity.angular.z = angular_velocity

        rospy.loginfo("Linear velocity = %f: Angular velocity = %f: ", linear_velocity, angular_velocity)
        pub.publish(velocity)
        rate.sleep()


if __name__ == '__main__':
    try:
        moveFunction_turtle(float(sys.argv[1]), float(sys.argv[2]))
    except rospy.ROSInterruptException:
        pass

How to run the script

Open new terminal and change the directory to the publisher and subscriber function containing folder and run the functions separately as follows;

python publisher_tutorial.py 2.0 4.0

Or

python subscriber_tutorial.py

Congratulations, now you are able to subscribe and publish to rostopic node!!