| blib/lib/Device/MiniSSCII.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 9 | 9 | 100.0 |
| branch | n/a | ||
| condition | n/a | ||
| subroutine | 3 | 3 | 100.0 |
| pod | n/a | ||
| total | 12 | 12 | 100.0 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Device::MiniSSCII; | ||||||
| 2 | 1 | 1 | 80143 | use strict; | |||
| 1 | 2 | ||||||
| 1 | 35 | ||||||
| 3 | 1 | 1 | 4 | use Carp; | |||
| 1 | 2 | ||||||
| 1 | 110 | ||||||
| 4 | |||||||
| 5 | our $VERSION = $VERSION = (qw($Revision: 1.2 $))[1]; | ||||||
| 6 | |||||||
| 7 | =head1 NAME | ||||||
| 8 | |||||||
| 9 | Device::MiniSSCII - Perl device driver for the Mini SSC II serial servo controller | ||||||
| 10 | |||||||
| 11 | =head1 SYNOPSIS | ||||||
| 12 | |||||||
| 13 | my $ssc = Device::MiniSSCII->new( | ||||||
| 14 | device => '/dev/ttyS0', | ||||||
| 15 | baudrate => 9600 | ||||||
| 16 | ); | ||||||
| 17 | $ssc->move( 0, 100 ); | ||||||
| 18 | $ssc->close; | ||||||
| 19 | |||||||
| 20 | =head1 DESCRIPTION | ||||||
| 21 | |||||||
| 22 | This module implements a driver for the Mini SSC II servo controller from Scott Edwards Electronics Inc (http://www.seetron.com/ssc.htm). | ||||||
| 23 | |||||||
| 24 | =cut | ||||||
| 25 | |||||||
| 26 | 1 | 1 | 6 | use base 'Class::Accessor'; | |||
| 1 | 7 | ||||||
| 1 | 866 | ||||||
| 27 | __PACKAGE__->mk_accessors(qw(device baudrate _sp)); | ||||||
| 28 | |||||||
| 29 | use Device::SerialPort; | ||||||
| 30 | |||||||
| 31 | =head1 METHODS | ||||||
| 32 | |||||||
| 33 | =head2 B |
||||||
| 34 | |||||||
| 35 | my $ssc = Device::MiniSSCII->new( | ||||||
| 36 | device => '/dev/ttyS0', | ||||||
| 37 | baudrate => 9600 | ||||||
| 38 | ) | ||||||
| 39 | |||||||
| 40 | The constructor expects two arguments, a C |
||||||
| 41 | |||||||
| 42 | =cut | ||||||
| 43 | |||||||
| 44 | sub new { | ||||||
| 45 | my ($proto, %arg) = @_; | ||||||
| 46 | my $self = bless {}, ref($proto) || $proto; | ||||||
| 47 | |||||||
| 48 | die "I need a device" unless defined $arg{device}; | ||||||
| 49 | die "I need a baudrate" unless defined $arg{baudrate}; | ||||||
| 50 | |||||||
| 51 | $self->device( $arg{device} ); | ||||||
| 52 | $self->baudrate( $arg{baudrate} ); | ||||||
| 53 | |||||||
| 54 | my $sp = Device::SerialPort->new( $self->device, 1 ) | ||||||
| 55 | || die "Could not open serial port on " . $self->device; | ||||||
| 56 | |||||||
| 57 | $sp->baudrate( $self->baudrate ) || die "Could not set baudrate"; | ||||||
| 58 | $sp->databits(8) || die "Could set 8 databits"; | ||||||
| 59 | $sp->parity("none") || die "Could not set parity to 'none'"; | ||||||
| 60 | $sp->stopbits(1) || die "Could not set stopbits to 1"; | ||||||
| 61 | $sp->handshake("none") || die "Could not set handshake to 'none'"; | ||||||
| 62 | $sp->write_settings || die "Could not activate settings"; | ||||||
| 63 | $self->_sp( $sp ); | ||||||
| 64 | |||||||
| 65 | return $self; | ||||||
| 66 | } | ||||||
| 67 | |||||||
| 68 | =head2 B |
||||||
| 69 | |||||||
| 70 | $ssc->move( 0, 128 ); | ||||||
| 71 | |||||||
| 72 | This method sets the position of a servo. The first argument is the servo number, in the range from 0 to 255. The second parameter represents the position to set the servo to, also in the range from 0 to 255. | ||||||
| 73 | |||||||
| 74 | =cut | ||||||
| 75 | |||||||
| 76 | sub move { | ||||||
| 77 | my ($self, $servo, $position) = @_; | ||||||
| 78 | |||||||
| 79 | my $count = $self->_sp->write(pack("C*", 255, $servo, $position)); | ||||||
| 80 | carp "Error moving servo $servo to position $position." if $count != 3; | ||||||
| 81 | |||||||
| 82 | return $self; | ||||||
| 83 | } | ||||||
| 84 | |||||||
| 85 | =head2 B |
||||||
| 86 | |||||||
| 87 | $ssc->close; | ||||||
| 88 | |||||||
| 89 | =cut | ||||||
| 90 | |||||||
| 91 | sub close { | ||||||
| 92 | my ($self) = @_; | ||||||
| 93 | |||||||
| 94 | $self->_sp->close; | ||||||
| 95 | } | ||||||
| 96 | |||||||
| 97 | 1; | ||||||
| 98 | |||||||
| 99 | __END__ |