| 1 | package RobotC::Wrapper::Utils; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use YAML qw/LoadFile/; | ||
| 6 | use Carp qw/croak/; | ||
| 7 | |||
| 8 | our $VERSION = 1.00; | ||
| 9 | |||
| 10 | use parent qw/RobotC::Wrapper/; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ( $class, %opt ) = @_; | ||
| 14 | |||
| 15 | if (!$opt{config}) { | ||
| 16 | croak "you must provide a config/yaml file"; | ||
| 17 | } | ||
| 18 | |||
| 19 | my $self = $class->SUPER::new; | ||
| 20 | $self->{conf} = LoadFile( $opt{config} ); | ||
| 21 | |||
| 22 | return bless $self, $class; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub setup { | ||
| 26 | my $self = shift; | ||
| 27 | $self->auto( $self->{conf}->{auto}->{state} ); | ||
| 28 | $self->reflect( | ||
| 29 | port => $self->{conf}->{reflect}->{port}, | ||
| 30 | bool => $self->{conf}->{reflect}->{state} | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub drive { | ||
| 35 | my $self = shift; | ||
| 36 | $self->start_void( 'drive' ); | ||
| 37 | $self->motor( | ||
| 38 | port => $self->{conf}->{motor_port}->{right}, | ||
| 39 | speed => $self->{conf}->{speed}->{forward} | ||
| 40 | ); | ||
| 41 | $self->motor( | ||
| 42 | port => $self->{conf}->{motor_port}->{left}, | ||
| 43 | speed => $self->{conf}->{speed}->{forward} | ||
| 44 | ); | ||
| 45 | $self->end; | ||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub turn_left { | ||
| 50 | my $self = shift; | ||
| 51 | |||
| 52 | $self->start_void('turn_left'); | ||
| 53 | $self->motor( | ||
| 54 | port => $self->{conf}->{motor_port}->{right}, | ||
| 55 | speed => $self->{conf}->{speed}->{reverse} | ||
| 56 | ); | ||
| 57 | $self->motor( | ||
| 58 | port => $self->{conf}->{motor_port}->{left}, | ||
| 59 | speed => $self->{conf}->{speed}->{forward} | ||
| 60 | ); | ||
| 61 | $self->end; | ||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub halt { | ||
| 66 | my $self = shift; | ||
| 67 | $self->start_void( 'halt' ); | ||
| 68 | $self->motor( | ||
| 69 | port => $self->{conf}->{motor_port}->{right}, | ||
| 70 | speed => $self->{conf}->{speed}->{stopped} | ||
| 71 | ); | ||
| 72 | $self->motor( | ||
| 73 | port => $self->{conf}->{motor_port}->{left}, | ||
| 74 | speed => $self->{conf}->{speed}->{stopped} | ||
| 75 | ); | ||
| 76 | $self->end; | ||
| 77 | return $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub turn_right { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | $self->start_void( 'turn_right' ); | ||
| 84 | $self->motor( | ||
| 85 | port => $self->{conf}->{motor_port}->{right}, | ||
| 86 | speed => $self->{conf}->{speed}->{forward} | ||
| 87 | ); | ||
| 88 | $self->motor( | ||
| 89 | port => $self->{conf}->{motor_port}->{left}, | ||
| 90 | speed => $self->{conf}->{speed}->{reverse} | ||
| 91 | ); | ||
| 92 | $self->end; | ||
| 93 | return $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub set_cont { | ||
| 97 | my $self = shift; | ||
| 98 | $self->start_void( 'cont' ); | ||
| 99 | $self->cont( | ||
| 100 | port => $self->{conf}->{motor_port}->{right}, | ||
| 101 | channel => $self->{conf}->{channel}->{2} | ||
| 102 | ); | ||
| 103 | $self->cont( | ||
| 104 | port => $self->{conf}->{motor_port}->{left}, | ||
| 105 | channel => $self->{conf}->{channel}->{1} | ||
| 106 | ); | ||
| 107 | $self->end; | ||
| 108 | return $self; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub basic_movements { | ||
| 112 | my $self = shift; | ||
| 113 | $self->setup; | ||
| 114 | $self->drive; | ||
| 115 | $self->turn_left; | ||
| 116 | $self->turn_right; | ||
| 117 | $self->halt; | ||
| 118 | $self->set_cont; | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; |