ARDrone/NavPacket.pm

filter: Statementstmt 90.9% 140 / 154 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 18 / 20 Podpod 33.3% 1 / 3 totaltotal 89.4% 169 / 189 CC 9 SCAR 6.6
CC
9
Cov
90%
_parse_option_demo
4.0
_parse_option
3.0
BUILDARGS
2.0
CRAP 1.9
« ARDrone/Video/Fileno.pm ↑ Summary ARDrone/Driver/Mock.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::NavPacket;
25 15 $UAV::Pilot::ARDrone::NavPacket::VERSION = '1.0';
26 15 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 15 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 15 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 15 ▶use UAV::Pilot::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 use constant { # Values used as Option IDs
33 15 NAVDATA_DEMO => 0,
34 NAVDATA_TIME => 1,
35 NAVDATA_RAW_MEASURES => 2,
36 NAVDATA_PHYS_MEASURES => 3,
37 NAVDATA_GYROS_OFFSETS => 4,
38 NAVDATA_EULER_ANGLES => 5,
39 NAVDATA_REFERNCES => 6,
40 NAVDATA_TRIMS => 7,
41 NAVDATA_RC_REFERENCES => 8,
42 NAVDATA_PWM => 9,
43 NAVDATA_ALTITUDE => 10,
44 NAVDATA_VISION_RAW => 11,
45 NAVDATA_VISION_OF => 12,
46 NAVDATA_VISION => 13,
47 NAVDATA_VISION_PERF => 14,
48 NAVDATA_TRACKERS_SEND => 15,
49 NAVDATA_VISION_DETECT => 16,
50 NAVDATA_WATCHDOG => 17,
51 NAVDATA_ADC_DATA_FRAME => 18,
52 NAVDATA_VIDEO_STREAM => 19,
53 NAVDATA_CKS => 0xffff,
54 15 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
55 use constant { # Bits for the drone state field
56 15 NAVDATA_STATE_FLYING => 0,
57 NAVDATA_STATE_VIDEO_ENABLED => 1,
58 NAVDATA_STATE_VISION_ENABLED => 2,
59 NAVDATA_STATE_CONTROL_ALGORITHM => 3,
60 NAVDATA_STATE_ALTITUDE_CONTROL_ACTIVE => 4,
61 NAVDATA_STATE_USER_FEEDBACK_ON => 5,
62 NAVDATA_STATE_CONTROL_RECEIVED => 6,
63 NAVDATA_STATE_TRIM_RECEIVED => 7,
64 NAVDATA_STATE_TRIM_RUNNING => 8,
65 NAVDATA_STATE_TRIM_SUCCEEDED => 9,
66 NAVDATA_STATE_NAV_DATA_DEMO_ONLY => 10,
67 NAVDATA_STATE_NAV_DATA_BOOTSTRAP => 11,
68 NAVDATA_STATE_MOTORS_DOWN => 12,
69 # 13 unknown (reserved for future use?)
70 NAVDATA_STATE_GYROMETERS_DOWN => 14,
71 NAVDATA_STATE_BATTERY_TOO_LOW => 15,
72 NAVDATA_STATE_BATTERY_TOO_HIGH => 16,
73 NAVDATA_STATE_TIMER_ELAPSED => 17,
74 NAVDATA_STATE_NOT_ENOUGH_POWER => 18,
75 NAVDATA_STATE_ANGELS_OUT_OF_RANGE => 19,
76 NAVDATA_STATE_TOO_MUCH_WIND => 20,
77 NAVDATA_STATE_ULTRASONIC_SENSOR_DEAF => 21,
78 NAVDATA_STATE_CUTOUT_SYSTEM_DETECTED => 22,
79 NAVDATA_STATE_PIC_VERSION_OK => 23,
80 NAVDATA_STATE_AT_CODED_THREAD_ON => 24,
81 NAVDATA_STATE_NAV_DATA_THREAD_ON => 25,
82 NAVDATA_STATE_VIDEO_THREAD_ON => 26,
83 NAVDATA_STATE_ACQUISITION_THREAD_ON => 27,
84 NAVDATA_STATE_CONTROL_WATCHDOG_DELAYED => 28,
85 NAVDATA_STATE_ADC_WATCHDOG_DELAYED => 29,
86 NAVDATA_STATE_COMMUNICATION_PROBLEM_OCURRED => 30,
87 NAVDATA_STATE_EMERGENCY => 31,
88 15 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
89 15 ▶use constant EXPECT_HEADER_MAGIC_NUM => 0x55667788;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 15 with 'UAV::Pilot::Logger';
92  
93 15 has 'header' => (
94 is => 'ro',
95 isa => 'Int',
96 );
97 15 has 'drone_state' => (
98 is => 'ro',
99 isa => 'Int',
100 );
101 15 has 'sequence_num' => (
102 is => 'ro',
103 isa => 'Int',
104 );
105 15 has 'vision_flag' => (
106 is => 'ro',
107 isa => 'Int',
108 );
109 15 has 'state_flying' => (
110 is => 'ro',
111 isa => 'Bool',
112 );
113 15 has 'state_video_enabled' => (
114 is => 'ro',
115 isa => 'Bool',
116 );
117 15 has 'state_vision_enabled' => (
118 is => 'ro',
119 isa => 'Bool',
120 );
121 15 has 'state_control_algorithm' => (
122 is => 'ro',
123 isa => 'Bool',
124 );
125 15 has 'state_altitude_control_active' => (
126 is => 'ro',
127 isa => 'Bool',
128 );
129 15 has 'state_user_feedback_on' => (
130 is => 'ro',
131 isa => 'Bool',
132 );
133 15 has 'state_control_received' => (
134 is => 'ro',
135 isa => 'Bool',
136 );
137 15 has 'state_trim_received' => (
138 is => 'ro',
139 isa => 'Bool',
140 );
141 15 has 'state_trim_running' => (
142 is => 'ro',
143 isa => 'Bool',
144 );
145 15 has 'state_trim_succeeded' => (
146 is => 'ro',
147 isa => 'Bool',
148 );
149 15 has 'state_nav_data_demo_only' => (
150 is => 'ro',
151 isa => 'Bool',
152 );
153 15 has 'state_nav_data_bootstrap' => (
154 is => 'ro',
155 isa => 'Bool',
156 );
157 15 has 'state_motors_down' => (
158 is => 'ro',
159 isa => 'Bool',
160 );
161 15 has 'state_gyrometers_down' => (
162 is => 'ro',
163 isa => 'Bool',
164 );
165 15 has 'state_battery_too_low' => (
166 is => 'ro',
167 isa => 'Bool',
168 );
169 15 has 'state_battery_too_high' => (
170 is => 'ro',
171 isa => 'Bool',
172 );
173 15 has 'state_timer_elapsed' => (
174 is => 'ro',
175 isa => 'Bool',
176 );
177 15 has 'state_not_enough_power' => (
178 is => 'ro',
179 isa => 'Bool',
180 );
181 15 has 'state_angles_out_of_range' => (
182 is => 'ro',
183 isa => 'Bool',
184 );
185 15 has 'state_too_much_wind' => (
186 is => 'ro',
187 isa => 'Bool',
188 );
189 15 has 'state_ultrasonic_sensor_deaf' => (
190 is => 'ro',
191 isa => 'Bool',
192 );
193 15 has 'state_cutout_system_detected' => (
194 is => 'ro',
195 isa => 'Bool',
196 );
197 15 has 'state_pic_version_ok' => (
198 is => 'ro',
199 isa => 'Bool',
200 );
201 15 has 'state_at_codec_thread_on' => (
202 is => 'ro',
203 isa => 'Bool',
204 );
205 15 has 'state_nav_data_thread_on' => (
206 is => 'ro',
207 isa => 'Bool',
208 );
209 15 has 'state_video_thread_on' => (
210 is => 'ro',
211 isa => 'Bool',
212 );
213 15 has 'state_acquisition_thread_on' => (
214 is => 'ro',
215 isa => 'Bool',
216 );
217 15 has 'state_control_watchdog_delayed' => (
218 is => 'ro',
219 isa => 'Bool',
220 );
221 15 has 'state_adc_watchdog_delayed' => (
222 is => 'ro',
223 isa => 'Bool',
224 );
225 15 has 'state_communication_problem_occured' => (
226 is => 'ro',
227 isa => 'Bool',
228 );
229 15 has 'state_emergency' => (
230 is => 'ro',
231 isa => 'Bool',
232 );
233 15 has 'checksum' => (
234 is => 'ro',
235 isa => 'Int',
236 );
237 15 has 'control_state' => (
238 is => 'ro',
239 isa => 'Maybe[Int]',
240 );
241 15 has 'battery_voltage_percentage' => (
242 is => 'ro',
243 isa => 'Maybe[Int]',
244 );
245 15 has 'pitch' => (
246 is => 'ro',
247 isa => 'Maybe[Num]',
248 );
249 15 has 'roll' => (
250 is => 'ro',
251 isa => 'Maybe[Num]',
252 );
253 15 has 'yaw' => (
254 is => 'ro',
255 isa => 'Maybe[Num]',
256 );
257 15 has 'altitude' => (
258 is => 'ro',
259 isa => 'Maybe[Int]',
260 );
261 15 has 'velocity_x' => (
262 is => 'ro',
263 isa => 'Maybe[Num]',
264 );
265 15 has 'velocity_y' => (
266 is => 'ro',
267 isa => 'Maybe[Num]',
268 );
269 15 has 'velocity_z' => (
270 is => 'ro',
271 isa => 'Maybe[Num]',
272 );
273 15 has 'video_frame_index' => (
274 is => 'ro',
275 isa => 'Maybe[Int]',
276 );
277 15 has 'camera_detection_type' => (
278 is => 'ro',
279 isa => 'Int',
280 );
281 15 has 'packet_bytes' => (
282 is => 'ro',
283 isa => 'ArrayRef[Int]',
284 );
285  
286  
287 sub BUILDARGS
288 {
289 14 ▶ my ($class, $args) = @_;
Subroutine BUILDARGS: called CC=2 SCAR=6.9
Pod documented
290 14 my $packet = $args->{packet};
291  
292 14 my @packet_bytes = unpack "C*", $packet;
293 14 my $header = $class->_convert_endian_32bit( @packet_bytes[0..3] );
294 14 my $state = $class->_convert_endian_32bit( @packet_bytes[4..7] );
295 14 my $seq = $class->_convert_endian_32bit( @packet_bytes[8..11] );
296 14 my $vision_flag = $class->_convert_endian_32bit( @packet_bytes[12..15] );
297 14 ▶ UAV::Pilot::NavPacketException::BadHeader->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $class->EXPECT_HEADER_MAGIC_NUM != $header 1 13 14
298 error => "Header '$header' did not match " . $class->EXPECT_HEADER_MAGIC_NUM,
299 got_header => $header,
300 ) if $class->EXPECT_HEADER_MAGIC_NUM != $header;
301  
302 13 my @option_bytes = @packet_bytes[16..$#packet_bytes];
303  
304 my %new_args = (
305 header => $header,
306 drone_state => $state,
307 sequence_num => $seq,
308 vision_flag => $vision_flag,
309 packet_bytes => \@packet_bytes,
310 13 %{ $class->_parse_state( $state ) },
311 13 %{ $class->_parse_options( @option_bytes ) },
312 );
313 13 return \%new_args;
314 }
315  
316  
317 sub to_string
318 {
319 0 ▶ my ($self) = @_;
Subroutine to_string: not called CC=1 SCAR=6.9
Pod undocumented
320 0 my @strs = ();
321  
322 0 push @strs => "Battery: " . $self->battery_voltage_percentage . '%';
323 0 push @strs => "Pitch: " . $self->pitch;
324 0 push @strs => "Roll: " . $self->roll;
325 0 push @strs => "Yaw: " . $self->yaw;
326 0 push @strs => "Altitude: " . $self->altitude;
327  
328 0 return join ', ', @strs;
329 }
330  
331 sub to_hex_string
332 {
333 1 ▶ my ($self) = @_;
Subroutine to_hex_string: called CC=1 SCAR=0.0
Pod undocumented
334 1 my @packet_bytes = @{ $self->packet_bytes };
335 1 return sprintf( ('%02x' x @packet_bytes), @packet_bytes );
336 }
337  
338  
339 sub _parse_state
340 {
341 13 ▶ my ($class, $state) = @_;
Subroutine _parse_state: called CC=1 SCAR=0.0
342 return {
343 13 state_flying => ($state & 1),
344 state_video_enabled => (($state >> 1) & 1),
345 state_vision_enabled => (($state >> 2) & 1),
346 state_control_algorithm => (($state >> 3) & 1),
347 state_altitude_control_active => (($state >> 4) & 1),
348 state_user_feedback_on => (($state >> 5) & 1),
349 state_control_received => (($state >> 6) & 1),
350 state_trim_received => (($state >> 7) & 1),
351 state_trim_running => (($state >> 8) & 1),
352 state_trim_succeeded => (($state >> 9) & 1),
353 state_nav_data_demo_only => (($state >> 10) & 1),
354 state_nav_data_bootstrap => (($state >> 11) & 1),
355 state_motors_down => (($state >> 12) & 1),
356 state_gyrometers_down => (($state >> 13) & 1),
357 state_battery_too_low => (($state >> 14) & 1),
358 state_battery_too_high => (($state >> 15) & 1),
359 state_timer_elapsed => (($state >> 16) & 1),
360 state_not_enough_power => (($state >> 17) & 1),
361 # 18 unknown (reserved for future use?)
362 state_angles_out_of_range => (($state >> 19) & 1),
363 state_too_much_wind => (($state >> 20) & 1),
364 state_ultrasonic_sensor_deaf => (($state >> 21) & 1),
365 state_cutout_system_detected => (($state >> 22) & 1),
366 state_pic_version_ok => (($state >> 23) & 1),
367 state_at_codec_thread_on => (($state >> 24) & 1),
368 state_nav_data_thread_on => (($state >> 25) & 1),
369 state_video_thread_on => (($state >> 26) & 1),
370 state_acquisition_thread_on => (($state >> 27) & 1),
371 state_control_watchdog_delayed => (($state >> 28) & 1),
372 state_adc_watchdog_delayed => (($state >> 29) & 1),
373 state_communication_problem_occured => (($state >> 30) & 1),
374 state_emergency => (($state >> 31) & 1),
375 };
376 }
377  
378 sub _parse_options
379 {
380 13 ▶ my ($class, @bytes) = @_;
Subroutine _parse_options: called CC=2 SCAR=6.9
381 13 my %args;
382  
383 13 ▶ while( @bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bytes) 259 13 272
384 259 my $id = $class->_convert_endian_16bit( shift(@bytes), shift(@bytes) );
385 259 my $size = $class->_convert_endian_16bit( shift(@bytes), shift(@bytes) );
386  
387 259 my @data = splice @bytes, 0, $size;
388  
389 %args = (
390 %args,
391 259 %{ $class->_parse_option( $id, @data ) },
392 );
393 }
394  
395 13 return \%args;
396 }
397  
398 sub _parse_option
399 {
400 259 ▶ my ($self, $id, @bytes) = @_;
Subroutine _parse_option: called CC=3 SCAR=11.1
401 259 ▶ my $ret =
Branch: 2 branches
BranchTrueFalseTotal
$self->NAVDATA_CKS == $id ? : 13 246 259
$self->NAVDATA_DEMO == $id ? : 246 0 246
402 $self->NAVDATA_CKS == $id ? $self->_parse_option_checksum( @bytes ) :
403 $self->NAVDATA_DEMO == $id ? $self->_parse_option_demo( @bytes ) :
404 {};
405 259 return $ret;
406 }
407  
408 sub _parse_option_checksum
409 {
410 13 ▶ my ($self, @data) = @_;
Subroutine _parse_option_checksum: called CC=2 SCAR=6.9
411 13 @data = reverse @data;
412  
413 13 my $data = 0;
414 13 foreach (@data) {
415 52 $data <<= 8;
416 52 $data |= $_;
417 }
418  
419 13 return { checksum => $data };
420 }
421  
422 sub _parse_option_demo
423 {
424 246 ▶ my ($self, @data) = @_;
Subroutine _parse_option_demo: called CC=4 SCAR=14.0
425 246 ▶ return {} unless scalar(@data) >= 64;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @data >= 64 243 3 246
426 3 my %args;
427  
428 3 $args{control_state} = $self->_convert_endian_32bit( @data[0..3] );
429 3 $args{battery_voltage_percentage} = $self->_convert_endian_32bit( @data[4..7] );
430 3 $args{pitch} = $self->_to_float_32bit_endian( @data[8..11] );
431 3 $args{roll} = $self->_to_float_32bit_endian( @data[12..15] );
432 3 $args{yaw} = $self->_to_float_32bit_endian( @data[16..19] );
433 3 $args{altitude} = $self->_convert_endian_32bit( @data[20..23] );
434 3 $args{velocity_x} = $self->_convert_endian_32bit( @data[24..27] );
435 3 $args{velocity_y} = $self->_convert_endian_32bit( @data[28..31] );
436 3 $args{velocity_z} = $self->_convert_endian_32bit( @data[32..35] );
437 3 $args{video_frame_index} = $self->_convert_endian_32bit( @data[36..39] );
438 # Bytes 40 - 47 are for deprecated parameters
439 3 $args{camera_detection_type} = $self->_convert_endian_32bit( @data[48..51] );
440 # Bytes 52 - 63 are for deprecated parameters
441  
442 # Check for NaN on floats, converting to zero and issuing a warning
443 3 foreach my $field (qw{ pitch roll yaw velocity_x velocity_y velocity_z }) {
444 18 ▶ if( $args{$field} =~ /\A -? nan \z/ix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$field} =~ /\A -? nan \z/iux) 0 18 18
445 0 $self->_logger->warn( "Field '$field' was set to NaN"
446 . ', converting to zero' );
447 0 $args{$field} = 0;
448 }
449 }
450  
451 3 return \%args;
452 }
453  
454 sub _convert_endian_32bit
455 {
456 80 ▶ my ($class, @bytes) = @_;
Subroutine _convert_endian_32bit: called CC=1 SCAR=0.0
457 80 my $val = $bytes[0]
458 | ($bytes[1] << 8)
459 | ($bytes[2] << 16)
460 | ($bytes[3] << 24);
461 80 return $val;
462 }
463  
464 sub _convert_endian_16bit
465 {
466 518 ▶ my ($class, @bytes) = @_;
Subroutine _convert_endian_16bit: called CC=1 SCAR=0.0
467 518 my $val = $bytes[0] | ($bytes[1] << 8);
468 518 return $val;
469 }
470  
471 sub _to_float_32bit
472 {
473 0 ▶ my ($self, @bytes) = @_;
Subroutine _to_float_32bit: not called CC=1 SCAR=6.9
474 0 my $val = $bytes[3]
475 | ($bytes[2] << 8)
476 | ($bytes[1] << 16)
477 | ($bytes[0] << 24);
478 0 my $float = unpack( "f", pack( "l", $val ) );
479 0 return $float;
480 }
481  
482 sub _to_float_32bit_endian
483 {
484 9 ▶ my ($self, @bytes) = @_;
Subroutine _to_float_32bit_endian: called CC=1 SCAR=0.0
485 9 my $val = $bytes[0]
486 | ($bytes[1] << 8)
487 | ($bytes[2] << 16)
488 | ($bytes[3] << 24);
489 9 my $float = unpack( "f", pack( "l", $val ) );
490 9 return $float;
491 }
492  
493  
494 15 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
495 15 __PACKAGE__->meta->make_immutable;
496 15 1;
497 __END__
498  
499  
500 =head1 NAME
501  
502 UAV::Pilot::ARDrone::NavPacket
503  
504 =head1 SYNOPSIS
505  
506 my $nav = UAV::Pilot::ARDrone::NavPacket->new({
507 packet => '...',
508 });
509 say "Roll: " . $nav->roll;
510 say "Pitch: " . $nav->pitch;
511 say "Yaw: " . $nav->yaw;
512  
513 =head1 DESCRIPTION
514  
515 Parses nav packets from the Parrot AR.Drone. Specifically handles packets with the
516 option for demo data, which provides basic roll/pitch/yaw/altitude, among a few other
517 things.
518  
519 Other nav packet types will be parsed in the future.
520  
521 =cut
« ARDrone/Video/Fileno.pm ↑ Summary ARDrone/Driver/Mock.pm »