WumpusRover/Control/Event.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« WumpusRover/Video/Mock.pm ↑ Summary WumpusRover/Packet/Ack.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::Control::Event;
25 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 # TODO relies on SDL
29 3 ▶use UAV::Pilot::SDL::Joystick;
Subroutine BEGIN: called CC=1 SCAR=2.6
30  
31 use constant CONTROL_UPDATE_TIME => 1 / 60;
32  
33 extends 'UAV::Pilot::WumpusRover::Control';
34  
35 has '_packet_queue' => (
36 is => 'ro',
37 isa => 'ArrayRef[UAV::Pilot::WumpusRover::Packet]',
38 default => sub { [] },
39 traits => ['Array'],
40 handles => {
41 _add_to_packet_queue => 'push',
42 },
43 );
44 has 'joystick_num' => (
45 is => 'ro',
46 isa => 'Int',
47 default => 0,
48 );
49  
50  
51 sub init_event_loop
52 {
53 my ($self, $cv, $event) = @_;
54 my $logger = $self->_logger;
55  
56 $logger->info( "Starting packet send event" );
57 my $event_timer; $event_timer = AnyEvent->timer(
58 after => 0.01,
59 interval => $self->CONTROL_UPDATE_TIME,
60 cb => sub {
61 $logger->info( "Event firing off packet send event" );
62 $self->send_move_packet;
63 $event_timer;
64 },
65 );
66  
67 $logger->info( "Starting ack callback event" );
68 $self->driver->set_ack_callback( sub {
69 my ($orig_packet, $ack_packet) = @_;
70 $event->send_event( 'ack_recv', $orig_packet, $ack_packet );
71 });
72  
73 $event->add_event( UAV::Pilot::SDL::Joystick->EVENT_NAME, sub {
74 my (@args) = @_;
75 $logger->info( 'Received joystick event' );
76 return $self->_process_sdl_input( @args );
77 });
78  
79 $logger->info( "Done setting events" );
80 return 1;
81 }
82  
83 sub _process_sdl_input
84 {
85 my ($self, $args) = @_;
86 return 0 if $args->{joystick_num} != $self->joystick_num;
87  
88 my $turn = sprintf( '%.0f', $self->_map_values(
89 UAV::Pilot::SDL::Joystick->MIN_AXIS_INT,
90 UAV::Pilot::SDL::Joystick->MAX_AXIS_INT,
91 -90, 90,
92 $args->{roll},
93 ) );
94 my $throttle = sprintf( '%.0f', $self->_map_values(
95 UAV::Pilot::SDL::Joystick->MIN_AXIS_INT,
96 UAV::Pilot::SDL::Joystick->MAX_AXIS_INT,
97 0, 100,
98 $args->{throttle},
99 ) );
100  
101 $self->turn( $turn );
102 $self->throttle( $throttle );
103  
104 return 1;
105 }
106  
107  
108 no Moose;
109 __PACKAGE__->meta->make_immutable;
110 1;
111 __END__
112  
113  
114 =head1 NAME
115  
116 UAV::Pilot::WumpusRoverControl::Event
117  
118 =head1 SYNOPSIS
119  
120 my $cv = AnyEvent->condvar;
121 my $event = UAV::Pilot::EasyEvent->new({
122 condvar => $cv,
123 });
124
125 my $driver = UAV::Pilot::WumpusRover::Driver->new({
126 host => $hostname,
127 });
128 $driver->connect;
129
130 my $control = UAV::Pilot::WumpusRover::Control::Event->new({
131 driver => $driver,
132 joystick_num => 0,
133 });
134 $control->init_event_loop( $cv, $event );
135  
136 $cv->recv;
137  
138 =head1 DESCRIPTION
139  
140 An event-driven version of the WumpusRover Control.
141  
142 =head1 METHODS
143  
144 =head2 init_event_loop
145  
146 init_event_loop( $cv, $event );
147  
148 Sets up the event loop. Takes C<$cv> (an C<AnyEvent::Condvar>) and C<$event>
149 (a C<UAV::Pilot::EasyEvent>).
150  
151 Will listen for joystick events.
152  
153 =cut
« WumpusRover/Video/Mock.pm ↑ Summary WumpusRover/Packet/Ack.pm »