Status : Completed
Tags: opencv python arduino raspberrypi image_processing pyserial
To track and follow a ball in real-time using a bot equipped with Raspberry Pi
raspberrypi
opencv
python
pyserial
Introduction
The project aims to develop a self-directed robot using Arduino and Raspberry Pi that tracks and follows a moving ball by detecting a specific color using a camera for image processing.
We used Raspberry Pi and Arduino for this project because they gave us great flexibility to use the Raspberry Pi camera module and code in Python. We used OpenCV, Python, and Numpy for image analysis in our ball-following project.
For controlling the motors, we used an H-Bridge to switch from clockwise to counter-clockwise or to stop the motors.
When the ball goes out of the camera scope, the ball takes a 360- degree until the ball comes back in the camera frame.
Components
It is a small single-board computer developed by the Raspberry Pi Foundation. The Raspberry Pi 4 uses a Broadcom BCM2711 SoC with a 1.5 GHz 64-bit quad-core ARM Cortex-A72 processor with a 1MB shared L2 cache.
The Raspberry Pi camera module is a small camera that can be attached to the Raspberry Pi board to capture high-quality still images and videos.
A dual H-Bridge motor driver allows speed and direction control of two DC motors simultaneously. The module can drive DC motors with voltages between 5 and 35V, with a peak current of up to 2A.
DC motors are electric motors that convert direct current electrical energy into mechanical energy. Here it is used to give rotation to the wheel.
It is an open-source microcontroller board based on the Microchip ATmega328P. The board is equipped with digital and analog input/output (I/O) pins that may be interfaced with various expansion boards (shields) and other circuits.
WORKING OF BOT
This project uses a wide range of functions for processing camera input and communicating with the Arduino to find and follow the ball.
We used the following modules.
1. Serial
It provides a way to communicate with devices connected to a serial port on a computer, such as USB-to-serial adapters. It allows you to configure the serial port settings, such as the baud rate, data bits, parity, and stop bits, and then send and receive data using the read() and write() methods.
2. PiRGBArray and PiCamera
PiRGBArray provides a way to store the image data captured by the camera in a format that is easy to work with in Python. And the PiCamera python module that provides a way to control and interface with the camera module on a Raspberry Pi.
3. OpenCV (Open Source Computer Vision)
It is an open-source computer vision and machine learning software library. It provides various functions and algorithms that can be used to perform tasks related to image and video processing.
4. NumPy (Numerical Python)
It is a Python library for scientific computing and data analysis. It provides powerful multi-dimensional arrays and matrices, as well as functions to manipulate and perform operations on them. NumPy is widely used in scientific computing, data analysis, and machine learning applications.
5. Virtual Environment(Venv)
We usually require different modules compared to the already installed versions of a certain software present on the system, therefore it's highly recommended to use a virtual environment for every project For our project the dependencies used in the virtual environment are available in the packages.txt.
The basis of implementation of the ball detection is differentiating the ball color from its background and finding its contour to make the bot follow the ball.
The working of a ball-following robot using OpenCV, Raspberry Pi, and Arduino can be summarized in the following steps:
Circuit Diagram
Role of Arduino-
Define the motor pins and the basic setup for the pins is done.
The loop function begins with a condition of Serial. Available() which returns true or false.
So the rest of the code runs if we receive a serial input from the raspberry pi.
We are reading the input from the serial that we are receiving in the form of a string
We are using necessary functions such as Serial.readStringUntil('\n') for getting the values encoded in the string and then atof(a.c_str()) for converting the string to a floating integer for the required values that is
1)Distance of the ball from the camera
2)X coordinate of the ball’s center in the frame
3) Y coordinate of the ball’s center in the frame
Then we are defining some functions for using in the logic is simple where the balls x coordinates are used to call the function according to where the center of the ball is.
Using this we can move the bot motors at the necessary speed and also calibrate the amount of left and right movement the bot needs to move. If the ball is not in the field of view then the bot rotates until it finds the ball.
Problems Faced
Environmental Issue: Change in lighting conditions can cause issues with the detection of the ball, as the camera or sensor may struggle to distinguish the ball from its surroundings.
Source Code
Github:https://github.com/roboclub-mnnit/Ball_following_bot-2022-23-Project
Youtube: https://www.youtube.com/watch?v=_hrhqHP5XtQ
Resources
Theoretical aspects of Arduino:
https://www.youtube.com/watch?v=zJ-LqeX_fLU
Arduino code and its interfaces:
https://m.youtube.com/playlist?list=PLGs0VKk2DiYw-L-RibttcvK-WBZm8WLEP
L298n Motor Driver Tutorial:
1. https://youtu.be/atuZCRCDrSY
2. https://www.youtube.com/watch?v=dyjo_ggEtVU
3. https://youtu.be/kv-9mxVaVzE
Opencv:
https://www.youtube.com/playlist?list=PLS1QulWo1RIa7D1O6skqDQ-JZ1GGHKK-K
Real-life Applications
1. Goods carrying and management:The bot can be upgraded to track and follow staff in industries that help and carry much heavier loads and dynamically follow any path followed by a staff which results in efficiency and effectiveness and is economical to the industries.
2. Research and development: Bot can aid research and development by studying the behavior of moving objects or developing new models for object tracking.
CONTRIBUTORS
Name |
Branch |
Reg. no. |
SR Sri Harsha Kedarisetty |
CSE |
20214527 |
Vikas Gaur |
ME |
20213038 |
Vishal Yadav |
ME |
20213044 |
MENTORS
Name |
Branch |
Reg. no. |
Kandukuri Yaswanth |
CE |
20201057 |
Anurag Gupta |
ECE |
20195168 |
Purushottam Kumar Agrawal |
EE |
20192042 |