Beginning Robotics with Raspberry Pi and Arduino: Using Python and OpenCV 2nd Edition Jeff Cicolani 2024 scribd download
Beginning Robotics with Raspberry Pi and Arduino: Using Python and OpenCV 2nd Edition Jeff Cicolani 2024 scribd download
com
https://textbookfull.com/product/beginning-robotics-with-
raspberry-pi-and-arduino-using-python-and-opencv-2nd-
edition-jeff-cicolani-2/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-2nd-edition-jeff-cicolani/
textboxfull.com
https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-2nd-edition-jeff-cicolani-2/
textboxfull.com
https://textbookfull.com/product/raspberry-pi-home-automation-with-
arduino-2nd-edition-andrew-k-dennis/
textboxfull.com
Hacking Electronics: Learning Electronics with Arduino and
Raspberry Pi, 2nd Edition Simon Monk
https://textbookfull.com/product/hacking-electronics-learning-
electronics-with-arduino-and-raspberry-pi-2nd-edition-simon-monk/
textboxfull.com
https://textbookfull.com/product/action-movement-light-and-sound-with-
arduino-and-raspberry-pi-first-edition-monk/
textboxfull.com
https://textbookfull.com/product/internet-of-things-with-raspberry-pi-
and-arduino-first-edition-edition-rajesh-singh/
textboxfull.com
TECHNOLOGY IN AC TION™
Beginning Robotics
with Raspberry Pi
and Arduino
Using Python and OpenCV
—
Second Edition
—
Jeff Cicolani
Beginning Robotics
with Raspberry Pi
and Arduino
Using Python and OpenCV
Second Edition
Jeff Cicolani
Beginning Robotics with Raspberry Pi and Arduino: Using Python
and OpenCV
Jeff Cicolani
Pflugerville, TX, USA
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
ix
Table of Contents
Index�������������������������������������������������������������������������������������������������349
x
About the Author
Jeff Cicolani currently lives in the Austin,
Texas, area with his wife, two dogs, and
dozen or so robots. He is currently working
as an embedded systems engineer, building
robotic and automated platforms for an AI
(artificial intelligence) company in Austin.
His journey to robotics was circuitous, taking
him through an odd career path that included
systems analysis and design and database
programming. In 2012, he joined The Robot
Group in Austin, where he joined a group of
robotics enthusiasts and began building robots as a hobby. In 2016, he
became president of The Robot Group. In this role, he leads the group in
their mission to promote STEM (science, technology, engineering, and
mathematics) education through robotics. He is currently working to
develop a better understanding of advanced robotics through ROS
(Robot Operating System) and machine learning.
xi
About the Technical Reviewer
Massimo Nardone has more than 22 years
of experience in security, web/mobile
development, cloud, and IT architecture. His
true IT passions are security and Android.
He has been programming and teaching
how to program with Android, Perl, PHP, Java,
VB, Python, C/C++, and MySQL for more than
20 years.
He holds a Master of Science degree in
Computing Science from the University of
Salerno, Italy.
He has worked as a project manager, software engineer, research
engineer, chief security architect, information security manager, PCI/
SCADA auditor, and senior lead IT security/cloud/SCADA architect for
many years.
xiii
Introduction
Robotics does not have to be difficult. In this book, I introduce you to the
field of robotics. The journey will be challenging; it’s intended to be. But
by the end of the book, you will have hands-on exposure to many of the
fundamental—and not so fundamental—aspects of robotics. You will work
with hardware, assemble and solder a circuit board, write code in two
programming languages, install and configure a Linux environment, and
work with computer vision. Everything else you do with robots will be an
extension of the lessons learned in this book.
xv
Introduction
This book is also for the student who wants to take their robot-building
experience beyond bricks and puzzle-piece programming, someone who
wants to work with hardware and software that more closely resembles
what they might see in college or in the professional world.
No assumptions are made about experience or background in
technology. As you go through the chapters, you may find parts that you
are already familiar with and you can skip ahead. But if you are new to
these topics, I try to provide you with a quick but easy introduction.
C
hapter Overview
You start by learning about the Raspberry Pi and how to work with it.
You download and install the Raspbian operating system (OS) and then
configure the Pi for our project. The goal is to set up your system to be able
to easily access your robot and write your code directly on it.
Once you are able to access your Pi remotely, in Chapter 3, you delve
into programming with Python. I show you how to write simple programs
on the Raspberry Pi. I also take you beyond the basics and cover some
intermediate topics, such as modules and classes. This is one of the longest
chapters since there is a lot of material to cover.
From there, you learn how to interface the Raspberry Pi with external
electronics, such as sensors and LEDs, through the Pi’s GPIO (general-
purpose input/output) header. Chapter 4 discusses the different ways
of addressing the pins on the header, some of the functionality exposed
through the header, and how to use an ultrasonic rangefinder to detect
objects. This gets you ready for the next chapter, which introduces the
Arduino.
In Chapter 5, you connect the Arduino to the Raspberry Pi. I discuss
some of the reasons you want to do this. I show you how to work with the
Arduino IDE (integrated development environment) to write programs.
xvi
Introduction
I cover serial communication between the two boards and how to pass
information back and forth between them. We do this using the same
ultrasonic rangefinder used in the previous chapter.
Chapter 6 has you turning motors with your Raspberry Pi. You use a
special board called a hat, or plate, to control the motors. This is where I
introduce another skill that you will inevitably need in robotics: soldering.
The header and terminals need to be soldered onto the board that was
selected for this purpose. The nice thing about soldering headers and
terminal blocks is that it’s hard to damage anything, and you will get plenty
of practice.
Chapter 7 is where we bring it all together. You build the robot, and I
discuss some of the physical characteristics of robotics. I cover some of
the design considerations that you will need to keep in mind when you
design your own chassis. Although I am listing a specific chassis kit for this
project, you do not need to use the same one. In fact, I encourage you to
explore other options to find the one that is right for you.
In Chapter 8, I introduce another type of sensor—the IR sensor—and
I show you how to use a very common control algorithm called a PID
(proportional, integral, and derivative) controller. I talk about the various
types of IR sensors and where you want to use them. This chapter also
discusses what PID control is and why you want to use it.
Chapter 9 is about computer vision, where you see the true power of
the Raspberry Pi. In this chapter, I cover an open source package called
OpenCV. By the end of this chapter, your little robot will be chasing a ball
around the table.
I leave you with some parting thoughts in Chapter 10. I provide a few
tips that I picked up, and I give you a glimpse into my workflow and tools.
After that, you will be ready to begin your own adventures in robotics.
xvii
Introduction
xviii
CHAPTER 1
Introduction
to Robotics
The word robotics can mean a lot of things. For some people, it is anything
that moves by itself; kinetic art is robotics. To other people, robotics means
something that is mobile or something that can move itself from place to
place. There is actually a field called mobile robotics; automatic vacuum
cleaners, such as a Roomba or a Neato, fall into this category. To me,
robotics falls somewhere in between kinetic art and mobile robotics.
A robot is technology that applies logic to perform a task in an
automated manner. This is a fairly broad definition, but robotics is a fairly
broad field. It can cover everything from a child’s toy to the automatic
parallel parking capabilities in some automobiles. We build a small mobile
robot in this book.
Many of the principles that you are exposed to in this book are easily
transferable to other areas. In fact, we will go through the entire process
of building a robot from beginning to end. A little later in this chapter, I go
over the project that we will build. At that time, I will provide a list of the
parts used in this book. These parts include sensors, drivers, motors, and
so forth. You are welcome to use whatever you have on hand because, for
the most part, everything we go through in this book can be applied to
other projects.
Robotics Basics
I like to tell people who are new to robotics, or are just robotics curious,
that a robot consists of three elements:
2
Chapter 1 Introduction to Robotics
3
Chapter 1 Introduction to Robotics
S
ensors and GPIO
GPIO stands for general-purpose input/output. It represents all the various
connections to devices. The Raspberry Pi has a lot of GPIO options: HDMI,
USB, audio, and so forth. However, when I talk about GPIO in this book, I’m
generally referring to the 40-pin GPIO header. This header provides direct
access to most of the board’s functionality. I discuss this in Chapter 2.
Arduino also has GPIO. In fact, one could argue that Arduino is all
GPIO and nothing else. This isn’t far from the truth given that all the other
connections are there to allow you to communicate with and power the
AVR chip at the heart of the Arduino.
4
Chapter 1 Introduction to Robotics
All of these headers and GPIO connections are there so we can access
sensors outside the boards themselves. A sensor is a device that gathers
data. There are many different types of sensors, and all serve a purpose.
Sensors can be used for detecting light levels, the range to an object,
temperature, speed, and so forth. In particular, we will use GPIO headers
with an ultrasonic rangefinder and an IR detector.
M
otion and Control
One thing that most definitions of a robot have in common is that it needs
to be able to move. Sure, you can have a robot that doesn’t actually move,
but this type of device generally falls under the moniker of IoT, the Internet
of Things.
There are many ways to add motion to your project. The most common
is the use of motors. But you can also use solenoids, air, or water pressure.
I discuss motors more in Chapter 6.
Although it is possible to drive a motor directly off a Raspberry Pi
or an Arduino board, it is strongly discouraged. Motors tend to draw
more current than the processors on the boards can handle. Instead,
it is recommended that you use a motor controller. Like motors, motor
controllers come in many forms. The motor control board that we will use
is accessed through the Raspberry Pi’s header. I also discuss how to drive
motors with an L298N dual motor controller.
5
Chapter 1 Introduction to Robotics
6
Chapter 1 Introduction to Robotics
there are a few places where it does not excel. One area is interfacing with
external devices. It can work with sensors and external devices, but the
Arduino does this much better.
Arduino is another small processing device that is readily available and
easy to use. Unlike a Raspberry Pi, however, it does not have the capacity
for a full operating system. Rather than running a microprocessor like the
ARM, it uses a different type of chip called a microcontroller. The difference
is that a microcontroller is specifically designed to interact with sensors,
motors, lights, and all kinds of devices. It directly interacts with these
external devices. The Pi works through many layers of processing before it
ever reaches the pins that a device is connected to.
By combining the Raspberry Pi and the Arduino, we are able to
leverage what each does best. The Raspberry Pi offers the high-level
processing power of a full computer. Arduino provides the raw control over
external devices. The Pi allows us to process a video stream from a simple
USB camera, whereas the Arduino allows us to gather the information
from the various sensors and apply logic to make sense of all that data and
then return concise findings to the Pi.
You will learn more about the Raspberry Pi in Chapter 2. Later on, you
will connect an Arduino to the Pi and learn about programming it, as well
as how to pass information back and forth between the Arduino and the Pi.
P
roject Overview
In this book, we will build a small mobile robot. The robot is designed to
demonstrate the lessons that you learn in each chapter. However, before
we can actually build the robot, we need to cover a lot of material and lay
the foundation for future lessons.
7
Chapter 1 Introduction to Robotics
T he Robot
The robot that we will build is a small two- or four-wheeled autonomous
rover. It will be able to detect obstacles and the edge of a table and to
follow a line. The chassis that I selected is a four-wheeled robot, but there
are other designs suitable for this project (see Figures 1-3 and 1-4).
Figure 1-3. The front of our robot shows the ultrasonic sensors and Pi
T-Cobbler on a breadboard
Figure 1-4. The back of our robot shows the Raspberry Pi and motor
control board
8
Chapter 1 Introduction to Robotics
Although I provide a list of the parts that I used for the project, you are
welcome to use whatever parts you wish. The important thing is that they
behave in a similar manner as those I have listed.
9
Chapter 1 Introduction to Robotics
Figure 1-5. Runt Rover chassis parts and the Pi T-Cobbler, ribbon
cable, motor control hat, and extended header
The following parts (shown in Figure 1-6) are fairly generic and can be
purchased from most vendors:
• Half-size breadboard
• Ultrasonic sensors – HC-SR04
10
Chapter 1 Introduction to Robotics
You may want to get a few of these. As you will discover, ultrasonic
sensors are unreliable at angles, and it is good to have an array of them.
I use at least three on most of my projects.
Most computer monitors do not have HDMI ports on them. You can
get HDMI-to-DVI converters that allow you to use your existing monitor,
however.
• A USB keyboard and mouse (I like the Logitech K400
wireless keyboard and touchpad combination, but
there are countless options out there)
• A network-connected computer
11
Chapter 1 Introduction to Robotics
Figure 1-7. Jumpers in ribbon cable form. Pull off what you need
12
Chapter 1 Introduction to Robotics
You don’t need to get fancy with the monitor and keyboard. Once you
read Chapter 2, where we install and configure the Raspberry Pi, you no
longer need them. I have a couple of the wireless keyboards because I
usually have several projects going at once. For a monitor, I simply use one
of my computer monitors with an HDMI-to-DVI adapter.
If you are not using a chassis kit with motors and wheels included, you
also need the following parts (see Figure 1-8):
If you do not want to use the Adafruit DC & Stepper Motor HAT, you
can also use virtually any motor controller, although each one has a
different interface and code. A common and fairly popular option is the
L298N dual motor controller (see Figure 1-9).
13
Random documents with unrelated
content Scribd suggests to you:
up his pacifism—or, as Mr. Graves calls it with reforming zeal, his
“pacificism”—as a result of his generous sympathy with insurgent
Italians and Hungarians. That was the thin end of the wedge. Having
once drawn the sword, Punch found it even more enjoyable than
drawing cartoons. He drew it fiercely against the Russians in the
Crimean War. He drew it fiercely against the Indians in the Indian
Mutiny. He drew it on behalf of General Eyre after the negro
outbreak in Jamaica. He drew it against Lincoln in the American Civil
War. Mr. Graves ought, for historical reasons, to have reprinted
Punch’s parody on one of Lincoln’s speeches. He is content, however,
to describe it as “a truly lamentable performance, in which the
President claims dictatorial powers, calls for whipcord to whip the
rebels, abuses the ‘rotten old world,’ talks with the utmost cynicism
of the blacks, and in general behaves like a vulgar buffoon.” Mr.
Graves, with an impartiality which cannot be too highly praised,
reminds the Punch of those days that “the magnanimous Lincoln
would never allow” the Southerners to be called rebels in his
presence—a significant reminder when we recall how Mr. Lloyd
George drew on the Lincoln parallel in defending his treatment of the
Irish. But, for the ironist, the most amusing of all Punch’s blunders in
regard to foreign policy is the welcome he offered to the birth of the
German Navy in an article called “Bravo, Bismarck!” “Britannia
through her Punch,” he wrote, “rejoices to weave among her naval
azures a new shade—Prussian blue.” It is only fair to say that Punch
was not consistent in his attitude to Germany. But he has shown a
curious capacity for backing the wrong horse—the horse that
seemed to “get away” at the start, but that was ultimately
disqualified by the stern judge, history. He gave up championing lost
causes and took to championing causes that would be lost a
generation later.
In the result, Mr. Graves, though a wit of distinction, has
produced in Mr. Punch’s History of Modern England a book that is
pathetic rather than amusing. It is a cemetery of dead jokes—the
offspring of a little gentleman with a long nose who was cross more
often than he was funny. Punch, indeed, has been for the most part
a grinner rather than a wit. It has had, and still has, brilliant writers
on its staff. But its temper is not the temper of its most brilliant
contributors. Its attitude is that of the prosperous clubman who
dislikes the advance both of the new rich and of the old poor. It has
undoubtedly made itself the most successful comic paper in the
world, but one sometimes wonders whether it has done so as a
result of allying itself with comedy or of allying itself with success.
Yet the fact remains that other men have started rivals to Punch,
and that they have not only been not so successful as Punch but not
so comic. Punch always baits the hook of its odious politics with a
reasonable amount of comedy about things in general, and in the
comedy of things in general, even if we think it might be done still
better, it has at least always been ahead of its rivals. There have
been men who have dreamed of a Punch that would bring the spirit
of comedy to bear on all sides impartially. There are others who
have dreamed of bringing the spirit of comedy to bear on the right
side. One would not, perhaps, mind what side Punch was on if only
it were a little more generous—if only it purveyed the human
comedy as a comedy, and not, as in the case of working men,
Irishmen, and non-Allied foreigners, as a sinister crook melodrama.
X
MR. H. M. TOMLINSON
Mr. Tomlinson has in that last sentence captured the ultimate secret
of a wet day in an African village. Even those of us who have never
seen Africa save on the map, know that often there is nothing more
to be said. Mr. Tomlinson, however, is something of a specialist in
bad weather, as, perhaps, any man who loves the sea as he does
must be. The weather fills the world for the seaman with gods and
demons. The weather is at once the day’s adventure and the day’s
pageant. Mr. Conrad has written one of the greatest stories in the
world simply about the weather and the soul of man. He may be
said to be the first novelist writing in English to have kept his
weather-eye open. Mr. Tomlinson shares Mr. Conrad’s sensitive care
for these things. His description of a storm of rain bursting on the
African hills makes you see the things as you read. In its setting,
even an unadorned and simple sentence like——
That is the farthest Mr. Tomlinson ever gets on the way towards
arrogance. He ignores Rome and Athens. They are not among the
ports of call of his imagination. He prefers the world that sailors tell
about to the world that scholars talk about. He will not write about—
he will scarcely even interest himself in—any world but that which he
has known in the intimacy of his imaginative or physical experience.
Places that he has seen and thought of, ships, children, stars, books,
animals, soldiers, workers—of all these things he will tell you with a
tender realism, lucid and human because they are part of his life.
But the tradition that is not his own he throws aside as a burden. He
will carry no pack save of the things that have touched his heart and
his imagination.
I wish all his sketches had been as long as “The African Coast.”
It is so good that it makes one want to send him travelling from star
to star of all those names that mean more to him than Byzantium.
One desires even to keep him a prisoner for a longer period among
the lights of New York. He should have written about the blazing city
at length, as he has written about the ferries. His description of the
lighted ferries and the woman passenger who had forgotten Jimmy’s
boots, remains in the memory. Always in his sketches we find some
such significant “thing seen.” On the voyage home from New York on
a floating hotel it is the passing of a derelict sailing ship, “mastless
and awash,” that suddenly recreates for him the reality of the ocean.
After describing the assaults of the seas on the doomed hulk, he
goes on:
A Russian critic has said that Tchehov had nothing to give his fellows
but a philosophy of hopelessness. He committed the crime of
destroying men’s faith in God, morals, progress, and art. This is an
accusation that takes one’s breath away. If ever there was a writer
who had a genius for consolation—a genius for stretching out a hand
to his floundering fellow-mortals—it was Tchehov. He was as active
in service as a professional philanthropist. His faith in the decency of
men was as inextinguishable as his doubt. His tenderness was a
passion. He was open-hearted to all comers. He never shut his door
either on a poor man needing medicine, or on a young man needing
praise. He was equally generous as author, doctor and reformer. He
who has been represented as a disbeliever in anything was no
disbeliever even in contemporary men of genius. His attitude to
Tolstoy was not one of idolatry, but it came as near being idolatrous
as is possible for a clever man. “I am afraid of Tolstoy’s death,” he
wrote in 1900. “If he were to die there would be a big, empty place
in my life.... I have never loved any man as much as him. I am not a
believing man, but of beliefs I consider his the nearest and most
akin to me.” In his gloomier moods he thought little enough of the
work either of himself or his younger contemporaries. “We are stale,”
he wrote; “we can only beget gutta-percha boys.” But this was
because he was on his knees before everything that is greatest in
literature. In a letter to his friend, Suvorin, editor of the Novoe
Vremya, he wrote:
The writers, who we say are for all time or are simply
good, and who intoxicate us, have one common and very
important characteristic—they are going towards something
and summoning you towards it, too, and you feel, not with
your mind but with your whole being, that they have some
object, just like the ghost of Hamlet’s father, who did not
come and disturb the imagination for nothing. Some have
more immediate objects—the abolition of serfdom, the
liberation of their country, politics, beauty, or simply vodka,
like Denis Davgdov; others have remote objects—God, life
beyond the grave, the happiness of humanity, and so on. The
best of them are idealists, and paint life as it is, but, through
every life’s being soaked in the consciousness of an object,
you feel, besides life as it is, the life which ought to be, and
that captivates you.
Strindberg, writing on the same date and signing himself “The best,
the highest God,” began his letter to Nietzsche: “I will, I will be
raving mad,” and concluded it:
Mr. Strindberg:
Alas! ... no more! Let us divorce!
“The Crucified.”
Such is his creed, and in the result his laughter, though often
amusing, is never happy. There is the laughter of sympathy, which is
Shakespeare’s, and there is the laughter of antipathy, which is Mr.
Douglas’s. That is, perhaps, why his publishers say that his is “a
book for the fastidious in particular.” You could not say of
Shakespeare that he is “for the fastidious in particular.”
We must grant an author his point of view, however, and the fact
remains that, however we may differ from Mr. Douglas’s preaching,
we go on reading him with pleasure, protest and curiosity. He puts
his life into his sentences, and so he stamps with experience even
such a piece of topographical information as:
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com