Hands-On Robotics Programming with C++

Book description

Enhance your programming skills to build exciting robotic projects

Key Features

  • Build an intelligent robot that can detect and avoid obstacles and respond to voice commands
  • Detect and track objects and faces using OpenCV
  • Control your robot with a GUI button designed using Qt5

Book Description

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. You'll even be able to implement C programs in Raspberry Pi with the WiringPi library.

The book will guide you through developing a fully functional car robot and writing programs to move it in different directions. You’ll then create an obstacle-avoiding robot using an ultrasonic sensor. In addition to this, you’ll find out how to control the robot wirelessly using your PC or 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.

What you will learn

  • Install software on a Raspberry Pi compatible with C++ programming
  • Program the Raspberry Pi in C++ to run a motor
  • Control a Raspberry Pi-powered robot wirelessly with your laptop or PC
  • Program a Raspberry Pi camera using OpenCV
  • Control a Raspberry Pi robot with voice commands
  • Implement face and object detection with Raspberry Pi

Who this book is for

This book is for developers, programmers, and robotics enthusiasts interested in leveraging C++ to build exciting robotics applications. Prior knowledge of C++ is necessary to understand the projects covered in this book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Robotics Programming with C++
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Section 1: Getting Started with wiringPi on a Raspberry Pi
  8. Introduction to the Raspberry Pi
    1. Technical requirements
      1. Software required
      2. Hardware requirements
        1. For Raspberry Pi 3B+ and Raspberry Pi Zero W
        2. Additional hardware for Raspberry Pi 3B+
        3. Additional hardware requirements for Raspberry Pi Zero W
    2. Understanding the Raspberry Pi
      1. The Raspberry Pi 3B+
      2. The Raspberry Pi Zero W
    3. Setting up a Raspberry Pi 3B+ as a desktop computer
      1. Installing Raspbian OS on an SD card
        1. Downloading and installing Etcher
        2. Downloading the Raspbian Stretch image
        3. Writing the Raspbian Stretch image to a microSD card
      2. Setting up the Raspberry Pi 3B+
        1. Connecting the Raspberry Pi 3B+ to the internet
    4. Connecting the Raspberry Pi 3B+ to a laptop via Wi-Fi
      1. Creating an SSH file on a microSD card
      2. Connecting the Raspberry Pi 3B+ to a Wi-Fi network using PuTTY
      3. Enabling the VNC server
      4. Viewing the Raspberry Pi output on the VNC Viewer
        1. Increase the VNC's screen resolution
        2. Handling VNC and PuTTY errors
    5. Setting up the Raspberry Pi Zero W as a desktop computer
      1. Setting up the Raspberry Pi Zero W
    6. Connecting the Raspberry Pi Zero W to a laptop via Wi-Fi
      1. Connecting the Raspberry Pi Zero W to a Wi-Fi network using PuTTY
      2. Enabling VNC Viewer for Raspberry Pi Zero W
      3. Viewing Raspberry Pi Zero W output on VNC Viewer
    7. Summary
    8. Questions
  9. Implementing Blink with wiringPi
    1. Technical requirements
    2. Installing the wiringPi library in the Raspberry Pi
      1. Accessing Raspberry Pi GPIO pins via wiringPi
    3. Making an LED blink
      1. Wiring connections
      2. The blinking program
        1. Uploading the code to the Raspberry Pi
    4. Smart light – working with digital sensor
      1. The LDR sensor and the way it works
      2. Wiring connection
      3. Smart light program
    5. Pulse Width Modulation using softPWM
      1. How PWM works
      2. The softPWM library
        1. Making an LED blink with the softPWM library
    6. Summary
    7. Questions
  10. Section 2: Raspberry Pi Robotics
  11. Programming the Robot
    1. Technical requirements
    2. Choosing a robot chassis
    3. Constructing and connecting the robot
      1. Constructing the robot
      2. Connecting the motor driver to the Raspberry Pi
        1. What is a motor driver?
        2. Wiring connections
    4. Working with H-bridge
    5. Moving the robot
      1. Moving the robot backward
      2. Stopping the robot
      3. Different types of turns
        1. Axial turns
          1. Axial left turn
          2. Axial right turn
        2. Radial turn
          1. Radial left turn
          2. Radial right turn
    6. Summary
    7. Questions
  12. Building an Obstacle-Avoiding Robot
    1. Technical requirements
    2. Using an ultrasonic sensor
      1. How an ultrasonic sensor measures distances
        1. The arithmetic equation for determining the time taken
      2. Wiring the ultrasonic sensor to the Raspberry Pi
      3. The HC-SR04 sensor program
    3. Using an LCD
      1. Wiring the 16x2 LCD to the Raspberry Pi
      2. Programming the LCD
        1. The LCD program
        2. The LCD and the ultrasonic sensor program
      3. What is the I2C protocol?
        1. Wiring the I2C LCD and the Raspberry Pi
        2. Programming the LCD with the I2C LCD module
        3. The I2C LCD and the ultrasonic sensor program
    4. Building an obstacle-avoiding robot
      1. Wiring connections
      2. Programming the obstacle-avoiding robot
    5. Summary
    6. Questions
  13. Controlling a Robot Using a Laptop
    1. Technical requirements
    2. Installing the ncurses library
      1. ncurses functions
      2. Writing a HelloWorld program with ncurses
        1. Compiling and running the program
    3. Controlling LEDs and a buzzer using ncurses
      1. Wiring connections
      2. Writing the LEDBuzzer.cpp program
    4. Controlling a rover using a laptop keyboard
      1. Building a laptop-controlled rover program
      2. Tracing a square path
    5. Installing and setting up QT5
      1. Setting up QT5
    6. Controlling LEDs with GUI buttons
      1. Creating a QT project
        1. Dealing with errors
    7. Controlling a rover using a laptop with QT5
    8. Summary
    9. Questions
  14. Section 3: Face and Object Recognition Robot
  15. Accessing the RPi Camera with OpenCV
    1. Technical requirements
    2. Installing OpenCV 4.0.0 on Raspberry Pi
      1. Uninstalling Wolfram and LibreOffice
      2. Updating your RPi
      3. Installing the cmake, image, video, and gtk packages
      4. Downloading and unzipping OpenCV 4.0 and its contribution repository
      5. Installing Python
      6. Compiling and installing OpenCV
      7. Linking OpenCV to Python
    3. Enabling and connecting the RPi camera to RPi
      1. Connecting the RPi camera to RPi
      2. Mounting the RPi camera on the robot
    4. Capturing images and video with the RPi camera
      1. Recording a video with the RPi camera
      2. Installing the v4l2 driver
    5. Reading an image using OpenCV
    6. Summary
    7. Questions
  16. Building an Object-Following Robot with OpenCV
    1. Technical requirements
    2. Image processing with OpenCV
      1. Important functions in OpenCV
      2. Object recognition using OpenCV
        1. Capturing the image
        2. Finding the RGB pixel values
        3. The object detection program
      3. The OpenCV camera feed program
    3. Building an object-following robot
      1. Ball tracing using moments
        1. Programming logic
        2. The ball tracing program
      2. Setting up the object-following robot
        1. Object-following robot program
    4. Summary
    5. Questions
  17. Face Detection and Tracking Using the Haar Classifier
    1. Technical requirements
    2. Face detection using the Haar cascade
      1. Basic working of the Viola-Jones algorithm
      2. Face-detection program
    3. Detecting the eyes and smile
      1. Detecting the eyes
        1. Eye detection using haarcascade_eye
        2. Eye detection using haarcascade_lefteye_2splits and haarcascade_righteye_2splits
          1. Detecting the left eye
          2. Detecting the right eye
      2. Recognizing a smile
        1. Programming logic for smile recognition
    4. Face-tracking robot
      1. Wiring connections
        1. The programming logic
        2. Using the white dot on the face triangle to move the robot
    5. Summary
    6. Questions
  18. Section 4: Smartphone-Controlled Robot
  19. Building a Voice-Controlled Robot
    1. Technical requirements
    2. An introduction to App Inventor
      1. Creating a Talking Pi Android app
        1. Designing the app
        2. Programming the blocks
          1. Main block
          2. Intermediate block
          3. Final block
        3. The Talking Pi program
        4. Importing and exporting the .aia file of the app
    3. Creating a voice-controlled bot app
      1. Designing the app
        1. Adding and customizing the components
      2. Programming the voice-controlled bot block
    4. Pairing the Android smartphone and RPi via Bluetooth
      1. Enabling the Bluetooth serial port
    5. Developing the Bluetooth program for RPi
      1. Socket programming
      2. VoiceBot server program
        1. Testing the code
    6. Summary
    7. Questions
  20. Assessments
    1. Chapter 1: Introduction to the Raspberry Pi
    2. Chapter 2: Implementing Blink with wiringPi
    3. Chapter 3: Programming the Robot
    4. Chapter 4: Building an Obstacle-Avoiding Robot
    5. Chapter 5: Controlling a Robot Using a Laptop
    6. Chapter 6: Building an Object-Following Robot with OpenCV
    7. Chapter 7: Accessing the RPi Camera with OpenCV
    8. Chapter 8: Face Detection and Tracking Using the Haar Classifier
    9. Chapter 9: Building a Voice-Controlled Robot
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Robotics Programming with C++
  • Author(s): Dinesh Tavasalkar
  • Release date: March 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789139006