blib/lib/RPi/StepperMotor.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package RPi::StepperMotor;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use RPi::Const qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use WiringPi::API qw(:perl);
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 our $VERSION = '3.1801';
12  
13 use constant FULL => 2;
14 use constant HALF => 1;
15 use constant NUM_PINS => 0..3;
16  
17 use constant STEPPER_SEQUENCE => [
18 [qw(1 0 0 1)],
19 [qw(1 0 0 0)],
20 [qw(1 1 0 0)],
21 [qw(0 1 0 0)],
22 [qw(0 1 1 0)],
23 [qw(0 0 1 0)],
24 [qw(0 0 1 1)],
25 [qw(0 0 0 1)],
26 ];
27  
28 use constant STEP_COUNT => 0+@{ STEPPER_SEQUENCE() };
29  
30 sub new {
31 my ($class, %args) = @_;
32  
33 my $self = bless {}, $class;
34  
35 if (! exists $args{pins}){
36 croak "'pins' parameter is required to use this module\n";
37 }
38  
39 setup_gpio();
40  
41 $self->_expander($args{expander});
42  
43 $self->_pins($args{pins});
44  
45 my $delay = $args{delay} // 0.01;
46 $self->delay($delay);
47  
48 my $speed = $args{speed} // 'half';
49 $self->speed($speed);
50  
51 $self->name($args{name});
52  
53 return $self;
54 }
55 sub cw {
56 my ($self, $degrees) = @_;
57 $self->_engage_motor($degrees, 'cw');
58 }
59 sub ccw {
60 my ($self, $degrees) = @_;
61 $self->_engage_motor($degrees, 'ccw');
62 }
63 sub cleanup {
64 my ($self) = @_;
65  
66 if (! $self->_expander) {
67 for (@{$self->_pins}) {
68 write_pin($_, LOW);
69 pin_mode($_, INPUT);
70 }
71 }
72 }
73 sub delay {
74 my ($self, $delay) = @_;
75 $self->{delay} = $delay if defined $delay;
76 return $self->{delay};
77 }
78 sub name {
79 my ($self, $name) = @_;
80 $self->{name} = $name if defined $name;
81 return $self->{name};
82 }
83 sub speed {
84 my ($self, $speed) = @_;
85 if (defined $speed){
86 if (! grep {$speed ne $_} qw(full half)){
87 croak "'speed' parameter must be either 'full' or 'half'\n";
88 }
89 $self->{speed} = $speed;
90 }
91 return $self->{speed};
92 }
93 sub _engage_motor {
94 my ($self, $degrees, $direction) = @_;
95  
96 if (! defined $degrees){
97 croak "a direction in integer degrees must be specified\n";
98 }
99 if (! defined $direction || $direction !~ /cw/){
100 croak "_engage_motor() requires either 'cw' or 'ccw' direction sent in";
101 }
102  
103 my $step_counter = 0;
104 my $pins = $self->_pins;
105  
106 for (1..$self->_turns($degrees)) {
107 for my $gpio_pin (NUM_PINS) {
108 if (STEPPER_SEQUENCE->[$step_counter][$gpio_pin]) {
109 $self->_expander
110 ? $self->_expander()->write($pins->[$gpio_pin], HIGH)
111 : write_pin($pins->[$gpio_pin], HIGH);
112 }
113 else {
114 $self->_expander
115 ? $self->_expander()->write($pins->[$gpio_pin], LOW)
116 : write_pin($pins->[$gpio_pin], LOW);
117 }
118 }
119  
120 if ($direction eq 'cw'){ # clockwise direction
121 $step_counter += $self->_phases;
122  
123 if ($step_counter >= STEP_COUNT){
124 $step_counter = 0;
125 }
126 }
127 else {
128 $step_counter += $self->_phases * - 1;
129  
130 if ($step_counter < 0) {
131 $step_counter = STEP_COUNT + $self->_phases * - 1;
132 }
133 }
134  
135 $self->_wait;
136 }
137 }
138 sub _expander {
139 my ($self, $exp) = @_;
140  
141 if (defined $exp){
142 $self->{expander} = $exp;
143 }
144  
145 return $self->{expander};
146 }
147 sub _phases {
148 return $_[0]->speed eq 'full' ? FULL : HALF;
149 }
150 sub _pins {
151 my ($self, $pins) = @_;
152  
153 if (defined $pins){
154 if (@$pins != 4){
155 croak "the 'pins' parameter must include an aref with four " .
156 "elements\n";
157 }
158  
159 if ($self->_expander){
160 for (@$pins){
161 $self->_expander()->mode($_, MCP23017_OUTPUT);
162 $self->_expander()->write($_, LOW);
163 }
164 }
165 else {
166 for (@$pins){
167 pin_mode($_, OUTPUT);
168 write_pin($_, LOW);
169 }
170 }
171  
172 $self->{pins} = $pins;
173 }
174  
175 return $self->{pins};
176 }
177 sub _turns {
178 # returns the number of "turns" to get to the degrees we want.
179 # 64 gear ratio * (degrees / turns for each phase)
180 # 5.625/360 degrees for all phases, 11.25/360 degrees for every other phase
181  
182 my ($self, $degrees) = @_;
183 return $self->_phases == 1
184 ? int($degrees / 5.625 + 0.5) * 64
185 : int($degrees / 11.25 + 0.5) * 64;
186 }
187 sub _wait {
188 my ($self) = @_;
189 select(undef, undef, undef, $self->delay);
190 }
191 sub __vim_placeholder {}
192  
193 1;
194 __END__
↑ Summary