Learn Robotics Programming - Second Edition

Book description

Develop an extendable smart robot capable of performing a complex series of actions with Python and Raspberry Pi

Key Features

  • Get up to speed with the fundamentals of robotic programming and build intelligent robots
  • Learn how to program a voice agent to control and interact with your robot's behavior
  • Enable your robot to see its environment and avoid barriers using sensors

Book Description

We live in an age where the most complex or repetitive tasks are automated. Smart robots have the potential to revolutionize how we perform all kinds of tasks with high accuracy and efficiency. With this second edition of Learn Robotics Programming, you'll see how a combination of the Raspberry Pi and Python can be a great starting point for robot programming.

The book starts by introducing you to the basic structure of a robot and shows you how to design, build, and program it. As you make your way through the book, you'll add different outputs and sensors, learn robot building skills, and write code to add autonomous behavior using sensors and a camera. You'll also be able to upgrade your robot with Wi-Fi connectivity to control it using a smartphone. Finally, you'll understand how you can apply the skills that you've learned to visualize, lay out, build, and code your future robot building projects.

By the end of this book, you'll have built an interesting robot that can perform basic artificial intelligence operations and be well versed in programming robots and creating complex robotics projects using what you've learned.

What you will learn

  • Leverage the features of the Raspberry Pi OS
  • Discover how to configure a Raspberry Pi to build an AI-enabled robot
  • Interface motors and sensors with a Raspberry Pi
  • Code your robot to develop engaging and intelligent robot behavior
  • Explore AI behavior such as speech recognition and visual processing
  • Find out how you can control AI robots with a mobile phone over Wi-Fi
  • Understand how to choose the right parts and assemble your robot

Who this book is for

This second edition of Learn Robotics Programming is for programmers, developers, and robotics enthusiasts who want to develop a fully functional robot and leverage AI to build interactive robots. Basic knowledge of the Python programming language will help you understand the concepts covered in this robot programming book more effectively.

Publisher resources

Download Example Code

Table of contents

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

Product information

  • Title: Learn Robotics Programming - Second Edition
  • Author(s): Danny Staple
  • Release date: February 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781839218804