Learn Robotics Programming - Danny Staple - E-Book

Learn Robotics Programming E-Book

Danny Staple

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

We live in an age where the most difficult human tasks are now automated. Smart and intelligent robots, which will perform different tasks precisely and efficiently, are the requirement of the hour. A combination of Raspberry Pi and Python works perfectly when making these kinds of robots.
Learn Robotics Programming starts by introducing you to the basic structure of a robot, along with how to plan, build, and program it. As you make your way through the book, you will gradually progress to adding different outputs and sensors, learning new building skills, and writing code for interesting behaviors with sensors. You’ll also be able to update your robot, and set up web, phone, and Wi-Fi connectivity in order to control it.
By the end of the book, you will have built a clever robot that can perform basic artificial intelligence (AI) operations.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 525

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Learn Robotics Programming
Build and control autonomous robots using Raspberry Pi 3 and Python
Danny Staple
BIRMINGHAM - MUMBAI

Learn Robotics Programming

Copyright © 2018 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: Gebin GeorgeAcquisition Editor: Akshay JethaniContent Development Editor: Priyanka DeshpandeTechnical Editor: Mohit HassijaCopy Editor: Safis EditingProject Coordinator: Drashti PanchalProofreader: Safis EditingIndexer: Pratik ShirodkarGraphics: Tom ScariaProduction Coordinator: Jyoti Chauhan

First published: November 2018

Production reference: 1281118

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78934-074-7

www.packtpub.com

To my dear wife, Carol, for her love, her inspiration, and her unwavering support.
mapt.io

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.

Why subscribe?

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

PacktPub.com

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.

Contributors

About the author

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform.

The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.

I would like to thank David Anderson for being a great person to bounce ideas off and for his motivational energy. I would like to thank Ben Nuttall and Dave Jones for GPIOZero, and for helping me out countless times on twitter. Dave Jones kickstarted my journey into computer vision in a restaurant in Cardiff and is the author of the PiCamera Library. Finally, I would like to thank my children, Helena and Jonathan, for their support and patience, even occasionally reviewing diagrams for me.

About the reviewer

Leo White is a professional software engineer, and a graduate of the University of Kent. His interests include electronics, 3D printing, and robotics. He first started programming on the Commodore 64, later wrote several applications for the Acorn Archimedes, and currently programs set-top boxes for his day job. Utilizing the Raspberry Pi as a base, he has mechanized children's toys and driven robot arms, blogging about his experiences and processes along the way, has given presentations at Raspberry Jams, and entered a variety of robots in the Pi Wars competition.

Packt is searching for authors like you

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.

Table of Contents

Title Page

Copyright and Credits

Learn Robotics Programming

Dedication

Packt Upsell

Why subscribe?

PacktPub.com

Contributors

About the author

About the reviewer

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

Code in Action

Conventions used

Get in touch

Reviews

Introduction to Robotics

What does robot mean?

Advanced and impressive robots

Robots that look like humans and animals

The Mars rovers

Robots in the home

The washing machine

Other household robots

Robots in industry

Robot arms

Warehouse robots

Competitive, educational, and hobby robots

Summary

Questions

Further reading

Exploring Robot Building Blocks - Code and Electronics

Technical requirements

What is inside a robot?

Types of motors, sensors, and actuators

Motors and actuators

Status indicators – displays, lights, and sounds

Types of sensors

Controllers and IO

IO pins

Controllers

Planning components and code structure

Planning our robot

Summary

Questions

Further reading

Introducing the Raspberry Pi - Starting with Raspbian

Technical requirements

What can the Raspberry Pi do?

Speed and power

Connectivity and networking

The recommended Raspberry Pi version

What connections will we use?

Raspberry Pi Hats

What is Raspbian?

Preparing an SD card

Flashing the card in Etcher

Getting Etcher

Using Etcher to write to the card

Summary

Questions

Further reading

Preparing a Raspberry Pi for a Robot - Headless by Default

Technical requirements

What does headless mean and why?

Setting up wireless on the Raspberry Pi and enabling SSH

The wpa_supplicant.conf file

Finding your Pi on the network

Apple macOS

Microsoft Windows

Linux

Testing the setup

What if you cannot reach the Raspberry Pi?

Using PuTTY or SSH to connect to your Raspberry Pi

Configuring Raspbian

Renaming your Pi

Securing your Pi (a little bit)

Reboot and reconnect

Updating the software on your Raspberry Pi

What is sudo?

Shutting down your Raspberry Pi

Summary

Questions

Further reading

Backing Up the Code with Git and SD Card Copies

Technical requirements

How code can be broken or lost

SD card data loss and corruption

Bad changes to the code or configuration

Combinations

Strategy 1 - keep the code on the PC and upload it

Strategy 2 – using Git to go back in time

Strategy 3 – making SD card backups

Windows

Mac and Linux

Linux

Mac

Cloning with DD

Summary

Questions

Further reading

Building Robot Basics - Wheels, Power, and Wiring

Technical requirements

Choosing a chassis kit

Size

Wheel count

Wheels and motors

Simplicity

Cost

Conclusion

Choosing a motor controller

Integration level

Pin usage

Size

Soldering

Connectors

Conclusion

Powering the robot

Test fitting the robot

Assembling the base

Assembling the base, motors, and wheels

Plastic motor brackets

Metal motor brackets

Adding the castor wheel

Putting the wheels on

Bringing the cables up

Fitting the Raspberry Pi

Adding the batteries

The USB power bank

Mounting the AA battery holder

The completed robot base

Summary

Questions

Further reading

Drive and Turn - Moving Motors with Python

Technical requirements

Connecting the motors to the Raspberry Pi

Wiring in

Independent power

Writing code to test your motors

Preparing libraries

Test – finding the motor hat

Test – the motors move

Troubleshooting

How does this code work?

Steering a robot

Types of steering

Steerable wheels

Fixed wheels

Other steering systems

Steering the robot we are building

The robot object – code for our experiments to talk to the robot

Why make this object?

What will we put in the robot object?

Write a script to follow a predetermined path

Summary

Questions

Further reading

Programming Line-Following Sensors Using Python

Technical requirements

Attaching line sensors to the robot and Raspberry Pi

What are optical line sensors?

What other new parts will we use?

Construction plan

Getting the correct length

Mounting sensor modules

Wiring the sensor in

Painting the test track

Writing the code – testing the sensors

Calibrating the sensors

Test code

Troubleshooting

Writing the line-following behavior

Adding the sensors to the Robot object

Creating the line-following behavior code

More troubleshooting

Extra ideas

Summary

Questions

Further reading

Programming RGB Strips in Python

Technical requirements

Comparing light strip technologies

RGB values

Attaching the light strip to the Raspberry Pi

Logic levels

Soldering headers on

Attaching the LED strip to the robot

Wiring the LED strip

Adding a power switch

Making a robot display code object

Adding LEDs to the robot object

Testing the LEDs

Troubleshooting

Using the light strip for debugging the line follower robot

Line follower basic LEDs

Color systems

Hue

Saturation

Value

Converting these

Adding a rainbow

Summary

Questions

Further reading

Using Python to Control Servo Motors

Technical requirements

What are servo motors?

What is in a servo?

Sending input positions to a servo motor

Positioning a servo

Code for turning a servo

Calibrating your servos

Adding a pan and tilt mechanism

Building the kit

Attaching the pan and tilt to the robot

Creating pan and tilt code

Making a servo object

Adding the servo to the robot class

Creating a behavior

Running it

Troubleshooting

Summary

Questions

Further reading

Programming Distance Sensors with Python

Technical requirements

Choosing light versus ultrasonic sensors

Optical sensors

Ultrasonic sensors

Why use two sensors?

Attaching and reading an ultrasonic sensor

Securing the sensors to the robot

Wiring the distance sensors

Voltages and level shifting

Wiring the breadboard

Reading an ultrasonic distance sensor

Troubleshooting

Avoiding walls – a script to avoid obstacles

Making our distance sensor object

Adding the sensor to the robot class

Making the obstacle avoid behaviors

First attempt at obstacle avoidance

More sophisticated object avoidance

Menu modes – choosing your robot's behavior

Managing robot modes

The web service

The template

Running it

Summary

Questions

Further reading

Programming Encoders with Python

Technical requirements

What are encoders?

Use of encoders

Types of encoders

Encoding direction and speed

The encoders we will be using

Attaching encoders to the robot

Lifting up the Raspberry Pi

Preparing the encoders

Wiring the encoders to Pi

Detecting the distance traveled in Python

Simple counting

Adding encoders to the Robot object

Extracting the class

Adding the device to the Robot object

Turning ticks into millimeters

Driving in a straight line

Concepts for correction

Creating a Python PID Control object

Straight line code

Troubleshooting this behavior

Driving a specific distance

Refactoring unit conversions into the EncoderCounter class

Setting the constants

Creating the combined behavior

Making a specific turn

The drive_arc function

Further ideas

Summary

Questions

Further reading

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 OpenCV

Setting up the Pi Camera software

Getting a picture from the Pi

Installing libraries

Building our first app with these tools

OpenCV camera server app overview

The CameraStream object

Building a template

The image server main app

Visual processing with behaviors

Web app core

Controllable behaviors

The template

Running the image server

Colors, masking, and filtering – chasing colored objects

Getting information about an object

Caveat about red objects

Enhancing the PID controller

The behavior code

The template

The behavior

Running the behavior

Tuning the PID controller settings

Troubleshooting

Enhancements that you could make

Detecting faces with Haar cascades

Finding objects in an image

Integral images

Basic features

Planning our behavior

The code for face tracking

Using this behavior

Troubleshooting

Summary

Questions

Further reading

Voice Communication with a Robot Using Mycroft

Technical requirements

Introducing Mycroft

Speech to text

Wake words

Utterances

Intent

Skills

Dialog

Vocabulary

Adding sound input and output to the Raspberry Pi

Physical installation

Installing Raspbian for the voice assist Pi

Installing the Respeaker software

Installing Mycroft on the Raspberry Pi

Base installation

Getting Mycroft to talk to the sound card

Starting to use Mycroft

Troubleshooting

Programming Mycroft skills for the robot functions

Building the intent

The settings file

The requirements file

Creating the vocabulary files

Dialog files

Current skill folder

Troubleshooting

Adding another intent

Vocabulary

Code

Running with the new intent

Summary

Questions

Further reading

Programming a Gamepad on Raspberry Pi with Python

Technical requirements

When speech control won't work - why we need to drive

Choosing a controller

Design and overview

Preparing the Raspberry Pi for our controller and driving with it

Enhancing the image app core

Writing the behavior

The template (web page)

The stylesheet

Creating the code for the sliders

Running this

Upgrading the menu and displaying code for full headless

Making menu modes compatible with Flask behaviors

Loading video services

Styling the menu

Making the menu template into buttons

Making it start when the Pi starts

Adding lights to the menu server

Using systemd to automatically start the robot

Enhancement Ideas

Summary

Questions

Further reading

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

2D design for illustration and diagrams

3D CAD

Skills for shaping and building

Machine skills and tools

Hand skills and tools

Electronics skills

Electronics principles

Taking soldering further

Custom circuits

Finding more information on computer vision

Books

Online courses

Social media

Extending into machine Learning

Robot Operating System

Summary

Further reading

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

Appendix

Finding parts

Converting this code to Python 3

Integer division

Print function

Input/raw input

Python 2 compatibility

Ranges have become generators

OpenCV versions

Find contours

Power smoothing capacitor

Assessments

Chapter 1, Introduction to Robotics

Chapter 2, Exploring Robot Building Blocks - Code and Electronics

Chapter 3, Introducing the Raspberry Pi - Starting with Raspbian

Chapter 4, Preparing a Raspberry Pi for a Robot - Headless by Default

Chapter 5, Backing Up the Code with Git and SD Card Copies

Chapter 6, Building Robot Basics - Wheels, Power, and Wiring

Chapter 7, Drive and Turn - Moving Motors with Python

Chapter 8, Programming Line-Following Sensors Using Python

Chapter 9, Programming RGB Strips in Python

Chapter 10, Using Python to Control Servo Motors

Chapter 11, Programming Distance Sensors with Python

Chapter 12, Programming Encoders with Python

Chapter 13, Robot Vision - Using a Pi Camera and OpenCV

Chapter 14, Voice Communication with a Robot Using Mycroft

Chapter 15, Programming a Gamepad on Raspberry Pi with Python

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

Learn Robotics is about building and programming a robot with smart behaviors. It covers the skills required to makes, and build, a gadget from parts, including how to choose them. This book follows with how to make the code to make those parts do something interesting. The book uses Python, together with a little bit of HTML/CSS and JS.

The technology shown here is intended to include things that are available and affordable, and the code intended to demonstrate concepts, so that these can be used and combined to create even more interesting code and robots.

It combines aspects of being a programmer, with aspects of being a robot maker, with a number of specialist topics such as computer vision and voice assistants thrown in.

Who this book is for

This book is intended for someone with a little programming experience. They do not need to be an expert-level programmer, but to have written some lines of code and be comfortable with looping, conditionals, and functions. Object oriented (class and object)-based programming isn't necessary, but is introduced in the book.

The book does not require a specialist workshop, although there will be a little soldering. This will be introduced later in the book.

The reader does not need to have any experience at all of electronics or making things, but hopefully a healthy interest in learning more, since some very basic concepts are introduced throughout the book. Being keen to build a robot, get it to do stuff, and find out what to do with it next is probably the most important aspect of the book.

What this book covers

Chapter 1, Introduction to Robotics, introduces what a robot is, together with examples.

Chapter 2, Exploring Robot Building Blocks – Code and Electronics, starts looking at the components of a robot. This is where we will start making choices about the robot's parts and it also introduces the block diagrams for both systems and code.

Chapter 3, Introducing the Raspberry Pi - Starting with Raspbian, introduces the Raspberry Pi and its connections, the Raspbian Linux operating system we'll use on it, and also covers the preparation of an SD card for use in a robot.

Chapter 4, Preparing a Raspberry Pi for a Robot - Headless by Default, looks at what a "headless" Raspberry Pi means and getting the Pi ready to run without a keyboard or screen.

Chapter 5, Backing Up the Code with Git and SD Card Copies, outlines how to restore things when they go wrong. This establishes habits to prevent you from losing your work.

Chapter 6, Building Robot Basics - Wheels, Power, and Wiring, introduces the choices and trade-offs for building the robot base, finding out what to buy, and taking your first steps in assembling it.

Chapter 7, Drive and Turn - Moving Motors with Python, covers how to connect our robot to the Raspberry Pi and write code to make the robot move, laying down the foundations for the code in subsequent chapters.

Chapter 8, Programming Line-Following Sensors Using Python, adds line sensors to the robot, explains how to fit them, and explains then how to use them to make line-following code.

Chapter 9, Programming RGB Strips in Python, demonstrates how to add an LED strip to the robot and write code to drive it, which the reader can then use to extend behaviors with colorful lights, for aesthetics, debugging, and information. This chapter also introduces soldering.

Chapter 10, Using Python to Control Servo Motors, gives our robot a set of servo motors, for the purpose of moving a "head" around, and shows the principles by which servo motors can be programmed.

Chapter 11, Programming Distance Sensors with Python, introduces distance sensors. Through the addition of a pair of variable inputs, we can make our robot avoid walls and obstacles autonomously.

Chapter 12, Programming Encoders with Python, demonstrates the concepts of odometry, measuring how far the robot has traveled according to wheel rotations, and uses it to compensate for motor variations and make accurate turns using a proportional-integral controller.

Chapter 13, Robot Vision - Using a Pi Camera and OpenCV, connects our robot to a camera mounted on the servo driven pan and tilt head. We program the robot to follow colored objects, or track faces in the camera, while allowing us to view its activity on a computer or phone.

Chapter 14, Voice Communication with a Robot Using Mycroft, introduces a voice assistant running on a second Pi, Mycroft, which we can program in Python to communicate with our robot and ask it to do things.

Chapter 15, Programming a Gamepad on Raspberry Pi with Python, is where we use HTML/CSS and JS to turn a phone into a smart game-like controller for our robot so that we can manually drive it, and launch autonomous behaviors at a touch, while seeing through the robot's camera.

Chapter 16, Taking Your Robot Programming Skills Further, looks at the wider world of robotics, what communities there are, how to get in touch with other robot builders and makers, potential development areas, and where to compete with a robot.

Chapter 17, Planning Your Next Robot Project - Putting It All Together, is the final chapter, where we summarize what you have seen in the book, while encouraging you to plan the construction of your next robot.

Chapter 18, Appendix, will cover extra information to help build your robot.

To get the most out of this book

Before you begin with this book, you need to have programmed a little in a text programming language. I am assuming some familiarity with variables, conditional statements, looping, and functions.

In terms of manual skills, I assume that you can use a screwdriver, that you can deal with occasional fiddly operations, and that you won't be too scared off by the possibility of soldering things.

Please find a well-lit work surface to build the robot on when those sections come, and somewhere to store it where it won't get dropped or damaged.

Download the example code files

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.packtpub.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 at https://github.com/PacktPublishing/Learn-Robotics-Programming. 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!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/9781789340747_ColorImages.pdf.

Code in Action

Visit the following link to check out videos of the code being run:

http://bit.ly/2FLWiIr

Conventions used

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: "This creates the myrobot.imgfileas a clone of the whole SD card in your home directory."

A block of code is set as follows:

import socketprint('%s is alive!' % socket.gethostname())

Any command-line input or output is written as follows:

C:\Users\danny>ping raspberrypi.local

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: "You'll see two download links, Download Torrent and Download Zip. Click the Download Zip button and save this file."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please 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.

Reviews

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 packtpub.com.

Introduction to Robotics

Throughout this book, we will build a robot and create programs for it that give the robot behaviors that make it feel intelligent and able to make decisions. We will write code to use sensors to observe the robot's surroundings, and build real-world examples of advanced topics such as vision, speech recognition, and talking.

You will see how the simple build techniques, when combined with a little bit of code, will result in a machine that feels like some kind of pet. You will also see how to debug it when things go wrong, which they will, and how to give the robot ways to indicate problems back to you, along with selecting the behavior you would like to demonstrate. We will connect a joypad to it, give it voice control, and finally show you how to plan a further robot build.

Before we start building a robot, it's worth spending a little time on an introduction to what robotics really is, or what a robot is. We can explore some of the types of robots, along with the basic principles that distinguish a robot from another type of machine. We will think a little about where the line between robot and non-robot machines are, then perhaps muddy that line a little bit with the somewhat fuzzy truth. We will then look at the types of robots that people start building in the hobbyist and amateur robotics scene.

In this chapter,we will be covering the following topics:

What does robot mean? Where the word comes from, and what exactly defines a robot.

Top robots, where we'll take a look at some amazing real robots.

Robots in industry, to explore robots seen in factories and warehouses.

Robots in the home, to show how robots are already with us in our lives.

We'll look at competitive, educational, and hobby robots where people are using robots for fun, learning, and showing off.

What does robot mean?

A robot is a machine that is able to make autonomous decisions based on input from sensors. A software agent is a program that is designed to automatically process input and produce output. Perhaps a robot can be best described as an autonomous software agent with sensors and moving outputs. Or, it could be described as an electromechanical platform with software running on it. Either way, a robot requires electronics, mechanical parts, and code.

The word robot conjures up images of fantastic sci-fi creations, devices with legendary strength and intelligence. These often follow the human body plan, making them an android, the term for a human-like robot. They are often given a personality and behave like a person who is in some simple way naive. Refer to the following diagram:

Science fiction and real-world robots. Images used are from the public domain OpenClipArt library

The word robot comes from sci-fi. The word is derived from the Czech for slave, and was first used in the 1921 Karel Capek play, Rossums Universal Robots. The science fiction author Isaac Asimov coined the word robotics as he explored intelligent robot behavior.

Most real robots in our homes and industries have a few cutting edge and eye catching examples standing out. Most do not stand on two legs, or indeed any legs at all. Some are on wheels, and some are not mobile but still have many moving parts and sensors.

Robots like washing machines, autonomous vacuum cleaners, fully self regulating boilers, and air sampling fans have infiltrated our homes and are part of everyday life. They aren't threatening, and have became just another machine around us. The 3D printer, robot arm, and learning toys are a bit more exciting though. Take a look at the following diagram:

The robot, reduced

At their core, robots can all be simplified down to what is represented in the preceding diagram with outputs, such as a motor, inputs, and a controller for processing or running code. So, the basis of a robot, represented as a list, would look something like this:

A robot has inputs, and sensors to measure, and sample a property of its environment

A robot has outputs, motors, lights, sounds, valves, sounds, heaters, or other types of output to alter it's environment

A robot will use the data from its inputs to make autonomous decisions about how it controls its outputs

Advanced and impressive robots

Now you have an overview of robots in general, I'll introduce some specific examples that represent the most impressive robots around, and what they are capable of. These robots are technical demonstrations, and with the exception of the Mars robots, have favored their closeness to human or animal adaptability and form over their practical and repeated use.

Robots that look like humans and animals

Take a look at the following picture and understand the similarities between robots and humans/animals:

A selection of human and animal-like robots. Cog: an Mit Project, Honda ASIMO By Morio, Nao From Softbank Robotic, Boston Dynamics Atlas, Boston Dynamics BigDog (https://commons.wikimedia.org/)

What these robots have in common is that they try to emulate humans and animals in the following ways:

The first robot on the left is Cog, from the Massachusetts Institute of Technology. Cog attempted to be human-like in its movements and sensors.

The second robot is the Honda ASIMO, which

walks and talks a little like a human. ASIMO's two cameras perform object avoidance, and gestures and face recognition, and have a laser distance sensor to sense the floor. It can follow marks on the floor with infrared sensors. ASIMO is able to accept voice commands in English and Japanese.

The third robot in this selection is the Nao robot from Softbank Robotics. This rather cute,

58 cm tall robot was

designed as a learning and play robot for users to program. It has sensors to detect its motion, including if it is falling, and ultrasonic distance sensors to avoid bumps. Nao uses speakers and a microphone for voice processing. Nao includes multiple cameras to perform similar feats to the ASIMO.

The fourth robot is Atlas from Boston Dynamics. This robot is speedy on two legs and is capable of natural looking movement. It has a laser radar (LIDAR) array, which it uses to sense what is around it to plan and avoid collisions.

The right-most robot is the Boston Dynamics BigDog, a four legged robot, or quadruped, which is able to run and is one of the most stable four legged robots, capable of being pushed, shoved, and walking in icy conditions while remaining stable.

We will incorporate some features like these in the robot we will build, using distance sensors to avoid obstacles, a camera for visual processing, line sensors to follow marks on the floor, and voice processing to follow and respond to spoken commands. We will use ultrasonic distance sensors like Nao, and experiment with distance sensors a little like Asimo. We will also look at pan and tilt mechanisms for camera a little like the head used in Cog.

The Mars rovers

The Mars rover robots are designed to function on a different planet, where there is no chance of human intervention if something goes wrong. They are robust by design. New code can only be sent to a Mars rover via a remote connection as it is not practical to send up a person with a screen and keyboard. The Mars rover is headless by design. Refer to the following photo:

The Curiosity Mars rover by NASA

Mars rovers depend on wheels instead of legs, since this is far simpler to make a robot stable, and there is far less that can go wrong. Each wheel on the Mars rovers has it's own motor. They are arranged to provide maximum grip and stability to tackle the rocky terrain and reduced gravity on Mars.

The Curiosity rover was deposited on Mars with its sensitive camera folded up. After landing, the camera was unfolded and positioned with servo motors. The camera package can be positioned using apan and tiltmechanism so it can take in as much of the Mars landscape as it can, sending back footage and pictures to NASA for analysis.

Like the Mars robot, the robot we will build in this book will use motor-driven wheels. Our robot will also be designed to run without a keyboard and mouse, being headless by design.As we expand the capabilities of our robot in this book, we will also use servo motors to drive a pan and tilt mechanism.

Robots in the home

Many robots have already infiltrated our homes. They are overlooked as robots because on first glance they appear commonplace and mundane. However, they are more sophisticated than they seem.

The washing machine

Let's start with the washing machine. This is used every day in some homes, with a constant stream of clothes to wash, spin, and dry. But how is this a robot? Let us understand this by referring to the following diagram:

The humble washing machine as a robot

The preceding diagram represents a washing machine as a block diagram. There is a central controller connected to the display, and with controls to select a program. The lines going out of the controller are outputs, and the lines going into the controller are data coming in from sensors. The dashed lines from outputs to the sensors show a closed loop of output actions in the real world causing sensor changes; this is feedback, an essential concept in robotics.

The washing machine uses the display and buttons to let the user choose the settings and see the status. After the start button is pressed, the machine will check the door sensor and sensibly refuse to start if the door is open. Once the door is closed and the start button is pressed, it will output to lock the door. After this, it uses heaters, valves, and pumps to fill the drum with heated water, using sensor feedback to regulate the water level and temperature.

Each process could be represented by a set of statements like these, which simultaneously fill the drum and keep it heated:

start water pumpturn on water heater

while

water is

not

filled

and

water is

not

at the right temperature:

if

water filled then stop water pump

if

water is at the right temperature then turn off heater

else

turn on water heater

Note the else there, which is in case the water temperature drops below the right temperature a bit. The washing machine then starts the drum spinning sequence: slow turns, fast spins, sensing the speed to meet the criteria. It will drain the drum, spin the clothes dry, release the door lock, and stop.

This washing machine is in every respect a robot. A washing machine has sensors and outputs to affect its environment. Processing allows it to follow a program and use sensors with feedback to reach and maintain conditions. A washing machine repair person may be more of a roboticist than I.

Other household robots

A gas central heating boiler has sensors, pumps, and valves and uses feedback mechanisms to maintain the temperature of the house, water flow through heating, gas flow, and ensure that the pilot light stays lit.

Smart fans use sensors to detect room temperature, humidity, and air quality, then output through the fan speed and heating elements.

A computer printer is also a robot, with moving part outputs and sensors to detect all those pesky paper jams.

Perhaps the most obvious home robot is the robot vacuum cleaner. Refer to the following diagram:

A robotic vacuum cleaner (PicaBot By Handitec)

This wheeled mobile robot is like the one we will build here, but prettier. They are packed with sensors to detect walls, bag levels, and barrier zones, and avoid collisions. They most represent the type of robot we are looking at.

As we build our robot, we will explore how to use its sensors to detect things and react to them, forming the same feedback loops we saw in the washing machine.

Robots in industry

Another place robots are commonly seen is in industry. The first useful robots have been used in factories, and have been there for a long time.

Robot arms

Robot arms range from very tiny and delicate robots for turning eggs, to colossal monsters moving shipping containers. Robot arms tend to use stepper and servo motors. We will look at servo motors in the pan and tilt mechanism used in this book. An impressive current industrial arm robot is Baxter from Rethink Robotics:

The Rethink Robotics Baxter Robot

Many robot arms are unsafe to work next to and could result in accidents. Not so with Baxter; it can sense a human and work around or pause for safety. In the preceding image, these sensors can be seen around the "head." The arm sensors and soft joints also allow Baxter to sense and react to collisions.

Baxter also has a training and repeat mechanism for workers to adapt it to work, using sensors in the joints to detect their position when being trained or playing back motions. Our robot will use encoder sensors so we can precisely program wheel movements.

Warehouse robots

Another common type of robot used in industry is those that move items around a factory floor or warehouse.

There are giant robotic crane systems capable of shifting pallets in storage complexes. They receive instructions on where goods need to be moved from and to within shelving systems:

Intellicart Line Following Robot

Smaller item-moving robot vehicles often employ line sensing technology, by following lines on the floor, wire underneath the floor via magnetic sensing, or marker beacons like ASIMO does. Our robot will follow lines like these. These line-following carts frequently use wheeled arrangements because these are simple to maintain and can form stable platforms.

Competitive, educational, and hobby robots

The most fun robots can be those built by amateur robot builders. This is an extremely innovative space.

Robotics always had a home in education, with academic builders using them for learning and experimentation platforms. Many commercial ventures have started in this setting. University robots tend to be group efforts, with access to increasingly hi-tech academic equipment to create them, as shown in the following picture:

Kismetand OhBot

Kismet was created at MIT in the late 90s. There are a number of hobbyist robots that are derived from it. It was groundbreaking at the time, using servo motors to drive face movements intended to mimic human expressions. This has been followed in the community with OhBot, an inexpensive hobbyist kit using servo motors, which can be linked with a Raspberry Pi, using voice recognition and facial camera processing to make a convincing display.

Hobby robotics is strongly linked with open source and blogging, sharing designs, and code, leading to further ideas. Hobbyist robots can be created from kits available on the internet, with modifications and additions. The kits cover a wide range of complexity from simple three-wheeled bases to drone kits and hexapods. They come with or without the electronics included. An investigation of kits will be covered in Chapter 6, Building Robot Basics - Wheels, Power, and Wiring. I used a hexapod kit to build SpiderBot to explore walking motion. Refer to the following photo:

Spiderbot - built by me, based on a kit. Controller is an esp8266 + Adafruit 16 Servo Controller

Skittlebot was my Pi Wars 2018 entry, built using toy hacking, repurposing a remote control excavator toy into a robot platform. Pi Wars is an autonomous robotics challenge for Raspberry Pi-based robots, which has both manual and autonomous challenges. There were entries with decorative cases and interesting engineering principles. Skittlebot uses three distance sensors to avoid walls, and we will investigate this kind of sensor in Chapter 11, Programming Distance Sensors with Python. Skittlebot uses a camera to seek out colored objects, as we will see in Chapter 13, Robot Vision - Using A Pi Camera And OpenCV. Here is a photo of Skittlebot:

Skittlebot - My PiWars 2018 Robot - based on a toy

Some hobbyist robots are built from scratch, using 3D printing, laser cutting, vacuum forming, woodwork, CNC, and other techniques to construct the chassis and parts. Refer to the following set of photos:

Building Armbot

I built the robot from scratch, for the London robotics group the Aurorans, in 2009. The robot was known as eeeBot in 2009, since it was intended to be driven by an Eee PC laptop. The Aurorans were a community who met to discuss robotics. The robot was later given a Raspberry Pi, and a robot arm kit seemed to fit it, earning it the name Armbot. In the current market, there are many chassis kits and a beginner will not need to measure and cut materials in this way to make a functioning robot. This was not built to compete, but to inspire other robot builders and kids to code. Towards the end of the book, we will cover some of the communities where robots are being built and shared, along with starting points on using construction techniques to build them from scratch.

The television series Robot Wars is a well known competitive robot event with impressive construction and engineering skills. There is no autonomous behavior in Robot Wars though; these are all manually driven, like remote control cars. Washing machines, although less exciting, are smarter, so they could be more strictly considered robots.

Summary

In this chapter, we have looked at what the word robot means, and the facts and fiction with robots. We have defined what a real robot is, and gained some idea of what a machine needs to do to be considered a robot.

We've investigated the robots seen in the home, and in industry, and those that are designed to amaze or have traveled to other planets. We've also looked at hobbyist and education robots, and how some of these are just built for fun. You've seen some block diagrams of real-world devices that may not have been considered robots, and have seen how our homes may already have a number of robots present.

Now we know what robots are, let's move on to the next chapter, in which we'll look at how to plan a robot so we can build it.

Questions

Based on the topics covered in this chapter, answer the following questions:

What element of a robot is used to monitor its environment?

What type of robot element do motors represent?

What are the three elements of a robotic system?

Where have robots been operating the longest in regular usage?

Why are wheels used more often than legs?

What is the principle connecting output, input, and control in a loop?

Why might a household washing machine be considered more robotic than a UK Robot Wars entry?

Further reading

Refer to the following links:

Honda Asimo:

http://asimo.honda.com/

Baxter at Rethink Robotics:

https://www.rethinkrobotics.com/baxter/

Kistmet at MIT:

http://www.ai.mit.edu/projects/humanoid-robotics-group/kismet/kismet.html

The OhBot:

http://www.ohbot.co.uk/

The Mars Science Laboratory at NASA:

https://mars.nasa.gov/msl/

Exploring Robot Building Blocks - Code and Electronics

In this chapter, we will go over the parts and systems that make up robots. What are the parts of a robot? These parts will be both software (code, and other) and hardware. How do they go together? When starting to make a robot, it's valuable to think about the parts you want and how they relate to each other. I recommend you sketch a plan of your robot—a block diagram as a guide to the connected code and parts.

In this chapter,we will be covering the following topics:

What is inside a robot? We will take apart a robot and examine its parts

Types of motors, sensors, and actuators - details on these robot parts

Controllers and IO - we will look at the main controllers and input/output functions

Planning components and code structure—we will make block diagrams to describe code, and components anywhere

A plan of our robot—example plans of the robot we will build in this book

Technical requirements

For this chapter, you will require basic drawing materials, such as a pen and paper. While software such as Draw.io, Dia, Pencil, Inkscape, or Visio could be used, a back-of-an envelope sketch of a block diagram is a great start to robot planning. Most of my robots start that way.

What is inside a robot?

We can start by looking at a robot as a physical system. However, instead of looking at it all joined together, you can see how a typical hobby rover looks when totally disassembled in the following diagram:

A hobby rover with components laid out

A robot can also be visualized as a block diagram of connected parts. Block diagrams use simple shapes to show a rough idea of how things may be connected. Refer to the following diagram:

A robot block diagram

The preceding diagram is a block diagram. This is not a formal notation; the important factor is that you can clearly see the blocks of functionality you intend in the hardware, with the high-level flow of data between them. This can be a sketch on the back of a bit of scrap paper. The key I've created is off the top of my head, but it must be something that helps distinguish sensors, outputs, and controllers.

An important thing to note is this is not a schematic, nor a scale diagram of a finished robot. It does not even attempt to represent the actual electronic connections, or small details such as having to signal an HCSR04 sensor before it responds; it just gives a general idea of the data flow. This is the right sketchto show how many motors there are, the kinds of motors, how many sensors, and, if known, what additional control circuitry is needed.

It is from this diagram that you can start to flesh out more detail, such as examining the electrical connections, thinking about the power requirements, the actual hardware, and how much space will be needed. For a bit of fun, pick up some paper and sketch a block diagram for a robot you'd like to create. This is the first step toward making it happen.

Types of motors, sensors, and actuators

A motor is an output device that results in rotation when power is applied. An actuator is an output device, which is any device that creates movements from an electrical signal. Examples of actuators are solenoids, valves, and pneumatic rams. Motors are a subset of actuators too.

A sensor is a device that provides input to a robot, allowing it to sense its environment. There are more sensor types than a single book can list, so we'll keep to the commonly available and fun-to-use ones.

Motors and actuators

I've laid out some of the different kinds of motors, all of which are used in robotics. Let's take a look at what each one does, and how we might use them for different kinds of motion, as shown in the following photo:

Different motor types

I've shown some common motor types in the preceding photo. Let's understand them in detail:

The most common type of motor is the DC motor. This has the property of spinning in proportion to the amount of voltage running through it.

A bare DC motor like this tends to spin a little too fast to be useful bare, and is very easily stopped. This is a DC Gear motor—a DC motor fitted with a gearbox, providing a reduction in speed and increase in mechanical advantage so it can move a larger load. Note that this gear motor is missing soldered leads! These types are most useful in the wheels of a robot. We will be attaching gear motors very much like the second image to our robot in

chapter 6

,

Building Robot Basics - Wheels, Power, and Wiring

and

chapter 7

,

Drive and Turn - Moving Motors with Python

.

This is the servo motor (or servomechanism). A controller sends a signal to this type of motor telling it what angular position to move to, with code working in degrees

. The motor will attempt to reach and hold that position using it's own internal feedback loop, which means it can repeatably reach the same position. Servo motors are used in pan and tilt mechanisms to position sensors, robot arms, or other limbs and

appendages, where the ease of positioning makes them more suitable than other motor types

. We will be programming servo motors in

Chapter 10

,

Using Python to Control Servo Motors

.

This is a stepper motor. These have coils powered in a sequence to allow the motor to step a certain number of degrees, so it can be used for very precise motions. Stepper motors tend to be slower and generate a lot of heat compared with DC motors or servo motors, but are most suitable for fine control, like that needed in 3D printers and high-end robot arms. They are also more expensive than the other types.

All motors will require some sort of hardware so a microcontroller/computer like the Pi can drive them. This hardware allows the Pi to switch power hungry devices without destroying them. Don't connect DC motors, stepper motors, or solenoids directly to a Raspberry Pi!

Linear actuators are devices where electrical signals are converted into motion along a single axis. These can be a stepper motor driving a screw in a fixed enclosure or magnetic systems that work like mag-lev trains and move very smoothly. Refer to the following photo:

Linear actuators: By Rollon91

A solenoid is a simple linear actuator using an electromagnetic coil with a metal core that is pulled or pushed away when power is applied to it. A common use of this type is in a doorbell mechanism. They are also used to operate valves on hydraulic or pneumatic actuators, which can create powerful motions like those seen in excavators and large industrial robotic arms.

Status indicators – displays, lights, and sounds

Another helpful output device is a display—a simple light (or LED) to indicate the status of some part of the robot, or an array of LEDs, a display that can show some text, or a full graphical display like those found on a mobile phone. We will be connecting an LED strip to the robot as a display in Chapter 9, Programming RGB Strips in Python.

Speakers and beepers can be used for a robot to communicate with humans by making sounds. The sound output from these can range from simple noises up to speech or playing music.

Many robots do not have any mounted status indicators and rely on a connected phone or laptop to display their status for them.

Types of sensors

I have a small selection of common sensors I use—similar to those that will be covered in this book. These have been used in robots I have built. Let's examine each one, and what we would use it for. Note that these may look different from the same sensor types seen before - there is a large variation in sensors that perform the same task, and when we get to adding them into the robot, we will cover their variants in more detail. Refer to the following set of photos:

A selection of sensors from my robots

Let's understand each image in detail:

This is a Raspberry Pi Camera module, which connects directly into a Raspberry Pi. We will be using this camera for our visual processing programming in

Chapter 13

,

Robot Vision - Using A Pi Camera And OpenCV

. This module can capture single images or video sequences. It can generate a lot of data quickly, which is one of the complications for robot vision. It is sensitive to lighting conditions.

This is a distance sensor, the VL53L0X Time of Flight laser ranging sensor. It uses an infrared laser to bounce off objects and detect how far away they are. This type of sensor sends a detected range directly to the controller. It can be sensitive to lighting conditions. The VL53L0X sensors use I2C, which means they send information as data directly to the Raspberry Pi, and can share their two communication pins with many other devices. I2C is good for when you have lots of sensors and outputs, and you are starting to run out of places to connect things. I2C sensors are handy, but they tend to be more expensive than sensors without this capability.

This is another distance/ranging sensor - the HCSR04 ultrasonic distance sensor - which bounces sound pulses off objects instead. It's more sensitive to the types of material an object is made from and will fail to detect some surfaces, but is impervious to lighting conditions. The HC-SR04 requires the controller to trigger a pulse of sound, and then time the echo, which we will have to manage in our code for it. It has a far greater range than the VL53L0x laser sensor, and is far cheaper, but is also less sensitive at near distances. We will be programming both sound and light-based range sensors in Chapter 11, .

This is a set of three line sensors, that use light to detect transitions from light to dark. They can be adjusted to sense in different conditions. There are a few variations of these modules. We will use a set of these sensors for line following in

Chapter 8

,

Programming Line-Following Sensors Using Python

. These will each provide an on or off signal, depending on light or dark areas beneath it. They are the simplest of the sensors we will use in this book.

The fifth sensor is a pair of microphones with some tape to stick them crudely to a robot. These can connect directly to PCM pins on a Pi, but other microphones may need to be connected to electronics to process their signal further, into something the Raspberry Pi uses. There are arrays of four microphones or a single microphones that can be used, and in some contexts can detect the direction of sound as well. Microphones will be used for voice processing in

Chapter 14

,

Voice Communication With A Robot Using Mycroft

.

The last sensor is an optical interrupt sensor, which passes infra-red light through a gap between two posts to detect whether something is between them, interrupting the beam. These are used with notched wheels to sense rotations, which can determine how far a robot has gone and introduce more accuracy into movements. Using an offset pair of these sensors with a wheel can encode the direction of the movement as well as its speed, and such an arrangement is known as an encoder. There are complete encoder assemblies available as sensors too, making it mechanically simpler to use them. We will investigate using encoders in

Chapter 12

,

Programming Encoders with Python

.

There are many more sensors not covered here to detect positions of limbs, light, smoke, heat sources, and magnetic fields. These can all be used to make more advanced robots and add more interesting behavior.

Controllers and IO