Ball Following Bot 2k23

 Status : Completed

Tags: opencv python arduino raspberrypi image_processing pyserial



AIM

To track and follow a ball in real-time using a bot equipped with Raspberry Pi


COMPONENTS AND TECHNOLOGIES USED

  • raspberrypi

  • opencv

  • python

  • pyserial


OVERVIEW

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 

  • Raspberry Pi:-

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.

  • Raspberry Pi Camera Module: 

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.

  • Dual H bridge motor driver(L298N): 

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 motor: 

DC motors are electric motors that convert direct current electrical energy into mechanical energy. Here it is used to give rotation to the wheel.

  • Arduino UNO: 

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:

  1. Capturing frames: The Raspberry Pi captures frames using a camera module connected to it.
  2. Image processing: The captured frames are then processed using OpenCV to identify the ball. The image processing algorithm searches for the ball using color filtering and edge detection techniques. The goal is to isolate the ball from the background.
  3. Contour detection: Once the ball is isolated, the contours of the ball are detected using OpenCV. The contours represent the boundaries of the ball, and their positions are used to track the ball's movement.
  4. Computing ball position: The center of the ball is computed from the detected contour, and the x and y coordinates of the ball are determined. The distance of the ball from the bot can also be computed based on the size of the ball in the image.
  5. Sending instructions to Arduino: The Raspberry Pi sends the x, y coordinates, and distance of the ball to the Arduino. The Arduino receives the instructions and uses them to control the movement of the bot. The movement of the bot is adjusted based on the position of the ball to keep it in the center of the camera's view.
  6. Movement of the bot: Based on the instructions received from the Raspberry Pi, the Arduino controls the movement of the bot. The bot's motors are adjusted to move it in the direction of the ball while keeping it in the center of the camera's view.
  7. Looping: The above steps are repeated in a loop to continuously track the ball and adjust the movement of the bot accordingly. The loop continues until the bot reaches a stopping condition, such as reaching a specific location or the ball is out of view. 

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 moveIf the ball is not in the field of view then the bot rotates until it finds the ball.

Problems Faced

  1. Speed Control:  Digital pins cannot precisely control motor speed, so analogWrite() function and PWM-enabled pins are used to achieve accurate speed control.
  2. Frame Rate:A low frame rate can cause the camera to capture fewer images of the ball, making it difficult to accurately track its movements., while a high frame rate can overload the system. To avoid this, reducing the camera's resolution and field of view can decrease data capture and processing requirements, leading to a higher frame rate without system overload.
  3.    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