Practical Robotics in C++ teaches the complete spectrum of Robotics, right from the setting up a computer for a robot controller to putting power to the wheel motors. The book brings you the workshop knowledge of the electronics, hardware, and software for building a mobile robot platform.
You will learn how to use sensors to detect obstacles, how to train your robot to build itself a map and plan an obstacle-avoiding path, and how to structure your code for modularity and interchangeability with other robot projects. Throughout the book, you can experience the demonstrations of complete coding of robotics with the use of simple and clear C++ programming.
In addition, you will explore how to leverage the Raspberry Pi GPIO hardware interface pins and existing libraries to make an incredibly capable machine on the most affordable computer platform ever.
Cover Page
Title Page
Copyright Page
Dedication Page
About the Author
Acknowledgements
Preface
Foreword
Errata
Table of Contents
Introduction
1. Choose and Set Up a Robot Computer
What is a Raspberry Pi?
What’s the difference then?
So the Raspberry Pi is the only choice for a robot controller?
Isn’t the Raspberry Pi for schools, hobbyists, and toys? I wanted to learn about real robotics.
Raspberry Pi models and why not all are suitable for our purposes
Raspberry Pi Zero and Raspberry Pi ZeroW
The Raspberry Pi 2B
The Raspberry Pi 3B - Best choice!!
The Raspberry Pi 3B+
The new Raspberry Pi 4
Operating system choices
Raspbian
Ubuntu
Operating system installation and setup
Full Ubuntu desktop on laptop or desktop PC
Lubuntu on your Raspberry Pi
Programming environment (IDE) installation and setup
Visual studio code for the laptop or desktop PC
Code blocks for the Raspberry Pi
Conclusion
Questions
2. GPIO Hardware Interface Pins Overview and Use
Introduction
What are GPIO pins
So what exactly does the GPIO do?
Electronics for programmers 101
Types of output data
Types of input data
Some common electronics hardware
Breadboards
GPIO pins as outputs
Two pin numbering systems
GPIO pins as inputs
Accessing the Raspberry Pi GPIO with C++ programs
Installing PIGPIO
Installing and setting up the PIGPIO library
Making sure Code::Blocks can link to PIGPIO
Running PIGPIO programs
Our first GPIO project – hello_blink
Digital input to control a digital output – hello_button
GPIO event callback functions
Conclusion
3. The Robot Platform
Introduction
Objective
Considering the size and operating environment
Differential drive versus Ackerman (car-like) steering
Differential drive
Ackerman steering
Ready-made robot platforms
Large pre-built robots
Small pre-built robots
Tips for building your own robot
Building materials
Batteries
Drive trains
Robot parts sources
Re-purposing robot vacuums or remote-controlled cars
Robot vacuums with the interface
Interfacing with a Roomba
Un-freezing your Roomba
Robot vacuums without an Interface
Hacking remote-controlled cars and trucks
Conclusion
Questions
4. Types of Robot Motors and Motor Control
Introduction
Objectives
Motor types
Alternating current (AC) versus direct current (DC) motors
Brush-type DC motors
Servos
Stepper motors
Brushless DC motors (also known as BLDC)
Introduction to the transistor and motor drivers
The most basic control: On/Off
Transistors
Pulse width modulation (PWM)
PWM to create analog voltages
PWM as a control signal
Motor drivers and motor controllers
Motor drivers
Controlling motors with an L298N dual H-Bridge motor driver
Motor controllers
Conclusion
Questions
Bonus challenge
5. Communication with Sensors and other Devices
Introduction
Objective
Binary (logical) signals
Debouncing switches
Wheel encoders
Binary signals from analog sensors
Binary communication summary
Serial communication primer
UART serial
Set up a Raspberry Pi and test UART serial communication
Fixing error opening serial port
I2C communication primer
To set up and use an I2C device with the Raspberry Pi:
Example and test program: hello_i2c_lsm303
Conclusion
Questions
6. Additional Helpful Hardware
Introduction
Objective:
Power supplies
5 volt supplies
Adjustable power supplies
Relay modules
Logic level converters
FTDIs
Arduinos
Digisparks
Conclusion
Questions
7. Adding the Computer to Control your Robot
Introduction
Structure
Objective
The steps
Step 1 - Mount and run power to the computer:
Interface (wire) the computer to the rest of the Robot:
Conclusion
Questions
8. Robot Control Strategy
Introduction
Structure
Objectives
Robot control: The big picture versus the small picture
The fundamental control loop
Observe and compare
React
Affect
Open-loop and closed-loop controllers:
Designing a big picture (also known as the master) controllers:
Designing a small picture (also known as a process) controllers:
Bang bang controllers (also known as On/Off controllers)
Proportional controllers
Designing controllers to accept some error
Setting a minimum output
Beyond proportional controllers
Conclusion
Questions
9. Coordinating the Parts
Introduction:
Structure:
Objective
What is the robot operating system?
ROS versus writing your robot control software
ROS and the commercial robotics industry
ROS setup
ROS melodic installation on your laptop or desktop
ROS kinetic installation on your Raspberry Pi 3B
ROS overview and crash-course
Packages, nodes, publishers, subscribers, topics, and messages
A handful of helpful tips
Creating and writing ROS packages and nodes
The ROS file system
Creating ROS packages
Writing ROS programs (Nodes)
Downloading, reviewing, and running the chapter download programs
Making life easier with roslaunch and .launch files
Conclusion
Questions
10. Maps for Robot Navigation
Introduction
Objectives
Angle, heading, and distance conventions
Receiving sensor data
Occupancy grid maps
Building occupancy grid maps (OGMs) with sensor data
Marking the occupied cells
Marking the free cells
Completing the map
Publishing the map as a ROS Message
Transforms in ROS
Understanding transforms
How transforms are used in ROS
Publishing transforms with the static transform publisher
Publishing transforms from nodes with a transform broadcaster
Getting transform data in your nodes
Viewing transform data from the command line
Mapping made easy with Gmapping
Gmapping 101
Getting Gmapping
Running Gmapping and parameters in launch files
Steps to create a map
Visualizing a live map
Saving a Map and using it later
Saving maps
Load previously saved map
Conclusion
Questions
11. Robot Tracking and Localization
Introduction
Objectives
The robot pose
Converting Euler angles to quaternions
Converting quaternions to Euler angles
Odometry and dead reckoning
Wheel odometry
Calculate the distance traveled for each wheel
Calculate the total distance the robot has traveled
Calculate the change in heading angle theta
Add the change in heading to old heading theta
Calculate the distance moved in the x direction and the y directions (also known as translation)
Add the distances calculated to the previous pose estimate
Dead reckoning
Publishing odometry data in ROS
Odometry transform publisher
Further tracking and localization
Manual pose updater
Fiducials
Laser scanner based localization
GPS and GNSS
Beacon-based localization systems
Conclusion
Questions
12. Autonomous Motion
Introduction
Objective
ROS robot motion overview
The motor controller - simple_diff_drive.cpp
The simple_diff_drive motor controller code steps
The differential drive motor controller code outline
The differential drive motor controller code
The drive controller: simple_drive_controller.cpp
Drive controller steps
Conclusion
Questions
13. Autonomous Path Planning
Introduction
Objectives
Path planning methods and challenges
Challenges
Path planning methods
Obstacle inflation
Costmaps
A* path planning
How it works
The A* algorithm by the steps
Walking through an A* routine
Writing the A* program as a ROS node
The standard stuff, helper functions, and main()
The heart of your A* Node: find_path()
Conclusion
Questions
14. Wheel Encoders for Odometry
Introduction
Objective
Wheel encoders 101
Optical encoders
Hall effect encoders
Wiring encoders
The Encoder tick publisher - tick_publisher.cpp
Encoder tick publisher code
Conclusion
Questions
15. Ultrasonic Range Detectors
Introduction
Objective
HC-SR04 ultrasonic range sensor basics
Reading HC-SR04 by the steps
Wiring the HC-SR04
Ultrasonic range data publisher: ultrasonic_publisher.cpp
Ultrasonic range publisher by the steps
Ultrasonic range publisher code
Ultrasonic range data for object detection
Conclusion
Questions
16. IMUs - Accelerometers, Gyroscopes and Magnetometers
Introduction
Objectives
Accelerometers
Accelerometer shortcomings
Publishing IMU Data in ROS
The ROS sensor_msgs::Imu data type
The IMU message publisher code
Gyroscopes
Gyroscope shortcomings
Adding gyroscope data to the IMU node
Magnetometers
Magnetometer shortcomings
Adding magnetometer data
Mounting the IMU
Conclusion
Questions
17. GPS and External Beacon Systems
Introduction
Objectives
How beacon systems work
GPS and GNSS basics
GPS/GNSS accuracy
GPS/GNSS-RTK for 2cm accuracy
GPS/GNSS limitations
GPS/GNSS data
NMEA data strings
Some key lat/long data representations
Publishing GPS/GNSS data in ROS
The ROS package: nmea_navsat_driver
Installing the nmea_navsat_driver package
Reading ROS package documentation
Running the nmea_serial_driver node with parameters
Conclusion
Questions
18. LIDAR Devices and Data
Introduction
Objective
LIDAR basics
LIDAR limitations
LIDAR types
Unidirectional (single point) LIDAR
2D LIDAR
3D LIDAR
Salvaged robot vacuum LIDAR
LIDAR selection considerations
LIDAR data: The sensor_msgs::LaserScan message
LIDAR mounting considerations
Setting up, running, and testing a common LIDAR unit
Setting up an RPLIDAR by following these steps:
Visualizing the LaserScan message
Conclusion
Questions
19. Real Vision with Cameras
Introduction
Objectives
What is an image?
Image attributes
Pixel coordinates
Checking or installing the required software
ROS Kinetic
ROS Melodic
Testing OpenCV in ROS
Image processing software (OpenCV) and ROS:
Step 1: Publishing images in ROS
Installing the usb_cam_node
Running the usb_cam_node
Test the camera output
Step 2: Subscribe to image in a different node
Create your ROS vision package
Coding the image message subscriber
Step 3: Use cv-bridge to convert the RGB image ROS uses to a BGR image OpenCV can work with
Step 4: Perform desired operations on the image
Step 5: Publish any non-image data as their own ROS message.
Step 6: Convert modified image back to RGB
Step 7: Publish result image under its own topic
More image processing basics
Kernels, apertures and blocks
The importance of working on copies instead of original images
A word about lighting
Step 4 revisited - more possible OpenCV operations
Practical Robotics in C++ teaches the complete spectrum of Robotics, right from the setting up a computer for a robot controller to putting power to the wheel motors. The book brings you the workshop knowledge of the electronics, hardware, and software for building a mobile robot platform.
You will learn how to use sensors to detect obstacles, how to train your robot to build itself a map and plan an obstacle-avoiding path, and how to structure your code for modularity and interchangeability with other robot projects. Throughout the book, you can experience the demonstrations of complete coding of robotics with the use of simple and clear C++ programming.
In addition, you will explore how to leverage the Raspberry Pi GPIO hardware interface pins and existing libraries to make an incredibly capable machine on the most affordable computer platform ever.
Table of contents
Cover Page
Title Page
Copyright Page
Dedication Page
About the Author
Acknowledgements
Preface
Foreword
Errata
Table of Contents
Introduction
1. Choose and Set Up a Robot Computer
What is a Raspberry Pi?
What’s the difference then?
So the Raspberry Pi is the only choice for a robot controller?
Isn’t the Raspberry Pi for schools, hobbyists, and toys? I wanted to learn about real robotics.
Raspberry Pi models and why not all are suitable for our purposes
Raspberry Pi Zero and Raspberry Pi ZeroW
The Raspberry Pi 2B
The Raspberry Pi 3B - Best choice!!
The Raspberry Pi 3B+
The new Raspberry Pi 4
Operating system choices
Raspbian
Ubuntu
Operating system installation and setup
Full Ubuntu desktop on laptop or desktop PC
Lubuntu on your Raspberry Pi
Programming environment (IDE) installation and setup
Visual studio code for the laptop or desktop PC
Code blocks for the Raspberry Pi
Conclusion
Questions
2. GPIO Hardware Interface Pins Overview and Use
Introduction
What are GPIO pins
So what exactly does the GPIO do?
Electronics for programmers 101
Types of output data
Types of input data
Some common electronics hardware
Breadboards
GPIO pins as outputs
Two pin numbering systems
GPIO pins as inputs
Accessing the Raspberry Pi GPIO with C++ programs
Installing PIGPIO
Installing and setting up the PIGPIO library
Making sure Code::Blocks can link to PIGPIO
Running PIGPIO programs
Our first GPIO project – hello_blink
Digital input to control a digital output – hello_button
GPIO event callback functions
Conclusion
3. The Robot Platform
Introduction
Objective
Considering the size and operating environment
Differential drive versus Ackerman (car-like) steering
Differential drive
Ackerman steering
Ready-made robot platforms
Large pre-built robots
Small pre-built robots
Tips for building your own robot
Building materials
Batteries
Drive trains
Robot parts sources
Re-purposing robot vacuums or remote-controlled cars
Robot vacuums with the interface
Interfacing with a Roomba
Un-freezing your Roomba
Robot vacuums without an Interface
Hacking remote-controlled cars and trucks
Conclusion
Questions
4. Types of Robot Motors and Motor Control
Introduction
Objectives
Motor types
Alternating current (AC) versus direct current (DC) motors
Brush-type DC motors
Servos
Stepper motors
Brushless DC motors (also known as BLDC)
Introduction to the transistor and motor drivers
The most basic control: On/Off
Transistors
Pulse width modulation (PWM)
PWM to create analog voltages
PWM as a control signal
Motor drivers and motor controllers
Motor drivers
Controlling motors with an L298N dual H-Bridge motor driver
Motor controllers
Conclusion
Questions
Bonus challenge
5. Communication with Sensors and other Devices
Introduction
Objective
Binary (logical) signals
Debouncing switches
Wheel encoders
Binary signals from analog sensors
Binary communication summary
Serial communication primer
UART serial
Set up a Raspberry Pi and test UART serial communication
Fixing error opening serial port
I2C communication primer
To set up and use an I2C device with the Raspberry Pi:
Example and test program: hello_i2c_lsm303
Conclusion
Questions
6. Additional Helpful Hardware
Introduction
Objective:
Power supplies
5 volt supplies
Adjustable power supplies
Relay modules
Logic level converters
FTDIs
Arduinos
Digisparks
Conclusion
Questions
7. Adding the Computer to Control your Robot
Introduction
Structure
Objective
The steps
Step 1 - Mount and run power to the computer:
Interface (wire) the computer to the rest of the Robot:
Conclusion
Questions
8. Robot Control Strategy
Introduction
Structure
Objectives
Robot control: The big picture versus the small picture
The fundamental control loop
Observe and compare
React
Affect
Open-loop and closed-loop controllers:
Designing a big picture (also known as the master) controllers:
Designing a small picture (also known as a process) controllers:
Bang bang controllers (also known as On/Off controllers)
Proportional controllers
Designing controllers to accept some error
Setting a minimum output
Beyond proportional controllers
Conclusion
Questions
9. Coordinating the Parts
Introduction:
Structure:
Objective
What is the robot operating system?
ROS versus writing your robot control software
ROS and the commercial robotics industry
ROS setup
ROS melodic installation on your laptop or desktop
ROS kinetic installation on your Raspberry Pi 3B
ROS overview and crash-course
Packages, nodes, publishers, subscribers, topics, and messages
A handful of helpful tips
Creating and writing ROS packages and nodes
The ROS file system
Creating ROS packages
Writing ROS programs (Nodes)
Downloading, reviewing, and running the chapter download programs
Making life easier with roslaunch and .launch files
Conclusion
Questions
10. Maps for Robot Navigation
Introduction
Objectives
Angle, heading, and distance conventions
Receiving sensor data
Occupancy grid maps
Building occupancy grid maps (OGMs) with sensor data
Marking the occupied cells
Marking the free cells
Completing the map
Publishing the map as a ROS Message
Transforms in ROS
Understanding transforms
How transforms are used in ROS
Publishing transforms with the static transform publisher
Publishing transforms from nodes with a transform broadcaster
Getting transform data in your nodes
Viewing transform data from the command line
Mapping made easy with Gmapping
Gmapping 101
Getting Gmapping
Running Gmapping and parameters in launch files
Steps to create a map
Visualizing a live map
Saving a Map and using it later
Saving maps
Load previously saved map
Conclusion
Questions
11. Robot Tracking and Localization
Introduction
Objectives
The robot pose
Converting Euler angles to quaternions
Converting quaternions to Euler angles
Odometry and dead reckoning
Wheel odometry
Calculate the distance traveled for each wheel
Calculate the total distance the robot has traveled
Calculate the change in heading angle theta
Add the change in heading to old heading theta
Calculate the distance moved in the x direction and the y directions (also known as translation)
Add the distances calculated to the previous pose estimate
Dead reckoning
Publishing odometry data in ROS
Odometry transform publisher
Further tracking and localization
Manual pose updater
Fiducials
Laser scanner based localization
GPS and GNSS
Beacon-based localization systems
Conclusion
Questions
12. Autonomous Motion
Introduction
Objective
ROS robot motion overview
The motor controller - simple_diff_drive.cpp
The simple_diff_drive motor controller code steps
The differential drive motor controller code outline
The differential drive motor controller code
The drive controller: simple_drive_controller.cpp
Drive controller steps
Conclusion
Questions
13. Autonomous Path Planning
Introduction
Objectives
Path planning methods and challenges
Challenges
Path planning methods
Obstacle inflation
Costmaps
A* path planning
How it works
The A* algorithm by the steps
Walking through an A* routine
Writing the A* program as a ROS node
The standard stuff, helper functions, and main()
The heart of your A* Node: find_path()
Conclusion
Questions
14. Wheel Encoders for Odometry
Introduction
Objective
Wheel encoders 101
Optical encoders
Hall effect encoders
Wiring encoders
The Encoder tick publisher - tick_publisher.cpp
Encoder tick publisher code
Conclusion
Questions
15. Ultrasonic Range Detectors
Introduction
Objective
HC-SR04 ultrasonic range sensor basics
Reading HC-SR04 by the steps
Wiring the HC-SR04
Ultrasonic range data publisher: ultrasonic_publisher.cpp
Ultrasonic range publisher by the steps
Ultrasonic range publisher code
Ultrasonic range data for object detection
Conclusion
Questions
16. IMUs - Accelerometers, Gyroscopes and Magnetometers
Introduction
Objectives
Accelerometers
Accelerometer shortcomings
Publishing IMU Data in ROS
The ROS sensor_msgs::Imu data type
The IMU message publisher code
Gyroscopes
Gyroscope shortcomings
Adding gyroscope data to the IMU node
Magnetometers
Magnetometer shortcomings
Adding magnetometer data
Mounting the IMU
Conclusion
Questions
17. GPS and External Beacon Systems
Introduction
Objectives
How beacon systems work
GPS and GNSS basics
GPS/GNSS accuracy
GPS/GNSS-RTK for 2cm accuracy
GPS/GNSS limitations
GPS/GNSS data
NMEA data strings
Some key lat/long data representations
Publishing GPS/GNSS data in ROS
The ROS package: nmea_navsat_driver
Installing the nmea_navsat_driver package
Reading ROS package documentation
Running the nmea_serial_driver node with parameters
Conclusion
Questions
18. LIDAR Devices and Data
Introduction
Objective
LIDAR basics
LIDAR limitations
LIDAR types
Unidirectional (single point) LIDAR
2D LIDAR
3D LIDAR
Salvaged robot vacuum LIDAR
LIDAR selection considerations
LIDAR data: The sensor_msgs::LaserScan message
LIDAR mounting considerations
Setting up, running, and testing a common LIDAR unit
Setting up an RPLIDAR by following these steps:
Visualizing the LaserScan message
Conclusion
Questions
19. Real Vision with Cameras
Introduction
Objectives
What is an image?
Image attributes
Pixel coordinates
Checking or installing the required software
ROS Kinetic
ROS Melodic
Testing OpenCV in ROS
Image processing software (OpenCV) and ROS:
Step 1: Publishing images in ROS
Installing the usb_cam_node
Running the usb_cam_node
Test the camera output
Step 2: Subscribe to image in a different node
Create your ROS vision package
Coding the image message subscriber
Step 3: Use cv-bridge to convert the RGB image ROS uses to a BGR image OpenCV can work with
Step 4: Perform desired operations on the image
Step 5: Publish any non-image data as their own ROS message.
Step 6: Convert modified image back to RGB
Step 7: Publish result image under its own topic
More image processing basics
Kernels, apertures and blocks
The importance of working on copies instead of original images
A word about lighting
Step 4 revisited - more possible OpenCV operations