Learn Robotics Programming: Build and control AI-enabled autonomous robots using the Raspberry Pi and Python

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Develop an extendable smart robot capable of performing a complex series of actions with Python and Raspberry Pi Key Features • Get up to speed with the fundamentals of robotic programming and build intelligent robots • Learn how to program a voice agent to control and interact with your robot's behavior • Enable your robot to see its environment and avoid barriers using sensors Book Description We live in an age where the most complex or repetitive tasks are automated. Smart robots have the potential to revolutionize how we perform all kinds of tasks with high accuracy and efficiency. With this second edition of Learn Robotics Programming, you'll see how a combination of the Raspberry Pi and Python can be a great starting point for robot programming. The book starts by introducing you to the basic structure of a robot and shows you how to design, build, and program it. As you make your way through the book, you'll add different outputs and sensors, learn robot building skills, and write code to add autonomous behavior using sensors and a camera. You'll also be able to upgrade your robot with Wi-Fi connectivity to control it using a smartphone. Finally, you'll understand how you can apply the skills that you've learned to visualize, lay out, build, and code your future robot building projects. By the end of this book, you'll have built an interesting robot that can perform basic artificial intelligence operations and be well versed in programming robots and creating complex robotics projects using what you've learned. What you will learn • Leverage the features of the Raspberry Pi OS • Discover how to configure a Raspberry Pi to build an AI-enabled robot • Interface motors and sensors with a Raspberry Pi • Code your robot to develop engaging and intelligent robot behavior • Explore AI behavior such as speech recognition and visual processing • Find out how you can control AI robots with a mobile phone over Wi-Fi • Understand how to choose the right parts and assemble your robot Who this book is for This second edition of Learn Robotics Programming is for programmers, developers, and robotics enthusiasts who want to develop a fully functional robot and leverage AI to build interactive robots. Basic knowledge of the Python programming language will help you understand the concepts covered in this robot programming book more effectively.

Author(s): Danny Staple
Edition: 2
Publisher: Packt Publishing
Year: 2021

Language: English
Commentary: Vector PDF
Pages: 602
City: Birmingham, UK
Tags: Robotics; OpenCV; Python; Flask; Raspberry Pi; Git; Sensors; Electronics; Mycroft; Accelerometer; DC Motors; LEDs; Temperature Sensors; Servo Motors; Gyroscope

Cover
Title Page
Copyright and Credits
About Packt
Contributors
Table of Contents
Preface
Section 1: The Basics – Preparing for Robotics
Chapter 1: Introduction to Robotics
What does robot mean?
Exploring advanced and impressive robots
The Mars rovers
Discovering robots in the home
The washing machine
Other household robots
Exploring robots in industry
Robot arms
Warehouse robots
Competitive, educational, and hobby robots
Summary
Assessment
Further reading
Chapter 2: Exploring Robot Building Blocks – Code and Electronics
Technical requirements
Looking at what's inside a robot
Exploring types of robot components
Types of motors
Other types of actuators
Status indicators – displays, lights, and sounds
Types of sensors
Exploring controllers and I/O
I/O pins
Controllers
Choosing a Raspberry Pi
Planning components and code structure
Planning the physical robot
Summary
Exercise
Further reading
Chapter 3: Exploring the Raspberry Pi
Technical requirements
Exploring the Raspberry Pi's capabilities
Speed and power
Connectivity and networking
Picking the Raspberry Pi 3A+
Choosing the connections
Raspberry Pi HATs
What is Raspberry Pi OS?
Preparing an SD card with Raspberry Pi OS
Summary
Assessment
Further reading
Chapter 4: Preparing a Headless Raspberry Pi for a Robot
Technical requirements
What is a headless system, and why is it useful in a robot?
Setting up Wi-Fi on the Raspberry Pi and enabling SSH
Finding your Pi on the network
Setting up Bonjour for Microsoft Windows
Testing the setup
Troubleshooting
Using PuTTY or SSH to connect to your Raspberry Pi
Configuring Raspberry Pi OS
Renaming your Pi
Securing your Pi (a little bit)
Rebooting and reconnecting
Updating the software on your Raspberry Pi
Shutting down your Raspberry Pi
Summary
Assessment
Further reading
Chapter 5: Backing Up the Code with Git and SD Card Copies
Technical requirements
Understanding how code can be broken or lost
SD card data loss and corruption
Changes to the code or configuration
Strategy 1 – Keeping the code on a PC and uploading it
Strategy 2 – Using Git to go back in time
Strategy 3 – Making SD card backups
Windows
Mac
Linux
Summary
Assessment
Further reading
Section 2: Building an Autonomous Robot – Connecting Sensors and Motors to a Raspberry Pi
Chapter 6: Building Robot Basics – Wheels, Power, and Wiring
Technical requirements
Choosing a robot chassis kit
Size
Wheel count
Wheels and motors
Simplicity
Cost
Conclusion
Choosing a motor controller board
Integration level
Pin usage
Size
Soldering
Power input
Connectors
Conclusion
Powering the robot
Test fitting the robot
Assembling the base
Attaching the encoder wheels
Fitting the motor brackets
Adding the castor wheel
Putting the wheels on
Bringing the wires up
Fitting the Raspberry Pi
Adding the batteries
The completed robot base
Connecting the motors to the Raspberry Pi
Wiring the Motor HAT in
Independent power
Summary
Exercises
Further reading
Chapter 7: Drive and Turn – Moving Motors with Python
Technical requirements
Writing code to test your motors
Preparing libraries
Test – finding the Motor HAT
Test – demonstrating that the motors move
Troubleshooting
Understanding how the code works
Steering a robot
Types of steering
Steering the robot we are building
Making a Robot object – code for our experiments to talk to the robot
Why make this object?
What do we put in the robot object?
Writing a script to follow a predetermined path
Summary
Exercises
Further reading
Chapter 8: Programming Distance Sensors with Python
Technical requirements
Choosing between optical and ultrasonic sensors
Optical sensors
Ultrasonic sensors
Logic levels and shifting
Why use two sensors?
Attaching and reading an ultrasonic sensor
Securing the sensors to the robot
Adding a power switch
Wiring the distance sensors
Installing Python libraries to communicate with the sensor
Reading an ultrasonic distance sensor
Troubleshooting
Avoiding walls – writing a script to avoid obstacles
Adding the sensors to the robot class
Making the obstacle avoid behaviors
Summary
Exercises
Further reading
Chapter 9: Programming RGB Strips in Python
Technical requirements
What is an RGB strip?
Comparing light strip technologies
RGB values
Attaching the light strip to the Raspberry Pi
Attaching the LED strip to the robot
Making a robot display the code object
Making an LED interface
Adding LEDs to the Robot object
Testing one LED
Making a rainbow display with the LEDs
Colour systems
Making a rainbow on the LEDs
Using the light strip for debugging the avoid behavior
Adding basic LEDs to the avoid behavior
Adding rainbows
Summary
Exercises
Further reading
Chapter 10: Using Python to Control Servo Motors
Technical requirements
What are servo motors?
Looking inside a servo
Sending input positions to a servo motor
Positioning a servo motor with the Raspberry Pi
Writing code for turning a servo
Troubleshooting
Controlling DC motors and servo motors
Calibrating your servos
Adding a pan and tilt mechanism
Building the kit
Attaching the pan and tilt mechanism to the robot
Creating pan and tilt code
Making the servo object
Adding the servo to the robot class
Circling the pan and tilt head
Running it
Troubleshooting
Building a scanning sonar
Attaching the sensor
Installing the library
Behavior code
Summary
Exercises
Further reading
Chapter 11: Programming Encoders with Python
Technical requirements
Measuring the distance traveled with encoders
Where machines use encoders
Types of encoders
Encoding absolute or relative position
Encoding direction and speed
The encoders we are using
Attaching encoders to the robot
Preparing the encoders
Lifting the Raspberry Pi
Fitting the encoders onto the chassis
Wiring the encoders to the Raspberry Pi
Detecting the distance traveled in Python
Introducing logging
Simple counting
Adding encoders to the Robot object
Turning ticks into millimeters
Driving in a straight line
Correcting veer with a PID
Creating a Python PID controller object
Writing code to go in a straight line
Troubleshooting this behavior
Driving a specific distance
Refactoring unit conversions into the EncoderCounter class
Setting the constants
Creating the drive distance behavior
Making a specific turn
Writing the drive_arc function
Summary
Exercises
Further reading
Chapter 12: IMU Programming with Python
Technical requirements
Learning more about IMUs
Suggested IMU models
Soldering – attaching headers to the IMU
Making a solder joint
Attaching the IMU to the robot
Physical placement
Wiring the IMU to the Raspberry Pi
Reading the temperature
Installing the software
Troubleshooting
Reading the temperature register
Troubleshooting
Simplifying the VPython command line
Reading the gyroscope in Python
Understanding the gyroscope
Adding the gyroscope to the interface
Plotting the gyroscope
Reading an accelerometer in Python
Understanding the accelerometer
Adding the accelerometer to the interface
Displaying the accelerometer as a vector
Working with the magnetometer
Understanding the magnetometer
Adding the magnetometer interface
Displaying magnetometer readings
Summary
Exercises
Further reading
Section 3: Hearing and Seeing – Giving a Robot Intelligent Sensors
Chapter 13: Robot Vision – Using a Pi Camera and OpenCV
Technical requirements
Setting up the Raspberry Pi camera
Attaching the camera to the pan-and-tilt mechanism
Wiring in the camera
Setting up computer vision software
Setting up the Pi Camera software
Getting a picture from the Raspberry Pi
Installing OpenCV and support libraries
Building a Raspberry Pi camera stream app
Designing the OpenCV camera server
Writing the CameraStream object
Writing the image server main app
Building a template
Running the server
Troubleshooting
Running background tasks when streaming
Following colored objects with Python
Turning a picture into information
Enhancing the PID controller
Writing the behavior components
Running the behavior
Troubleshooting
Tracking faces with Python
Finding objects in an image
Planning our behavior
Writing face-tracking code
Running the face-tracking behavior
Troubleshooting
Summary
Exercises
Further reading
Chapter 14: Line Following with a Camera in Python
Technical requirements
Introduction to line following
What is line following?
Usage in industry
Types of line following
Making a line-follower test track
Getting the test track materials in place
Making a line
Line-following computer vision pipeline
Camera line-tracking algorithms
The pipeline
Trying computer vision with test images
Why use test images?
Capturing test images
Writing Python to find the edges of the line
Locating the line from the edges
Trying test pictures without a clear line
Line following with the PID algorithm
Creating the behavior flow diagram
Adding time to our PID controller
Writing the initial behavior
Tuning the PID
Troubleshooting
Finding a line again
Summary
Exercises
Further reading
Chapter 15: Voice Communication with a Robot Using Mycroft
Technical requirements
Introducing Mycroft – understanding voice agent terminology
Speech to text
Wake words
Utterances
Intent
Dialog
Vocabulary
Skills
Limitations of listening for speech on a robot
Adding sound input and output to the Raspberry Pi
Physical installation
Installing a voice agent on a Raspberry Pi
Installing the ReSpeaker software
Getting Mycroft to talk to the sound card
Starting to use Mycroft
Troubleshooting
Programming a Flask API
Overview of Mycroft controlling the robot
Starting a behavior remotely
Programming the Flask control API server
Troubleshooting
Programming a voice agent with Mycroft on the Raspberry Pi
Building the intent
Troubleshooting
Adding another intent
Summary
Exercises
Further reading
Chapter 16: Diving Deeper with the IMU
Technical requirements
Programming a virtual robot
Modeling the robot in VPython
Detecting rotation with the gyroscope
Calibrating the gyroscope
Rotating the virtual robot with the gyroscope
Detecting pitch and roll with the accelerometer
Getting pitch and roll from the accelerometer vector
Smoothing the accelerometer
Fusing accelerometer and gyroscope data
Detecting a heading with the magnetometer
Calibrating the magnetometer
Getting a rough heading from the magnetometer
Combining sensors for orientation
Driving a robot from IMU data
Summary
Exercises
Further reading
Chapter 17: Controlling the Robot with a Phone and Python
Technical requirements
When speech control won't work – why we need to drive
Menu modes – choosing your robot's behavior
Managing robot modes
Troubleshooting
The web service
The template
Running it
Troubleshooting
Choosing a controller — how we are going to drive the robot, and why
Design and overview
Preparing the Raspberry Pi for remote driving—get the basic driving system going
Enhancing the image app core
Writing the manual drive behavior
The template (web page)
The style sheet
Creating the code for the sliders
Running this
Troubleshooting
Making the robot fully phone-operable
Making menu modes compatible with Flask behaviors
Loading video services
Styling the menu
Making the menu start when the Pi starts
Adding lights to the menu server
Using systemd to automatically start the robot
Summary
Exercises
Further reading
Section 4: Taking Robotics Further
Chapter 18: Taking Your Robot Programming Skills Further
Online robot building communities – forums and social media
YouTube channels to get to know
Technical questions – where to get help
Meeting robot builders – competitions, makerspaces, and meetups
Makerspaces
Maker Faires, Raspberry Jams, and Dojos
Competitions
Suggestions for further skills – 3D printing, soldering, PCB, and CNC
Design skills
Skills for shaping and building
Electronics skills
Finding more information on computer vision
Books
Online courses
Social media
Extending to machine learning
Robot Operating System
Summary
Further reading
Chapter 19: Planning Your Next Robot Project – Putting It All Together
Technical requirements
Visualizing your next robot
Making a block diagram
Choosing the parts
Planning the code for the robot
Letting the world know
Summary
Other Books You May Enjoy
Index