 GeeksforGeeks App
Open App Browser
Continue

# Draw a circle using Turtlesim in ROS-Python

In this article, we are going to see how to draw a circle using Turtlesim in ROS-Python.

ROS stands for Robot Operating System. ROS is a set of libraries and tools that help build robot applications. It’s extensively used in robotics projects. ROS is an open-source, meta-operating system for robots. The software in the ROS ecosystem has both language-dependent and language-independent tools. ROS supports Python, C++, Lisp and other languages.

rospy is a pure Python client library ROS. We shall make use of this library to implement our code. Turtlesim is a common tool specifically made to teach ROS and ROS packages.

The idea is to import Twist from geometry_msgs.msg library and assign appropriate values for velocity components. Twist expresses the velocity of turtle in 3D space broken into 3 linear components and 3 angular components. Turtle here is 2D and is governed by 1 linear component (x-component) and 1 angular component (z-component). This is because the turtle cannot move in y or z directions. Hence. all other components are equated to 0.

## Stepwise implementation:

Step 1: First, import all the packages used in the program. rospy is a ROS-python library that contains different functions like creating a node, getting time, creating a publisher, etc.  The geometry_msgs library contains a useful variable type Twist which is used to describe velocity in 3D.

## Python3

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

Step 2: Next, we define our turtle_circle function, inside which we initiate our turtlesim node and our publisher. We also specify a rate equal to 10Hz, that is, the program goes through the loop 10 times per second. A Twist variable ‘vel’ is also created.

## Python3

 `def` `turtle_circle(radius):``    ``rospy.init_node(``'turtlesim'``, anonymous``=``True``)``    ``pub ``=` `rospy.Publisher(``'/turtle1/cmd_vel'``,``                          ``Twist, queue_size``=``10``)``    ``rate ``=` `rospy.Rate(``10``)``    ``vel ``=` `Twist()`

Step 3: We now create a while loop that allows the turtle to run in a circle indefinitely. Inside the while loop, we provide the velocity components of the turtle appropriately as discussed above in the approach, and then publish them to the turtle. We also print the radius for each loop using rospy.loginfo() function. rate.sleep() is added at the end. rate object keeps track of the time since the last rate.sleep() was executed and sleeps for the correct amount of time to maintain a 10Hz frequency.

## Python3

 `rospy.loginfo(``"Radius = %f"``, radius)``pub.publish(vel)``rate.sleep()`

Step 4: Finally, we have the main loop which calls the function and handles exceptions if any exist:

## Python3

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

## Steps for Execution the Turtlesim:

Start ROS in the terminal using the command:

`\$ roscore`

Start the turtlesim node on a new terminal using the command:

`\$ rosrun turtlesim turtlesim_node`

Execute the program using the following command:

`\$ rosrun my_package turtlesim.py 2.0`

Below is the implementation:

## Python3

 `#!/usr/bin/env python``# author : Sumanth Nethi``import` `rospy``from` `geometry_msgs.msg ``import` `Twist``import` `sys`  `def` `turtle_circle(radius):``    ``rospy.init_node(``'turtlesim'``, anonymous``=``True``)``    ``pub ``=` `rospy.Publisher(``'/turtle1/cmd_vel'``,``                          ``Twist, queue_size``=``10``)``    ``rate ``=` `rospy.Rate(``10``)``    ``vel ``=` `Twist()``    ``while` `not` `rospy.is_shutdown():``        ``vel.linear.x ``=` `radius``        ``vel.linear.y ``=` `0``        ``vel.linear.z ``=` `0``        ``vel.angular.x ``=` `0``        ``vel.angular.y ``=` `0``        ``vel.angular.z ``=` `1``        ``rospy.loginfo(``"Radius = %f"``,``                      ``radius)``        ``pub.publish(vel)``        ``rate.sleep()`  `if` `__name__ ``=``=` `'__main__'``:``    ``try``:``        ``turtle_circle(``float``(sys.argv[``1``]))``    ``except` `rospy.ROSInterruptException:``        ``pass`

Output: My Personal Notes arrow_drop_up