Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn Robotics Programming - Second Edition

You're reading from  Learn Robotics Programming - Second Edition

Product type Book
Published in Feb 2021
Publisher Packt
ISBN-13 9781839218804
Pages 602 pages
Edition 2nd Edition
Languages
Concepts
Author (1):
Danny Staple Danny Staple
Profile icon Danny Staple

Table of Contents (25) Chapters

Preface 1. Section 1: The Basics – Preparing for Robotics
2. Chapter 1: Introduction to Robotics 3. Chapter 2: Exploring Robot Building Blocks – Code and Electronics 4. Chapter 3: Exploring the Raspberry Pi 5. Chapter 4: Preparing a Headless Raspberry Pi for a Robot 6. Chapter 5: Backing Up the Code with Git and SD Card Copies 7. Section 2: Building an Autonomous Robot – Connecting Sensors and Motors to a Raspberry Pi
8. Chapter 6: Building Robot Basics – Wheels, Power, and Wiring 9. Chapter 7: Drive and Turn – Moving Motors with Python 10. Chapter 8: Programming Distance Sensors with Python 11. Chapter 9: Programming RGB Strips in Python 12. Chapter 10: Using Python to Control Servo Motors 13. Chapter 11: Programming Encoders with Python 14. Chapter 12: IMU Programming with Python 15. Section 3: Hearing and Seeing – Giving a Robot Intelligent Sensors
16. Chapter 13: Robot Vision – Using a Pi Camera and OpenCV 17. Chapter 14: Line-Following with a Camera in Python 18. Chapter 15: Voice Communication with a Robot Using Mycroft 19. Chapter 16: Diving Deeper with the IMU 20. Chapter 17: Controlling the Robot with a Phone and Python 21. Section 4: Taking Robotics Further
22. Chapter 18: Taking Your Robot Programming Skills Further 23. Chapter 19: Planning Your Next Robot Project – Putting It All Together 24. Other Books You May Enjoy

Creating pan and tilt code

We build our pan and tilt code in layers. We create a Servos class and put the previous calculations into it. We set up our robot class to have an instance of the Servos class, and ways to access the servo to pan and the servo to tilt.

Making the servo object

In this class, we encapsulate (internally manage the details of) converting an angle into a servo movement, and the quirks, such as channel numbers, of our servo board. We make a Servos class in a servos.py file for this:

  1. The servos.py file starts with an import and then goes straight into the constructor (the __init__ function):
    from Raspi_MotorHAT.Raspi_PWM_Servo_Driver import PWM
    class Servos:
      def __init__(self, addr=0x6f, deflect_90_in_ms=0.6):

    Here we have an address for the PWM device. There's a deflection/calibration parameter called deflect_90_in_ms so that it can be overridden with the value obtained while calibrating your servos.

  2. Next, we will add a comment...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}