| 1 | package Device::MiniSSCII; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 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 | ▶ | use base 'Class::Accessor'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | __PACKAGE__->mk_accessors(qw(device baudrate _sp)); | ||
| 28 | |||
| 29 | use Device::SerialPort; | ||
| 30 | |||
| 31 | =head1 METHODS | ||
| 32 | |||
| 33 | =head2 B<new> - constructor | ||
| 34 | |||
| 35 | my $ssc = Device::MiniSSCII->new( | ||
| 36 | device => '/dev/ttyS0', | ||
| 37 | baudrate => 9600 | ||
| 38 | ) | ||
| 39 | |||
| 40 | The constructor expects two arguments, a C<device> that denotes the serial port and a C<baudrate> that can be either C<2400> or C<9600>. | ||
| 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<move> - Set the position of a servo | ||
| 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<close> - Close serial connection and clean-up | ||
| 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__ | ||