Remotely controlled pan/tilt camera unit

 

One of my colleagues at work works remotely, and we make good use of Skype and gchat to communicate with each other. However, when he’s calling in and talking to a big group, it can be hard for him to see everyone at once unless we constantly rotate the laptop around to focus on whoever is talking. Thus I thought it would be neat for him to able to control the movement of a webcam remotely.
I was inspired by seeing this pan/tilt bracket from SparkFun.com. Using an Arduino would make things very easy, since it has the Servo library and built-in serial communication. As for the “remotely controlled” part, I set up an apache webserver on the local machine (apache http server is actually already installed if you’re running OSX, more on that later) that would use a CGI script to send commands to the Arduino via a serial connection.

Parts List

Assembling the pan/tilt unit

Assembly was pretty straightforward, using the instructions on SparkFun’s product page.

 

I used balsa wood (available at hardware stores and art supply stores–that is, pretty much everywhere) and wood glue to provide a mount for the pan servo. This is probably the cheapest and most low-tech approach, but you could of course use any material you like.

 

For mounting the camera, I pulled the camera part from the base and then screwed this on to the top of the pan/tilt bracket. Again, this will depend on your camera. Some cameras, like the Microsoft LifeCam VX-3000 Webcam, have screw-on bases and thus make this part pretty easy.

Arduino connections

I wanted to keep things modular, so to connect the servos to the Arduino I installed 2 3-pin male headers on a PC board. I wired wires going from the servo signal pins to Arduino pins 8 and 9, and from the power pins to Arduino pins GND and +5V. I also threw in an LED in parallel with the power as an extra indicator that the board was plugged in and powered.

Arduino Code

githubblack Code available at Github here: https://github.com/jessicaaustin/robotics-projects/tree/master/pan-tilt-unit

Once you’ve uploaded the following code to your Arduino, you should be able to control the pan/tilt unit via the Serial Monitor.

#include <Servo.h> 
 
Servo pan_servo;
Servo tilt_servo;
int incomingByte;
 
void setup()
{
 // attach the servos and startup the serial connection
 pan_servo.attach(9);
 tilt_servo.attach(8);
 Serial.begin(9600);
 resetAll();
}
 
void loop()
{
 
 // check to see if something was sent via the serial connection
 if (Serial.available() > 0) {
 incomingByte = Serial.read();
 
 // move the servos based on the byte sent
 if (incomingByte == 'e') {
 moveServo(tilt_servo, 1);
 } else if (incomingByte == 'x') {
 moveServo(tilt_servo, -1);
 } else if (incomingByte == 'd') {
 moveServo(pan_servo, 1);
 } else if (incomingByte == 's') {
 moveServo(pan_servo, -1);
 } else if (incomingByte == 'r') {
 resetAll();
 } else if (incomingByte == '/') {
 Serial.println("pan: " + pan_servo.read());
 Serial.println("tilt: " + tilt_servo.read());
 }
 }

}
 
// move the servo a given amount
void moveServo(Servo servo, int delta) {
 int previousValue = servo.read();
 int newValue = previousValue + delta;
 if (newValue > 180 || newValue < 30) {
 return;
 }
 servo.write(newValue);
}
 
// put the servos back to the "default" position
void resetAll() {
 reset(pan_servo);
 reset(tilt_servo);
}
 
// put a servo back to the "default" position (100 deg)
void reset(Servo servo) {
 
 int newPos = 130;
 int previousPos = servo.read();
 if (newPos > previousPos) {
 for (int i=previousPos; i<newPos; i++) {
 servo.write(i);
 delay(15);
 }
 } else {
 for (int i=previousPos; i>newPos; i--) {
 servo.write(i);
 delay(15);
 }
 }
 
}

Controlling the Arduino via a script

I used todbot‘s arduno-serial.c program to control the Arduino via the command line.

Get arduino-serial.c and test it out:


wget http://todbot.com/arduino/host/arduino-serial/arduino-serial.c

gcc -o arduino-serial arduino-serial.c

# test moving the camera up and to the right

./arduino-serial -b 9600 -p /dev/tty.usbmodem3d11 -s dddddddddddddddeeeeeeeeeeeeeee

Running the script via Apache HTTP Server

At this point, someone could ssh in to the computer running the camera and control it via the command line, but I wanted to have a slightly more sophisticated interface. I decided to go with a simple jQuery-powered web page that hits a CGI script served up by apache. (Note: of course, if you want people to be able to control the camera from outside your local network, you’ll need a static IP address for your machine. In that case, you should probably also make sure you set up basic authentication for your apache server.)

If you’re running OSX, apache is already installed. You can start it up by running sudo apachectl start, the config is located at /etc/apache2, and the DocumentRoot points to /Library/WebServer/ by default.

cd /Library/Webserver/CGI-Executables
vim pan-tilt.cgi

pan-tilt.cgi is the following ruby script:

#!/opt/local/bin/ruby

query_string=`echo $QUERY_STRING`

if query_string.length != 0
`/opt/local/bin/arduino-serial -b 9600 -p /dev/tty.usbmodem3d11 -s #{query_string}`
result=`echo $?`
result=result.gsub(/n/,"")
end

print "Content-type: application/jsonnn"
print "{"result": "#{result}"}n"

Finally, create the web page:

cd /Library/Webserver/Documents
vim pan-tilt.html

pan-tilt.html:


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>// <![CDATA[
            // send the input to the cgi script
            // note: sending 3x the input for every keypress, to make the movement smoother
            var submitInput = function(input) {
                $.post("/cgi-bin/pan-tilt.cgi?" + input + input + input);
            };

            // submit input when someone presses a key down
            $(document).keydown(function(event) {
                console.log(event.keyCode);
                switch(event.keyCode) {
                   case 82:
                    submitInput('r');
                    break;
                  case 83:
                    submitInput('s');
                    break;
                  case 68:
                    submitInput('d');
                    break;
                  case 69:
                    submitInput('e');
                    break;
                  case 88:
                    submitInput('x');
                    break;
                  case 37:
                    submitInput('s');
                    break;
                  case 39:
                    submitInput('d');
                    break;
                  case 38:
                    submitInput('e');
                    break;
                  case 40:
                    submitInput('x');
                    break;
                  default:
                }
            });
        
// ]]></script>

&nbsp;
<h1>Pan/Tilt Camera Control</h1>
<div>
<table>
<tbody>
<tr>
<td></td>
<td>&amp;amp;uarr;</td>
<td></td>
</tr>
<tr>
<td>&amp;amp;larr;</td>
<td>r</td>
<td>&amp;amp;rarr;</td>
</tr>
<tr>
<td></td>
<td>&amp;amp;darr;</td>
<td></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td></td>
<td>e</td>
<td></td>
</tr>
<tr>
<td>s</td>
<td>r</td>
<td>d</td>
</tr>
<tr>
<td></td>
<td>x</td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div style="clear: left;">
todo: add security</div>

Testing

At this point you should be able to go http://localhost/pan-tilt.html and control the camera from there. Check out the video below!

5 thoughts on “Remotely controlled pan/tilt camera unit”

  1. Hello, thanks for this enlightening post, I was searching for that… Some comments if you have the time to look at them.. You don’t mention how you prevent the arduino from resetting on DTR when arduino-serial access the port. I use a 27uF between reset and ground, it works for me (arduino nano). Also I have to leave the serial monitor open to have a speedy response when I press the arrows keys on the web page. Lastly the web page does not display correctly, I only get text:

    Pan/Tilt Camera Control
    &uarr;
    &larr; r &rarr;
    &darr;
    e
    s r d
    x
    todo: add security

    but hey, like you often say, it works :-) ! and I will be able to pan and tilt my webcam in Montreal when I’m away for the winter in Florida !
    Thank’s again , jrb

Leave a Reply

Your email address will not be published. Required fields are marked *