WumpusRover/Driver.pm

filter: Statementstmt 86.0% 80 / 93 Branchbran 50.0% 3 / 6 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 2 totaltotal 83.6% 102 / 122 CC 6 SCAR 6.5
CC
6
Cov
84%
_init_connection
6.0
send_radio_output_packet
3.0
_process_ack
2.0
CRAP 1.9
« WumpusRover/PacketFactory.pm ↑ Summary WumpusRover/Control.pm »
1 # Copyright (c) 2014 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::WumpusRover::Driver;
25 6 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use UAV::Pilot::WumpusRover;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use UAV::Pilot::WumpusRover::PacketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 6 ▶use constant MAX_PACKET_QUEUE_LENGTH => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34  
35 6 has 'port' => (
36 is => 'ro',
37 isa => 'Int',
38 default => UAV::Pilot::WumpusRover::DEFAULT_PORT,
39 );
40 6 has 'host' => (
41 is => 'ro',
42 isa => 'Str',
43 );
44 6 has '_socket' => (
45 is => 'rw',
46 isa => 'IO::Socket::INET',
47 );
48 has '_ack_callback' => (
49 is => 'rw',
50 isa => 'CodeRef',
51 4 ▶ default => sub { sub {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 6 writer => 'set_ack_callback',
53 );
54 has '_packet_queue' => (
55 is => 'ro',
56 isa => 'HashRef',
57 default => sub {
58 4 ▶ my %tie = ();
Subroutine __ANON__: called CC=1 SCAR=0.0
59 4 tie %tie, 'Tie::IxHash';
60 4 \%tie;
61 },
62 6 );
63  
64 6 with 'UAV::Pilot::Driver';
65 6 with 'UAV::Pilot::Logger';
66  
67  
68 sub connect
69 {
70 3 ▶ my ($self) = @_;
Subroutine connect: called CC=1 SCAR=0.0
Pod undocumented
71 3 my $logger = $self->_logger;
72  
73 3 $logger->info( 'Connecting . . . ' );
74 3 $self->_init_connection;
75  
76 3 my $startup_request = UAV::Pilot::WumpusRover::PacketFactory->fresh_packet(
77 'RequestStartupMessage' );
78 # TODO find out what Ardupilot wants for these params
79 3 $startup_request->system_type( 0x00 );
80 3 $startup_request->system_id( 0x00 );
81 3 $logger->info( 'Sending RequestStartupMessage packet' );
82 3 $self->_send_packet( $startup_request );
83  
84 3 return 1;
85 }
86  
87  
88 sub send_radio_output_packet
89 {
90 7 ▶ my ($self, @channels) = @_;
Subroutine send_radio_output_packet: called CC=3 SCAR=11.0
Pod undocumented
91 7 my $radio_packet = UAV::Pilot::WumpusRover::PacketFactory->fresh_packet(
92 'RadioOutputs' );
93  
94 7 foreach my $i (1..8) {
95 56 ▶ my $value = $channels[$i-1] // 0;
Condition100%2/2
$channels[$i - 1] // 0
l!l
1343
MC/DC100%1/1
$channels[$i - 1] // 0
$channels[$i - 1]
Truth table100%2/2
A
A= $channels[$i - 1]
Aresult
11
00
96 56 my $packet_field = 'ch' . $i . '_out';
97 56 $radio_packet->$packet_field( $value );
98 }
99  
100 7 $self->_send_packet( $radio_packet );
101 7 return 1;
102 }
103  
104  
105 sub _init_connection
106 {
107 0 ▶ my ($self) = @_;
Subroutine _init_connection: not called CC=2 SCAR=17.9
108 0 my $logger = $self->_logger;
109  
110 0 $logger->info( 'Open UDP socket to ' . $self->host . ':' . $self->port );
111 0 ▶ my $socket = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::INET"->new("Proto", "udp", "PeerHost", $self->host, "PeerPort", $self->port) or "UAV::Pilot::IOException"->throw({"error", 'Could not open socket: ' . $!}) 0 0 0
112 Proto => 'udp',
113 PeerHost => $self->host,
114 PeerPort => $self->port,
115 ) or UAV::Pilot::IOException->throw({
116 error => 'Could not open socket: ' . $!,
117 });
118 0 $logger->info( 'Done opening socket' );
119  
120 0 $self->_socket( $socket );
121 0 return 1;
122 }
123  
124 sub _packet_queue_size
125 {
126 5 ▶ my ($self) = @_;
Subroutine _packet_queue_size: called CC=1 SCAR=0.0
127 5 return scalar keys %{ $self->_packet_queue };
128 }
129  
130 sub _send_packet
131 {
132 0 ▶ my ($self, $packet) = @_;
Subroutine _send_packet: not called CC=1 SCAR=6.9
133 0 $packet->make_checksum_clean;
134 0 $packet->write( $self->_socket );
135 0 $self->_add_to_packet_queue( $packet );
136 0 return 1;
137 }
138  
139 sub _process_ack
140 {
141 11 ▶ my ($self, $ack) = @_;
Subroutine _process_ack: called CC=2 SCAR=7.1
142  
143 11 my $key = $ack->make_ack_packet_queue_key;
144 11 $self->_logger->info( "Processing ack packet with key [$key]" );
145  
146 11 my $orig_packet = delete $self->_packet_queue->{$key};
147 11 ▶ if( defined $orig_packet ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orig_packet) { } 11 0 11
148 11 $self->_ack_callback->( $orig_packet, $ack )
149 }
150 else {
151 0 $self->_logger->warn( "Received Ack packet for key $key, but couldn't"
152 . " find a matching packet" );
153 }
154  
155 11 return 1;
156 }
157  
158 sub _add_to_packet_queue
159 {
160 32 ▶ my ($self, $packet) = @_;
Subroutine _add_to_packet_queue: called CC=1 SCAR=0.0
161  
162 32 my $key = $packet->make_packet_queue_map_key;
163 32 $self->_logger->info( "Adding packet to queue for key $key" );
164  
165 32 $self->_reduce_queue_length_to_max;
166 32 $self->_packet_queue->{$key} = $packet;
167  
168 32 return 1;
169 }
170  
171 sub _reduce_queue_length_to_max
172 {
173 32 ▶ my ($self) = @_;
Subroutine _reduce_queue_length_to_max: called CC=2 SCAR=6.9
174  
175 32 my @keys = keys %{ $self->_packet_queue };
176 32 my $packets_removed = 0;
177 32 ▶ while( scalar(@keys) >= $self->MAX_PACKET_QUEUE_LENGTH ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys >= $self->MAX_PACKET_QUEUE_LENGTH) 2 32 34
178 2 my $next_key = shift @keys;
179 2 $self->_logger->warn( "Queue too large, removing packet $next_key" );
180  
181 2 delete $self->_packet_queue->{$next_key};
182 2 $packets_removed++;
183 }
184  
185 32 return $packets_removed;
186 }
187  
188  
189 6 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
190 6 __PACKAGE__->meta->make_immutable;
191 6 1;
192 __END__
193  
194  
195 =head1 NAME
196  
197 UAV::Pilot::WumpusRover::Driver
198  
199 =head1 SYNOPSIS
200  
201 use UAV::Pilot::WumpusRover::Driver;
202
203 my $driver = UAV::Pilot::WumpusRover::Driver->new({
204 host => '10.0.0.10',
205 });
206 $driver->connect;
207  
208 $driver->send_radio_output_packet( 90, 100 );
209  
210 =head1 DESCRIPTION
211  
212 Driver for the WumpusRover. Does the C<UAV::Pilot::Driver> role.
213  
214 This is not intended to be used directly. See
215 C<UAV::Pilot::WumpusRover::Control::Event> for the best way to control the
216 WumpusRover from client code.
217  
218 =cut
« WumpusRover/PacketFactory.pm ↑ Summary WumpusRover/Control.pm »