TurtleSim Playground
Series of projects explaining robotics concepts
Hello there!!!
This is the first article in the series of articles based on small robotics projects.
Robotics is a vast field that consists of various domains such as perception, localization, navigation, and control of a robot. Each of these domains has a lot of concepts associated with it. For the successful functioning of a mobile robot, all these domains need to work in unison.
Robot Operating System (ROS) is a framework that is used to implement all these domains on a robot. In this series of articles, I have tried to explain the concepts associated with the above-mentioned domains using ROS and turtlesim.
The following is the content of this article:
- Target Audience
- Series Introduction
- Project1: Turtlesim-Sketch
- Details about the sequential approach
- Details about the parallel approach - Future Work
- GitHub Repository
Target Audience
The target audience for this series is — individuals who are familiar with Ubuntu, ROS and have a prior basic understanding of robotics.
The series is not intended to teach ROS but showcase the application of ROS concepts(like nodes, topics, custom messages, services, actions, etc.) in action to explain robotics concepts such as image processing, transformations, PID controller and many more.
Series Introduction
The series consists of multiple projects. Each project has a particular goal to be accomplished using concepts across multiple domains. These concepts will be demonstrated in action using a 2D simulator called TurtleSim. There are numerous simulators out there such as Gazebo, Ignition, Carla, etc. which can be used to demonstrate the same robotics concepts but I will be using a turtlesim in this series. Two main reasons to choose turtlesim over other simulators are:
1. The ease of implementing the concepts in turtlesim compared to other
2. The primary focus is on robotics concepts and not on the tools used to explain these concepts
In this series, I will be using a turtlesim package which has support across various distributions of ROS(Robot Operating System).
What is a turtlesim?
Turtlesim
- It is a 2D simulator with 500 x 500 pixels window
- It has a turtle and a configurable blue coloured background
- The turtle’s motion can be controlled using linear and angular velocities
- The turtle can be controlled using ROS services and messages
- At an initial look, the simulator looks like a very basic application which cannot be used for the complex robotics concepts
- But in this series of articles, the following concepts will be demonstrated in different projects:
- ROS Concepts
- Publisher Subscriber,
- Custom messages,
- Custom ROS services,
- ROS actions,
- ROS parameters,
- Dynamic reconfigure,
- ROS Gazebo plugins,
- ROS rqt plugins,
- ROS nodeltes,
- Coordinate transformation using `tf`, etc. - Robotic Vision
- OpenCV,
- Basic image processing,
- Deep Learning - Control Systems
- Modelling,
- Control algorithms — PID, MPC, LQR - Reinforcement learning to play games created using turtlesim
- Swarm robotics — basic concepts implementation with multiple robots using turtlesim
In short, I will try to explain the concepts in action using the turtlesim. These articles will serve as an entry point for the various important concepts used in the Robotics Industry.
Project1: Turtlesim-Sketch
This is a first project in the series. Here, I have sketched contours of an image using turtlesim.
The above image shows the original image on the left, Canny edges in the centre and the contours drawn using turtlesim. In this project, OpenCV is used to import and find the edges in an image. Using ROS and turtlesim, the contours are extracted from the image and drawn on the simulator.
The stepwise procedure is mentioned below:
Step1: Import an image
- To import an image and perform image processing operations, OpenCV library is used
# Code to import an image
img = cv2.imread(IMAGE_PATH)
- To find the contours and sketch them on the turtlesim it is important to have an image as the same size as the turtlesim. Hence, it is resized to 500x500 pixels using OpenCV
# Resize the image
img = cv2.resize(img, (500, 500))
Step2: Find edges in the image
- OpenCV has a built-in function to find edges in an image using various types of edge detectors like Sobel edge detector, Laplacian edge detector, Canny edge detector etc.
- To detect the edges, the colour image is converted to grayscale
# Grayscale Conversion
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- The grayscale image contains values between 0–255 for each pixel. To get a binary image, thresholding is done in which the pixels with values below a certain threshold are ignored and only the pixels above a certain threshold are filtered out
- Canny edge is a special kind of edge detector in which two thresholds are used — minimum value and maximum value. To filter an edge, all the edges above maximum value are considered as edges while the one which is below minimum value are discarded. The edges between the minimum and maximum region are selected based on the connectivity of these edges
- As per the diagram, edge C is selected while B is discarded since C is connected to the edges above the maximum threshold
# Canny edge detection
edges = cv2.Canny(img, 100, 200)
- To select the minimum and maximum threshold values,
dynamic reconfigure
the package is used. The trackbars are used to adjust the minimum and maximum threshold values
Step3: Find the contours in the image
Using OpenCV built-in functions, we can find the contours from the edges in an image
# Contours
contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
Step4: Spawn a turtle at the first point of each contour
Using ROS services, we can spawn turtles at the first point of each contour
Step5: Sketch the contours
- The contours can be sketched using a sequential mode or a parallel mode
- In a sequential mode, one turtle will sketch at a time while in parallel mode all the turtles will sketch simultaneously
- To change the mode of sketching, change the argument value in
sketcher.launch
file.args="1"
selects the parallel mode of execution whileargs="0"
selects the sequential mode of execution
<!-- Draw Sketch of the selected contours -->
<node name="Sketch" pkg="sketch" type="sketcher.py" args="1" output="screen"></node>
- For the parallel execution, multi-processing module is used. A new process is spawned for each turtle
- The coordinate of the points are mapped from 500x500 to 11x11 using a linear transformation
- To sketch the contours, each turtle is teleported to the next point in the contour using ROS service associated with the turtlesim
Step6: Remove the turtles
- After the sketch is completed, all the turtles are removed from the screen using a ROS service associated in the turtlesim package
The GitHub repository link attached below contains the source code for the projects. The README.md
explains the procedure to set up and run the project on your local system. To select sequential or parallel execution, change the arg
tag in the sketch.launch
file.
Future work
Below are some features that will be added to this project:
- Create an rqt plugin
- Create an animation file
GitHub Repository
To know more about the updates regarding this project and the series of projects, please click on the watch
button on the GitHub