0%

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.

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