robotic-arm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/robotic-arm.rb +207 -0
  2. metadata +64 -0
@@ -0,0 +1,207 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: robotic-arm.rb
4
+
5
+ require 'libusb'
6
+
7
+
8
+ OFF = 0x00
9
+
10
+ class RoboticArm
11
+
12
+ attr_reader :led, :wrist, :elbow, :shoulder, :base, :grip
13
+
14
+ class Component
15
+
16
+ def initialize(robot_arm) @ra = robot_arm end
17
+ def active?() @active end
18
+
19
+ protected
20
+
21
+ def activate(switch, val, seconds)
22
+ @active = val >= 0
23
+ @prev_val = val
24
+ @ra.command(switch, val)
25
+ if seconds > 0 then
26
+ sleep seconds
27
+ @ra.command(switch, -(val))
28
+ @active = false
29
+ end
30
+ end
31
+ end
32
+
33
+ class Led < Component
34
+
35
+ def initialize(robot_arm)
36
+
37
+ super(robot_arm)
38
+ @switch = 2
39
+ end
40
+
41
+ def on(seconds=0)
42
+ activate(@switch, 0x01, seconds) unless on?
43
+ end
44
+
45
+ def off(seconds=0)
46
+ activate(@switch, -0x01, seconds) if on?
47
+ end
48
+
49
+ alias on? active?
50
+ end
51
+
52
+ class ComponentMoving < Component
53
+
54
+ def initialize(robot_arm)
55
+ super(robot_arm)
56
+ @switch, @val = 0, OFF
57
+ end
58
+
59
+ def activate(switch, val, seconds=0)
60
+ if active? then
61
+ @val = @prev_val
62
+ stop
63
+ end
64
+ super(switch, val, seconds)
65
+ end
66
+
67
+ def stop()
68
+ if moving? then
69
+ @active = false
70
+ activate(@switch, -(@prev_val))
71
+ end
72
+ end
73
+
74
+ alias moving? active?
75
+
76
+ end
77
+
78
+ class ComponentUpDown < ComponentMoving
79
+
80
+ def up(seconds=0)
81
+ activate(@switch, @upval,seconds)
82
+ end
83
+
84
+ def down(seconds=0)
85
+ activate(@switch, @downval, seconds)
86
+ end
87
+ end
88
+
89
+ class Shoulder < ComponentUpDown
90
+
91
+ def initialize(robot_arm)
92
+ super(robot_arm)
93
+ @upval, @downval = 0x40, 0x80
94
+ end
95
+ end
96
+
97
+ class Elbow < ComponentUpDown
98
+
99
+ def initialize(robot_arm)
100
+ super(robot_arm)
101
+ @upval, @downval = 0x10, 0x20
102
+ end
103
+ end
104
+
105
+ class Wrist < ComponentUpDown
106
+
107
+ def initialize(robot_arm)
108
+ super(robot_arm)
109
+ @upval, @downval = 0x04, 0x08
110
+ end
111
+ end
112
+
113
+ class Base < ComponentMoving
114
+
115
+ def initialize(robot_arm)
116
+ super(robot_arm)
117
+ @switch = 1
118
+ end
119
+
120
+ def left(seconds=0)
121
+ activate(@switch, @val=0x02, seconds)
122
+ end
123
+
124
+ def right(seconds=0)
125
+ activate(@switch, @val=0x01, seconds)
126
+ end
127
+ end
128
+
129
+ class Grip < ComponentMoving
130
+
131
+ def open(seconds=0)
132
+ activate(@switch, @val=0x02, seconds)
133
+ end
134
+
135
+ def close(seconds=0)
136
+ activate(@switch, @val=0x01, seconds)
137
+ end
138
+ end
139
+
140
+
141
+ def initialize()
142
+
143
+ # Get the device object
144
+ usb = LIBUSB::Context.new
145
+ arm = usb.devices(:idVendor => 0x1267, :idProduct => 0x0000).first
146
+
147
+ if arm == nil
148
+ puts "Arm not found!"
149
+ exit
150
+ end
151
+
152
+ # Take control of the device
153
+ @handle = arm.open
154
+ @handle.claim_interface(0)
155
+
156
+ @led = Led.new self
157
+ @wrist = Wrist.new self
158
+ @elbow = Elbow.new self
159
+ @shoulder = Shoulder.new self
160
+ @base = Base.new self
161
+ @grip = Grip.new self
162
+ @register = [OFF,OFF,OFF]
163
+ end
164
+
165
+ def left(seconds=0) @base.left seconds end
166
+ def right(seconds=0) @base.right seconds end
167
+
168
+ # register and invoke the robotic action
169
+ #
170
+ def command(switch, val)
171
+
172
+ @register[switch] += val
173
+ handle_command
174
+ end
175
+
176
+ # Send the signal
177
+ #
178
+ def handle_command()
179
+
180
+ @handle.control_transfer(:bmRequestType => 0x40, :bRequest => 6, \
181
+ :wValue => 0x100, :wIndex => 0, :dataOut => @register.pack('CCC'), \
182
+ :timeout => 1000)
183
+ end
184
+
185
+ # turn off all active signals
186
+ #
187
+ def off()
188
+ @register= [OFF,OFF,OFF]
189
+ handle_command
190
+ @handle.release_interface(0)
191
+ end
192
+
193
+ # stop all robotic movement
194
+ #
195
+ def stop()
196
+ @register[0] = OFF
197
+ @register[1] = OFF
198
+ handle_command
199
+ end
200
+
201
+ end
202
+
203
+ if __FILE__ == $0 then
204
+ ra = RoboticArm.new
205
+ ra.led.on 1
206
+ end
207
+
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robotic-arm
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - James Robertson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-12-18 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: libusb
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ description:
27
+ email:
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - lib/robotic-arm.rb
36
+ homepage:
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.23
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: robotic-arm
63
+ test_files: []
64
+