If there is no money for AXIS, or a managed webcam with your own hands, moderately crooked

    When I first saw the broadcast of video from a managed webcam on the 13th floor of Moscow State University, I was very impressed. So much so that I wanted to arrange something similar. I have no extra money on cameras like AXIS 214PTZ, so I had to leave this idea for a while. Until I found out about the Arduino and the fact that you can attach a “network card” and motors to it. The necessary components were quickly purchased and I had everything for the construction of a full-fledged webcam controlled.

    A study of the Internet showed that the idea of ​​attaching a camera to the servers comes first to all the happy owners of arduins and servos. However, none of the already implemented control schemes (buttons, PS / 2 mouse, from a managed router, from a computer, from a web page on a server built into the arduino) impressed me, because it is inconvenient, IMHO. I wanted something universal so that I could put the web interface on the site and manage it.

    In the end, there was such a thing:


    The usual Logitech Pro 9000 webcam (won a long time ago at Computerra) is attached to the rocker of servos No. 1 (Y axis). Serva No. 1, in turn, is attached to a rocking chair of a servo No. 2 (X axis). And server No. 2 is rather firmly screwed to the base - a heavy wooden block. Servas are the simplest and cheapest SRM-102 (about 400 rubles apiece).

    That's the whole design, everything is elementary. For connections, strips from a children's designer were used (remember, such old Soviet metal designers? They are still on sale).

    The controller of all this disgrace consists of a bunch of Arduino (more precisely, Freeduino 2009) and Ethernet Shield v2. Nothing too complicated.

    An approximate control scheme was invented. The script in the web interface receives commands (button presses, mouse movements, etc.) and writes them to the database in a certain format. Arduina cyclically makes a GET request to the script on the server and the script issues all the commands from the database, after which the database is cleared. Maybe you can do it more beautifully, but so far everything suits me in this form.

    On my server, MySQL is spinning, the HEAP database (to make it faster). The structure is like this:

    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, sketch for sobssno Arduina. The sketch is not particularly licked, so its stopudovo can be improved. But, since it works stably, it suits me so far. I don’t explain how to fill in the sketch, because if you are familiar with Arduino, you already know that. I use the Arduino 18 IDE, so there is a sketch for it. In the new version, the IDE most likely will not start!

    File servowebclient.pde After uploading the

    sketch to Arduino and plugging in the network cable, half the work is done. Arduin is online and polls the desired script, lusting to work out the received commands. But teams must appear from somewhere. The following scripts are used for this.

    index.php - sobssno, a page with a web-based camera control interface.
    set.php - asynchronously called from index.php and writes the necessary commands to move the camera to the
    database get.php - the script is called remotely by Arduina, reads the commands from the database and gives them plain text.

    These files, and in general the whole structure of the web muzzle directory can be taken here in the archive. serva.rar
    Used libraries for Arduins here - libs.rar

    Basically, that's it. I hope that now all those who called me a megamind will relax and say “yes, every fool can do it” :-)

    Of course, there is something to work on. Some kind of authorization is needed so that the camera is not shammed by anyone. I intentionally post, as is to demonstrate the basic capabilities, elementary control details.

    The control logic is perfectly programmed in Javascript in the main index.php file. Since the MakeAction function is responsible for placing commands, you can do anything with it. For example, on a webmord there are links like “Fixed points - Balcony”. This is a normal call to this MakeAction (11, coordinate_x, coordinate_y), i.e. where the camera should be directed. And in this way, any movements can be programmed. But we must not forget that in this way you can mess up the queue of teams at the very reluctance. For example, I had a Javascript function "left_right". Which in the cycle changed the X coordinate, as a result, the camera cyclically drove back and forth. And if someone had this function turned on in the browser, it was impossible to stop it until it was turned off by the one who turned it on. The temporary solution was to set a timer to load another file via jQuery, who forcibly killed that timer. It is ugly, but it worked.

    In general, I just wanted to talk about a pretty nice opportunity to make a good managed webcam and show how it works. Then someone else will have a fantasy, because in the same way you can control anything and anything.

    Now a little about the testing process, because I just have to talk about it.

    The first time I turned on this camera a couple of days ago. I started the broadcast, posted a link to the web interface on my blog. A dozen people chased her a little and dumped her. The next day I dragged the camera at work, put it in the office where our girls are sitting. The camera was spinning almost constantly. A girl walks around the office, and a camera moves synchronously behind her, people didn’t give a pass :-) In addition, we have a parrot Pitrovich in our office, who first wildly disliked the camera and constantly attacked her. By the way, this infection gnawed the power cable from the laptop and wire to the servers. Many thanks to the girls - they bravely endured the whole working day under the tireless eye of the camera. And constantly attracted new testers :-)



    I kept trying to post the description of the camera here on Habré, but because of the small but very positive karma I couldn’t do it at all. Well, no matter how, at least you crack. Desperate, I posted the question in Q&A . As a result, they filled in my karma in full, much more than was required to place the topic. For which, once again, thanks to the guys. The people from Habr really liked (judging by the comments) my control implementation, so the camera has been spinning almost continuously for nine hours right now (taken from the broadcast statistics, see .com). Servas are cold, do not heat nifiga. Arduina is slightly warm. For all this time there was only one glitch - the arduina lost the grid. As it turned out, “I had a cliff” (c) Anton Uralsky

    There were still glitches associated with the included very long playback of trained movements (someone hacked the training, drove the mouse for a long time and then pressed Play), so I limited this mode to a hundred commands. Of course, this restriction is easily bypassed :-)

    Tomorrow I have a working day, at night I turn off the camera, but turn it on again in the morning, at work. There will be even more fun.

    UPD> The first design (on a wooden bar) turned out to be not very stable - we managed to overturn it several times, drastically shaking the camera from side to side. So there was a new modification - servo No. 2 is bolted to a small wooden block. A hole was drilled in the block, just under the diameter of the screw on a standard tripod mount. Well, the bar is riveted to the tripod from a conventional camera. The design turned out to be mega-reliable - people have been stubbornly trying to overturn it for 7 hours, but it doesn’t work out.

    In addition, next to the main camera was placed the second (exactly the same Logitech, but stationary), through which it was possible to observe how the first sausage. Very fascinating sight, to be honest.



    The reaction of the camera turned out to be surprisingly fast - the delay in relation to control in the web-face is (for me, anyway) a fraction of a second. Although I am sitting at home, the server is at work and between us the usual Corbino / Beeline Internet. Video broadcasting on smotri.com slows down, but this is not always the case.

    Well, then a few photos of the camera and a few videos, for clarity of design.











    A few demo videos (the video doesn’t insert something, so I just give the links):
    www.youtube.com/watch?v=diEv3LS1nWg
    www.youtube.com/watch?v=JEpCG5_nl1s
    www.youtube.com/watch?v= t6cCTCtWCqg

    By the way, the daughter loves this toy. True, she does not care if the camera is there or something else. The main thing that moves www.youtube.com/watch?v=dEOh0asfVJ0

    And here, in fact, the controller. Sandwich from Freeduino and Ethernet Shield


    Well, finally,



    God, give my server strength to withstand the Habraeffect when the topic is published. And do not let the camera bend in the very dawn of strength.

    PS I checked the work in Opera 10.63 and in fresh Chrome. Other browsers should theoretically cope.

    PPS Taking this opportunity, I want to say hello to the SysCat habrayuzer , who once gave me my first Arduin with an e-mail, and thereby pushed me to a bunch of interesting research.

    UPD> I forgot the most important thing - a working web interface right here www.g0l.ru/test/serva
    Please treat with understanding - the camera and servos are not rubber, so do not twitch if it is already moving. I’ll leave it on until morning, then I will take it with me to work and turn it on there.

    UPD> Management through the web interface is made, but from the gamepad. You need a Joy2Key type softphone to map gamepad buttons to keyboard buttons. If you assign the buttons up-left-down-right to the keyboard WASD, you can control it from the gamepad. Basic keyboard buttons also work.

    Also popular now: