Reader small image

You're reading from  Learn Robotics Programming - Second Edition

Product typeBook
Published inFeb 2021
PublisherPackt
ISBN-139781839218804
Edition2nd Edition
Concepts
Right arrow
Author (1)
Danny Staple
Danny Staple
author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple

Right arrow

Chapter 10: Using Python to Control Servo Motors

Servo motors can make precise and repeatable motions. Motion such as this is vital for moving sensors, controlling robot legs or arms, and moving them to a known position. Its uses are so engineers can predict robot behavior, so a robot can repeat things in automation, or so code can move limbs to accurately respond to what their sensors are instructing them. Raspberry Pi or add-on boards can control them. In this chapter, we will use these motors to build a pan and tilt mechanism—a head to position a sensor.

In this chapter, we will cover the following topics:

  • What are servo motors?
  • Positioning a servo motor with the Raspberry Pi
  • Adding a pan and tilt mechanism
  • Creating pan and tilt code

Technical requirements

For this chapter, you require the following:

  • The robot with the Raspberry Pi built in the previous chapters
  • Screwdrivers—small Phillips
  • Small pliers or a set of miniature spanners
  • Nylon bolts and standoffs kit—2.5 mm
  • A two-axis mini pan-tilt micro servo kit
  • Two micro SG90/9g or MG90s servo motors, with their hardware and servo horns. The pan-tilt kit may already include the following:
  • Cutting pliers or side cutters
  • Safety goggles

The code for this chapter is available at https://github.com/PacktPublishing/Learn-Robotics-Programming-Second-Edition/blob/master/chapter10/.

Check out the following video to see the Code in Action: https://bit.ly/2LKh92g.

What are servo motors?

Servo motors, or servomechanism motors, are used to position robotic appendages such as arms, grippers, legs, and sensor mounts. They create other movements where the position is the main factor, unlike the wheel motors (DC motors), where speed is the controlling factor. Servo motors are used where (to some level of accuracy) turning to a specific place might be required. You can use code to control these precise positioning movements or a sequence of them:

Figure 10.1 – A small selection of servo motors

Servos come in many sizes, from the very small at around 20-30 mm (shown in Figure 10.1) to those large enough to move heavy machinery. Figure 10.2 shows some of the miniature hobby servos I use for my robots:

Figure 10.2 – A small selection of servo motors in robots

Now that you've seen where you might use servo motors, we can dive deeper and find out how a servo motor works.

Looking...

Positioning a servo motor with the Raspberry Pi

To position a servo, we need to set up a servo horn to see it move, and then plug it into the motor controller board. A servo horn is a small collar with one or more arms, usually used to connect the servo spindle/axle to a mechanism they move. Figure 10.5 shows how to attach a horn to a servo:

Figure 10.5 – Fitting a servo horn

The images in Figure 10.5 show how to fit a servo horn. Perform the following steps:

  1. Servo motors usually come with small bags of hardware, containing a few different horn types and screws to attach them to the servo and the parts you want them to move.
  2. Use the very short small screws for this, as the longer screws can break the servo.
  3. Screw a one-armed servo horn into the servo. The long collar of the horn fits over the servo's output spindle.
  4. The servo should now look like this. Don't over-tighten the collar screw, as you may need to loosen it...

Adding a pan and tilt mechanism

We are now going to build and add a pan and tilt servo mechanism to our robot. This mechanism is like a head for our robot to mount sensors on it. A pan and tilt mechanism, shown in Figure 10.8, moves a sensor (or anything else) through two axes under servo motor control.

Pan means to turn left or right. Tilt means to tilt up or down:

Figure 10.8 – A pan and tilt mechanism from a typical kit

Kits like the one in Figure 10.8 are available from Mouser, along with Adafruit outlets. You may need to purchase the servo motors separately. There are other types of pan-tilt. Ensure it is the type that uses two servos and refer to the manufacturer's documentation where it is different. We build the kit, mount it onto our robot, and plug it into the controller.

Our robot block diagram with the servos looks like Figure 10.9:

Figure 10.9 – Block diagram of the robot with servo motors...

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...

Building a scanning sonar

Using the distance sensor we attached in Chapter 8, Programming Distance Sensors with Python, with the pan and tilt mechanism allows us to set up an interesting experiment. If we attach the distance sensor to the head, and then slowly sweep in a direction (for example, the pan direction), we can create a sensor sweep of an area. We can then use some Python code to plot this, making a small map of things in front of the robot.

A sensor similar to this combination is found in advanced robots (like those from Boston Dynamics) and autonomous cars. LIDAR and RADAR sensors use laser light or radio frequencies with a fast spinning drum to perform the same kind of sweeps far faster than our example. LIDAR sensors are starting to appear on the hobbyist market, but are still a little costly.

To visualize this, we are going to use a special kind of chart – a polar plot. This plots around a circle, with the x-axis being where we are around a circle (in radians...

Summary

In this chapter, you have learned about servo motors, how to control them with your motor controller, and how they work. You've built a pan and tilt mechanism with them and added code to the Robot object to work with that mechanism. Finally, you've demonstrated all of the parts with the circling behavior.

You will be able to use the code you've seen to control other servo motor systems, such as robot arms. The animation style techniques can be useful for smooth movement and circular motions. I used a system a little like this when controlling the 18 motors in SpiderBot's legs.

You've seen how to use a servo with a sensor on a head to make some kind of map of the world and related it to the LIDAR systems used on bigger and more expensive robots.

In the next chapter, we will look at another way to map and observe the world with encoders. These sensors will detect wheels turning on our robot to determine how our robot is moving.

Exercises

  1. Consider how you might build other servo-based extensions. A robot arm needs at least four servos, but a simple gripper/grabber can use the two additional channels our robot has left.
  2. Look around at kits for a gripper, a design with a pincer, and perhaps an up/down control.
  3. How would you write the code for this gripper? What would you add to the Robot object?
  4. What demo behavior would you make for this gripper?
  5. A gripper might move too violently when just given a different position to be in. How would you make a slower smooth movement?

Further reading

Please refer to the following links for more information:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Robotics Programming - Second Edition
Published in: Feb 2021Publisher: PacktISBN-13: 9781839218804
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.
undefined
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

Author (1)

author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple