lifted from the following blog entry:
The Pan/Tilt kit that we sell, developed by Dagu with the guys over at Let’s Make Robots, is a great way to mount mobile sensors on your robotic projects. In this tutorial we look at how you can control the Pan/Tilt kit with a Raspberry Pi and the Python programming language.
Required Materials and Equipment
- Pan/Tilt Kit
- Raspberry Pi
- Breadboard for making connections
- Breadboard header pins
- Wires with 2.54mm connectors
- A separate power supply for the servo motors (optional)
AssemblyIn order to control the servos of the pan/tilt kit with the Pi, we need to provide the servos with power, and connect their signal lines to GPIO pins on the Pi. Nearly all RC servos have the same wiring layout, although the colours may vary slightly. On the pan/tilt servos, the red (middle) wire is Vcc, the brown wire is Ground and the orange wire is the signal wire.
Connect the servos to the Pi as shown in the circuit diagram below. To begin with, we’d recommend that you get the servos working before you fully assemble the Pan/Tilt kit, as this way you can work out where the centre points of the servos are.
As the the servos in the Dagu pan/tilt kit don’t require much current, we can power them from the 5V line of the Pi. If you’d prefer to power them from another power supply, or if you’re using different servos that require more current, or a higher voltage, then you can modify the circuit, as shown in this image.
Understanding Servo MotorsUnlike standard DC electric motors, which turn continuously at a given speed, servo motors contain control circuitry which is constantly trying to drive the servo motor to a set angle. Most servo motors are given their target angle by sending a Pulse Width Modulated (PWM) signal along their signal wire. A PWM signal consists of a pulse that repeats at a fixed frequency (usually close to 50Hz for a servo motor), the width of the pulse determines the angle that that we want the servo to move to.
Most servos will be centred with a pulse width of approximately 1500μs, and then turn left or right with smaller or bigger pulse widths. The minimum and maximum allowable pulse widths vary from servo to servo but are usually close to 800μs and 2200μs respectively. You should avoid trying to move a servo beyond its minimum and maximum limits, as this can damage the servo.
Controlling the Pan/Tilt Kit with PythonOnce you’ve assembled the circuit, you can control the servos using this Python script. This script requires the RPIO Python library, which can be installed with the following steps
sudo apt-get install python-dev python-pip sudo pip install RPIOTo use the script run
sudo python py_pan_tilt_control.pyThe script will then sit in a loop, waiting for one of 3 commands. A ‘p’ will move the pan servo, ‘t’ will move the tilt servo, and ‘e’ will run an exploration animation, scanning the pan/tilt head back and forth. You can specify either an angle in degrees, or a pulse width in μs for the ‘p’ and ‘t’ commands by putting a number straight after them (no space). The program treats any number less than 500 as an angle in degrees, and all other numbers and a pulse width. As an example
: p90 - Will move the pan servo to 90 degrees : p2000 - Will set the pan servo's pulse width to 2000μs : t50 - Will set the tilt servo to 50 degrees : e - Will the start the exploration animationThe script is a bit too long to explain in detail, but the important bit occurs on lines 144 to 151
Once you have your servo motors turning accurately, you can then finish assembling your pan/tilt kit. This approach takes much less time than hurriedly assembling the pan/tilt kit when you first get it, and then having to take it apart again to get it moving accurately, which was our initial approach.
In order to hold our pan/tilt kit steady as it moved, we mounted the complete circuit onto a bit of foamboard.