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.
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.
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.
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.
Step 4: Finally, we have the main loop which calls the function and handles exceptions if any exist:
Steps for Execution the Turtlesim:
Start ROS in the terminal using the command:
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:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course