Share E-Book

Learn Robotics Programming Build and control cutting-edge AI robots with Raspberry Pi and Python (Danny Staple) (z-library.sk, 1lib.sk, z-lib.sk)

Author

Rating No ratings yet

Log in to rate

Programming
Language English

Build smart, AI-powered robots with Raspberry Pi and Python through hands-on projects, from rovers to intelligent autonomous systems Key Features • Boost your Python skills with practical examples to program complex robot behaviors and functionalities • Build a robotics learning and exploration platform • Leverage AI neural network models for voice recognition and synthesis, and OpenCV for computer vision Learn Robotics Programming, Third Edition, addresses a common challenge for developers, hobbyists, and newcomers: turning robotics ideas into building and programming real, functional robots. While many resources focus either on theory or isolated components, it can be difficult to bring together hardware, software, and intelligent behavior into a working robotic system. This book provides a structured, hands-on path to designing and building robots using Raspberry Pi and Python. You’ll start by assembling a mobile robot and setting up its core systems, then progressively add capabilities such as motor control, sensor integration, and remote operation through web interfaces. As you advance, you’ll implement vision and voice features using OpenCV and ML voice models and explore intelligent behaviors, including localisation and sensor fusion, to help your robot navigate and respond to its environment. By the end of the book, you’ll have built a fully functional robot and developed the skills to design, program, and extend your own robotic systems. Whether you are getting started or already have programming experience, you’ll learn how to combine hardware and software into cohesive solutions and apply practical techniques for creating responsive, intelligent robots. Who this book is for This robotics book is ideal for programmers, developers, and robotics enthusiasts with at least beginner coding skills who are eager to design, build, and program cutting-edge robots using autonomous robot algorithms along with AI models…

Format PDF
Size 12.9 MB
10
Views
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
Learn Robotics Program m ing T h ird E d itio n D a n n y S ta p le EXPERT INSIGHT Build and control cutt ing-edge AI robots with Raspberry Pi and Python Learn Robotics Programming, Third Edition, addresses a common challenge for developers, hobbyists, and newcomers: turning robotics ideas into building and programming real, functional robots. While many resources focus either on theory or isolated components, it can be diff icult to bring together hardware, software, and intelligent behavior into a working robotic system. This book provides a structured, hands-on path to designing and building robots using Raspberry Pi and Python. You’ll start by assembling a mobile robot and sett ing up its core systems, then progressively add capabilities such as motor control, sensor integration, and remote operation through web interfaces. As you advance, you’ll implement vision and voice features using OpenCV and ML voice models and explore intelligent behaviors, including localisation and sensor fusion, to help your robot navigate and respond to its environment. By the end of the book, you’ll have built a fully functional robot and developed the skills to design, program, and extend your own robotic systems. Whether you are gett ing started or already have programming experience, you’ll learn how to combine hardware and software into cohesive solutions and apply practical techniques for creating responsive, intelligent robots. Things you will learn: • Manage your robots with control panels and view their state with interactive dashboards • Integrate sensor systems for dynamic robot navigation and interaction • Learn voice recognition with Piper and Vosk, and build computer vision with OpenCV • Explore encoder-based localisation with the BNO055 module to enhance your robot’s movement accuracy • Automate installation and updates with scripts to keep your robot up to date eff ortlessly • Use practical algorithms to process sensor data to guide robot behavior www.packtpub.com Learn Robotics Programming Third Edition Get a free PDF of this book packtpub.com/unlock/9781803246987 Learn Robotics Programming Third Edition Danny Staple Foreword by Kevin McAleer, Robotics YouTuber
Page 2
Learn Robotics Programming Third Edition Build and control cutting-edge AI robots with Raspberry Pi and Python Danny Staple
Page 3
Learn Robotics Programming Third Edition Copyright © 2026 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. Portfolio Director: Rohit Rajkumar Relationship Lead: Tanisha Mehrotra Program Manager: Sandip Tadge Content Engineer: Shazeen Iqbal, Shreya Sarkar Technical Editor: Tejas Mhasvekar Copy Editor: Safis Editing Indexer: Pratik Shirodkar Proofreader: Shreya Sarkar Production Designer: Ponraj Dhandapani Growth Lead: Namita Velgekar First published: November 2018 Second edition: February 2021 Third edition: May 2026 Production reference: 1190526 Published by Packt Publishing Ltd. Grosvenor House 11 St Paul's Square Birmingham B3 1RB, UK. ISBN 978-1-80324-698-7 www.packtpub.com
Page 4
To my dear wife, Carol, for her love, her inspiration, and her unwavering support. – Danny Staple
Page 5
(This page has no text content)
Page 6
Foreword Robotics sits at the intersection of programming, electronics, and mechanical engineering. For me, it's an endlessly fascinating hobby—there's always more to learn, new skills to develop, and ways to stretch your abilities. At first, it can feel overwhelming to get started. There are foundational concepts you need to grasp, and without a clear starting point, it's easy to feel lost. Learn Robotics Programming is one of the most comprehensive books available for anyone wanting to dive into robotics. It covers the essential basics as well as more advanced topics. Whether you're an absolute beginner or already have some experience with programming or robotics, this book has something to offer, from solid foundations to challenging stretch goals. When I began my robotics journey, this book didn't exist. But it's exactly the kind of guide I wish I'd had. It's a fantastic resource for learning how to build robots using Python. Robotics is becoming an increasingly important area of knowledge, and the tools and resources available today are more accessible than ever before. I hope you enjoy learning robotics as much as I do. You're in excellent hands with Danny Staple, who has a real gift for teaching and will guide you through an exciting and rewarding adventure. I hope you enjoy the book! Kevin McAleer Robotics YouTuber
Page 7
Contributors About the author Danny Staple is a British robot builder and programmer. He has been a professional software engineer since 2000, using Python as part of DevOps and automation consultancy. Danny regularly contributes to open-source projects. Danny has been building robots at home since 2004 and has a workshop full of projects, including robots with wheels, cameras, tank tracks, legs, and arms, made from plastic, cardboard, metal, kits, lunchboxes, and modified toys. Danny authored Robotics at Home with Raspberry Pi Pico, published in 2023 by Packt, and wrote magazine articles for The MagPi. He runs the robotics YouTube channel Orionrobots and brings his robots to events like Pi Wars and Arduino Day. Danny has run robotics clubs since 2005, and currently mentors at a robotics track CoderDojo KU, where he shows kids how to program a fleet of MicroPython robots. In addition to my wife Carol, I would like to thank the Pi Wars and Adafruit communities for answering my tricky questions, my kids for their patience and for checking diagrams for me, and my review team for the great feedback throughout this work.
Page 8
About the reviewers Leo White is a professional software engineer and a graduate of the University of Kent whose interests include electronics, 3D printing, and robotics. He first started programming on the Commodore 64, then later wrote several applications for the Acorn Archimedes, and currently programs set-top boxes for his day job. Utilizing the Raspberry Pi as a foundation, he has mechanized children's toys and driven robot arms, blogging about his experiences and processes along the way, as well as giving presentations at Raspberry Jams and entering a variety of robots into the Pi Wars competition. Doug Blanding is the author of the engineering book, Exact Constraint: Machine Design using Kinematic Principles, and a mechanical design engineer with an interest in robots, CAD, Python, Linux, small computers, and microcontrollers. He has been involved in many optical- mechanical design projects while working at the research labs of Kodak and Corning Incorporated. Keegan Neave is a robot builder and creator of the NE-Five robot. He runs the Neave Engineering company and specializes in open-source robotics. He is a software engineer by trade and loves tinkering with hardware. He is not afraid to build a robot to solve a problem, including one to retrieve a lost spanner from under the floorboards.
Page 9
(This page has no text content)
Page 10
Table of Contents Preface xxvii Free benefits with your book ........................................................................... xxxiv Part 1: The Basics – Preparing for Robotics 1 Chapter 1: Introduction to Robotics 3 What does the word robot mean? .......................................................................... 4 Exploring real-world robots .................................................................................. 6 Mars rovers • 8 Robots in industry ................................................................................................ 9 Robot arms • 9 Warehouse robots • 10 Robots in the home • 11 The robot vacuum cleaner • 12 Discovering competitive, educational, and hobby robots • 14 Looking at what's inside a robot .......................................................................... 17 Hobbyist robot parts • 18 Exploring different types of robot components .................................................... 20 Types of actuators • 20 Types of motors • 21 Status indicators – displays, lights, and sounds • 24 Types of sensors • 24 Summary ........................................................................................................... 26 Challenges ......................................................................................................... 27 Further reading .................................................................................................. 27 Chapter 2: Exploring Robot Building Blocks – Code and Electronics 29 Technical requirements ...................................................................................... 30
Page 11
Planning components and code structure ............................................................ 30 Planning the physical robot ................................................................................ 33 Choosing a robot chassis kit ................................................................................ 36 Size • 36 Wheel count • 38 Wheels • 40 Motors and encoders • 41 Simplicity • 41 Cost • 42 Recommended chassis for this book • 42 Exploring controllers and I/O ............................................................................. 43 I/O pins • 43 Choosing a robot main controller • 46 Exploring Raspberry Pi's capabilities .................................................................. 48 Speed and power • 48 Connectivity and networking • 48 Choosing a Raspberry Pi • 49 GPIO connections • 50 Power pins • 52 Data buses • 52 General-Purpose I/O • 52 Choosing a main breakout board ......................................................................... 52 Integration level • 52 Raspberry Pi HATs • 53 Pin usage • 53 Size and power handling • 53 Soldering • 54 Connectors • 54 Conclusion • 54 Powering the robot ............................................................................................. 56 Sensors • 59 Test fitting the robot ........................................................................................... 59 Table of Contents x
Page 12
Summary ........................................................................................................... 60 Challenges .......................................................................................................... 61 Further reading ................................................................................................... 61 Chapter 3: Preparing a Headless Raspberry Pi for a Robot 63 Technical requirements ...................................................................................... 63 What is Raspberry Pi OS? .................................................................................... 64 Understanding headless systems ......................................................................... 64 Preparing an SD card with Raspberry Pi OS ......................................................... 67 Setting up Wi-Fi on the Raspberry Pi and enabling SSH ....................................... 69 Finding your Raspberry Pi on the network .......................................................... 74 Testing the setup • 74 Troubleshooting • 75 Using SSH to connect to your Raspberry Pi .......................................................... 76 Troubleshooting • 78 Configuring Raspberry Pi OS ............................................................................... 78 Updating the software on your Raspberry Pi • 78 Shutting down your Raspberry Pi • 79 Summary ........................................................................................................... 80 Challenges ......................................................................................................... 80 Further reading .................................................................................................. 81 Chapter 4: Protecting Robot Code with Git 83 Technical requirements ...................................................................................... 83 Understanding how code can be broken or lost .................................................... 84 SD card data loss and corruption • 84 Changes to the code or configuration • 84 Strategy 1 – Keeping code on a PC and uploading it .............................................. 85 Getting started with PyInfra • 86 Making a file we can reuse to update the code • 88 Troubleshooting • 89 Strategy 2 – Using Git to go back in time .............................................................. 90 xi Table of Contents
Page 13
Adding a different version • 92 Tracking and undoing changes • 93 Strategy 3 – Automating Raspberry Pi setup with a script .................................... 96 Updating packages • 96 Troubleshooting • 97 Setting up base packages • 98 A new robot script • 99 Storing it in Git • 100 Summary .......................................................................................................... 101 Challenges ....................................................................................................... 102 Further reading ................................................................................................ 102 Part 2: Building an Autonomous Robot – Connecting Sensors and Motors to a Raspberry Pi 105 Chapter 5: Building Robot Basics – Wheels, Power, and Wiring 107 Technical requirements .................................................................................... 108 Assembling the base ......................................................................................... 108 Fitting the motor brackets • 110 Putting the wheels and support on • 113 Assembling the rest of the chassis • 114 Fitting the Raspberry Pi ..................................................................................... 115 Connecting the motors to the Raspberry Pi ......................................................... 119 Making connector cables • 119 Wiring the cables in • 124 Connecting independent power ......................................................................... 125 Power on test • 127 The completed robot base .................................................................................. 127 Summary .......................................................................................................... 129 Challenges ........................................................................................................ 130 Further reading ................................................................................................. 130 133 Table of Contents xii
Page 14
Chapter 6: Drive and Turn – Moving Motors with MQTT and Python Technical requirements ..................................................................................... 133 Using Python to test robot motors ...................................................................... 134 Preparing libraries • 134 Test – finding the Inventor HAT Mini • 137 Test – demonstrating that the motors move • 138 Troubleshooting • 140 Understanding how the code works • 141 Steering a robot ................................................................................................. 142 Types of steering • 143 Steerable wheels • 143 Fixed-wheel steering • 145 Other steering systems • 146 Steering the robot we are building • 147 Making an MQTT service for the main breakout ................................................. 149 Why make this service? • 149 What is MQTT? • 150 Installing MQTT with PyInfra • 150 What do we put in the motors service? • 153 Writing MQTT service code in Python • 153 Troubleshooting ............................................................................................... 157 Driving forward with MQTT • 157 Following a path with MQTT ............................................................................. 159 Summary .......................................................................................................... 162 Challenges ........................................................................................................ 162 Further reading ................................................................................................. 163 Chapter 7: Driving the Robot with a Phone and Python 165 Technical requirements ..................................................................................... 166 Choosing a controller for the robot .................................................................... 166 Design and overview • 166 xiii Table of Contents
Page 15
Software architecture • 167 Making services start when the Raspberry Pi starts ............................................ 169 Writing a systemd unit to start the Inventor HAT service • 170 Adding lights to the HAT service • 174 Troubleshooting • 175 Building a phone app ......................................................................................... 175 Getting started with HTML • 175 Styling it like a menu • 178 Serving HTML from the Raspberry Pi • 180 Make a drive manually page • 181 Adding a joystick • 182 Troubleshooting • 186 Connecting the phone app to MQTT .................................................................. 186 Enabling MQTT WebSockets on the robot • 187 Installing an MQTT library in our app • 188 Connecting the manual drive joystick via MQTT • 190 Troubleshooting • 191 Adding more control options to the web app ...................................................... 192 Adding a launcher • 192 Making a control page for the drive path behavior • 194 Adding the power-off page • 195 Summary .......................................................................................................... 197 Challenges ....................................................................................................... 198 Further reading ................................................................................................ 198 Chapter 8: Programming Distance Sensors with Python 201 Technical requirements .................................................................................... 201 Choosing between optical and ultrasonic sensors .............................................. 202 Optical sensors • 203 Ultrasonic sensors • 204 Logic levels and shifting • 205 How many sensors do we need? • 206 Table of Contents xiv
Page 16
Attaching and reading a distance sensor ............................................................ 209 Securing the sensor to the robot • 209 Wiring the distance sensors • 214 Installing Python libraries to communicate with the sensor • 215 Reading a distance sensor • 216 Troubleshooting • 218 Making sensor data available in MQTT .............................................................. 219 System design • 219 Upgrading our MQTT client • 220 Bringing sensor data to MQTT • 220 Troubleshooting • 224 Viewing sensor data in a smartphone app ......................................................... 225 Switching the HTTP server port • 225 Graphing output with Chart.js • 225 Troubleshooting • 233 Fixing some repetition • 233 Adding a distance sensor page to the driving page • 236 Fixing the sensor performance problem • 240 Summary ......................................................................................................... 240 Challenges ........................................................................................................ 241 Further reading ................................................................................................. 241 Chapter 9: Avoiding Obstacles with Distance Sensors 243 Technical requirements .................................................................................... 243 Making a test area for your robot ...................................................................... 244 Choosing a test area layout • 245 Choosing a building material and tools • 247 Designing the parts • 247 Cutting foamboard • 250 Building a wall-avoiding behavior .................................................................... 254 How the fixed-distance approach works • 255 Writing the fixed-distance avoider code • 258 xv Table of Contents
Page 17
Troubleshooting • 261 Controlling the wall avoider • 261 Visualizing wall-avoider data ........................................................................... 262 Making time series line charts • 263 Plotting a behavior's state • 265 Troubleshooting • 268 Using proportional control ............................................................................... 268 Programming the smooth distance avoider • 271 Troubleshooting • 274 Understanding the behavior • 274 Summary .......................................................................................................... 274 Challenges ........................................................................................................ 275 Further reading ................................................................................................. 275 Chapter 10: Using Python to Control Servo Motors 277 Technical requirements .................................................................................... 278 What are servo motors? .................................................................................... 278 Positioning a servo motor with Raspberry Pi ...................................................... 281 The pan-and-tilt mechanism • 281 Connecting the servo motor • 282 Writing code for turning a servo • 283 Smoother movements • 285 Troubleshooting • 286 Looking inside a servo • 287 Sending input positions to a servo motor • 287 Controlling servo motors via MQTT .................................................................. 289 System design • 289 Adding servo motors to the Inventor HAT service • 291 Troubleshooting • 293 Making the Inventor HAT service more robust • 293 Adding a servo control phone page • 294 Physically calibrating your servos • 295 Table of Contents xvi
Page 18
Attaching the pan-and-tilt mechanism to the robot ........................................... 298 Creating servo motor behaviors ........................................................................ 300 Fine-tuning the servo motors • 300 Circling the pan-and-tilt head • 301 Troubleshooting • 303 Summary ......................................................................................................... 304 Challenges ....................................................................................................... 305 Further reading ................................................................................................ 305 Chapter 11: Programming Encoders with Python 307 Technical requirements .................................................................................... 307 Measuring the distance traveled with encoders ................................................. 308 The encoder on your robot • 308 Simple sensor counting with the Inventor HAT Mini • 309 Troubleshooting • 310 Encoding absolute or relative position • 311 Encoding direction and speed • 312 Other types of encoders • 314 Detecting the distance traveled in Python and MQTT ......................................... 315 Adding encoders to the Inventor HAT service • 315 Troubleshooting encoders on MQTT • 318 Turning ticks into wheel revolutions • 318 Where other machines use encoders • 320 Driving a specific distance ................................................................................ 320 Accounting for the wheel diameter • 320 Measuring speeds • 323 Driving a known distance • 324 Making the drive known distance page • 326 Troubleshooting • 327 Chart helper fix • 328 Driving in a straight line ................................................................................... 329 Tracking over time • 329 xvii Table of Contents
Page 19
Closed-loop motor control with a PID • 330 Creating a Python PID controller object • 331 Writing the wheel control service • 332 Troubleshooting • 336 PID tuning first attempt • 337 Adding a configuration store • 340 Troubleshooting the MQTT config store • 343 Using the config store in a behavior • 343 Troubleshooting wheel control with the config store • 345 Putting config controls on the web pages • 345 Troubleshooting the config system • 349 PID tuning take 2 • 349 Using PID control to stop closer • 350 Making a specific turn ...................................................................................... 354 Troubleshooting driving a curve • 362 Summary ......................................................................................................... 363 Exercises .......................................................................................................... 363 Further reading ................................................................................................ 364 Chapter 12: Encoder-Based Localisation with Python 365 Technical requirements .................................................................................... 366 Representing and displaying the robot as poses ................................................. 366 Modeling the pose of a rover • 366 Robot poses in code • 368 Building a pose system • 369 Troubleshooting • 375 Transforming poses • 375 Modeling a test area ......................................................................................... 379 More poses • 384 Moving poses with encoders ............................................................................. 387 Adding movement to the control page • 388 Tracing curves • 390 Table of Contents xviii
Page 20
Troubleshooting • 394 Modeling movement inaccuracy • 395 Localisation debugging tools ............................................................................ 398 Debugging encoders • 398 Tracking a single pose • 400 Refining pose probabilities ............................................................................... 401 Creating a boundary observation model • 401 Resampling the poses • 403 Troubleshooting • 409 Improving the boundary model • 409 Controlling the rate of resampling ..................................................................... 412 Measuring information in the system • 412 Calculating the ESS • 414 Building the ESS into our code • 415 Observing the ESS • 417 Testing the robot with ESS • 419 Summary .......................................................................................................... 419 Challenges ....................................................................................................... 420 Further reading ................................................................................................ 420 Chapter 13: IMU-Based Robot Navigation with Python 423 Technical requirements .................................................................................... 424 Learning about the inertial measurement unit .................................................. 424 Comparing IMU models • 425 Inspecting the BNO055 • 426 Connecting an IMU to a Raspberry Pi robot ....................................................... 427 Attaching the IMU to the robot • 427 Physical placement • 427 Wiring the IMU to the Raspberry Pi • 430 Verifying we are connected to it • 432 Connecting code to the BNO055 module • 433 Testing the gyroscope • 434 xix Table of Contents
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List