PTZ with uvcdynctrl for Logitech Orbit Cam

Forum for questions and support relating to the 1.24.x releases only.
Locked
mikerick
Posts: 4
Joined: Mon Mar 08, 2010 10:17 am

PTZ with uvcdynctrl for Logitech Orbit Cam

Post by mikerick »

Hi there,
I'm not good at coding and the script might even hurt your camera. I played with the script an stuff from this post
http://www.zoneminder.com/forums/viewto ... ht=control
I messed around with it and it really looks bad, but it moves my cam. Hardcoded the video-device to my video1.
So here is what it looks like:

Code: Select all

# ==========================================================================
#
# ZoneMinder QuickcamSphere Control Protocol Module, $Date: 2009-02-08
# Copyright (C) 2009  Bokusouchi
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ==========================================================================
#
# This module contains the implementation of the QuickcamSphere camera control
# protocol
#
package ZoneMinder::Control::QuickcamSphere;

use 5.006;
use strict;
use warnings;
use IO::Handle;
use File::Basename;
use File::Find;

#==========================================================================
#
# These are the elements you can edit to suit your installation
#
# ==========================================================================

use constant DBG_ID => "QuickcamSphere"; # Tag that appears in debug to identify source
use constant DBG_LEVEL => 0; # 0 is errors, warnings and info only, > 0 for debug

# ==========================================================================



require ZoneMinder::Base;
require ZoneMinder::Control;

our @ISA = qw(ZoneMinder::Control);

our $VERSION = $ZoneMinder::Base::VERSION;

# ==========================================================================
#
# QuickcamSphere Control Protocol
#
# ==========================================================================

use ZoneMinder::Debug qw(:all);

use Time::HiRes qw( usleep );

use constant SYNC => 0xff;
use constant COMMAND_GAP => 100000; # In ms

my $untainted_device = 'video1';
my $device_temp_file = '/tmp/dynctrl.tmp';

sub new
{
    my $class = shift;
    my $id = shift;
    my $self = ZoneMinder::Control->new( $id );
    bless( $self, $class );
    srand( time() );
    $self->loadMonitor();
    $self->where_am_i();
    return $self;
}

our $AUTOLOAD;

sub AUTOLOAD
{
    my $self = shift;
    my $class = ref($self) || croak( "$self not object" );
    my $name = $AUTOLOAD;
    $name =~ s/.*://;
    if ( exists($self->{$name}) )
    {
        return( $self->{$name} );
    }
    Fatal( "Can't access $name member of object of class $class" );
}


sub open
{
    my $self = shift;

    $self->loadMonitor();

    $self->{state} = 'open';
}

sub close
{
    my $self = shift;
    $self->{state} = 'closed';
}

sub printMsg
{
    my $self = shift;
    my $msg = shift;
    my $msg_len = length($msg);

    Debug( $msg."[".$msg_len."]" );
}


sub moveRelUp
{
    my $self = shift;
    my $params = shift;
    $self->loadMonitor();
    my $tiltstep = $self->getParam( $params, 'tiltstep' );
    Debug( "Step Down" );
    my $pan_min=$self->{Monitor}->{MinPanRange};
    my $pan_max=$self->{Monitor}->{MaxPanRange};
    my $tilt_min=$self->{Monitor}->{MinTiltRange};
    my $tilt_max=$self->{Monitor}->{MaxTiltRange};
    my $pan_cur=$self->{Monitor}->{pan_cur};
    my $tilt_cur=$self->{Monitor}->{tilt_cur};
    my $focus_cur=$self->{Monitor}->{focus_cur};
    my $panstep=0;
     if ($tilt_cur + $tiltstep >  $tilt_max) {
           $tiltstep = $tilt_max - $tilt_cur;
      }
   Debug( "panstep=$panstep tiltstep=$tiltstep ,before untaint" );
my $untaint1="0";
   my $untaint2="0";
   ($untaint1)="$panstep"=~ /^(-?[0-9]+)$/;
   Debug ("untaint1= $untaint1");
   ($untaint2)="$tiltstep"=~ /^(-?[0-9]+)$/;
   Debug ("untaint2= $untaint2");
   $panstep=$untaint1;
   $tiltstep=$untaint2;
   my $untainted_device="video1";
   #($untainted_device) =$self->{Monitor}->{uvcdynctrl_device}=~ /^([\w.</Debug>{Monitor}->{pan_cur}=$pan_cur;
       $tilt_cur=$self->{Monitor}->{tilt_cur}=$tilt_cur;
   my $device_temp_file=$self->{Monitor}->{device_temp_file};
        my $fh=IO::Handle->new();
   Debug( "device_temp_file=$device_temp_file");
   CORE::open($fh,">","$device_temp_file") || die("Cannot open $device_temp_file");
   CORE::print($fh "$pan_cur:$tilt_cur:$focus_cur");
   CORE::close($fh);
        Debug( "Moving to $pan_cur,$tilt_cur \n" );
   $tiltstep = $tiltstep * 64 ;
   $panstep = $panstep  * 64 ;
        Debug( "real_panstep = $panstep real_tiltstep = $tiltstep\n" );
   if ( $tiltstep == 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d", $untainted_device ,"-s","Tilt (relative)" ,"--","$tiltstep") ;
   }
   if ( $panstep == 0 )
   {

      CORE:system("/usr/bin/uvcdynctrl" ,"-d",$untainted_device ,"-s","Pan (relative)" ,"--","$panstep") ;
   }

   if ( $tiltstep != 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d", $untainted_device ,"-s","Tilt (relative)" ,"--","-$tiltstep") ;
      CORE:usleep (400);
        }
   if ( $panstep != 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d",$untainted_device ,"-s","Pan (relative)" ,"--","$panstep") ;
      CORE:usleep (400);
   }
}

sub moveRelDown
{
    my $self = shift;
    my $params = shift;
 $self->loadMonitor();
    my $tiltstep = $self->getParam( $params, 'tiltstep' );
    Debug( "Step Down" );
    my $pan_min=$self->{Monitor}->{MinPanRange};
    my $pan_max=$self->{Monitor}->{MaxPanRange};
    my $tilt_min=$self->{Monitor}->{MinTiltRange};
    my $tilt_max=$self->{Monitor}->{MaxTiltRange};
    my $pan_cur=$self->{Monitor}->{pan_cur};
    my $tilt_cur=$self->{Monitor}->{tilt_cur};
    my $focus_cur=$self->{Monitor}->{focus_cur};
    my $panstep=0;
     if ($tilt_cur + $tiltstep >  $tilt_max) {
           $tiltstep = $tilt_max - $tilt_cur;
      }
   Debug( "panstep=$panstep tiltstep=$tiltstep ,before untaint" );
   my $untaint1="0";
   my $untaint2="0";
   ($untaint1)="$panstep"=~ /^(-?[0-9]+)$/;
   Debug ("untaint1= $untaint1");
   ($untaint2)="$tiltstep"=~ /^(-?[0-9]+)$/;
   Debug ("untaint2= $untaint2");
   $panstep=$untaint1;
   $tiltstep=$untaint2;
   my $untainted_device="video1";
   #($untainted_device) =$self->{Monitor}->{uvcdynctrl_device}=~ /^([\w.</Debug>{Monitor}->{pan_cur}=$pan_cur;
       $tilt_cur=$self->{Monitor}->{tilt_cur}=$tilt_cur;
   my $device_temp_file=$self->{Monitor}->{device_temp_file};
        my $fh=IO::Handle->new();
   Debug( "device_temp_file=$device_temp_file");
   CORE::open($fh,">","$device_temp_file") || die("Cannot open $device_temp_file");
   CORE::print($fh "$pan_cur:$tilt_cur:$focus_cur");
   CORE::close($fh);
        Debug( "Moving to $pan_cur,$tilt_cur \n" );
   $tiltstep = $tiltstep * 64 ;
   $panstep = $panstep  * 64 ;
        Debug( "real_panstep = $panstep real_tiltstep = $tiltstep\n" );
   if ( $tiltstep == 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d", $untainted_device ,"-s","Tilt (relative)" ,"--","$tiltstep") ;
   }
   if ( $panstep == 0 )
   {

      CORE:system("/usr/bin/uvcdynctrl" ,"-d",$untainted_device ,"-s","Pan (relative)" ,"--","$panstep") ;
   }

   if ( $tiltstep != 0 )
 {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d", $untainted_device ,"-s","Tilt (relative)" ,"--","$tiltstep") ;
      CORE:usleep (400);
        }
   if ( $panstep != 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d",$untainted_device ,"-s","Pan (relative)" ,"--","$panstep") ;
      CORE:usleep (400);
   }
}

sub moveRelLeft
{
    my $self = shift;
    my $params = shift;
    $self->loadMonitor();
    my $tiltstep=0;
    my $panstep =  $self->getParam( $params, 'panstep' );
    Debug( "panstep=$panstep" );
    Debug( "Step Left" );
    my $pan_max=$self->{Monitor}->{MaxPanRange};
    Debug("pan_max=$pan_max");
    my $pan_min=$self->{Monitor}->{MinPanRange};
    Debug("pan_min=$pan_min");
    my $tilt_min=$self->{Monitor}->{MinTiltRange};
    my $tilt_max=$self->{Monitor}->{MaxTiltRange};
    my $pan_cur=$self->{Monitor}->{pan_cur};
    my $tilt_cur=$self->{Monitor}->{tilt_cur};
    my $focus_cur=$self->{Monitor}->{focus_cur};
    if ($pan_cur + $panstep >  $pan_max) {
         $panstep = $pan_max - $pan_cur;
       Debug( "Hitting the limite --> recalculating panstep" );
    }
   Debug( "panstep=$panstep tiltstep=$tiltstep ,before untaint" );
   my $untaint1="0";
   my $untaint2="0";
   ($untaint1)="$panstep"=~ /^(-?[0-9]+)$/;
   Debug ("untaint1= $untaint1");
   ($untaint2)="$tiltstep"=~ /^(-?[0-9]+)$/;
   Debug ("untaint2= $untaint2");
   $panstep=$untaint1;
   $tiltstep=$untaint2;
   my $untainted_device="video1";
  # ($untainted_device) =$self->{Monitor}->{uvcdynctrl_device}=~ /^([\w.</Debug>{Monitor}->{pan_cur}=$pan_cur;
       $tilt_cur=$self->{Monitor}->{tilt_cur}=$tilt_cur;
   my $device_temp_file=$self->{Monitor}->{device_temp_file};
        my $fh=IO::Handle->new();
  Debug( "device_temp_file=$device_temp_file");
   CORE::open($fh,">","$device_temp_file") || die("Cannot open $device_temp_file");
   CORE::print($fh "$pan_cur:$tilt_cur:$focus_cur");
   CORE::close($fh);
        Debug( "Moving to $pan_cur,$tilt_cur \n" );
   $tiltstep = $tiltstep * 64 ;
   $panstep = $panstep  * 64 ;
        Debug( "real_panstep = $panstep reial_tiltstep = $tiltstep\n" );
   if ( $tiltstep == 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d", $untainted_device ,"-s","Tilt (relative)" ,"--","$tiltstep") ;
   }
   if ( $panstep == 0 )
   {

      CORE:system("/usr/bin/uvcdynctrl" ,"-d",$untainted_device ,"-s","Pan (relative)" ,"--","$panstep") ;
   }

   if ( $tiltstep != 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d", $untainted_device ,"-s","Tilt (relative)" ,"--","$tiltstep") ;
      CORE:usleep (400);
        }
   if ( $panstep != 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d",$untainted_device ,"-s","Pan (relative)" ,"--","$panstep") ;
      CORE:usleep (400);
        }
}

sub moveRelRight
{
    my $self = shift;
    my $params = shift;
    $self->loadMonitor();
    my $tiltstep=0;
    my $panstep =  $self->getParam( $params, 'panstep' );
    Debug( "panstep=$panstep" );
    Debug( "Step Left" );
    my $pan_max=$self->{Monitor}->{MaxPanRange};
    Debug("pan_max=$pan_max");
    my $pan_min=$self->{Monitor}->{MinPanRange};
    Debug("pan_min=$pan_min");
    my $tilt_min=$self->{Monitor}->{MinTiltRange};
    my $tilt_max=$self->{Monitor}->{MaxTiltRange};
    my $pan_cur=$self->{Monitor}->{pan_cur};
    my $tilt_cur=$self->{Monitor}->{tilt_cur};
 my $focus_cur=$self->{Monitor}->{focus_cur};
    if ($pan_cur + $panstep >  $pan_max) {
         $panstep = $pan_max - $pan_cur;
       Debug( "Hitting the limite --> recalculating panstep" );
    }
   Debug( "panstep=$panstep tiltstep=$tiltstep ,before untaint" );
   my $untaint1="0";
   my $untaint2="0";
   ($untaint1)="$panstep"=~ /^(-?[0-9]+)$/;
   Debug ("untaint1= $untaint1");
   ($untaint2)="$tiltstep"=~ /^(-?[0-9]+)$/;
   Debug ("untaint2= $untaint2");
   $panstep=$untaint1;
   $tiltstep=$untaint2;
   my $untainted_device="video1";
  # ($untainted_device) =$self->{Monitor}->{uvcdynctrl_device}=~ /^([\w.</Debug>{Monitor}->{pan_cur}=$pan_cur;
       $tilt_cur=$self->{Monitor}->{tilt_cur}=$tilt_cur;
   my $device_temp_file=$self->{Monitor}->{device_temp_file};
        my $fh=IO::Handle->new();
   Debug( "device_temp_file=$device_temp_file");
   CORE::open($fh,">","$device_temp_file") || die("Cannot open $device_temp_file");
   CORE::print($fh "$pan_cur:$tilt_cur:$focus_cur");
   CORE::close($fh);
        Debug( "Moving to $pan_cur,$tilt_cur \n" );
   $tiltstep = $tiltstep * 64 ;
 $panstep = $panstep  * 64 ;
        Debug( "real_panstep = $panstep reial_tiltstep = $tiltstep\n" );
   if ( $tiltstep == 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d", $untainted_device ,"-s","Tilt (relative)" ,"--","$tiltstep") ;
   }
   if ( $panstep == 0 )
   {

      CORE:system("/usr/bin/uvcdynctrl" ,"-d",$untainted_device ,"-s","Pan (relative)" ,"--","$panstep") ;
   }

   if ( $tiltstep != 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d", $untainted_device ,"-s","Tilt (relative)" ,"--","$tiltstep") ;
      CORE:usleep (400);
        }
   if ( $panstep != 0 )
   {
      CORE:system("/usr/bin/uvcdynctrl" ,"-d",$untainted_device ,"-s","Pan (relative)" ,"--","-$panstep") ;
      CORE:usleep (400);
        }
}

sub moveRelUpLeft
{
    my $self = shift;
    my $params = shift;
}

sub moveRelUpRight
{
    my $self = shift;
    my $params = shift;
}

sub moveRelDownLeft
{
    my $self = shift;
    my $params = shift;
}

sub moveRelDownRight
{
    my $self = shift;
    my $params = shift;
}

sub focusConNear
{
    my $self = shift;
    my $params = shift;
    Debug( "Focus Near" );
}

sub focusConFar
{
    my $self = shift;
    my $params = shift;
    Debug( "Focus Far" );
}



sub presetClear
{
    my $self = shift;
    my $params = shift;
    my $preset = $self->getParam( $params, 'preset', 1 );
    Debug( "Clear Preset $preset" );
}

sub presetSet
{
    my $self = shift;
    my $params = shift;
    my $preset = $self->getParam( $params, 'preset', 1 );
}

sub presetGoto
{
    my $self = shift;
    my $params = shift;
    my $preset = $self->getParam( $params, 'preset', 1 );
    Debug( "Goto Preset $preset" );
}

sub presetHome
{
    my $self = shift;
    my $params = shift;
#    Debug("device=    $self->{Monitor}->{ControlDevice}");
#    Debug("device=    $self->{Monitor}->{ControlDevice}");
#    Debug("device=    $self->{Monitor}->{ControlDevice}");
#    Debug("device=    $self->{Monitor}->{ControlDevice}");
#    Debug("device=    $self->{Monitor}->{ControlDevice}");
#    Debug("device=    $self->{Monitor}->{ControlDevice}");
#    $self->where_am_i();
#    my @msg = ( 0x80|$self->{Monitor}->{ControlAddress}, 0x01, 0x06, 0x04, SYNC );
#    $self->sendCmd( \@msg );
}


sub where_am_i
{
my $self = shift;
my $params = shift;
# We have a device ... where is it now?
Debug("n ::where_am_i:: ");
$self->loadMonitor();
Debug("device=    $self->{Monitor}->{ControlDevice}");
$self->{Monitor}->{MinPanRange}=-55,
$self->{Monitor}->{MaxPanRange}= 55,
$self->{Monitor}->{MinTiltRange}=-28,
$self->{Monitor}->{MaxTiltRange}= 24,
Debug("MinPanRange=    $self->{Monitor}->{MinPanRange}");
Debug("MaxPanRange=    $self->{Monitor}->{MaxPanRange}");
Debug("MinTiltRange=    $self->{Monitor}->{MinTiltRange}");
Debug("MaxTiltRange=    $self->{Monitor}->{MaxTiltRange}");
my $sym_device_basename=File::Basename::basename($self->{Monitor}->{ControlDevice});
Debug("sym_device_basename= $sym_device_basename");
#my $device_temp_file="/tmp/"."$sym_device_basename".".current";
Debug ( "grrr   device_temp_file= $device_temp_file \n" );
$self->{Monitor}->{device_temp_file}=$device_temp_file;
my $fh=IO::Handle->new();
CORE::open($fh, "$device_temp_file") || Debug ("Could not open $device_temp_file");
my $diamond_handle=$fh;
my ($pan_cur,$tilt_cur,$focus_cur)=CORE::split(/:/,$diamond_handle);
CORE::close($fh);
Debug ( "Currently at $pan_cur,$tilt_cur,$focus_cur" );
$self->{Monitor}->{pan_cur}=$pan_cur;
$self->{Monitor}->{tilt_cur}=$tilt_cur;
$self->{Monitor}->{focus_cur}=$focus_cur;
Debug ( "Currently(monitor object) at $self->{Monitor}->{pan_cur}, $self->{Monitor}->{tilt_cur},$self->{Monitor}->{focus_cur}" );
#first we deduce the real device name
my $real_device="video1";
#$real_device=File::find::readlink("$sym_device");
#$real_device=CORE::readlink("$self->{Monitor}->{ControlDevice}");
Debug ( "real_device= $real_device" );
my $tainted_device=File::Basename::basename($real_device);
Debug ( "tainted_device= $tainted_device" );
$self->{Monitor}->{uvcdynctrl_device}=$tainted_device ;
#my uvcdynctrl_device="glop";
#$uvcdynctrl_device) = $tainted_device=~ /^([\w.<>\/,"=]+)$/;
#Debug ( "uvcdynctrl_device= $uvcdynctrl_device \n" );
#srand( time() );
}




1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

ZoneMinder::Database - Perl extension for blah blah blah

=head1 SYNOPSIS

  use ZoneMinder::Database;
  blah blah blah

=head1 DESCRIPTION

Stub documentation for ZoneMinder, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.

Blah blah blah.

=head2 EXPORT

None by default.



=head1 SEE ALSO

Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.

If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.

=head1 AUTHOR

Bokusouchi

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009  bokusouchi

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.3 or,
at your option, any later version of Perl 5 you may have available.


=cut
Name the script as QuickcamSphere.pm an put it in /usr/share/perl5/ZoneMinder/Control or where your Perl-modules from your Zoneminderinstallation is.

Set the Cam as Controllable (remember to to check OPT_CONTROL in Option/System). click EDIT beside 'Control Type', then 'Add new control'. Name it to QuickcamSphere (although you can name it to whatever you want), Type Local, Protocol QuickcamSphere (this is important, name it exactly like that) Dont check Can Wake, Can Sleep, Can Reset. On the Move Tab check Can Move and Can Move Relative. On the Pan-Tab Check Can Pan, Min Pan Range -7000, Max Pan Range 7000, Min Pan Step 10 and Max Pan Step 10. Other stuff there unchecked. On the Tilt-Tab Check Can Tilt, Min Tilt Range -2500 and Max Tilt Range 2500, Min Tilt Step 10 and Max Tilt Step 10. everything else unchecked. Dont touch the other Tabs. Hit Save, close the Control Capabilities window. On your Monitor click Buffer-Tab (or any other) and back to Control zu select your new Control Type.
Hit Save. Almost done :-)
When vieving the monitor, there is the 'Control' Link. hit that, you get some arrows. Try them, it should work.

AS I SAID. NO WARRANTY. Of course you have to habe libwebcam and uvcdynctrl as described here http://www.quickcamteam.net/software/libwebcam
get this working on the commandline first make sure it works there with e.g.

Code: Select all

uvcdynctrl -d video1 -s "Tilt (relative)" -- 900
Well, i messed up the module, but it works. when you do a

Code: Select all

perl -w /usr/share/perl5/ZoneMinder/Control/QuickcamSphere.pm
there should be no output/error. This happend with the original script (Thanks a lot for that by the way to bokusouchi), and it would not load. In zmcontrol.log you get a

Code: Select all

 zmcontrol[19435].INF [Control server 7/QuickcamSphere.pm starting at 10/03/11 11:31:16]
03/11/10 11:31:36.412291 zmcontrol[19434].FAT [Can't connect: No such file or directory]
then.

Well, i guess thats it. Have fun feel free to ask when having Problems. Thanks to the whole Zomeninder-Team for their great Software.

Maybe someone with Knowledge in Perl can somehow clean up the mess i made in the module :-(

Regards

Michael
mgc8
Posts: 8
Joined: Sat Dec 20, 2008 1:32 am

Works nicely

Post by mgc8 »

Hello,

I had written a similar script for the OrbitMP (which used to work with the PWC driver), however I made an upgrade recently and the new Orbit AF camera evidently has a different interface for control... Thanks to your script, it was easy to get ZM to work with it!

The only annoyance currently is that some features don't work (map, focus), maybe if I get some spare time I'll look into extending the script with that as well... Also, the commands are very slow to process, but that is a uvcdynctrl problem, since the same happens on the command-line -- if anyone has a suggestion to make that happen faster, feel free to enlighten us :)

Thank you,
Mihnea
Locked