Beginning_Robotics_with_Raspberry

372 307 0
Beginning_Robotics_with_Raspberry

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

TECHNOLOGY IN AC TION™ Beginning Robotics with Raspberry Pi and Arduino Using Python and OpenCV — Jeff Cicolani Beginning Robotics with Raspberry Pi and Arduino Using Python and OpenCV Jeff Cicolani Beginning Robotics with Raspberry Pi and Arduino: Using Python and OpenCV Jeff Cicolani Pflugerville, Texas, USA ISBN-13 (pbk): 978-1-4842-3461-7 https://doi.org/10.1007/978-1-4842-3462-4 ISBN-13 (electronic): 978-1-4842-3462-4 Library of Congress Control Number: 2018937971 Copyright © 2018 by Jeff Cicolani This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed Trademarked names, logos, and images may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made The publisher makes no warranty, express or implied, with respect to the material contained herein Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Aaron Black Development Editor: James Markham Coordinating Editor: Jessica Vakili Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc) SSBM Finance Inc is a Delaware corporation For information on translations, please e-mail rights@apress.com, or visit http://www.apress com/rights-permissions Apress titles may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/978-1-4842-3461-7 For more detailed information, please visit http://www.apress.com/source-code Printed on acid-free paper For Martha, my beautiful and patient wife, for putting up with random robot parts strewn about the house, pretty much constantly Table of Contents About the Author���������������������������������������������������������������������������������xi About the Technical Reviewer�����������������������������������������������������������xiii Introduction����������������������������������������������������������������������������������������xv Chapter 1: Introduction to Robotics�����������������������������������������������������1 Robotics Basics�����������������������������������������������������������������������������������������������������2 Linux and Robotics������������������������������������������������������������������������������������������3 Sensors and GPIO��������������������������������������������������������������������������������������������4 Motion and Control������������������������������������������������������������������������������������������5 Raspberry Pi and Arduino�������������������������������������������������������������������������������������5 Project Overview���������������������������������������������������������������������������������������������������8 The Robot��������������������������������������������������������������������������������������������������������8 Bill of Materials (BOM) ������������������������������������������������������������������������������������9 Summary������������������������������������������������������������������������������������������������������������15 Chapter 2: An Introduction to Raspberry Pi����������������������������������������17 Downloading and Installing Raspbian�����������������������������������������������������������������17 Raspbian with OpenCV����������������������������������������������������������������������������������19 The “Hard” Way���������������������������������������������������������������������������������������������19 The “Easy” Way���������������������������������������������������������������������������������������������22 Connecting Raspberry Pi�������������������������������������������������������������������������������������24 Configuring Your Pi����������������������������������������������������������������������������������������������26 v Table of Contents Using raspi-config�����������������������������������������������������������������������������������������27 Users�������������������������������������������������������������������������������������������������������������34 Connecting to a Wireless Network����������������������������������������������������������������37 Going Headless���������������������������������������������������������������������������������������������������37 Remote Access����������������������������������������������������������������������������������������������38 Summary������������������������������������������������������������������������������������������������������������45 Chapter 3: A Crash Course in Python��������������������������������������������������47 Python Overview�������������������������������������������������������������������������������������������������49 Downloading and Installing Python���������������������������������������������������������������������49 Python Tools��������������������������������������������������������������������������������������������������������50 The Python Shell��������������������������������������������������������������������������������������������51 The Python Editor������������������������������������������������������������������������������������������52 The Zen of Python������������������������������������������������������������������������������������������55 Writing and Running a Python Program��������������������������������������������������������������56 Hello World����������������������������������������������������������������������������������������������������57 Basic Structure����������������������������������������������������������������������������������������������58 Running a Program����������������������������������������������������������������������������������������61 Programming in Python��������������������������������������������������������������������������������������61 Variables��������������������������������������������������������������������������������������������������������62 Data Types�����������������������������������������������������������������������������������������������������62 A Final Note on Variables�������������������������������������������������������������������������������74 Control Structures�����������������������������������������������������������������������������������������75 Functions�������������������������������������������������������������������������������������������������������81 Adding Functionality through Modules����������������������������������������������������������85 Classes����������������������������������������������������������������������������������������������������������91 Styling�����������������������������������������������������������������������������������������������������������99 Summary����������������������������������������������������������������������������������������������������������101 vi Table of Contents Chapter 4: Raspberry Pi GPIO�����������������������������������������������������������103 Raspberry Pi GPIO���������������������������������������������������������������������������������������������103 Pin Numbering���������������������������������������������������������������������������������������������105 Connecting to the Raspberry Pi�������������������������������������������������������������������106 Limitations of Raspberry Pi’s GPIO��������������������������������������������������������������107 Accessing GPIO with Python������������������������������������������������������������������������108 Simple Output: LED Example�����������������������������������������������������������������������110 Simple Input������������������������������������������������������������������������������������������������117 Summary����������������������������������������������������������������������������������������������������������127 Chapter 5: Raspberry Pi and Arduino�����������������������������������������������129 Raspberry Pi’s GPIO in Review��������������������������������������������������������������������������130 Real-Time or Near Real-Time Processing���������������������������������������������������130 Analog Input������������������������������������������������������������������������������������������������131 Analog Output����������������������������������������������������������������������������������������������131 Arduino to the Rescue���������������������������������������������������������������������������������������132 Using Arduino����������������������������������������������������������������������������������������������������134 Installing the Arduino IDE����������������������������������������������������������������������������135 Connecting an Arduino��������������������������������������������������������������������������������135 Programming Arduino����������������������������������������������������������������������������������136 Sketches������������������������������������������������������������������������������������������������������145 A Brief Introduction to the Arduino Language���������������������������������������������������149 Including Other Files������������������������������������������������������������������������������������150 Variables and Data Types�����������������������������������������������������������������������������150 Control Structures���������������������������������������������������������������������������������������155 Working with Pins����������������������������������������������������������������������������������������162 Objects and Classes������������������������������������������������������������������������������������167 Serial�����������������������������������������������������������������������������������������������������������167 Arduino to Pi and Back Again����������������������������������������������������������������������171 vii Table of Contents Pinguino������������������������������������������������������������������������������������������������������������181 Setting up the Circuit�����������������������������������������������������������������������������������182 Summary����������������������������������������������������������������������������������������������������������185 Chapter 6: Driving Motors����������������������������������������������������������������187 Motors & Drivers�����������������������������������������������������������������������������������������������188 Types of Motors�������������������������������������������������������������������������������������������188 Motor Properties������������������������������������������������������������������������������������������192 Motor Drivers�����������������������������������������������������������������������������������������������195 Working with Motor Controllers������������������������������������������������������������������������195 Adafruit DC & Stepper Motor HAT����������������������������������������������������������������196 L298N Generic Motor Driver������������������������������������������������������������������������217 Summary����������������������������������������������������������������������������������������������������������228 Chapter 7: Assembling the Robot�����������������������������������������������������229 Assembling the Chassis������������������������������������������������������������������������������������230 Choosing a Material�������������������������������������������������������������������������������������230 The Whippersnapper�����������������������������������������������������������������������������������231 Mounting the Electronics����������������������������������������������������������������������������������239 Wiring����������������������������������������������������������������������������������������������������������������246 Mounting Sensors���������������������������������������������������������������������������������������������249 The Finished Robot�������������������������������������������������������������������������������������������252 Making the Robot Mobile�����������������������������������������������������������������������������253 Summary����������������������������������������������������������������������������������������������������������266 Chapter 8: Working with Infrared Sensors���������������������������������������267 Infrared Sensors�����������������������������������������������������������������������������������������������267 Types of IR Sensors�������������������������������������������������������������������������������������268 Working with IR Sensors�����������������������������������������������������������������������������������272 viii Table of Contents Connecting an IR Sensor�����������������������������������������������������������������������������273 Mounting the IR Sensors�����������������������������������������������������������������������������276 The Code������������������������������������������������������������������������������������������������������278 Understanding PID Control��������������������������������������������������������������������������������289 Control Loops����������������������������������������������������������������������������������������������289 Implementing the PID Controller�����������������������������������������������������������������292 Summary����������������������������������������������������������������������������������������������������������296 Chapter 9: An Introduction to OpenCV����������������������������������������������297 Computer Vision������������������������������������������������������������������������������������������������297 OpenCV��������������������������������������������������������������������������������������������������������299 Selecting a Camera�������������������������������������������������������������������������������������302 Installing the Camera����������������������������������������������������������������������������������304 OpenCV Basics��������������������������������������������������������������������������������������������������305 Working with Images�����������������������������������������������������������������������������������306 Capturing Images����������������������������������������������������������������������������������������308 Image Transformations��������������������������������������������������������������������������������316 Working with Color��������������������������������������������������������������������������������������319 Blobs and Blob Detection����������������������������������������������������������������������������325 Ball-Chasing Bot�����������������������������������������������������������������������������������������������333 Summary����������������������������������������������������������������������������������������������������������340 Chapter 10: Conclusion���������������������������������������������������������������������343 Types of Robotics����������������������������������������������������������������������������������������������344 Tools�����������������������������������������������������������������������������������������������������������������344 Software������������������������������������������������������������������������������������������������������345 Hardware�����������������������������������������������������������������������������������������������������351 Summary�����������������������������������������������������������������������������������������������������354 Index�������������������������������������������������������������������������������������������������355 ix About the Author Jeff Cicolani currently lives in the Austin, Texas, area with his wife, two dogs, and dozen or so robots He is currently working as an embedded systems engineer, building robotic and automated platforms for an AI (artificial intelligence) company in Austin His journey to robotics was circuitous, taking him through an odd career path that included systems analysis and design and database programming In 2012, he joined The Robot Group in Austin, where he joined a group of robotics enthusiasts and began building robots as a hobby In 2016, he became president of The Robot Group In this role, he leads the group in their mission to promote STEM (science, technology, engineering, and mathematics) education through robotics He is currently working to develop a better understanding of advanced robotics through ROS (the robot operating system) and machine learning   xi

Ngày đăng: 29/11/2019, 09:58

Từ khóa liên quan

Mục lục

  • Table of Contents

  • About the Author

  • About the Technical Reviewer

  • Introduction

  • Chapter 1: Introduction to Robotics

    • Robotics Basics

      • Linux and Robotics

      • Sensors and GPIO

      • Motion and Control

    • Raspberry Pi and Arduino

    • Project Overview

      • The Robot

      • Bill of Materials (BOM)

    • Summary

  • Chapter 2: An Introduction to Raspberry Pi

    • Downloading and Installing Raspbian

      • Raspbian with OpenCV

      • The “Hard” Way

      • The “Easy” Way

    • Connecting Raspberry Pi

    • Configuring Your Pi

      • Using raspi-config

      • Users

        • Securing Root

        • Change the Default Username

      • Connecting to a Wireless Network

    • Going Headless

      • Remote Access

        • Remote Desktop with xrdp

        • SSH with PuTTY

        • Finding Your Device on a Network

    • Summary

  • Chapter 3: A Crash Course in Python

    • Python Overview

    • Downloading and Installing Python

    • Python Tools

      • The Python Shell

      • The Python Editor

      • The Zen of Python

    • Writing and Running a Python Program

      • Hello World

      • Basic Structure

        • Program Parts

        • Indentation

        • Comments

      • Running a Program

    • Programming in Python

      • Variables

      • Data Types

        • Strings

          • String Manipulation

        • Numbers

        • Lists

        • Tuples

        • Dictionaries

        • None Type

      • A Final Note on Variables

      • Control Structures

        • if Statements

        • Loops

          • for Loop

          • while Loop

      • Functions

        • Defining a Function

        • Passing Arguments

        • Default Values

        • Return Values

      • Adding Functionality through Modules

        • Importing and Using Modules

        • Built-in Modules

        • Extended Modules

        • Custom Modules

      • Classes

        • Creating a Class

          • Planning

            • Initial Sample Robot Class

            • Initial Sample Robot Class

      • Styling

        • Blank Lines

        • Commenting

        • Naming Conventions

    • Summary

  • Chapter 4: Raspberry Pi GPIO

    • Raspberry Pi GPIO

      • Pin Numbering

      • Connecting to the Raspberry Pi

      • Limitations of Raspberry Pi’s GPIO

      • Accessing GPIO with Python

      • Simple Output: LED Example

        • Hooking Up the Circuit

        • Writing the Code

        • Pulse Width Modulation (PWM)

          • Hooking Up the Circuit

          • Writing the Code

      • Simple Input

        • Push-button Example

          • Hooking up the Circuit

          • Writing the Code

        • Sonic Rangefinder Example

          • Hooking Up the Circuit

          • Writing the Code

    • Summary

  • Chapter 5: Raspberry Pi and Arduino

    • Raspberry Pi’s GPIO in Review

      • Real-Time or Near Real-Time Processing

      • Analog Input

      • Analog Output

    • Arduino to the Rescue

    • Using Arduino

      • Installing the Arduino IDE

      • Connecting an Arduino

      • Programming Arduino

        • The Arduino IDE

          • Icons and the Menu

          • Creating a New Sketch

          • Saving a Sketch

          • Opening Exiting Sketches

          • Board and Port Selection

          • Cheating with Examples

          • Using Tabs and Multiple Files

      • Sketches

        • Hello Arduino

        • Anatomy of a Sketch

    • A Brief Introduction to the Arduino Language

      • Including Other Files

      • Variables and Data Types

        • Characters and Strings

          • The String Object

        • Numbers

        • Arrays

      • Control Structures

        • if and else

        • while Loops

        • for Loops

        • Functions

      • Working with Pins

        • Digital Operations

        • Analog Input

        • Analog Output (PWM)

        • Pulsing LED

          • The Circuit

          • The Code

      • Objects and Classes

      • Serial

        • Connecting to Serial

        • Sending Serial Data

        • Receiving Serial Data

      • Arduino to Pi and Back Again

        • Installing PySerial

        • Sending Data to the Raspberry Pi

        • Sending Data to the Arduino

    • Pinguino

      • Setting up the Circuit

        • The Code

          • Arduino

          • Raspberry Pi

    • Summary

  • Chapter 6: Driving Motors

    • Motors & Drivers

      • Types of Motors

        • DC Motors

        • Brushless Motors

        • Stepper Motors

          • Servos

      • Motor Properties

        • Voltage

        • Amperage

          • Motors and Amps

      • Motor Drivers

    • Working with Motor Controllers

      • Adafruit DC & Stepper Motor HAT

        • Some Assembly Required

        • Hooking up the Motor Controller

        • Using the Motor HAT

          • Installing the Library

          • The Code

            • Turning a Single Motor

            • Turning Two Motors

      • L298N Generic Motor Driver

        • H-bridge Motor Controller

        • Using the L298N

          • Arduino Code

          • Hooking up the L298N

          • Turning One Motor

          • Turning Two Motors

    • Summary

  • Chapter 7: Assembling the Robot

    • Assembling the Chassis

      • Choosing a Material

      • The Whippersnapper

    • Mounting the Electronics

    • Wiring

    • Mounting Sensors

    • The Finished Robot

      • Making the Robot Mobile

        • The Plan

          • Sensors

          • Motors

          • Behavior

        • The Code

          • Arduino Code

          • Raspberry Pi Code

    • Summary

  • Chapter 8: Working with Infrared Sensors

    • Infrared Sensors

      • Types of IR Sensors

        • Reflectance Sensors

          • Line and Edge Detection

          • Rangefinders

        • Interrupt Sensors

        • PIR Motion Detectors

    • Working with IR Sensors

      • Connecting an IR Sensor

      • Mounting the IR Sensors

      • The Code

        • Arduino Code

        • Python Code

    • Understanding PID Control

      • Control Loops

        • Proportional, Integral, and Derivative Control

          • Proportional Control

          • Integral Control

          • Derivative Control

        • Bringing Them Together

      • Implementing the PID Controller

        • The Code

          • Raspberry Pi Code

    • Summary

  • Chapter 9: An Introduction to OpenCV

    • Computer Vision

      • OpenCV

        • Installing OpenCV

      • Selecting a Camera

      • Installing the Camera

    • OpenCV Basics

      • Working with Images

        • Displaying an Image

      • Capturing Images

        • Opening an Image File

        • Capturing Video

          • Viewing the Camera

          • Recording Video

          • Reading Video from a File

      • Image Transformations

        • Flipping

        • Resizing

      • Working with Color

        • Color Spaces

        • Color Filters

      • Blobs and Blob Detection

        • Finding a Blob

          • The Parameters

          • The Filters

            • filterByColor

            • filterByArea

            • filterByCircularity

            • filterByInertia

            • filterByConvexity

        • Blob Tracking

    • Ball-Chasing Bot

    • Summary

  • Chapter 10: Conclusion

    • Types of Robotics

    • Tools

      • Software

        • Choosing an IDE

        • Design Software

          • 2D Drawing

          • Circuit Board Design

          • 3D Design

      • Hardware

        • Basic Tools

        • Specialty Tools

        • Makerspaces

    • Summary

  • Index

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan