robotic-arm 0.1.0 → 0.1.1

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 +48 -61
  2. metadata +2 -2
data/lib/robotic-arm.rb CHANGED
@@ -10,18 +10,22 @@ OFF = 0x00
10
10
  class RoboticArm
11
11
 
12
12
  attr_reader :led, :wrist, :elbow, :shoulder, :base, :grip
13
+ attr_reader :light, :gripper # aliases for :led and :grip
13
14
 
14
15
  class Component
15
16
 
16
17
  def initialize(robot_arm) @ra = robot_arm end
17
- def active?() @active end
18
+
19
+ protected
18
20
 
19
- protected
21
+ def active?() @active end
20
22
 
21
- def activate(switch, val, seconds)
23
+ def activate(switch, val, seconds=0)
24
+
22
25
  @active = val >= 0
23
26
  @prev_val = val
24
27
  @ra.command(switch, val)
28
+
25
29
  if seconds > 0 then
26
30
  sleep seconds
27
31
  @ra.command(switch, -(val))
@@ -38,13 +42,8 @@ class RoboticArm
38
42
  @switch = 2
39
43
  end
40
44
 
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
45
+ def on(seconds=0) activate(@switch, 0x01, seconds) unless on? end
46
+ def off() activate(@switch, -0x01) if on? end
48
47
 
49
48
  alias on? active?
50
49
  end
@@ -56,34 +55,25 @@ class RoboticArm
56
55
  @switch, @val = 0, OFF
57
56
  end
58
57
 
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
58
+ protected
59
+
60
+ def stop() (@active = false; activate(@switch, -(@prev_val))) if moving? end
73
61
 
74
62
  alias moving? active?
75
-
63
+
64
+ def activate(switch, val, seconds=0)
65
+ (@val = @prev_val; stop) if active?
66
+ super(switch, val, seconds)
67
+ end
68
+
76
69
  end
77
70
 
78
71
  class ComponentUpDown < ComponentMoving
79
72
 
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
73
+ protected
74
+
75
+ def up (seconds=0) activate(@switch, @upval, seconds) end
76
+ def down(seconds=0) activate(@switch, @downval, seconds) end
87
77
  end
88
78
 
89
79
  class Shoulder < ComponentUpDown
@@ -117,24 +107,14 @@ class RoboticArm
117
107
  @switch = 1
118
108
  end
119
109
 
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
110
+ def left (seconds=0) activate(@switch, @val=0x02, seconds) end
111
+ def right(seconds=0) activate(@switch, @val=0x01, seconds) end
127
112
  end
128
113
 
129
- class Grip < ComponentMoving
130
-
131
- def open(seconds=0)
132
- activate(@switch, @val=0x02, seconds)
133
- end
114
+ class Gripper < ComponentMoving
134
115
 
135
- def close(seconds=0)
136
- activate(@switch, @val=0x01, seconds)
137
- end
116
+ def open (seconds=0) activate(@switch, @val=0x02, seconds) end
117
+ def close(seconds=0) activate(@switch, @val=0x01, seconds) end
138
118
  end
139
119
 
140
120
 
@@ -144,10 +124,7 @@ class RoboticArm
144
124
  usb = LIBUSB::Context.new
145
125
  arm = usb.devices(:idVendor => 0x1267, :idProduct => 0x0000).first
146
126
 
147
- if arm == nil
148
- puts "Arm not found!"
149
- exit
150
- end
127
+ (puts "Arm not found!"; exit) unless arm
151
128
 
152
129
  # Take control of the device
153
130
  @handle = arm.open
@@ -158,8 +135,10 @@ class RoboticArm
158
135
  @elbow = Elbow.new self
159
136
  @shoulder = Shoulder.new self
160
137
  @base = Base.new self
161
- @grip = Grip.new self
138
+ @grip = Gripper.new self
162
139
  @register = [OFF,OFF,OFF]
140
+
141
+ @light, @gripper = @led, @grip #aliases
163
142
  end
164
143
 
165
144
  def left(seconds=0) @base.left seconds end
@@ -173,15 +152,6 @@ class RoboticArm
173
152
  handle_command
174
153
  end
175
154
 
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
155
  # turn off all active signals
186
156
  #
187
157
  def off()
@@ -198,6 +168,23 @@ class RoboticArm
198
168
  handle_command
199
169
  end
200
170
 
171
+ private
172
+
173
+ # Send the signal
174
+ #
175
+ def handle_command()
176
+
177
+ message = {
178
+ bmRequestType: 0x40,
179
+ bRequest: 6,
180
+ wValue: 0x100,
181
+ wIndex: 0,
182
+ dataOut: @register.pack('CCC'),
183
+ timeout: 1000
184
+ }
185
+ @handle.control_transfer message
186
+ end
187
+
201
188
  end
202
189
 
203
190
  if __FILE__ == $0 then
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: robotic-arm
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-12-18 00:00:00 Z
13
+ date: 2012-12-19 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: libusb