icaro 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/icaro.rb +150 -0
  2. metadata +46 -0
data/lib/icaro.rb ADDED
@@ -0,0 +1,150 @@
1
+ #Icaro Ruby Api
2
+ #
3
+ require 'rubygems'
4
+ require 'serialport'
5
+ class Icaro
6
+
7
+ #added aliases to use the same names as the apicaro on python api
8
+ alias :activar :start
9
+ alias :cerrar :close
10
+ alias :leer_analogico :read_analog
11
+ alias :leer_digital :read_digital
12
+ alias :activar_servo :servo
13
+ alias :sonido :sound
14
+
15
+ #look for the device ttyACM
16
+ #initialize the device and check if Icaro board is conected and set
17
+ # while icaro load the device may not be ready so we manage it with a rescue
18
+ # icaro board is normally recognice as a /dev/ttyACM0 how ever in some cases
19
+ # it sets on /dev/ttyACM1 so we read the device and set verify if its ready
20
+ def initialize
21
+ tty=Dir["/dev/ttyACM*"]
22
+ unless tty.count==0
23
+ tty.each do |device|
24
+ begin
25
+ @sp=SerialPort::new(device,{"baud"=>9600, "data_bits"=>8, "stop_bits"=>1, "parity"=>0})
26
+ @sp.read_timeout=128
27
+ @sp.write('b')
28
+ ttyread=@sp.read
29
+ if ttyread.include?("icaro")
30
+ return true
31
+ else
32
+ return false
33
+ end
34
+ rescue Errno::ENODEV => ex
35
+ STDERR.puts ex.message
36
+ rescue Errno::EBUSY => ex
37
+ STDERR.puts ex.message
38
+ rescue IOError => ex
39
+ STDERR.puts ex.message
40
+ end
41
+ end
42
+ else
43
+ puts "Please connect Icaro"
44
+ end
45
+ end
46
+ def close
47
+ @sp.close
48
+ end
49
+ # set Icaro board to ready status
50
+ # icaro=Icaro.new
51
+ # icaro.start('1')
52
+ def start(value)
53
+ begin
54
+ @sp.write('s')
55
+ @sp.write(value)
56
+ return true
57
+ rescue IOError => ex
58
+ return ex
59
+ end
60
+ end
61
+ #Motor move robot DC motors
62
+ # 1 forward
63
+ # 2 back
64
+ # 3 left
65
+ # 4 right
66
+ # 5 stop
67
+ # values are strings
68
+ # icaro.motor('1') move icaro forward
69
+ #
70
+ def motor(value)
71
+ if [1,2,3,4].include?(value.to_i)
72
+ begin
73
+ @sp.write('l')
74
+ @sp.write(value)
75
+ return true
76
+ rescue IOError => ex
77
+ return ex
78
+ end
79
+ else
80
+ return false
81
+ end
82
+ end
83
+ #read analog input sensors
84
+ #there are 8 analog input ports
85
+ # icaro.read_analog('3')
86
+ def read_analog(value)
87
+ if [1,2,3,4,5,6,7,8].include?(value.to_i)
88
+ begin
89
+ @sp.write('e')
90
+ @sp.write(value)
91
+ return @sp.read
92
+ rescue IOError => ex
93
+ return ex
94
+ end
95
+ else
96
+ return false
97
+ end
98
+ end
99
+ #read digital input sensor
100
+ #there are 4 digital input sensors ports
101
+ # d=icaro.read_digital('3')
102
+ def read_digital(value)
103
+ if [1,2,3,4].include?(value.to_i)
104
+ begin
105
+ @sp.write('d')
106
+ @sp.write(value)
107
+ return @sp.read==1 ? 1:0
108
+ rescue IOError => ex
109
+ return ex
110
+ end
111
+ else
112
+ return false
113
+ end
114
+ end
115
+ #move servo motors
116
+ #Icaro board has 5 servo engines ports
117
+ #value is one of 255 characters and move the engine to that position
118
+ # icaro.servo('1','x')
119
+ def servo(servo,value)
120
+ if [1,2,3,4,5].include?(servo.to_i)
121
+ begin
122
+ @sp.write('m')
123
+ @sp.write(servo)
124
+ @sp.write(value)
125
+ return true
126
+ rescue IOError => ex
127
+ return ex
128
+ end
129
+ else
130
+ return false
131
+ end
132
+ end
133
+ #sound produce a sound on the port
134
+ # icaro.sound('2','2')
135
+ def sound(audio,value)
136
+ begin
137
+ @sp.write('a')
138
+ @sp.write(audio)
139
+ @sp.write(value)
140
+ sleep(0.01)
141
+ @sp.write('s')
142
+ @sp.write('0')
143
+ return true
144
+ rescue IOError => ex
145
+ return ex
146
+ end
147
+
148
+ end
149
+ end
150
+
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: icaro
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alejandro Pérez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Ruby api for Icaro Robot
15
+ email: alejandro.perez.torres@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/icaro.rb
21
+ homepage: http://rubygems.org/gems/icaro
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements:
40
+ - serialport
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.25
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Icaro Ruby Api
46
+ test_files: []