Network-controlled webcam

With this camera began the obsession with the idea of controlling real objects through the internet. The implementation is extremely simple, but for the sake of history let this article be...

 

When I first saw a broadcast video from a controllable webcam on the 13th floor of the Moscow State University, I was very impressed. So much so, that I wanted to arrange something similar. I don't have spare money to buy cameras like AXIS 214PTZ, so I had to leave the idea for some time. Until I found out about Arduino and the fact that it is possible to attach a "network card" and motors to it. The required accessories were quickly purchased and I got hold of all the components necessary to build of a full-fledged network controlled webcam.

A research of the internets revealed that the idea to attach a camera to the servos is the first one to enlighten all the happy owners of an Arduino and some servos. However, none of the already implemented control schemes (keyboard, PS/2 mouse, from a managed router, from a computer, from a web-page on Arduino's buit-in server) impressed me, as they're all inconvenient, if you ask me. I wanted something universal, so that I could upload a web interface to a site and then control through it.

So I came up with this:

 

 

A regular Logitech Pro 9000 webcam (won long ago in "Computerra") is attached to the arm of servo #1 (axis Y). Servo #1, in turn, is attached to the arm of servo #2 (axis X). And servo #2 is pretty tightly screwed to the base - a heavy wooden bar. The servos are the simplest and cheapest SRM-102 (about €10 a piece).

That's it, that's the whole design, as simple as that. For connectors I used strips from a children's construction set (You know those old Soviet metal Meccano-style sets? They still make those).

The controller of this infernal contraption is made from an Arduino (or rather, Freeduino 2009) bundled with an Ethernet Shield v2. No big deal either. An approximate control scheme came to mind. The script in the web interface receives commands (button presses, mouse movents, etc.) and writes them to the database in a specific format. The Arduino cyclically sends GET requests to the script on the server and the script returns all the commands from the database, then the db is cleared. It could probably be done cleaner, but I'm quite happy with it in this form for now.

MySQL is running on the server, with a HEAP base (to make things faster). The structure is as follows:

CREATE TABLE `servo` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`cam_id` tinyint(4) NOT NULL,
`action` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `cam_id` (`cam_id`)
) ENGINE=MEMORY DEFAULT CHARSET=cp1251 AUTO_INCREMENT=1 ;

Next are the scripts. The first, most important one, is a sketch for the Arduino itself. The sketch is not very polished, so it can definitely be improved. But since it is stable, I'm fine with it for now. I won't explain how to upload the sketch, since you already know it if you know Arduino. I use IDE Arduino 18, so the sketch is written for it. It probably won't run under the thew version of the IDE!

File: servowebclient.pde

After uploading the sketch into the Arduino and connecting the netwok cable, you're half-way there. Arduino is online, querying the required script, waiting to analyze the received commands. But the commands have to come from somewhere. Here enter the following scripts.

index.php - the main page with te web interface for controlling the camera.

set.php - is asynchronously called from index.php and writes into the DB the required commands for moving the camera

get.php - the script is called remotely by Arduino, reads the commands from the DB and prints them out as plain text

These files, along with all the structure of the web interface folder can be found here, archived: serva.rar

The libraries used for Arduino are here - libs.rar

Basically, that's it. Hopefully, now, all those who used to call me Megabrain will relax and say: "an idiot could do that" :-) Of course, there is still work to do here. There's need for some kind of authorization, so that mere passers by don't get to control the camera. I am purposely uploading simple control details as they are, to demonstrate the basic features. The control logic can be easily programmed in JavaScript in the main file - index.php. Since command placing is done by the MakeAction function, anything can be done with it.

For example, in the web interface there are links like "Fixed points - Balcony". This is a common call of that very MakeAction (11, coordinate_x, coordinate_y), i.e. where the camera should be pointed at. This way you can program any movement. But you shouldn't forget that in this way you can fill up the command cue with junk data. For example, I had a "left-right" JavaScript function that changed the X coordinate in a cycle resulting in the camera cyclically turning back and forth. And if someone had this function enabled in their browser, it was impossible to stop it until it was turned off by the user who had turned it on. A temporary solution was to set up a timer to load another file through jQuery that would force-kill the first timer. It's ugly, but it worked.

Anyway, I just wanted to tell you about a rather nice way to make a fairly good controlable webcam and show you how it works. The rest is up to your own personal imagination, for in the same way you can control anything you want any way you want.

Now, a word about the testing process, because I just have to tell you about it. I first turned this camera on a couple of days ago. I launched a broadcast, posted a link to the web interface in my blog. A dozen of people did some test driving on it fpr a while and went away. The next day I brought the camera to work, put in the room where our girls are sitting. The camera was turning around almost constantly. A girl goes through the room, and the camera follows her almost instantly, people were queing :-) Besides, in our office lives a parrot, Pitrovich, who extremely disliked the camera at first and constantly attacked it. By the way, this bastard nibbled the power cable from the laptop and the wires to the servos. Many thanks to the girls - they bravely endured all day under the relentless eye of the camera. And constantly attracted new testers :-)

I kept trying to post a description of the camera at Habrahabr, but due to a small, but very positive, karma I could not. No way. Desperate, I posted a question in the Q&A. Which resulted in my karma getting bumped up far more than needed to post the topic. Thanks to the guys for that, once again. The people of Habr liked very much (judging by the comments) my implementation of control, so the camera, to this moment, has been moving almost continuously for nine hours (based on stats from the smotri.com broadcast). The servos are cold, not even warm. The Arduino is a bit lukewarm. During all this time there was only one glitch - the Arduino lost connectivity. As it turned out, I had an interruption. There were other glitches when launching a very long playback of learned movements (somebody turned on learning mode, kept moving the mouse for a long time, and then pressed Play), so I limited this mode to a hundred commands. Of course, this restriction can be easily overridden :-)

Tomorrow I have another day at work, I will switch the camera off for the night, but I will turn it back on again in the morning at work. It will be even more fun there. The first design (on a wooden block) turned out to be not very stable - people managed to turn it over several times by abruptly turning the camera from side to side. So there is a new modification - servo number 2 is bolted to a small wooden wedge, in which a hole is drilled, fitting the diameter of the screw on a standard tripod mounting point. So the wedge is bolted on to a regular photo tripod. This design has turned out to be megastable - people has been persistently trying to overturn it for 7 hours now, but to no avail. In addition, next to the main camera I placed another one (exact same Logitech model, but fixed), through which yoou can see how the first one is moving. Very fascinating sight to be honest.

 

The camera's reaction turned out to be surprisingly quick - when controlling from the web interface, the delay is (at least for me) a fraction of a second. Although I am at home and the server is at work, and between us is a regular Corbina/Beeline internet connection. The live video streaming is a bit jerky on smotri.com, but this is not always the case. Here are some photos of the camera and a few videos, so you can have a better idea of the whole design.

 

 

 

 

 

Here is the controller sandwich itself. Made from a Freeduino and an Ethernet Shield.

And finally,

God. please give my server strength to sustain the Habraeffect, when the topic will get published. And don't let the camera die at the peak of its glory.

P.S. Checked the system working in Opera 10.63 and fresh Chrome. Other browsers should theoretically be able to cope as well.

P.P.S. I would like to take this opportunity to say hello to Habrahabr user SysCat, who once gave me my first Arduino with an e-shield, thus encouraging me to engage in a bunch of interesting research.

UPD> Forgot the most important part - a working copy of the web interface can be found here http://www.g0l.ru/test/serva/ Please show some understanding - the camera and the servos are not elastic, so do not jerk it if it's already moving. I will leave it on until morning, then I'll take it to work and turn it back on there.

UPD> I made web interface control for a gamepad. You need a piece of software like Joy2Key to map gamepad buttons to keyboard keys. If you map up-left-down-right to W A S D on the keyboard, then you can control from a gamepad. Regular keyboard keys work too.

P.S. Thi article was written purposefully to be published on Habrahabr, where it still can be found.

P.P.S. A saved copy of the Habr post with all comments is here.

Some example videos:

By the way, my daughter loves this toy. Although, she doesn't care whether it has a camera or something else. The important part is that it moves.