ARDrone/Commands.pm untested

filter: Statementstmt 0.0% 0 / 179 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 247 CC 5 SCAR 13.9
CC
5
Cov
0%
start_video
12.0
start_joystick
6.0
start_nav
6.0
CRAP 4.0
1  
2 use v5.14;
3 use UAV::Pilot::Exceptions;
4 use UAV::Pilot::Video::FileDump;
5 use UAV::Pilot::ARDrone::Video;
6 use UAV::Pilot::ARDrone::Video::Stream;
7 use UAV::Pilot::ARDrone::Video::Fileno;
8 use UAV::Pilot::Events;
9  
10 use constant VIDEO_EXTERNAL_STREAM_EXEC => 'ardrone_display_video.pl';
11  
12 our $dev = undef;
13  
14 {
15 my @NO_ARG_STRAIGHT_COMMANDS = qw(
16 takeoff
17 land
18 calibrate
19 phi_m30
20 phi_30
21 theta_m30
22 theta_30
23 theta_20deg_yaw_200
24 theta_20deg_yaw_m200
25 turnaround
26 turnaround_godown
27 yaw_shake
28 yaw_dance
29 phi_dance
30 theta_dance
31 vz_dance
32 wave
33 phi_theta_mixed
34 double_phi_theta_mixed
35 flip_ahead
36 flip_behind
37 flip_left
38 flip_right
39 emergency
40 hover
41 start_userbox_nav_data
42 stop_userbox_nav_data
43 cancel_userbox_nav_data
44 );
45 foreach my $name (@NO_ARG_STRAIGHT_COMMANDS) {
46 no strict 'refs';
47 *$name = sub () {
48 $dev->$name;
49 };
50 }
51 }
52  
53 {
54 my @SINGLE_ARG_STRAIGHT_COMMANDS = qw(
55 pitch
56 roll
57 yaw
58 vert_speed
59 );
60 foreach my $name (@SINGLE_ARG_STRAIGHT_COMMANDS) {
61 no strict 'refs';
62 *$name = sub ($) {
63 $dev->$name( @_ );
64 };
65 }
66 }
67  
68 {
69 my @TWO_ARG_STRAIGHT_COMMANDS = qw(
70 led_blink_green_red
71 led_blink_green
72 led_blink_red
73 led_blink_orange
74 led_snake_green_red
75 led_fire
76 led_standard
77 led_red
78 led_green
79 led_red_snake
80 led_blank
81 led_right_missile
82 led_left_missile
83 led_double_missile
84 led_front_left_green_others_red
85 led_front_right_green_others_red
86 led_rear_right_green_others_red
87 led_rear_left_green_others_red
88 led_left_green_right_red
89 led_left_red_right_green
90 led_blink_standard
91 );
92 foreach my $name (@TWO_ARG_STRAIGHT_COMMANDS) {
93 no strict 'refs';
94 *$name = sub ($$) {
95 $dev->$name( @_ );
96 };
97 }
98 }
99  
100 sub take_picture ($$;$)
101 {
102 $dev->take_picture( @_ );
103 }
104  
105 sub record_usb ()
106 {
107 $dev->record_usb;
108 }
109  
110 {
111 my $cv = undef;
112 my $events = undef;
113 my $easy_events = undef;
114 my $init_events = sub {
115 return 1 if defined $events;
116 die "Can't init UAV::Pilot::Events without a condvar\n" unless defined $cv;
117  
118 $events = UAV::Pilot::Events->new({
119 condvar => $cv,
120 });
121 $easy_events = UAV::Pilot::EasyEvent->new({
122 condvar => $cv,
123 });
124 $dev->setup_read_nav_event( $easy_events );
125 $events->init_event_loop;
126 $easy_events->init_event_loop;
127  
128 # If we can load SDL, then init it here
129 eval "use UAV::Pilot::SDL::Events";
130 if(! $@ ) {
131 my $sdl_events = UAV::Pilot::SDL::Events->new;
132 $events->register( $sdl_events );
133 }
134  
135 $dev->init_event_loop( $cv, $easy_events );
136 return 1;
137 };
138  
139 my $sdl = undef;
140 my $init_sdl = sub {
141 return 1 if defined $sdl;
142 eval "use UAV::Pilot::SDL::Window;";
143 die "Can't use UAV::Pilot::SDL::Window: $@\n" if $@;
144  
145 $init_events->();
146 $sdl = UAV::Pilot::SDL::Window->new;
147 $events->register( $sdl );
148 return 1;
149 };
150  
151 my $vid_driver = undef;
152 my $init_vid_driver = sub {
153 return 1 if defined $vid_driver;
154  
155 $vid_driver = UAV::Pilot::ARDrone::Video->new({
156 condvar => $cv,
157 driver => $dev->driver,
158 });
159 $vid_driver->init_event_loop;
160 $dev->video( $vid_driver );
161  
162 return 1;
163 };
164  
165 my $fork_process_win32 = sub {
166 my (@args) = @_;
167 my $pid = system( 1, @args )
168 or die "Could not execute " . $args[0] . ": $!\n";
169 return $pid;
170 };
171 my $fork_process_unixy = sub {
172 my (@args) = @_;
173  
174 my $pid = fork();
175 if( $pid ) {
176 # Parent
177 }
178 else {
179 # Child
180 exec( @args ) or die "Could not execute " . $args[0] . ": $!\n";
181 }
182  
183 # Remember, child won't get here because of exec()
184 return $pid;
185 };
186  
187 my @PIDS;
188 my $start_video_single_process = sub {
189 $init_events->();
190 $init_sdl->();
191 $init_vid_driver->();
192 eval "use UAV::Pilot::SDL::Video";
193 die "Problem loading UAV::Pilot::SDL::Video: $@\n" if $@;
194 eval "use UAV::Pilot::Video::H264Decoder";
195 die "Problem loading UAV::Pilot::Video::H264Decoder: $@\n" if $@;
196  
197 my $display = UAV::Pilot::SDL::Video->new;
198 my $video = UAV::Pilot::Video::H264Decoder->new({
199 displays => [ $display ],
200 });
201 $vid_driver->add_handler( $video );
202 $display->add_to_window( $sdl, $sdl->TOP );
203 $vid_driver->init_event_loop;
204  
205 say "Outputting video";
206 return 1;
207 };
208 my $start_video_external_process = sub {
209 $init_events->();
210  
211 my $pid = open( my $out_fh, '|-', VIDEO_EXTERNAL_STREAM_EXEC )
212 or die "Can't execute " . VIDEO_EXTERNAL_STREAM_EXEC . ": $!\n";
213 push @PIDS, $pid;
214  
215 $vid_driver = UAV::Pilot::ARDrone::Video::Stream->new({
216 condvar => $cv,
217 driver => $dev->driver,
218 out_fh => $out_fh,
219 });
220 $vid_driver->init_event_loop;
221  
222 say "Outputting video on external process (pipe)";
223 return 1;
224 };
225 my $start_video_external_process_fileno = sub {
226 my ($out_file) = @_;
227 $init_events->();
228  
229 $vid_driver = UAV::Pilot::ARDrone::Video::Fileno->new({
230 condvar => $cv,
231 driver => $dev->driver,
232 });
233 my $fileno = $vid_driver->fileno;
234  
235 $SIG{CHLD} = 'IGNORE';
236 my @exec = ( VIDEO_EXTERNAL_STREAM_EXEC,
237 '--fileno=' . $fileno,
238 (defined $out_file ? '--out-file=' . $out_file : ()),
239 );
240 my $pid = 'MSWin32' eq $^O
241 ? $fork_process_win32->( @exec )
242 : $fork_process_unixy->( @exec );
243 push @PIDS, $pid;
244  
245 say "Outputting video on external process (fileno)";
246 return 1;
247 };
248  
249 my $cleanup_processes = sub {
250 kill 'HUP', @PIDS;
251 };
252  
253  
254 sub uav_module_init
255 {
256 my ($class, $cmd, $args) = @_;
257 $cv = $$args{condvar};
258  
259 $easy_events = UAV::Pilot::EasyEvent->new({
260 condvar => $cv,
261 });
262 $easy_events->init_event_loop;
263  
264 $dev = $cmd->controller_callback_ardrone->(
265 $cmd, $cv, $easy_events );
266 return 1;
267 }
268  
269 sub uav_module_quit
270 {
271 $cleanup_processes->();
272 }
273  
274  
275 sub start_joystick ()
276 {
277 $init_events->();
278 eval "use UAV::Pilot::SDL::Joystick";
279 die "Problem loading UAV::Pilot::SDL::Joystick: $@\n" if $@;
280  
281 my $joystick = UAV::Pilot::SDL::Joystick->new({
282 condvar => $cv,
283 events => $easy_events,
284 });
285 $events->register( $joystick );
286  
287 say 'Ready for joystick input on ['
288 . SDL::Joystick::name( $joystick->joystick->index )
289 . ']';
290  
291 return 1;
292 }
293  
294 sub start_nav ()
295 {
296 eval "use UAV::Pilot::ARDrone::SDLNavOutput";
297 die "Problem loading UAV::Pilot::ARDrone::SDLNavOutput: $@\n" if $@;
298 $init_sdl->();
299 $init_events->();
300  
301 my $nav = UAV::Pilot::ARDrone::SDLNavOutput->new({
302 feeder => $dev,
303 });
304 $dev->driver->add_nav_collector( $nav );
305 $nav->add_to_window( $sdl, $sdl->BOTTOM );
306  
307 say "Outputting navigation data";
308  
309 return 1;
310 }
311  
312 sub start_video (;$$)
313 {
314 my ($type, $out_file) = @_;
315 $type //= 0;
316  
317 return
318 (0 == $type) ?$start_video_external_process_fileno->($out_file):
319 (1 == $type) ? $start_video_external_process->( $out_file ) :
320 $start_video_single_process->( $out_file );
321 }
322  
323 sub dump_video_to_file ($)
324 {
325 my ($file) = @_;
326 $init_vid_driver->();
327  
328 open( my $fh, '>', $file ) or UAV::Pilot::IOException->throw({
329 error => "Can't open [$file] for writing: $!\n",
330 });
331 my $vid_dump = UAV::Pilot::Video::FileDump->new({
332 fh => $fh,
333 });
334  
335 $vid_driver->add_handler( $vid_dump );
336 say "Dumping video to file '$file'";
337 return 1;
338 }
339 }
340  
341  
342 1;