ARDrone/Control/Event.pm

filter: Statementstmt 67.0% 57 / 85 Branchbran 27.2% 6 / 22 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 82.3% 14 / 17 Podpod 66.6% 2 / 3 totaltotal 55.3% 83 / 150 CC 15 SCAR 21.5
CC
15
Cov
55%
_process_sdl_input
30.0
_convert_sdl_input
12.0
__ANON__
9.7
CRAP 8.6
« ARDrone/Driver.pm ↑ Summary ARDrone/Commands.pm »
1 # Copyright (c) 2015 Timm Murray
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 # POSSIBILITY OF SUCH DAMAGE.
24 package UAV::Pilot::ARDrone::Control::Event;
25 3 $UAV::Pilot::ARDrone::Control::Event::VERSION = '1.0';
26 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 3 ▶eval "use UAV::Pilot::SDL::Joystick";
Subroutine BEGIN: called CC=1 SCAR=1.2
32 3 ▶my $CAN_LOAD_JOYSTICK = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 3 0 3
33 3 ▶eval "use UAV::Pilot::SDL::NavFeeder";
Subroutine BEGIN: called CC=1 SCAR=1.2
34 3 ▶my $CAN_LOAD_NAV_FEEDER = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 3 0 3
35  
36  
37 3 extends 'UAV::Pilot::ARDrone::Control';
38  
39 # The AR.Drone SDK manual says sending commands every 30ms is needed for smooth control
40 3 ▶use constant CONTROL_TIMING_INTERVAL => 30 / 1000;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 3 has 'cur_pitch' => (
43 is => 'ro',
44 isa => 'Num',
45 default => 0,
46 writer => 'pitch',
47 );
48 3 has 'cur_roll' => (
49 is => 'ro',
50 isa => 'Num',
51 default => 0,
52 writer => 'roll',
53 );
54 3 has 'cur_yaw' => (
55 is => 'ro',
56 isa => 'Num',
57 default => 0,
58 writer => 'yaw',
59 );
60 3 has 'cur_vert_speed' => (
61 is => 'ro',
62 isa => 'Num',
63 default => 0,
64 writer => 'vert_speed',
65 );
66 3 has 'joystick_num' => (
67 is => 'ro',
68 isa => 'Int',
69 default => 0,
70 );
71 3 has 'joystick_takeoff_btn' => (
72 is => 'ro',
73 isa => 'Int',
74 default => 0,
75 );
76 3 has 'joystick_takeoff_btn_last_state' => (
77 is => 'ro',
78 isa => 'Bool',
79 default => 0,
80 writer => '_set_joystick_takeoff_btn_last_state',
81 );
82 has 'do_init_joystick' => (
83 is => 'ro',
84 isa => 'Bool',
85 1 ▶ default => sub { $CAN_LOAD_JOYSTICK },
Subroutine __ANON__: called CC=1 SCAR=0.0
86 3 );
87  
88 3 ▶with 'UAV::Pilot::SDL::NavFeeder' if $CAN_LOAD_NAV_FEEDER;
Branch: 1 branch
BranchTrueFalseTotal
if $CAN_LOAD_NAV_FEEDER 0 3 3
89 3 with 'UAV::Pilot::Logger';
90  
91  
92  
93 sub BUILD
94 {
95 1 ▶ my ($self) = @_;
Subroutine BUILD: called CC=3 SCAR=13.3
Pod undocumented
96  
97 1 ▶ if( $self->do_init_joystick && !$CAN_LOAD_JOYSTICK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->do_init_joystick and not $CAN_LOAD_JOYSTICK) 0 1 1
Condition33%1/3
$self->do_init_joystick and not $CAN_LOAD_JOYSTICK
!ll&&!rl&&r
100
MC/DC0%0/2
$self->do_init_joystick and not $CAN_LOAD_JOYSTICK
$self->do_init_joystick not $CAN_LOAD_JOYSTICK
Truth table33%1/3
A and not(B)
A= $self->do_init_joystickB= not $CAN_LOAD_JOYSTICK
ABresult
0X0
100
111
98 0 die "Can't init joystick, because UAV::Pilot::SDL is not installed\n";
99 }
100  
101 1 return 1;
102 }
103  
104  
105 sub init_event_loop
106 {
107 1 ▶ my ($self, $cv, $event) = @_;
Subroutine init_event_loop: called CC=2 SCAR=8.3
Pod documented
108 1 $self->hover;
109  
110 1 my $control_timer; $control_timer = AnyEvent->timer(
111 after => 0.1,
112 interval => $self->CONTROL_TIMING_INTERVAL,
113 cb => sub {
114 97 ▶ if( $self->cur_roll
Subroutine __ANON__: called CC=5 SCAR=22.7
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cur_roll or $self->cur_pitch or $self->cur_vert_speed or $self->cur_yaw) 0 97 97
Condition33%3/9
$self->cur_roll or $self->cur_pitch or $self->cur_vert_speed or $self->cur_yaw
l!l&&r!l&&!r
0097
$self->cur_roll or $self->cur_pitch or $self->cur_vert_speed
l!l&&r!l&&!r
0097
$self->cur_roll or $self->cur_pitch
l!l&&r!l&&!r
0097
MC/DC0%0/4
$self->cur_roll or $self->cur_pitch or $self->cur_vert_speed or $self->cur_yaw
$self->cur_roll $self->cur_pitch $self->cur_vert_speed $self->cur_yaw
Truth table20%1/5
A or B or C or D
A= $self->cur_rollB= $self->cur_pitchC= $self->cur_vert_speedD= $self->cur_yaw
ABCDresult
1XXX1
01XX1
001X1
00011
00000
115 || $self->cur_pitch
116 || $self->cur_vert_speed
117 || $self->cur_yaw
118 ) {
119 0 $self->driver->at_pcmd( 1, 0,
120 $self->cur_roll,
121 $self->cur_pitch,
122 $self->cur_vert_speed,
123 $self->cur_yaw,
124 );
125 }
126  
127 97 $control_timer;
128 },
129 1 );
130 1 my $comwatch_timer; $comwatch_timer = AnyEvent->timer(
131 after => 1,
132 interval => 1.5,
133 cb => sub {
134 2 ▶ $self->reset_watchdog;
Subroutine __ANON__: called CC=1 SCAR=0.0
135 2 $comwatch_timer;
136 },
137 1 );
138  
139 1 ▶ if( $self->do_init_joystick ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->do_init_joystick) { } 0 1 1
140 0 $self->_logger->info( "Catching joystick events" );
141 $event->add_event( UAV::Pilot::SDL::Joystick->EVENT_NAME, sub {
142 0 ▶ my (@args) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 return $self->_process_sdl_input( @args );
144 0 });
145 }
146 else {
147 1 $self->_logger->info( "Not catching joystick events"
148 . " (possible: $CAN_LOAD_JOYSTICK)" );
149 }
150  
151 1 return 1;
152 }
153  
154 sub hover
155 {
156 1 ▶ my ($self) = @_;
Subroutine hover: called CC=2 SCAR=6.9
Pod documented
157 1 $self->$_( 0 ) for qw{
158 pitch
159 roll
160 yaw
161 vert_speed
162 };
163 1 return 1;
164 }
165  
166 sub _process_sdl_input
167 {
168 0 ▶ my ($self, $args) = @_;
Subroutine _process_sdl_input: not called CC=5 SCAR=34.0
169 0 ▶ return 0 if $args->{joystick_num} != $self->joystick_num;
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"joystick_num"} != $self->joystick_num 0 0 0
170 0 my $takeoff_btn_cur_state = $args->{buttons}->[$self->joystick_takeoff_btn];
171  
172 0 $self->roll( $self->_convert_sdl_input( $args->{roll} ) );
173 0 $self->pitch( $self->_convert_sdl_input( $args->{pitch} ) );
174 0 $self->yaw( $self->_convert_sdl_input( $args->{yaw} ) );
175 0 $self->vert_speed( $self->_convert_sdl_input( $args->{throttle} ) );
176  
177 # Toggle takeoff btn
178 0 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (not $takeoff_btn_cur_state and $self->joystick_takeoff_btn_last_state) 0 0 0
Condition0%0/3
not $takeoff_btn_cur_state and $self->joystick_takeoff_btn_last_state
!ll&&!rl&&r
000
MC/DC0%0/2
not $takeoff_btn_cur_state and $self->joystick_takeoff_btn_last_state
not $takeoff_btn_cur_state $self->joystick_takeoff_btn_last_state
Truth table0%0/3
not(A) and B
A= not $takeoff_btn_cur_stateB= $self->joystick_takeoff_btn_last_state
ABresult
0X0
100
111
179 (! $takeoff_btn_cur_state) &&
180 ($self->joystick_takeoff_btn_last_state)
181 ) {
182 0 ▶ if( $self->in_air ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->in_air) { } 0 0 0
183 0 $self->land;
184 }
185 else {
186 0 $self->takeoff;
187 }
188 }
189 0 $self->_set_joystick_takeoff_btn_last_state( $takeoff_btn_cur_state );
190  
191 0 return 1;
192 }
193  
194 sub _convert_sdl_input
195 {
196 0 ▶ my ($self, $num) = @_;
Subroutine _convert_sdl_input: not called CC=3 SCAR=24.8
197 0 my $float = $num / UAV::Pilot::SDL::Joystick->MAX_AXIS_INT;
198 0 ▶ $float = 1.0 if $float > 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $float > 1 0 0 0
199 0 ▶ $float = -1.0 if $float < -1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $float < -1 0 0 0
200 0 return $float;
201 }
202  
203  
204 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
205 3 __PACKAGE__->meta->make_immutable;
206 3 1;
207 __END__
208  
209  
210 =head1 NAME
211  
212 UAV::Pilot::ARDrone::Control::Event
213  
214 =head1 SYNOPSIS
215  
216 my $cv = AnyEvent->condvar;
217 my $event = UAV::Pilot::EasyEvent->new({
218 condvar => $cv,
219 });
220
221 my $driver = UAV::Pilot::Driver::ARDrone->new( ... );
222 $driver->connect;
223 my $dev = UAV::Pilot::Control::ARDrone::Event->new({
224 driver => $driver,
225 joystick_num => 0,
226 joystick_takeoff_btn => 3,
227 });
228 $uav->init_event_loop( $cv, $event );
229  
230 $dev->pitch( -0.8 );
231 $cv->recv; # Will now pitch forward until you kill the process
232  
233 =head1 DESCRIPTION
234  
235 AnyEvent-based version of C<UAV::Pilot::Control::ARDrone>. With the normal module, you
236 need to send movement commands yourself every 30ms to maintain smooth control. By using
237 an event loop, this module handles the timing for you.
238  
239 =head1 METHODS
240  
241 =head2 init_event_loop
242  
243 init_event_loop( $cv, $event )
244  
245 Sets up the event loop. Takes C<$cv> (an C<AnyEvent::Condvar>) and C<$event>
246 (a C<UAV::Pilot::EasyEvent>).
247  
248 Will listen for joystick events.
249  
250 =head2 hover
251  
252 Stops all movement.
253  
254 =cut
« ARDrone/Driver.pm ↑ Summary ARDrone/Commands.pm »