Class: Icaro
- Inherits:
-
Object
- Object
- Icaro
- Defined in:
- lib/icaro.rb
Instance Method Summary collapse
- #close ⇒ Object (also: #cerrar)
-
#initialize ⇒ Icaro
constructor
look for the device ttyACM initialize the device and check if Icaro board is conected and set while icaro load the device may not be ready so we manage it with a rescue icaro board is normally recognice as a /dev/ttyACM0 how ever in some cases it sets on /dev/ttyACM1 so we read the device and set verify if its ready.
-
#motor(value) ⇒ Object
Motor move robot DC motors 1 forward 2 back 3 left 4 right 5 stop values are strings icaro.motor(‘1’) move icaro forward.
-
#read_analog(value) ⇒ Object
(also: #leer_analogico)
read analog input sensors there are 8 analog input ports icaro.read_analog(‘3’).
-
#read_digital(value) ⇒ Object
(also: #leer_digital)
read digital input sensor there are 4 digital input sensors ports d=icaro.read_digital(‘3’).
-
#servo(servo, value) ⇒ Object
(also: #activar_servo)
move servo motors Icaro board has 5 servo engines ports value is one of 255 integer and move the engine to that position icaro.servo(‘1’,‘x’).
-
#sound(audio, value) ⇒ Object
(also: #sonido)
sound produce a sound on the port icaro.sound(‘2’,‘2’).
-
#start(value) ⇒ Object
(also: #activar)
icaro.start(‘1’).
Constructor Details
#initialize ⇒ Icaro
look for the device ttyACM initialize the device and check if Icaro board is conected and set while icaro load the device may not be ready so we manage it with a rescue icaro board is normally recognice as a /dev/ttyACM0 how ever in some cases it sets on /dev/ttyACM1 so we read the device and set verify if its ready
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/icaro.rb', line 14 def initialize tty=Dir["/dev/ttyACM*"] unless tty.count==0 tty.each do |device| begin @sp=SerialPort::new(device,{"baud"=>9600, "data_bits"=>8, "stop_bits"=>1, "parity"=>0}) @sp.read_timeout=128 @sp.write('b') ttyread=@sp.read if ttyread.include?("icaro") return true else return false end rescue Errno::EACCES => ex STDERR.puts ex. + 'hint: Add your user to group dialout' rescue Errno::ENODEV => ex STDERR.puts ex. rescue Errno::EBUSY => ex STDERR.puts ex. rescue Errno::EIO => ex STDERR.puts ex. rescue IOError => ex STDERR.puts ex. end end else puts "Please connect Icaro" end end |
Instance Method Details
#close ⇒ Object Also known as: cerrar
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/icaro.rb', line 45 def close begin @sp.close return true rescue Errno::EIO => ex STDERR.puts ex. rescue IOError => ex return ex end end |
#motor(value) ⇒ Object
Motor move robot DC motors 1 forward 2 back 3 left 4 right 5 stop values are strings icaro.motor(‘1’) move icaro forward
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/icaro.rb', line 83 def motor(value) if [1,2,3,4,5].include?(value.to_i) begin @sp.write('l') @sp.write(value) return true rescue Errno::EIO => ex STDERR.puts ex. rescue IOError => ex return ex end else return false end end |
#read_analog(value) ⇒ Object Also known as: leer_analogico
read analog input sensors there are 8 analog input ports icaro.read_analog(‘3’)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/icaro.rb', line 102 def read_analog(value) if [1,2,3,4,5,6,7,8].include?(value.to_i) begin @sp.write('e') @sp.write(value) return @sp.read rescue Errno::EIO => ex STDERR.puts ex. rescue IOError => ex return ex end else return false end end |
#read_digital(value) ⇒ Object Also known as: leer_digital
read digital input sensor there are 4 digital input sensors ports d=icaro.read_digital(‘3’)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/icaro.rb', line 123 def read_digital(value) if [1,2,3,4].include?(value.to_i) begin @sp.write('d') @sp.write(value) return @sp.read==1 ? 1:0 rescue Errno::EIO => ex STDERR.puts ex. rescue IOError => ex return ex end else return false end end |
#servo(servo, value) ⇒ Object Also known as: activar_servo
move servo motors Icaro board has 5 servo engines ports value is one of 255 integer and move the engine to that position icaro.servo(‘1’,‘x’)
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/icaro.rb', line 145 def servo(servo,value) if [1,2,3,4,5].include?(servo.to_i) begin @sp.write('m') @sp.write(servo) @sp.write(value.to_i) return true rescue Errno::EIO => ex STDERR.puts ex. rescue IOError => ex return ex end else return false end end |
#sound(audio, value) ⇒ Object Also known as: sonido
sound produce a sound on the port icaro.sound(‘2’,‘2’)
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/icaro.rb', line 166 def sound(audio,value) begin @sp.write('a') @sp.write(audio) @sp.write(value) sleep(0.01) @sp.write('s') @sp.write('0') return true rescue Errno::EIO => ex STDERR.puts ex. rescue IOError => ex return ex end end |
#start(value) ⇒ Object Also known as: activar
icaro.start(‘1’)
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/icaro.rb', line 60 def start(value) begin @sp.write('s') @sp.write(value) return true rescue Errno::EIO => ex STDERR.puts ex. rescue IOError => ex return ex end end |