Friday, April 18, 2014

pan / tilt unit for pi

This can be done with different servos, etc, from scratch, but this is a nice unit to have as a starting point.

lifted from the following blog entry:

http://blog.dawnrobotics.co.uk/2013/10/using-the-dagu-pantilt-kit-with-the-raspberry-pi/

Using the Dagu Pan/Tilt Kit with the Raspberry Pi

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

Assembly

In 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.
Circuit Diagram
Circuit Diagram – Made using Fritzing
 
External Servo Power
External Servo Power – Diagram made using Fritzing
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 Motors

Unlike 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 Python

Once 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 RPIO
To use the script run
sudo python py_pan_tilt_control.py
The 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 animation
The script is a bit too long to explain in detail, but the important bit occurs on lines 144 to 151
?
142
143
144
145
146
147
148
149
150
151
#-----------------------------------------------------------
# Create ServoPWM instances to control the servos
panServoPWM = ServoPWM( PAN_PWM_PIN,
    minAnglePulseWidthPair=( 45.0, 1850 ),
    midAnglePulseWidthPair=( 90.0, 1400 ),
    maxAnglePulseWidthPair=( 135.0, 1000.0 ) )
tiltServoPWM = ServoPWM( TILT_PWM_PIN,
    minAnglePulseWidthPair=( 45.0, 1850 ),
    midAnglePulseWidthPair=( 90.0, 1500 ),
    maxAnglePulseWidthPair=( 180.0, 500.0 ) )
This allows you to specify 3 ( angle, pulse width ) pairs for each servo, that tell the script which pulse width corresponds to each angle. The script can then work out the pulse widths to use for other angles by using linear interpolation. The precise pulse widths will vary from servo to servo, so you should experiment with different pulse widths to get your servos turning accurately, and then adjust the ( angle, pulse width ) pairs accordingly.
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.
Assembled Circuit
Assembled Circuit

Taking Things Further

Pan/Tilt Kit with Camera
With the pan/tilt kit up and running, you can now mount sensors on it, and give them a much wider field of view than they would have if they were just mounted statically. The obvious sensor to mount for the Raspberry Pi is the camera, and we’ll hopefully have a look at using the camera with the pan/tilt kit in a future post.


1 comment:

  1. Hi. I really enjoyed my brief visit on your site and I’ll be sure to be back for more.
    Can you please consider placing my website on your link list?

    Please email me back.

    Thanks!
    Kevin
    kevincollins1012 gmail.com

    ReplyDelete