| 1 | package UAV::Pilot::WumpusRover::Server::Backend::RaspberryPiI2C; | ||
| 2 | 1 | ▶ | use v5.14; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use UAV::Pilot::WumpusRover::Server::Backend; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use UAV::Pilot::WumpusRover::PacketFactory; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use HiPi::Device::I2C (); | ||
| 8 | use HiPi::BCM2835::I2C qw( :all ); | ||
| 9 | use Time::HiRes (); | ||
| 10 | |||
| 11 | |||
| 12 | has '_i2c' => ( | ||
| 13 | is => 'ro', | ||
| 14 | isa => 'HiPi::BCM2835::I2C', | ||
| 15 | writer => '_set_i2c', | ||
| 16 | ); | ||
| 17 | has 'slave_addr' => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => 'Int', | ||
| 20 | default => 0x09, | ||
| 21 | ); | ||
| 22 | has 'throttle_register' => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => 'Int', | ||
| 25 | default => 0x01, | ||
| 26 | ); | ||
| 27 | has 'turn_register' => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => 'Int', | ||
| 30 | default => 0x02, | ||
| 31 | ); | ||
| 32 | has 'i2c_device' => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => 'Int', | ||
| 35 | default => BB_I2C_PERI_1, | ||
| 36 | ); | ||
| 37 | has '_last_time_packet_sent' => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => 'Num', | ||
| 40 | default => 0.0, | ||
| 41 | ); | ||
| 42 | has 'ch1_max_out' => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'Int', | ||
| 45 | default => 2000, # Could be 2300, depending on ESC | ||
| 46 | ); | ||
| 47 | has 'ch1_min_out' => ( | ||
| 48 | is => 'ro', | ||
| 49 | isa => 'Int', | ||
| 50 | default => 1000, # Could be 700, depending on ESC | ||
| 51 | ); | ||
| 52 | has 'ch2_max_out' => ( | ||
| 53 | is => 'ro', | ||
| 54 | isa => 'Int', | ||
| 55 | default => 180, | ||
| 56 | ); | ||
| 57 | has 'ch2_min_out' => ( | ||
| 58 | is => 'ro', | ||
| 59 | isa => 'Int', | ||
| 60 | default => 0, | ||
| 61 | ); | ||
| 62 | has 'ch3_max_out' => ( | ||
| 63 | is => 'ro', | ||
| 64 | isa => 'Int', | ||
| 65 | default => 100, | ||
| 66 | ); | ||
| 67 | has 'ch3_min_out' => ( | ||
| 68 | is => 'ro', | ||
| 69 | isa => 'Int', | ||
| 70 | default => 0, | ||
| 71 | ); | ||
| 72 | has 'ch4_max_out' => ( | ||
| 73 | is => 'ro', | ||
| 74 | isa => 'Int', | ||
| 75 | default => 100, | ||
| 76 | ); | ||
| 77 | has 'ch4_min_out' => ( | ||
| 78 | is => 'ro', | ||
| 79 | isa => 'Int', | ||
| 80 | default => 0, | ||
| 81 | ); | ||
| 82 | has 'ch5_max_out' => ( | ||
| 83 | is => 'ro', | ||
| 84 | isa => 'Int', | ||
| 85 | default => 100, | ||
| 86 | ); | ||
| 87 | has 'ch5_min_out' => ( | ||
| 88 | is => 'ro', | ||
| 89 | isa => 'Int', | ||
| 90 | default => 0, | ||
| 91 | ); | ||
| 92 | has 'ch6_max_out' => ( | ||
| 93 | is => 'ro', | ||
| 94 | isa => 'Int', | ||
| 95 | default => 100, | ||
| 96 | ); | ||
| 97 | has 'ch6_min_out' => ( | ||
| 98 | is => 'ro', | ||
| 99 | isa => 'Int', | ||
| 100 | default => 0, | ||
| 101 | ); | ||
| 102 | has 'ch7_max_out' => ( | ||
| 103 | is => 'ro', | ||
| 104 | isa => 'Int', | ||
| 105 | default => 100, | ||
| 106 | ); | ||
| 107 | has 'ch7_min_out' => ( | ||
| 108 | is => 'ro', | ||
| 109 | isa => 'Int', | ||
| 110 | default => 0, | ||
| 111 | ); | ||
| 112 | has 'ch8_max_out' => ( | ||
| 113 | is => 'ro', | ||
| 114 | isa => 'Int', | ||
| 115 | default => 100, | ||
| 116 | ); | ||
| 117 | has 'ch8_min_out' => ( | ||
| 118 | is => 'ro', | ||
| 119 | isa => 'Int', | ||
| 120 | default => 0, | ||
| 121 | ); | ||
| 122 | |||
| 123 | with 'UAV::Pilot::WumpusRover::Server::Backend'; | ||
| 124 | with 'UAV::Pilot::Logger'; | ||
| 125 | |||
| 126 | |||
| 127 | sub BUILD | ||
| 128 | { | ||
| 129 | my ($self) = @_; | ||
| 130 | my $logger = $self->_logger; | ||
| 131 | $logger->info( 'Attempting to init i2c comm on slave addr [' | ||
| 132 | . $self->slave_addr . ']' ); | ||
| 133 | |||
| 134 | my $i2c = HiPi::BCM2835::I2C->new( | ||
| 135 | peripheral => $self->i2c_device, | ||
| 136 | address => $self->slave_addr, | ||
| 137 | ); | ||
| 138 | $self->_set_i2c( $i2c ); | ||
| 139 | |||
| 140 | $logger->info( 'Init i2c comm done' ); | ||
| 141 | return 1; | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | sub _packet_request_startup | ||
| 146 | { | ||
| 147 | my ($self, $packet) = @_; | ||
| 148 | $self->_set_started( 1 ); | ||
| 149 | return 1; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub _packet_radio_trims | ||
| 153 | { | ||
| 154 | # Ignore | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _packet_radio_out | ||
| 158 | { | ||
| 159 | my ($self, $packet, $server) = @_; | ||
| 160 | $self->_logger->info( 'Writing packet: ' . ref($packet) ); | ||
| 161 | |||
| 162 | my $throttle = $self->_map_ch1_value( $server, $packet->ch1_out ); | ||
| 163 | my $turn = $self->_map_ch2_value( $server, $packet->ch2_out ); | ||
| 164 | my @throttle_bytes = ( ($throttle >> 8), ($throttle & 0xff) ); | ||
| 165 | my @turn_bytes = ( ($turn >> 8), ($turn & 0xff) ); | ||
| 166 | |||
| 167 | $self->_write_packet( $self->throttle_register, @throttle_bytes ); | ||
| 168 | $self->_write_packet( $self->turn_register, @turn_bytes ); | ||
| 169 | return 1; | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | sub _write_packet | ||
| 174 | { | ||
| 175 | my ($self, $register, @bytes) = @_; | ||
| 176 | my $logger = $self->_logger; | ||
| 177 | |||
| 178 | eval { | ||
| 179 | $logger->info( "Writing [@bytes] to register [$register]" ); | ||
| 180 | $self->_i2c->bus_write( $register, @bytes ); | ||
| 181 | }; | ||
| 182 | if( $@ ) { | ||
| 183 | $logger->warn( 'Could not write i2c data: ' . $@ ); | ||
| 184 | } | ||
| 185 | |||
| 186 | return 1; | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | no Moose; | ||
| 191 | __PACKAGE__->meta->make_immutable; | ||
| 192 | 1; | ||
| 193 | __END__ | ||
| 194 | |||
| 195 | |||
| 196 | =head1 NAME | ||
| 197 | |||
| 198 | UAV::Pilot::WumpusRover::Server::Backend::RaspberryPiI2C | ||
| 199 | |||
| 200 | =head1 DESCRIPTION | ||
| 201 | |||
| 202 | Does the C<UAV::Pilot::WumpusRover::Server::Backend> role. Communicates using | ||
| 203 | the Raspberry Pi's I2C interface, using a protocol compatible with the | ||
| 204 | WumpusRover Arduino code. | ||
| 205 | |||
| 206 | The Arduino code and hardware description are available at: | ||
| 207 | |||
| 208 | https://github.com/frezik/wumpus-rover | ||
| 209 | |||
| 210 | =cut | ||