29,99 €
C/C++ is one the legacy programming language for Robotics Programming. This book will help you understand and build complexly structured robots and implement various C/C++ programming libraries in it.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 233
Veröffentlichungsjahr: 2019
Copyright © 2019 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Vijin BorichaAcquisition Editor:Shrilekha InaniContent Development Editor:Abhijit SreedharanTechnical Editor:Mohd Riyan KhanCopy Editor:Safis EditingLanguage Support Editor: Mary McGowanProject Coordinator:Jagdish PrabhuProofreader: Safis EditingIndexer:Priyanka DhadkeGraphics:Tom ScariaProduction Coordinator: Jisha Chirayil
First published: March 2019
Production reference: 1290319
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78913-900-6
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Dinesh Tavasalkar is a trainer and online instructor from India. He has trained more than 8,000+ students on topics related to robotics, Internet of Things, Arduino, Raspberry Pi, Android app development, augmented reality, and virtual reality. Dinesh's online courses have been undertaken by 25,000+ people on Udemy from more than 150+ countries. Some of his popular courses on Udemy include Robotics for beginners, Smartphone Control Robot using Arduino and Android, Build Augmented Reality apps using Unity and Vuforia, and Build Virtual Reality games for Google Cardboard using Unity. He also runs a YouTube channel called EngineersDream, where he teaches Android application development.
Lentin Joseph is an author and robotics entrepreneur from India. He runs a robotics software company called Qbotics Labs in India. He has 8 years' experience in the robotics domain, primarily in ROS, OpenCV, and PCL. He has authored seven books on ROS, namely Learning Robotics Using Python, (First and Second Editions), Mastering ROS for Robotics Programming, (First and Second Editions), ROS Robotics Projects, Robot Operating System for Absolute Beginners, and ROS Programming. He did his master's in robotics in India and also did some research at Robotics Institute, CMU, USA.
Shahid Memon holds an MSc in autonomous robotics engineering and possesses a BSc in Computer Science. He has collaborated with colleagues on product feasibility studies and new product ideas to meet clients' needs and support companies' objectives. He has coordinated several product development projects and assisted in the design and testing phases. He is a strategic thinker with the ability to drive company goals and analyze research to impact product and business needs. He is an avid researcher of the latest trends within the technology industry and how these trends affects business. He is a proven leader with outstanding communication, interpersonal, project management, and supervisory skills.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Hands-On Robotics Programming with C++
Dedication
About Packt
Why subscribe?
Packt.com
Contributors
About the author
About the reviewers
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Section 1: Getting Started with wiringPi on a Raspberry Pi
Introduction to the Raspberry Pi
Technical requirements
Software required
Hardware requirements
For Raspberry Pi 3B+ and Raspberry Pi Zero W
Additional hardware for Raspberry Pi 3B+
Additional hardware requirements for Raspberry Pi Zero W
Understanding the Raspberry Pi
The Raspberry Pi 3B+
The Raspberry Pi Zero W
Setting up a Raspberry Pi 3B+ as a desktop computer
Installing Raspbian OS on an SD card
Downloading and installing Etcher
Downloading the Raspbian Stretch image
Writing the Raspbian Stretch image to a microSD card
Setting up the Raspberry Pi 3B+
Connecting the Raspberry Pi 3B+ to the internet
Connecting the Raspberry Pi 3B+ to a laptop via Wi-Fi
Creating an SSH file on a microSD card
Connecting the Raspberry Pi 3B+ to a Wi-Fi network using PuTTY
Enabling the VNC server
Viewing the Raspberry Pi output on the VNC Viewer
Increase the VNC's screen resolution
Handling VNC and PuTTY errors
Setting up the Raspberry Pi Zero W as a desktop computer
Setting up the Raspberry Pi Zero W
Connecting the Raspberry Pi Zero W to a laptop via Wi-Fi
Connecting the Raspberry Pi Zero W to a Wi-Fi network using PuTTY
Enabling VNC Viewer for Raspberry Pi Zero W
Viewing Raspberry Pi Zero W output on VNC Viewer
Summary
Questions
Implementing Blink with wiringPi
Technical requirements
Installing the wiringPi library in the Raspberry Pi
Accessing Raspberry Pi GPIO pins via wiringPi
Making an LED blink
Wiring connections
The blinking program
Uploading the code to the Raspberry Pi
Smart light – working with digital sensor
The LDR sensor and the way it works
Wiring connection
Smart light program
Pulse Width Modulation using softPWM
How PWM works
The softPWM library
Making an LED blink with the softPWM library
Summary
Questions
Section 2: Raspberry Pi Robotics
Programming the Robot
Technical requirements
Choosing a robot chassis
Constructing and connecting the robot
Constructing the robot
Connecting the motor driver to the Raspberry Pi
What is a motor driver?
Wiring connections
Working with H-bridge
Moving the robot
Moving the robot backward
Stopping the robot
Different types of turns
Axial turns
Axial left turn
Axial right turn
Radial turn
Radial left turn
Radial right turn
Summary
Questions
Building an Obstacle-Avoiding Robot
Technical requirements
Using an ultrasonic sensor
How an ultrasonic sensor measures distances
The arithmetic equation for determining the time taken
Wiring the ultrasonic sensor to the Raspberry Pi
The HC-SR04 sensor program
Using an LCD
Wiring the 16x2 LCD to the Raspberry Pi
Programming the LCD
The LCD program
The LCD and the ultrasonic sensor program
What is the I2C protocol?
Wiring the I2C LCD and the Raspberry Pi
Programming the LCD with the I2C LCD module
The I2C LCD and the ultrasonic sensor program
Building an obstacle-avoiding robot
Wiring connections
Programming the obstacle-avoiding robot
Summary
Questions
Controlling a Robot Using a Laptop
Technical requirements
Installing the ncurses library
ncurses functions
Writing a HelloWorld program with ncurses
Compiling and running the program
Controlling LEDs and a buzzer using ncurses
Wiring connections
Writing the LEDBuzzer.cpp program
Controlling a rover using a laptop keyboard
Building a laptop-controlled rover program
Tracing a square path
Installing and setting up QT5
Setting up QT5
Controlling LEDs with GUI buttons
Creating a QT project
Dealing with errors
Controlling a rover using a laptop with QT5
Summary
Questions
Section 3: Face and Object Recognition Robot
Accessing the RPi Camera with OpenCV
Technical requirements
Installing OpenCV 4.0.0 on Raspberry Pi
Uninstalling Wolfram and LibreOffice
Updating your RPi
Installing the cmake, image, video, and gtk packages
Downloading and unzipping OpenCV 4.0 and its contribution repository
Installing Python
Compiling and installing OpenCV
Linking OpenCV to Python
Enabling and connecting the RPi camera to RPi
Connecting the RPi camera to RPi
Mounting the RPi camera on the robot
Capturing images and video with the RPi camera
Recording a video with the RPi camera
Installing the v4l2 driver
Reading an image using OpenCV
Summary
Questions
Building an Object-Following Robot with OpenCV
Technical requirements
Image processing with OpenCV
Important functions in OpenCV
Object recognition using OpenCV
Capturing the image
Finding the RGB pixel values
The object detection program
The OpenCV camera feed program
Building an object-following robot
Ball tracing using moments
Programming logic
The ball tracing program
Setting up the object-following robot
Object-following robot program
Summary
Questions
Face Detection and Tracking Using the Haar Classifier
Technical requirements
Face detection using the Haar cascade
Basic working of the Viola-Jones algorithm
Face-detection program
Detecting the eyes and smile
Detecting the eyes
Eye detection using haarcascade_eye
Eye detection using haarcascade_lefteye_2splits and haarcascade_righteye_2splits
Detecting the left eye
Detecting the right eye
Recognizing a smile
Programming logic for smile recognition
Face-tracking robot
Wiring connections
The programming logic
Using the white dot on the face triangle to move the robot
Summary
Questions
Section 4: Smartphone-Controlled Robot
Building a Voice-Controlled Robot
Technical requirements
An introduction to App Inventor
Creating a Talking Pi Android app
Designing the app
Programming the blocks
Main block
Intermediate block
Final block
The Talking Pi program
Importing and exporting the .aia file of the app
Creating a voice-controlled bot app
Designing the app
Adding and customizing the components
Programming the voice-controlled bot block
Pairing the Android smartphone and RPi via Bluetooth
Enabling the Bluetooth serial port
Developing the Bluetooth program for RPi
Socket programming
VoiceBot server program
Testing the code
Summary
Questions
Assessments
Chapter 1: Introduction to the Raspberry Pi
Chapter 2: Implementing Blink with wiringPi
Chapter 3: Programming the Robot
Chapter 4: Building an Obstacle-Avoiding Robot
Chapter 5: Controlling a Robot Using a Laptop
Chapter 6: Building an Object-Following Robot with OpenCV
Chapter 7: Accessing the RPi Camera with OpenCV
Chapter 8: Face Detection and Tracking Using the Haar Classifier
Chapter 9: Building a Voice-Controlled Robot
Other Books You May Enjoy
Leave a review - let other readers know what you think
C++ is one of the most popular legacy programming languages for robotics, and a combination of C++ and robotics hardware is used in many leading industries. This book will bridge the gap between Raspberry Pi and C/C++ programming and enable you to develop applications for Raspberry Pi. To follow along with the projects covered in the book, you can implement C programs in Raspberry Pi with the wiringPi library.
With this book, you’ll develop a fully functional car robot and write programs to move it in different directions. You’ll then create an obstacle-avoiding robot using an ultrasonic sensor. Furthermore, you’ll find out how to control the robot wirelessly using your PC/Mac. This book will also help you work with object detection and tracking using OpenCV, and guide you through exploring face detection techniques. Finally, you will create an Android app and control the robot wirelessly with an Android smartphone.
By the end of this book, you will have gained experience in developing a robot using Raspberry Pi and C/C++ programming.
This book is designed for developers, programmers, and robotics enthusiasts interested in leveraging C++ to build exciting robotics applications. Some prior knowledge of C++ is necessary.
Chapter 1, Introduction to the Raspberry Pi, covers different modes of Raspberry Pi and GPIO pin configuration. Then, we will set up Raspberry Pi B+ and Raspberry Pi Zero and install the Raspbian OS on it. We will also learn how to connect a Raspberry Pi to a laptop wirelessly via a Wi-Fi network.
Chapter 2, Implementing Blink with wiringPi, covers the installation of the wiringPi library. In this chapter, we will understand the wiringPi pin connections for the Raspberry Pi. Then, we will write two C++ programs and will upload them onto our Raspberry Pi.
Chapter 3, Programming the Robot, covers the criteria for selecting a robot chassis. After that, we will construct our car, connect the motor driver to the Raspberry Pi, and understand the workings of an H-bridge circuit. Finally, we will write programs to move the robot forward, backward, left, and right.
Chapter 4, Building an Obstacle-Avoiding Robot, looks at how an ultrasonic sensor works, and we will write a program to measure distance values. Next, we will program the 16 x 2 LCD to read the ultrasonic distance value. We will also look at the I2C LCD, which takes the 16 LCD pin as an input and provides four pins as an output, thus simplifying the wiring connections. Finally, we will fit the ultrasonic sensor on our robot to create our obstacle-avoiding robot. This robot will move freely when there are no obstacles near it, and if it approaches an obstacle, it will avoid it by taking a turn.
Chapter 5, Controlling a Robot Using a Laptop, looks at two different techniques for controlling the robot using a laptop. In the first technique, we will use the ncurses library to take input from the keyboard to move the robot accordingly. In the second technique, we will use the QT Creator IDE to create GUI buttons, and then use these buttons to move the robot in different directions.
Chapter 6, Accessing Rpi Cam with OpenCV, focuses on the installation of OpenCV on the Raspberry Pi. You will also be introduced to the Raspberry Pi camera module and, after setting up the Pi camera, you will take pictures and record a short video clip using the Pi camera.
Chapter 7, Building an Object-Following Robot with OpenCV, covers some of the important functions inside OpenCV libraries. After that, we will put these functions to the test and attempt to recognize an object from an image. Then, we will learn how to read a video feed from the Pi camera, how to threshold a colored ball, and how to place a red dot on top of it. Finally, we will use the Pi camera and the ultrasonic sensor to detect the ball and follow it.
Chapter 8, Face Detection and Tracking Using Haar Classifier, uses the Haar face classifier to detect a face from a video feed and draw a rectangle around it. Next, we will detect eyes and a smile on the given face and create a circle surrounding the eyes and mouth. After using this knowledge of face and eye detection, we will first turn the LED on/off when the eyes and smile are detected. Next, by creating a white dot in the center of the face, we will make the robot follow the face.
Chapter 9, Building a Voice-Controlled Robot, starts with creating our first Android application, called Talking Pi, in which text written inside the textbox will be displayed in a label and also read out by the smartphone. We will then develop a voice-controlled Android app for the bot, which will recognize our voice and send text to the RPi via Bluetooth. After this, using the terminal window, we will pair the Android smartphone's Bluetooth with the RPi's Bluetooth. Finally, we will look at socket programming and write the VoiceBot program to establish a connection with the Android smartphone's Bluetooth in order to control the robot.
To get through the code in this book, Raspberry Pi 3B+ or Raspberry Pi Zero board is required. The additional hardware and software is mentioned in the Technical requirements section of each chapter.
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Hands-On-Robotics-Programming-with-Cpp. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/9781789139006_ColorImages.pdf.
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "The code for taking axial and radial turns is added to the RobotMovement.cpp program."
A block of code is set as follows:
digitalWrite(0,HIGH); //PIN O & 2 will STOP the Left MotordigitalWrite(2,HIGH);digitalWrite(3,HIGH); //PIN 3 & 4 will STOP the Right MotordigitalWrite(4,HIGH);delay(3000);
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
digitalWrite(0,HIGH); //PIN O & 2 will STOP the Left MotordigitalWrite(2,HIGH);digitalWrite(3,HIGH); //PIN 3 & 4 will STOP the Right MotordigitalWrite(4,HIGH);
delay(3000);
Any command-line input or output is written as follows:
sudo nano /boot/config.txt
Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select the Remember password option and press OK."
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
In this section, you will first be introduced to the basics of Raspberry Pi and learn how to install the Raspbian OS on your Raspberry Pi. Next, you will work with the wiringPi library and execute your first C program on Raspberry Pi.
The following chapters are included in this section:
Chapter 1
,
Introduction to the Raspberry Pi
Chapter 2
,
Implementing Blink with wiringPi
Initially developed with the idea of teaching and promoting basic computer programming in schools across the UK, the Raspberry Pi (RPi) became an instant hit. At a price of just $25 when it was initially released, it became so popular that it was, and still is, used by developers, hobbyists, and engineers all over the world.
In this chapter, you will explore the basic idea of a Raspberry Pi. You will then learn to install an operating system on the device. Finally, you will configure Wi-Fi on your Raspberry Pi and learn to connect it to a laptop over Wi-Fi and set up a remote desktop.
You will achieve each of these objectives through the following topics:
Understanding the Raspberry Pi
Installing Raspbian OS on a Raspberry Pi 3B+
Connecting a Raspberry Pi 3B+ to a laptop via Wi-Fi
Installing Raspbian OS on a Raspberry Pi Zero W
Connecting a Raspberry Pi Zero W to a laptop via Wi-Fi
For this chapter, the following software and hardware will be required.
Please download the following software if you want to follow along with the instructions in this chapter:
Raspbian Stretch
: Raspbian Stretch is the
operating system
(
OS
) that we will write to a microSD card. Stretch is the OS that will run our Raspberry Pi. It can be downloaded from
https://www.raspberrypi.org/downloads/raspbian/
This OS is developed specifically for the Raspberry Pi.
Balena Etcher
: This software will format the microSD card and write the Raspbian Stretch image to the microSD card. It can be downloaded from
https://www.balena.io/etcher/
.
PuTTY
: We will use PuTTY to connect our Raspberry Pi to a Wi-Fi network and find the IP address that the Wi-Fi network assigns to it. It can be downloaded from
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
.
VNC Viewer
: With VNC Viewer, we will be able to view the Raspberry Pi display on our laptop. It can be downloaded from
https://www.realvnc.com/en/connect/download/viewer/
.
Bonjour
: This is generally used to connect printers to computers over Wi-Fi. It can be downloaded from
https://support.apple.com/kb/DL999?viewlocale=en_MY&locale=en_MY
.
Notepad++
: We will need Notepad++ to edit the code in the Raspbian Stretch image. It can be downloaded from
https://notepad-plus-plus.org/download/v7.5.9.html
.
Brackets:
Brackets allows those using macOS to edit the code in the Rapbian Stretch image. To download Brackets, go to
http://www.brackets.io/
.
We need the following hardware to follow along with the instructions in this chapter.
If you use the Raspberry Pi 3B+ or the Raspberry Pi Zero W, you will need the following hardware:
Keyboard
Mouse
SD card—t
his should have a minimum of 8 GB of storage, but 32 GB is recommended
MicroSD card reader
Display unit—a computer monitor or TV that contains an HDMI port
HDMI cable
5V mobile charger or power bank. This will power the Raspberry Pi
The Raspberry Pi 3B+ needs the following additional hardware:
An Ethernet cable
Since the Raspberry Pi Zero has a micro USB port and a Micro HDMI port, it needs the following additional hardware:
A USB hub
A micro USB B-to-USB connector (also known as
an
OTG connector
)
An HDMI-to-mini HDMI connector
Raspberry Pi is a credit card sized, Linux-based minicomputer invented by the Raspberry Pi Foundation in 2012. The first Raspberry Pi model was called the Raspberry Pi 1B, which was then followed by the Model A. Raspberry Pi boards were initially intended to promote computer science programs in schools. However, their inexpensive hardware and free, open source software, quickly made the Raspberry Pi popular among hackers and robotics developers.
The Raspberry Pi can be used as a fully functional computer. It can be used to perform tasks such as browsing the internet, playing games, and watching HD videos, as well as creating Excel and Word documents. But what really differentiates it from a normal computer is its programmable GPIO pins. The Raspberry Pi consists of 40 digital I/O GPIO pins that can be programmed.
In simple terms, the Raspberry Pi can be thought of as a combination of a minicomputer, as it can be used as a fully fledged computer, and an electronics hardware board, as it can be used to create electronics and robotics projects.
There are different Raspberry Pi models. In this book, we are going to be using the following two models:
The Raspberry Pi 3B+
The Raspberry Pi Zero W
The Raspberry Pi 3B+ was released in February 2018. Its specifications are shown in the following annotated photo:
The Raspberry Pi 3B+ specifications are as follows:
A Broadcom BCM2837 quad-core 1.4 GHz processor
1 GB RAM
A Broadcom VideoCore GPU
Bluetooth 4.2
Dual-band 2.4 GHz and 5 GHz Wi-Fi
An Ethernet port
Storage with a microSD card via a microSD slot
40 programmable GPIO pins
Four USB 2.0 ports
An HDMI port
A 3.5 mm audio jack
The
Camera Serial Interface
(
CSI