ARDrone/SDLNavOutput.pm untested

filter: Statementstmt 0.0% 0 / 292 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 323 CC 3 SCAR 12.1
CC
3
Cov
0%
draw
12.0
BUILDARGS
2.0
_draw_line_value
2.0
CRAP 3.4
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::SDLNavOutput;
25 $UAV::Pilot::ARDrone::SDLNavOutput::VERSION = '1.0';
26 use v5.14;
27 use Moose;
28 use namespace::autoclean;
29 use File::Spec;
30 use Math::Trig ();
31 # TODO relies on SDL
32 use SDL;
33 use SDLx::Text;
34 use SDL::Event;
35 use SDL::Events;
36 use UAV::Pilot;
37 use UAV::Pilot::ARDrone::NavPacket;
38  
39  
40 use constant {
41 BG_COLOR => [ 0, 0, 0 ],
42 DRAW_VALUE_COLOR => [ 0x33, 0xff, 0x33 ],
43 DRAW_FEEDER_VALUE_COLOR => [ 0x33, 0x33, 0xff ],
44 DRAW_CIRCLE_VALUE_COLOR => [ 0xa8, 0xa8, 0xa8 ],
45 TEXT_LABEL_COLOR => [ 0, 0, 255 ],
46 TEXT_VALUE_COLOR => [ 255, 0, 0 ],
47 TEXT_SIZE => 20,
48 TEXT_FONT => 'typeone.ttf',
49  
50 ROLL_LABEL_X => 50,
51 PITCH_LABEL_X => 150,
52 YAW_LABEL_X => 250,
53 ALTITUDE_LABEL_X => 350,
54 BATTERY_LABEL_X => 450,
55  
56 ROLL_VALUE_X => 50,
57 PITCH_VALUE_X => 150,
58 YAW_VALUE_X => 250,
59 ALTITUDE_VALUE_X => 350,
60 BATTERY_VALUE_X => 450,
61  
62 ROLL_DISPLAY_X => 50,
63 PITCH_DISPLAY_X => 150,
64 YAW_DISPLAY_X => 250,
65 ALTITUDE_DISPLAY_X => 350,
66 VERT_SPEED_DISPLAY_HALF_HEIGHT => 10,
67 VERT_SPEED_DISPLAY_WIDTH => 10,
68 VERT_SPEED_BORDER_WIDTH_MARGIN => 2,
69 BATTERY_DISPLAY_X => 450,
70  
71 ROLL_PITCH_YAW_MAX_VALUE => 30_000,
72 FEEDER_ROLL_PITCH_YAW_MAX_VALUE => 1.0,
73  
74 LINE_VALUE_HALF_MAX_HEIGHT => 10,
75 LINE_VALUE_HALF_LENGTH => 40,
76  
77 CIRCLE_VALUE_RADIUS => 40,
78
79 BAR_MAX_HEIGHT => 40,
80 BAR_WIDTH => 10,
81 BAR_PERCENT_COLOR_GRADIENT => [
82 [ '255', '0', '0' ],
83 [ '252', '3', '0' ],
84 [ '250', '5', '0' ],
85 [ '247', '8', '0' ],
86 [ '245', '10', '0' ],
87 [ '242', '13', '0' ],
88 [ '240', '15', '0' ],
89 [ '237', '18', '0' ],
90 [ '234', '21', '0' ],
91 [ '232', '23', '0' ],
92 [ '229', '26', '0' ],
93 [ '227', '28', '0' ],
94 [ '224', '31', '0' ],
95 [ '222', '33', '0' ],
96 [ '219', '36', '0' ],
97 [ '216', '39', '0' ],
98 [ '214', '41', '0' ],
99 [ '211', '44', '0' ],
100 [ '209', '46', '0' ],
101 [ '206', '49', '0' ],
102 [ '203', '52', '0' ],
103 [ '201', '54', '0' ],
104 [ '198', '57', '0' ],
105 [ '196', '59', '0' ],
106 [ '193', '62', '0' ],
107 [ '191', '64', '0' ],
108 [ '188', '67', '0' ],
109 [ '185', '70', '0' ],
110 [ '183', '72', '0' ],
111 [ '180', '75', '0' ],
112 [ '178', '77', '0' ],
113 [ '175', '80', '0' ],
114 [ '173', '82', '0' ],
115 [ '170', '85', '0' ],
116 [ '167', '88', '0' ],
117 [ '165', '90', '0' ],
118 [ '162', '93', '0' ],
119 [ '160', '95', '0' ],
120 [ '157', '98', '0' ],
121 [ '155', '100', '0' ],
122 [ '152', '103', '0' ],
123 [ '149', '106', '0' ],
124 [ '147', '108', '0' ],
125 [ '144', '111', '0' ],
126 [ '142', '113', '0' ],
127 [ '139', '116', '0' ],
128 [ '137', '118', '0' ],
129 [ '134', '121', '0' ],
130 [ '131', '124', '0' ],
131 [ '129', '126', '0' ],
132 [ '126', '129', '0' ],
133 [ '124', '131', '0' ],
134 [ '121', '134', '0' ],
135 [ '118', '137', '0' ],
136 [ '116', '139', '0' ],
137 [ '113', '142', '0' ],
138 [ '111', '144', '0' ],
139 [ '108', '147', '0' ],
140 [ '106', '149', '0' ],
141 [ '103', '152', '0' ],
142 [ '100', '155', '0' ],
143 [ '98', '157', '0' ],
144 [ '95', '160', '0' ],
145 [ '93', '162', '0' ],
146 [ '90', '165', '0' ],
147 [ '88', '167', '0' ],
148 [ '85', '170', '0' ],
149 [ '82', '173', '0' ],
150 [ '80', '175', '0' ],
151 [ '77', '178', '0' ],
152 [ '75', '180', '0' ],
153 [ '72', '183', '0' ],
154 [ '70', '185', '0' ],
155 [ '67', '188', '0' ],
156 [ '64', '191', '0' ],
157 [ '62', '193', '0' ],
158 [ '59', '196', '0' ],
159 [ '57', '198', '0' ],
160 [ '54', '201', '0' ],
161 [ '52', '203', '0' ],
162 [ '49', '206', '0' ],
163 [ '46', '209', '0' ],
164 [ '44', '211', '0' ],
165 [ '41', '214', '0' ],
166 [ '39', '216', '0' ],
167 [ '36', '219', '0' ],
168 [ '33', '222', '0' ],
169 [ '31', '224', '0' ],
170 [ '28', '227', '0' ],
171 [ '26', '229', '0' ],
172 [ '23', '232', '0' ],
173 [ '21', '234', '0' ],
174 [ '18', '237', '0' ],
175 [ '15', '240', '0' ],
176 [ '13', '242', '0' ],
177 [ '10', '245', '0' ],
178 [ '8', '247', '0' ],
179 [ '5', '250', '0' ],
180 [ '3', '252', '0' ],
181 [ '0', '255', '0' ],
182 ],
183 };
184  
185  
186 has 'feeder' => (
187 is => 'ro',
188 isa => 'Maybe[UAV::Pilot::SDL::NavFeeder]',
189 );
190 has 'width' => (
191 is => 'ro',
192 isa => 'Int',
193 default => 640,
194 );
195 has 'height' => (
196 is => 'ro',
197 isa => 'Int',
198 default => 200,
199 );
200 has '_txt_label' => (
201 is => 'ro',
202 isa => 'SDLx::Text',
203 );
204 has '_txt_value' => (
205 is => 'ro',
206 isa => 'SDLx::Text',
207 );
208 has '_last_nav_packet' => (
209 is => 'ro',
210 isa => 'Maybe[UAV::Pilot::ARDrone::NavPacket]',
211 writer => 'got_new_nav_packet',
212 );
213  
214 with 'UAV::Pilot::SDL::WindowEventHandler';
215 with 'UAV::Pilot::NavCollector';
216 with 'UAV::Pilot::Logger';
217  
218  
219 sub BUILDARGS
220 {
221 my ($class, $args) = @_;
222 my @txt_color_parts = @{ $class->TEXT_LABEL_COLOR };
223 my @txt_value_color_parts = @{ $class->TEXT_VALUE_COLOR };
224  
225 my $font_path = File::Spec->catfile(
226 UAV::Pilot->default_module_dir,
227 $class->TEXT_FONT,
228 );
229 my $label = SDLx::Text->new(
230 font => $font_path,
231 color => [ @txt_color_parts ],
232 size => $class->TEXT_SIZE,
233 h_align => 'center',
234 );
235 my $value = SDLx::Text->new(
236 font => $font_path,
237 color => [ @txt_value_color_parts ],
238 size => $class->TEXT_SIZE,
239 h_align => 'center',
240 );
241  
242  
243 $$args{_txt_label} = $label;
244 $$args{_txt_value} = $value;
245 return $args;
246 }
247  
248  
249 sub draw
250 {
251 my ($self, $window) = @_;
252 my $nav = $self->_last_nav_packet;
253 if(! defined $nav) {
254 $self->_logger->info( 'No nav packet yet, not drawing anything' );
255 return $self->_draw_no_nav_packet( $window );
256 }
257 $self->_logger->info( 'Drawing nav packet' );
258 $window->clear_screen;
259  
260 my $txt_label = $self->_txt_label;
261 $window->draw_txt( 'ROLL', $self->ROLL_LABEL_X, 150, $txt_label);
262 $window->draw_txt( 'PITCH', $self->PITCH_LABEL_X, 150, $txt_label);
263 $window->draw_txt( 'YAW', $self->YAW_LABEL_X, 150, $txt_label);
264 $window->draw_txt( 'ALTITUDE', $self->ALTITUDE_LABEL_X, 150, $txt_label);
265 $window->draw_txt( 'BATTERY', $self->BATTERY_LABEL_X, 150, $txt_label);
266  
267 my $txt_val = $self->_txt_value;
268 $window->draw_txt( sprintf('%.2f', $nav->roll ),
269 $self->ROLL_VALUE_X, 30, $txt_val );
270 $window->draw_txt( sprintf('%.2f', $nav->pitch ),
271 $self->PITCH_VALUE_X, 30, $txt_val );
272 #$window->draw_txt( sprintf('%.2f', $nav->yaw ),
273 # $self->YAW_VALUE_X, 30, $txt_val );
274 $window->draw_txt( sprintf('%.2f cm', $nav->altitude ),
275 $self->ALTITUDE_VALUE_X, 30, $txt_val );
276 $window->draw_txt( $nav->battery_voltage_percentage . '%',
277 $self->BATTERY_VALUE_X, 30, $txt_val );
278  
279 my $line_color = $self->DRAW_VALUE_COLOR;
280  
281 my $feeder = $self->feeder;
282 if( defined $feeder) {
283 my $feeder_line_color = $self->DRAW_FEEDER_VALUE_COLOR;
284 $self->_draw_line_value( $feeder->cur_roll,
285 $self->FEEDER_ROLL_PITCH_YAW_MAX_VALUE,
286 $self->ROLL_DISPLAY_X, 100,
287 $feeder_line_color, $window );
288 $self->_draw_line_value( $feeder->cur_pitch,
289 $self->FEEDER_ROLL_PITCH_YAW_MAX_VALUE,
290 $self->PITCH_DISPLAY_X, 100,
291 $feeder_line_color, $window );
292 $self->_draw_circle_value( $feeder->cur_yaw,
293 $self->FEEDER_ROLL_PITCH_YAW_MAX_VALUE,
294 $self->YAW_DISPLAY_X, 100,
295 $feeder_line_color, $window );
296 $self->_draw_line_vert_indicator( $feeder->cur_vert_speed,
297 $self->ALTITUDE_VALUE_X, 100, $self->VERT_SPEED_DISPLAY_HALF_HEIGHT,
298 $self->VERT_SPEED_DISPLAY_WIDTH, $feeder_line_color, $line_color,
299 $self->VERT_SPEED_BORDER_WIDTH_MARGIN,
300 $window );
301 }
302  
303 $self->_draw_line_value( $nav->roll,
304 $self->ROLL_PITCH_YAW_MAX_VALUE,
305 $self->ROLL_DISPLAY_X, 100, $line_color, $window );
306 $self->_draw_line_value( $nav->pitch,
307 $self->ROLL_PITCH_YAW_MAX_VALUE,
308 $self->PITCH_DISPLAY_X, 100, $line_color, $window );
309 # For the AR.Drone, yaw this seems to be an absolute heading. For now,
310 # decided to only show the input rather than the value back from the UAV.
311 #$self->_draw_circle_value( $nav->yaw,
312 # $self->ROLL_PITCH_YAW_MAX_VALUE,
313 # $self->YAW_DISPLAY_X, 100, $line_color, $window );
314  
315 # Should we draw anything for altitude?
316 $self->_draw_bar_percent_value( $nav->battery_voltage_percentage,
317 $self->BATTERY_DISPLAY_X, 100, $window );
318  
319 $self->_logger->info( 'Done drawing nav packet' );
320 return 1;
321 }
322  
323 before 'got_new_nav_packet' => sub {
324 my ($self, $packet) = @_;
325 $self->_logger->info( 'Received nav packet to draw' );
326 return 1;
327 };
328  
329  
330 sub _draw_line_value
331 {
332 my ($self, $value, $max_value, $center_x, $center_y, $color, $window) = @_;
333  
334 my $corrected_value = $value / $max_value;
335 my $y_addition = int( $self->LINE_VALUE_HALF_MAX_HEIGHT * $corrected_value);
336 my $right_y = $center_y - $y_addition;
337 my $left_y = $center_y + $y_addition;
338  
339 my $right_x = $center_x + $self->LINE_VALUE_HALF_LENGTH;
340 my $left_x = $center_x - $self->LINE_VALUE_HALF_LENGTH;
341  
342 $window->draw_line( [$left_x, $left_y], [$right_x, $right_y], $color );
343 return 1;
344 }
345  
346 sub _draw_circle_value
347 {
348 my ($self, $value, $max_value, $center_x, $center_y, $value_color, $window)
349 = @_;
350 my $radius = $self->CIRCLE_VALUE_RADIUS;
351 my $color = $self->DRAW_VALUE_COLOR;
352  
353 my $corrected_value = $value / $max_value;
354 # Note use of radians, not degrees
355 my $angle = Math::Trig::pip2 * $corrected_value;
356 my $line_x = $center_x - (sin($angle) * $radius);
357 my $line_y = $center_y - (cos($angle) * $radius);
358  
359 $window->draw_circle( [$center_x, $center_y], $radius, $color );
360 $window->draw_line( [$center_x, $center_y],
361 [$center_x, $center_y - $radius], $color );
362  
363 $window->draw_line( [$center_x, $center_y], [$line_x, $line_y],
364 $value_color );
365  
366 return 1;
367 }
368  
369 sub _draw_bar_percent_value
370 {
371 my ($self, $value, $center_x, $center_y, $window) = @_;
372 my $color = $self->BAR_PERCENT_COLOR_GRADIENT->[$value - 1];
373 my $half_max_height = $self->BAR_MAX_HEIGHT / 2;
374 my $half_width = $self->BAR_WIDTH / 2;
375  
376 my $left_x = $center_x - $half_width;
377 my $right_x = $center_x + $half_width;
378 my $top_y = $center_y - $half_max_height;
379 my $bottom_y = $center_y + $half_max_height;
380  
381 my $percentage_height = sprintf( '%.0f', $self->BAR_MAX_HEIGHT * ($value / 100) );
382 my $top_percentage_y = $bottom_y - $percentage_height;
383  
384 $window->draw_line( $$_[0], $$_[1], $color ) for (
385 [ [$left_x, $top_y], [$right_x, $top_y] ],
386 [ [$right_x, $top_y], [$right_x, $bottom_y] ],
387 [ [$right_x, $bottom_y], [$left_x, $bottom_y] ],
388 [ [$left_x, $bottom_y], [$left_x, $top_y] ],
389 );
390 $window->draw_rect( [ $left_x, $top_percentage_y,
391 $self->BAR_WIDTH, $percentage_height ], $color );
392  
393 return 1;
394 }
395  
396 sub _draw_line_vert_indicator
397 {
398 my ($self, $value, $center_x, $center_y, $half_height, $width, $color, $top_bottom_color, $border_width_margin, $window) = @_;
399 my $half_width = $width / 2;
400  
401 my $left_x = $center_x - $half_width;
402 my $right_x = $center_x + $half_width;
403 my $border_left_x = $left_x - $border_width_margin;
404 my $border_right_x = $right_x + $border_width_margin;
405 my $border_top_y = $center_y - $half_height;
406 my $border_bottom_y = $center_y + $half_height;
407  
408 my $indicator_y = $center_y - ($half_height * $value);
409  
410 $window->draw_line( [$border_left_x, $border_top_y],
411 [$border_right_x, $border_top_y], $top_bottom_color );
412 $window->draw_line( [$border_left_x, $border_bottom_y],
413 [$border_right_x, $border_bottom_y], $top_bottom_color );
414 $window->draw_line( [$left_x, $indicator_y], [$right_x, $indicator_y],
415 $color );
416  
417 return 1;
418 }
419  
420 sub _draw_no_nav_packet
421 {
422 my ($self, $window) = @_;
423 $window->clear_screen;
424 my $txt_label = $self->_txt_label;
425 $window->draw_txt( 'No Nav Data Received', 20, 100, $txt_label );
426 return 1;
427 }
428  
429  
430 no Moose;
431 __PACKAGE__->meta->make_immutable;
432 1;
433 __END__
434  
435  
436 =head1 NAME
437  
438 UAV::Pilot::ARDrone::SDLNavOutput
439  
440 =head1 SYNOPSIS
441  
442 my $condvar = AnyEvent->condvar;
443 my $events = UAV::Pilot::Events->new({
444 condvar => $condvar,
445 });
446
447 my $window = UAV::Pilot::SDL::Window->new;
448 my $sdl_nav = UAV::Pilot::ARDrone::SDLNavOutput->new({
449 driver => UAV::Pilot::ARDrone::Driver->new( ... ),
450 window => $window,
451 });
452 $events->register( $sdl_nav );
453  
454 =head1 DESCRIPTION
455  
456 Graphically renders a C<UAV::Pilot::ARDrone::NavPacket> using SDL.
457  
458 It does the C<UAV::Pilot::EventHandler> role, and thus can be processed by
459 C<UAV::Pilot::Events>. It's recommended to also add the
460 C<UAV::Pilot::SDL::Events> handler to the events object, as that will
461 take care of the C<SDL_QUIT> events. Without that, there's no way to stop
462 the process other than C<kill -9>.
463  
464 =head1 METHODS
465  
466 =head2 new
467  
468 new({
469 feeder => ...
470 })
471  
472 Constructor. The param C<feeder> takes a C<UAV::Pilot::SDL::NavFeeder> object.
473  
474 =head2 render
475  
476 render( $nav_packet )
477  
478 Updates the graphic with the given nav packet data.
479  
480 =cut