class Icaro
Public Class Methods
Source
# 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.message + 'hint: Add your user to group dialout' rescue Errno::ENODEV => ex STDERR.puts ex.message rescue Errno::EBUSY => ex STDERR.puts ex.message rescue Errno::EIO => ex STDERR.puts ex.message rescue IOError => ex STDERR.puts ex.message end end else puts "Please connect Icaro" end end
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
Public Instance Methods
Source
# File lib/icaro.rb, line 45 def close begin @sp.close return true rescue Errno::EIO => ex STDERR.puts ex.message rescue IOError => ex return ex end end
Also aliased as: cerrar
Source
# 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.message rescue IOError => ex return ex end else return false end end
Motor move robot DC motors
1 forward 2 back 3 left 4 right 5 stop values are strings icaro.motor('1') move icaro forward
Source
# 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.message rescue IOError => ex return ex end else return false end end
read analog input sensors there are 8 analog input ports
icaro.read_analog('3')
Also aliased as: leer_analogico
Source
# 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.message rescue IOError => ex return ex end else return false end end
read digital input sensor there are 4 digital input sensors ports
d=icaro.read_digital('3')
Also aliased as: leer_digital
Source
# 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.message rescue IOError => ex return ex end else return false end end
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')
Also aliased as: activar_servo
Source
# 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.message rescue IOError => ex return ex end end
sound produce a sound on the port
icaro.sound('2','2')
Also aliased as: sonido