Learn Robotics Programming

Book description

Gain experience of building a next-generation collaboration robot

Key Features

  • Get up and running with the fundamentals of robotic programming
  • Program a robot using Python and the Raspberry Pi 3
  • Learn to build a smart robot with interactive and AI-enabled behaviors

Book Description

We live in an age where the most difficult human tasks are now automated. Smart and intelligent robots, which will perform different tasks precisely and efficiently, are the requirement of the hour. A combination of Raspberry Pi and Python works perfectly when making these kinds of robots.

Learn Robotics Programming starts by introducing you to the basic structure of a robot, along with how to plan, build, and program it. As you make your way through the book, you will gradually progress to adding different outputs and sensors, learning new building skills, and writing code for interesting behaviors with sensors. You'll also be able to update your robot, and set up web, phone, and Wi-Fi connectivity in order to control it.

By the end of the book, you will have built a clever robot that can perform basic artificial intelligence (AI) operations.

What you will learn

  • Configure a Raspberry Pi for use in a robot
  • Interface motors and sensors with a Raspberry Pi
  • Implement code to make interesting and intelligent robot behaviors
  • Understand the first steps in AI behavior such as speech recognition visual processing
  • Control AI robots using Wi-Fi
  • Plan the budget for requirements of robots while choosing parts

Who this book is for

Learn Robotics Programming is for programmers, developers, and enthusiasts interested in robotics and developing a fully functional robot. No major experience required just some programming knowledge would be sufficient.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Learn Robotics Programming
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Code in Action
      4. Conventions used
    4. Get in touch
      1. Reviews
  7. Introduction to Robotics
    1. What does robot mean?
    2. Advanced and impressive robots
      1. Robots that look like humans and animals
      2. The Mars rovers
    3. Robots in the home
      1. The washing machine
      2. Other household robots
    4. Robots in industry
      1. Robot arms
      2. Warehouse robots
    5. Competitive, educational, and hobby robots
    6. Summary
    7. Questions
    8. Further reading
  8. Exploring Robot Building Blocks - Code and Electronics
    1. Technical requirements
    2. What is inside a robot?
    3. Types of motors, sensors, and actuators
      1. Motors and actuators
      2. Status indicators – displays, lights, and sounds
      3. Types of sensors
    4. Controllers and IO
      1. IO pins
      2. Controllers
    5. Planning components and code structure
    6. Planning our robot
    7. Summary
    8. Questions
    9. Further reading
  9. Introducing the Raspberry Pi - Starting with Raspbian
    1. Technical requirements
    2. What can the Raspberry Pi do?
      1. Speed and power
      2. Connectivity and networking
      3. The recommended Raspberry Pi version
    3. What connections will we use?
      1. Raspberry Pi Hats
    4. What is Raspbian?
    5. Preparing an SD card
      1. Flashing the card in Etcher
        1. Getting Etcher
        2. Using Etcher to write to the card
    6. Summary
    7. Questions
    8. Further reading
  10. Preparing a Raspberry Pi for a Robot - Headless by Default
    1. Technical requirements
    2. What does headless mean and why?
    3. Setting up wireless on the Raspberry Pi and enabling SSH
      1. The wpa_supplicant.conf file
    4. Finding your Pi on the network
      1. Apple macOS
      2. Microsoft Windows
      3. Linux
      4. Testing the setup
      5. What if you cannot reach the Raspberry Pi?
    5. Using PuTTY or SSH to connect to your Raspberry Pi
    6. Configuring Raspbian
      1. Renaming your Pi
      2. Securing your Pi (a little bit)
      3. Reboot and reconnect
      4. Updating the software on your Raspberry Pi
      5. What is sudo?
      6. Shutting down your Raspberry Pi
    7. Summary
    8. Questions
    9. Further reading
  11. Backing Up the Code with Git and SD Card Copies
    1. Technical requirements
    2. How code can be broken or lost
      1. SD card data loss and corruption
      2. Bad changes to the code or configuration
      3. Combinations
    3. Strategy 1 - keep the code on the PC and upload it
    4. Strategy 2 – using Git to go back in time
    5. Strategy 3 – making SD card backups
      1. Windows
      2. Mac and Linux
        1. Linux
        2. Mac
        3. Cloning with DD
    6. Summary
    7. Questions
    8. Further reading
  12. Building Robot Basics - Wheels, Power, and Wiring
    1. Technical requirements
    2. Choosing a chassis kit
      1. Size
      2. Wheel count
      3. Wheels and motors
      4. Simplicity
      5. Cost
      6. Conclusion
    3. Choosing a motor controller
      1. Integration level
      2. Pin usage
      3. Size
      4. Soldering
      5. Connectors
      6. Conclusion
    4. Powering the robot
    5. Test fitting the robot
    6. Assembling the base
      1. Assembling the base, motors, and wheels
        1. Plastic motor brackets
        2. Metal motor brackets
        3. Adding the castor wheel
        4. Putting the wheels on
        5. Bringing the cables up
      2. Fitting the Raspberry Pi
      3. Adding the batteries
        1. The USB power bank
        2. Mounting the AA battery holder
      4. The completed robot base
    7. Summary
    8. Questions
    9. Further reading
  13. Drive and Turn - Moving Motors with Python
    1. Technical requirements
    2. Connecting the motors to the Raspberry Pi
      1. Wiring in
      2. Independent power
    3. Writing code to test your motors
      1. Preparing libraries
      2. Test – finding the motor hat
      3. Test – the motors move
      4. Troubleshooting
      5. How does this code work?
    4. Steering a robot
      1. Types of steering
        1. Steerable wheels
        2. Fixed wheels
        3. Other steering systems
        4. Steering the robot we are building
    5. The robot object – code for our experiments to talk to the robot
      1. Why make this object?
      2. What will we put in the robot object?
    6. Write a script to follow a predetermined path
    7. Summary
    8. Questions
    9. Further reading
  14. Programming Line-Following Sensors Using Python
    1. Technical requirements
    2. Attaching line sensors to the robot and Raspberry Pi
      1. What are optical line sensors?
      2. What other new parts will we use?
      3. Construction plan
      4. Getting the correct length
      5. Mounting sensor modules
      6. Wiring the sensor in
    3. Painting the test track
    4. Writing the code – testing the sensors
      1. Calibrating the sensors
      2. Test code
      3. Troubleshooting
    5. Writing the line-following behavior
      1. Adding the sensors to the Robot object
      2. Creating the line-following behavior code
      3. More troubleshooting
      4. Extra ideas
    6. Summary
    7. Questions
    8. Further reading
  15. Programming RGB Strips in Python
    1. Technical requirements
    2. Comparing light strip technologies
      1. RGB values
    3. Attaching the light strip to the Raspberry Pi
      1. Logic levels
      2. Soldering headers on
      3. Attaching the LED strip to the robot
      4. Wiring the LED strip
      5. Adding a power switch
    4. Making a robot display code object
      1. Adding LEDs to the robot object
      2. Testing the LEDs
      3. Troubleshooting
    5. Using the light strip for debugging the line follower robot
      1. Line follower basic LEDs
      2. Color systems
        1. Hue
        2. Saturation
        3. Value
        4. Converting these
      3. Adding a rainbow
    6. Summary
    7. Questions
    8. Further reading
  16. Using Python to Control Servo Motors
    1. Technical requirements
    2. What are servo motors?
      1. What is in a servo?
      2. Sending input positions to a servo motor
    3. Positioning a servo
      1. Code for turning a servo
      2. Calibrating your servos
    4. Adding a pan and tilt mechanism
      1. Building the kit
      2. Attaching the pan and tilt to the robot
    5. Creating pan and tilt code
      1. Making a servo object
      2. Adding the servo to the robot class
      3. Creating a behavior
      4. Running it
      5. Troubleshooting
    6. Summary
    7. Questions
    8. Further reading
  17. Programming Distance Sensors with Python
    1. Technical requirements
    2. Choosing light versus ultrasonic sensors
      1. Optical sensors
      2. Ultrasonic sensors
      3. Why use two sensors?
    3. Attaching and reading an ultrasonic sensor
      1. Securing the sensors to the robot
      2. Wiring the distance sensors
        1. Voltages and level shifting
        2. Wiring the breadboard
      3. Reading an ultrasonic distance sensor
      4. Troubleshooting
    4. Avoiding walls – a script to avoid obstacles
      1. Making our distance sensor object
      2. Adding the sensor to the robot class
      3. Making the obstacle avoid behaviors
        1. First attempt at obstacle avoidance
        2. More sophisticated object avoidance
    5. Menu modes – choosing your robot's behavior
      1. Managing robot modes
      2. The web service
      3. The template
      4. Running it
    6. Summary
    7. Questions
    8. Further reading
  18. Programming Encoders with Python
    1. Technical requirements
    2. What are encoders?
      1. Use of encoders
      2. Types of encoders
      3. Encoding direction and speed
      4. The encoders we will be using
    3. Attaching encoders to the robot
      1. Lifting up the Raspberry Pi
      2. Preparing the encoders
      3. Wiring the encoders to Pi
    4. Detecting the distance traveled in Python
      1. Simple counting
      2. Adding encoders to the Robot object
        1. Extracting the class
        2. Adding the device to the Robot object
      3. Turning ticks into millimeters
    5. Driving in a straight line
      1. Concepts for correction
      2. Creating a Python PID Control object
      3. Straight line code
      4. Troubleshooting this behavior
    6. Driving a specific distance
      1. Refactoring unit conversions into the EncoderCounter class
      2. Setting the constants
      3. Creating the combined behavior
    7. Making a specific turn
      1. The drive_arc function
      2. Further ideas
    8. Summary
    9. Questions
    10. Further reading
  19. 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 OpenCV
      1. Setting up the Pi Camera software
      2. Getting a picture from the Pi
      3. Installing libraries
      4. Building our first app with these tools
        1. OpenCV camera server app overview
        2. The CameraStream object
        3. Building a template
        4. The image server main app
      5. Visual processing with behaviors
        1. Web app core
        2. Controllable behaviors
        3. The template
        4. Running the image server
    4. Colors, masking, and filtering – chasing colored objects
      1. Getting information about an object
        1. Caveat about red objects
      2. Enhancing the PID controller
      3. The behavior code
        1. The template
        2. The behavior
      4. Running the behavior
        1. Tuning the PID controller settings
      5. Troubleshooting
      6. Enhancements that you could make
    5. Detecting faces with Haar cascades
      1. Finding objects in an image
        1. Integral images
        2. Basic features
      2. Planning our behavior
      3. The code for face tracking
      4. Using this behavior
      5. Troubleshooting
    6. Summary
    7. Questions
    8. Further reading
  20. Voice Communication with a Robot Using Mycroft
    1. Technical requirements
    2. Introducing Mycroft
      1. Speech to text
      2. Wake words
      3. Utterances
      4. Intent
      5. Skills
      6. Dialog
      7. Vocabulary
    3. Adding sound input and output to the Raspberry Pi
      1. Physical installation
      2. Installing Raspbian for the voice assist Pi
      3. Installing the Respeaker software
    4. Installing Mycroft on the Raspberry Pi
      1. Base installation
      2. Getting Mycroft to talk to the sound card
      3. Starting to use Mycroft
      4. Troubleshooting
    5. Programming Mycroft skills for the robot functions
      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
        2. Code
        3. Running with the new intent
    6. Summary
    7. Questions
    8. Further reading
  21. Programming a Gamepad on Raspberry Pi with Python
    1. Technical requirements
    2. When speech control won't work - why we need to drive
    3. Choosing a controller
      1. Design and overview
    4. Preparing the Raspberry Pi for our controller and driving with it
      1. Enhancing the image app core
      2. Writing the behavior
      3. The template (web page)
      4. The stylesheet
      5. Creating the code for the sliders
      6. Running this
    5. Upgrading the menu and displaying code for full headless
      1. Making menu modes compatible with Flask behaviors
      2. Loading video services
      3. Styling the menu
        1. Making the menu template into buttons
    6. Making it start when the Pi starts
      1. Adding lights to the menu server
      2. Using systemd to automatically start the robot
      3. Enhancement Ideas
    7. Summary
    8. Questions
    9. Further reading
  22. 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 into machine Learning
      1. Robot Operating System
    6. Summary
    7. Further reading
  23. 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. Planning the code for the robot
    6. Letting the world know
    7. Summary
  24. Appendix
    1. Finding parts
    2. Converting this code to Python 3
      1. Integer division
      2. Print function
      3. Input/raw input
        1. Python 2 compatibility
      4. Ranges have become generators
    3. OpenCV versions
      1. Find contours
    4. Power smoothing capacitor
  25. Assessments
    1. Chapter 1, Introduction to Robotics
    2. Chapter 2, Exploring Robot Building Blocks - Code and Electronics
    3. Chapter 3, Introducing the Raspberry Pi - Starting with Raspbian
    4. Chapter 4, Preparing a Raspberry Pi for a Robot - Headless by Default
    5. Chapter 5, Backing Up the Code with Git and SD Card Copies
    6. Chapter 6, Building Robot Basics - Wheels, Power, and Wiring
    7. Chapter 7, Drive and Turn - Moving Motors with Python
    8. Chapter 8, Programming Line-Following Sensors Using Python
    9. Chapter 9, Programming RGB Strips in Python
    10. Chapter 10, Using Python to Control Servo Motors
    11. Chapter 11, Programming Distance Sensors with Python
    12. Chapter 12, Programming Encoders with Python
    13. Chapter 13, Robot Vision - Using a Pi Camera and OpenCV
    14. Chapter 14, Voice Communication with a Robot Using Mycroft
    15. Chapter 15, Programming a Gamepad on Raspberry Pi with Python
  26. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learn Robotics Programming
  • Author(s): Danny Staple
  • Release date: November 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789340747