Hands-On Robotics
Programming
with C++
Leverage Raspberry
Pi
3
and C++
libraries
to
build
intelligent
robotics
applications
Dinesh Tavasalkar
Table of Contents
Preface i
Section
1:Getting
Started withwiringPi
on aRaspberry
PiChapter
1: Introduction to theRaspberry
Pi gTechnical
requirements
9Software
required
10Hardware requirements 10
ForRaspberry Pi 3B+ and Raspberry Pi Zero W 11
Additional hardware forRaspberry Pi 3B+ 11
Additional hardware requirementsforRaspberry Pi Zero W 11
Understanding
theRaspberry
Pi nThe RaspberryPi 3B+ 12
The RaspberryPi Zero W 13
Setting
up aRaspberry
Pi 3B+ as adesktop computer
15Installing
Raspbian OSon an SD card 15Downloading and installingEtcher 16
DownloadingtheRaspbian Stretch image 16
WritingtheRaspbian Stretch imagetoamicroSDcard 17
Setting up the Raspberry Pi 3B+ 19
ConnectingtheRaspberry Pi3B+to the internet 22
Connecting
theRaspberry
Pi 3B+ to alaptop
via Wi-Fi 23Creating
anSSH fileon a microSD card 24Connecting the Raspberry Pi 3B+ toa Wi-Fi network
using
PuTTY 28Enablingthe VNCserver 34
Viewingthe Raspberry Pi outputonthe VNC Viewer 37
Increase the VNC'sscreen resolution 39
HandlingVNC and PuTTYerrors 40
Setting
up theRaspberry
PiZero W as adesktop
computer
41Setting
up the Raspberry Pi Zero W 43Connecting
theRaspberry
Pi Zero W to alaptop
via Wi-Fi 44Connecting the Raspberry Pi Zero W toaWi-Fi network using PuTTY 48
EnablingVNC Viewer forRaspberryPi Zero W 48
Viewing
Raspberry
Pi Zero Woutputon VNC Viewer 48Summary
48Questions 49
Chapter
2:Implementing
Blink withwiringPi
51Technical
requirements
51TableofContents
Accessing Raspberry
Pi GPIOpins
via wiringPi 57Making
an LED blink 59Wiring connections 59
The blinking program 60
Uploadingthe code to the RaspberryPi 62
Smart
light
-working
withdigital
sensor 63 The LDRsensorand the way it works 63Wiring connection 65
Smartlightprogram 65
Pulse Width Modulation
using
softPWM 67How PWM works 67
The softPWM
library
69Makingan LED blink with the softPWM library 70
Summary
72Questions 72
Section 2:
Raspberry
Pi Robotics
Chapter
3:Programming
the Robot 75Technical
requirements
75Choosing
a robot chassis 76Constructing
andconnecting
the robot 77Constructing
the robot 77Connecting
the motordrivertotheRaspberry
Pi 78Whatis amotordriver? 78
Wiring connections 79
Working
withH-bridge
80Moving
the robot 83Moving
the robot backward 85Stopping
the robot 86Different types of turns 86
Axial turns 86
Axial left turn 87
Axialrightturn 87
Radialturn 88
Radial left turn 88
Radialrightturn 89
Summary
89Questions 90
Chapter
4:Building
anObstacle-Avoiding
Robot 91Technical
requirements
91Using
an ultrasonicsensor 92Howan ultrasonicsensor measures distances 93
The arithmetic equation fordetermining the timetaken 94
Wiring the ultrasonicsensorto the Raspberry Pi 95
Using
an LCD ggWiring
the 16x2 LCD to the Raspberry Pi 102Programming the LCD 104
The LCDprogram 104
The LCD and the ultrasonicsensorprogram 106
What is the I2C protocol? 107
Wiringthe I2C LCD and the Raspberry Pi 109
Programmingthe LCD with the I2C LCD module log
The I2C LCD and the ultrasonicsensorprogram 114
Building
anobstacle-avoiding
robot 115Wiring connections 116
Programming theobstacle-avoiding robot 116
Summary
118Questions 118
Chapter
5:Controlling
a RobotUsing
aLaptop
119Technical
requirements
119Installing
the ncurseslibrary
120ncursesfunctions 120
Writinga HelloWorld program with ncurses 121
Compilingand runningthe program 123
Controlling
LEDs and a buzzerusing
ncurses 125Wiring connections 125
Writingthe LEDBuzzer.cpp program 126
Controlling
a roverusing
alaptop keyboard
129Building a laptop-controlled roverprogram 130
Tracing
asquarepath 132Installing
andsetting
up QT5 133Setting
up QT5 135Controlling
LEDs with GUI buttons 140Creating a QTproject 141
Dealing with errors 14g
Controlling
a roverusing
alaptop
with QT5 150Summary
153Questions 153
Section
3: Face andObject Recognition
RobotChapter
6:Accessing
the RPi Camera withOpenCV
157Technical
requirements
157Installing OpenCV
4.0.0 onRaspberry
Pi 158Uninstalling
Wolfram and LibreOffice 158Updating
your RPi isgInstalling
the cmake, image, video, andgtk packages isgDownloading
and unzipping OpenCV4.0 and its contribution repository 160TableofContents
Compiling
and installing OpenCV 161Linking
OpenCV
toPython 163Enabling
andconnecting
the RPi camera to RPi 166Connectingthe RPicamerato RPi 168
Mounting
the RPicamera onthe robot 170Capturing
images
and video with the RPi camera 172Recording
avideo with the RPi camera 174Installing the v4I2 driver 175
Reading
animage using OpenCV
176Summary
178Questions 179
Chapter
7:Building
anObject-Following
RobotwithOpenCV
181Technical
requirements
181Image processing
withOpenCV
181Importantfunctions in
OpenCV
182Object
recognition usingOpenCV
190Capturingtheimage 190
Findingthe RGB pixelvalues 191
Theobjectdetection program 195
The OpenCVcamera feed program 199
Building
anobject-following
robot 201Ball
tracing using
moments 201Programming logic 201
The ball tracing program 202
Setting up the object-following robot 205
Object-followingrobotprogram 205
Summary
207Questions 208
Chapter
8: Face Detection andTracking Using
the Haar Classifier 209Technical
requirements
209Face detection
using
the Haar cascade 210Basicworking of the Viola-Jones
algorithm
210Face-detection program 212
Detecting
the eyes and smile 217Detectingthe eyes 217
Eye detection using haarcascade_eye 218
Eye detection using haarcascade_lefteye_2splits
andhaarcascade_righteye_2splits 219
Detectingtheleft eye 219
Detectingtherighteye 221
Recognizing a smile 223
Programming logicfor smilerecognition 223
Face-tracking
robot 224Wiring connections 225
Usingthe white doton the facetriangletomovethe robot 231
Summary
233Questions 234
Section 4:
Smartphone-Controlled
RobotChapter
9:Building
a Voice-Controlled Robot 237Technical
requirements
238An introduction to
App
Inventor 238Creating
aTalking
Pi Android app 238Designing the app 242
Programming the blocks 248
Main block 249
Intermediate block 249
Final block 250
TheTalking Pi program 251
Importing and exporting the .aia file of the app 255
Creating
a voice-controlled bot app 256Designing
the app 257Adding and customizingthecomponents 258
Programming
the voice-controlled botblock 265Pairing
the Androidsmartphone
and RPi via Bluetooth 270Enabling the Bluetooth serial port 276
Developing
the Bluetooth program for RPi 277Socket
programming
277VoiceBotserverprogram 280
Testingthe code 282
Summary
285Questions 285
Assessments 287
Other Books You May Enjoy 291