Beginners Guide to Robotics With Python

Shiv Last Updated : 23 Mar, 2021
7 min read
This article was published as a part of the Data Science Blogathon.

Robotics With Python

Robotics- the integration of core disciplines to create spellbinding machines which may gradually learn how to mimic human ethics, behavior, and qualities. The field of Robotics amazes and intrigues people of all ages around the globe. This article picks up from where the previous article left off.

Robots can learn, navigate, and make decisions all by themselves. This article will provide you with further experience with Robotics With Python. It will explain more concepts that are directly related to Robots and the field of Robotics. I will also show and explain to you the Python code that I have coded (Source: My PC) for a Robot. Now, let us get straight into it!

Robotics python image

Source: CIO.com

Important Robot Components

A well-built robot will possess the ability to learn, navigate, and make decisions autonomously and it will effectively and efficiently make a rational and logical decision in the event of an unforeseen change in its immediate environment. The element that enables a robot to become a physical part
of its surroundings are the components that are located on-board; Specifically, the Sensors. Sensors are crucial in a robotic system as they reduce the
need for interaction, hence increasing the autonomous levels in a system. A list of sensors that are available on the market are as follows (it is good to
know that the list is not limited to these):

  • Light sensors.
  • Temperature sensors.
  • Pressure sensors.
  • Position sensors.
  • Hall sensors.
  • Flex sensors.
  • Sound sensors.
  • Ultrasonic sensors.
  • Touch sensors.
  • PIR sensors.
  • Tilt sensors.
  • Gas sensors.

These are some examples of sensors that can be purchased from electrical stores. If you look closely, you will notice that each one of these sensors can detect a specific stimulus in the immediate or distant environment. And you will realize, that if integrated into a single system, these components will allow any object to respond to any stimulus as a human being can.

The sensor module will detect an input from a source, and it will respond to it with a certain output. Overall, a sensor will convert any non-electrical physical or chemical quantity into electrical signals. The input of a sensor needs to be maintained for you to have a consistent output- For example, a motion sensor will not output a red light if there is no object moving in its vicinity. There are three specific sensors that we shall discuss: Light, Sound, and Ultrasonic.

 

Light Sensors

This sensor converts light frequency into electrical signals, i.e., the output of these sensor signals. The signal tells us the intensity of the light being detected. It works by measuring the electromagnetic waves that exist in all (infrared, visible, ultraviolet) frequencies of light.

 

Sound Sensors

This sensor detects and converts sound waves into electrical signals. The microphone’s diaphragm must be in functioning condition. This is because
sound waves that travel in the air medium, strike the diaphragm, causing it to vibrate and change the capacitor readings. The capacitor readings are converted into digital signals which may be used for processing purposes.

Ultrasonic Sensors:

This sensor will convert reflected sound waves into electrical sensors. The Ultrasonic sensors have the ability to measure the distance between objects. The ultrasonic sensor has a transmitter and a receiver that are in-built. The transmitter will send out a sound wave in the form of piezoelectric signals and the receiver will read the sound waves after the process of reflection has occurred.

These small, yet powerful devices are used in everyday life and they will grant you access to performing high-end computation on your computer when you bring these into the light of Robotics. For example, the automotive company that is known on a global scale- Tesla Incorporated. Tesla vehicles utilize numerous sensors that are mounted around the car.

The Tesla vehicle makes use of Ultrasonic sensors to assist with wheel alignment and centering on the road, along with LIDAR (Light Detection and Ranging) for object detection, sound sensors for detecting objects in the vehicle’s vicinity, cameras for computer vision, machine learning, and other advanced technology. All these sensors are responsible for creating the autonomous vehicle that we see on the roads.

Robotics python electronic sensors

Source: Electronics Hub

Motors

A common motor to use when you are experimenting with Robotics With Python is the DC Motor. DC stands for Direct Current. The DC Motors will most likely have wheels connected to the output end- i.e., the mechanical energy being outputted will be used to turn the wheels. A DC A motor is an electrical component that requires an input of electric current and provides an output of mechanical energy or motion. The components within the DC motor includes the coil winding, magnets, rotors, brushes, stator, and the source current.

The idea in which this works is that when the coil is powered, i.e., a current is passed through it- A magnetic field is generated around the coil, causing the coil to interact with the magnets, hence triggering the coil to rotate and ultimately outputting mechanical energy.

 

Power Switch

A robot will require a power switch to power it on and off. The power switch serves as a power breaker for the robot, allowing the current to stop flowing in the circuitry and allowing the components to cool down. When the switch is on current flows in the circuit and when off, the current flow is paused.

 

Push Button Switch

This is a simple device that can allow or prevent the control of a specific machine or process. A button switch is built out of electrical circuits which allow current to pass through it. Tampering with the flow of this current is what triggers events to take place in our circuit.

Button Switches may be used for normally-on or normally-off circuits. In a normally-on circuit, there is currently flowing through the circuit, and upon pressing the button current flow is interrupted. In a normally-off circuit, there is no connection or flow of electricity until the button is pressed.

 

Transistor

A transistor is a semiconductor that has two functions. It may either act as a switch, or as an amplifier to current. A transistor by nature has two leads (or legs- tiny metal rods for wires to be connected). As a switch, the transistor will reduce the current on one side of the leads and increase the current on the other side. As an amplifier, the transistor will accept current into one lead, and output it from the other at a higher current.

Resistor

This is one of the most popular and commonly used electronic components. It has two terminals (leads/legs) and integrating this in your electrical circuit will implement resistance in your circuit. The resistor can be used to reduce the flow of current, divide voltage, as well as adjust the levels of electrical signals.

 

Controlling The Robot Components with Python.

Controlling the Push Button Switch.

# we begin by importing the necessary package
import RPi.GPIO as GPIO

# we configure our system to temporarily ignore all warnings
GPIO.setwarnings(False)

# we set our system to use the physical pin numbering
# on the Raspberry Pi
GPIO.setmode(GPIO.BOARD)

# we configure The GPIO pin number 10 on our Raspberry Pi to be
# an input pin and we set the initial state of the pin to be
# pulled low "PUD_DOWN". This means that the button is in
# an "OFF" state
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# we set up a While loop that is going to run forever
# the While loop is going to constantly check the state of
# our Push Button Switch. In the event of our button
# being pressed, there are words that will be printed
# to the console
while True:

# we check if the GPIO pin number 10 is pushed
# we check the current running throught the button
# if the current is high
if GPIO.input(10) == GPIO.HIGH:

# we print the following words
print("Button was pushed!")

 

Controlling the DC Motor(s)

# we begin by importing the necessary packages
import time
from easygopigo3 import EasyGoPiGo3

# we create an instance of the Robot that we are using
# If you wish to indulge in the field of Robotics
# navigate to a web browser and search "What is GoPiGo3"
# it is interesting :) - you may be persuaded to purchase a unit for yourself
gpg = EasyGoPiGo3()

# we configure the motors to move forward for two seconds
gpg.forward()
time.sleep(2)

# we stop all motor movement for one second
gpg.stop()
time.sleep(1)

# we drive all motors 50cm forward
gpg.drive_cm(50, True)
time.sleep(1)

# we turn the motors to the left
gpg.left()
time.sleep(1)

# we turn the motors to the right
gpg.right()
time.sleep(1)

# we stop all robot activity
gpg.stop()

# we print a final message telling us if the script
# was successful
print("Movement Successful!")

 

The Ultrasonic Sensor

You may be wondering how the ultrasonic sensor is used for a Robot. Now, the Ultrasonic sensor is actually used to create a LIDAR-based Map of the area in which the robot resides in the present time. The Ultrasonic Sensor is used in combination with a Servo (Motor) which allows for the Ultrasonic Sensor to be swiveled, hence allowing the robot to “see” more of its environment. I will not be providing the entire code as it is lengthy and requires understanding- I shall provide a truncated picture of the code located on my PC.

To give a short explanation of this map functions is as follows; it will digitally allow our robot to see the environment in which it is. The ultrasonic sensor as we know has the ability to measure distance- and when combined with Cameras (computer vision) and the real-time Map that is generated, we can effectively identify objects and navigate around them.

At the very end of the code, there exists an “if- statement” which does the final check to see if an object is within the specified distance of the robot, e.g., 10cm.

I hope that this article has provided you with more insight into Robotics With Python. If you are an individual who likes tinkering with Python and Information Technology or Robotics in general, I recommend that you purchase a sensor or two and a “Breadboard”. These are components are purchasable for reasonable prices in the market, and will definitely keep you intrigued.

Thank you for your time.

The media shown in this article- Robotics With Python are not owned by Analytics Vidhya and is used at the Author’s discretion.

Responses From Readers

Clear

Clay Lanzino
Clay Lanzino

Very interesting. Thanks for sharing.

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details