robotic-arm 0.1.4 → 0.2.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 +152 -10
  2. metadata +2 -2
data/lib/robotic-arm.rb CHANGED
@@ -7,19 +7,135 @@ require 'libusb'
7
7
 
8
8
  OFF = 0x00
9
9
 
10
+
11
+ module Session
12
+
13
+ class Recorder
14
+
15
+ def initialize(obj)
16
+ @log = []
17
+ @recording = false
18
+ @obj = obj
19
+ end
20
+
21
+ def start
22
+ @log = [{:time => Time.now, :method => :stop}]
23
+ @recording = true
24
+ end
25
+
26
+ def stop
27
+
28
+ return unless @recording == true
29
+ @log << {time: Time.now, class: nil, :method => :stop}
30
+
31
+ @recording = false
32
+
33
+ # convert the time to elapsed time in seconds
34
+
35
+ @log.first[:time] = @log[1][:time]
36
+ t1 = @log.first[:time]
37
+ @log.first[:sleep] = 0
38
+ @log[1..-2].each do |record|
39
+ t2 = record[:time]
40
+ record[:sleep] = (t2 - t1).round(2)
41
+ t1 = t2
42
+ end
43
+
44
+
45
+ @rlog = @log.reverse.map{|x| x.clone}
46
+ t1 = @rlog.shift[:time]
47
+ @rlog.pop
48
+
49
+ @rlog[0..-2].each_with_index do |record,i |
50
+
51
+ classname = record[:class]
52
+
53
+ tmp = record.clone
54
+ j = @rlog[(i+1)..-1].map{|x| x[:class]}.index(classname)
55
+
56
+ next unless j
57
+
58
+ record[:method] = swap_state @rlog[i+1+j][:method]
59
+ record[:sleep] = swap_state @rlog[i+1+j][:sleep]
60
+
61
+ @rlog[i+1+j][:method] = swap_state tmp[:method]
62
+ @rlog[i+1+j][:sleep] = tmp[:sleep]
63
+
64
+ end
65
+
66
+ @log
67
+ end
68
+
69
+ def record(classname, methodname)
70
+
71
+ @log << {
72
+ time: Time.now,
73
+ class: classname.to_sym,
74
+ method: methodname.to_sym
75
+ }
76
+ end
77
+
78
+ def recording?() @recording end
79
+ def playback() play @rlog end
80
+ def reverse_play() play @rlog end
81
+
82
+ private
83
+
84
+ def play(log)
85
+
86
+ log.each do |record|
87
+
88
+ sleep record[:sleep].to_f
89
+ component, action = *[:class, :method].map{|x| record[x]}
90
+
91
+ unless component.nil? then
92
+ @obj.method(component).call.method(action).call
93
+ else
94
+ @obj.method(action).call
95
+ end
96
+ end
97
+ end
98
+
99
+ def swap_state(methodname)
100
+ case methodname
101
+ when :up
102
+ :down
103
+ when :down
104
+ :up
105
+ when :left
106
+ :right
107
+ when :right
108
+ :left
109
+ when :open
110
+ :close
111
+ when :close
112
+ :open
113
+ else
114
+ methodname
115
+ end
116
+ end
117
+
118
+ end
119
+
120
+ def recording() @sr ||= Recorder.new(self) end
121
+ def recording?() @sr ||= Recorder.new(self); @sr.recording? end
122
+ def record(classname, methodname) @sr.record classname, methodname end
123
+
124
+ end
125
+
10
126
  class RoboticArm
127
+ include Session
11
128
 
12
129
  attr_reader :led, :wrist, :elbow, :shoulder, :base, :grip
13
130
  attr_reader :light, :gripper # aliases for :led and :grip
14
131
 
15
132
  class Component
16
133
 
17
- def initialize(robot_arm) @ra = robot_arm end
134
+ def initialize(robot_arm) @ra = robot_arm end
135
+ def active?() @active end
18
136
 
19
- protected
137
+ protected
20
138
 
21
- def active?() @active end
22
-
23
139
  def activate(switch, val, seconds=0)
24
140
 
25
141
  return if val == @previous_val
@@ -34,6 +150,9 @@ class RoboticArm
34
150
  @active = false
35
151
  end
36
152
  end
153
+
154
+ def inspect() '<' + self.class.to_s + '>' end
155
+
37
156
  end
38
157
 
39
158
  class Led < Component
@@ -43,7 +162,7 @@ class RoboticArm
43
162
  super(robot_arm)
44
163
  @switch = 2
45
164
  end
46
-
165
+
47
166
  def on(seconds=0) activate(@switch, 0x01, seconds) unless on? end
48
167
  def off() activate(@switch, -0x01) if on? end
49
168
 
@@ -173,7 +292,8 @@ class RoboticArm
173
292
 
174
293
  @light, @gripper = @led, @grip #aliases
175
294
  end
176
-
295
+
296
+ def inspect() '#<RoboticArm:>' end
177
297
  def left(seconds=0) @base.left seconds end
178
298
  def right(seconds=0) @base.right seconds end
179
299
 
@@ -182,23 +302,45 @@ class RoboticArm
182
302
  def command(switch, val)
183
303
 
184
304
  @register[switch] += val
185
- handle_command
305
+ handle_command
306
+
307
+ if recording? then
308
+
309
+ commands = {
310
+ '21' => [:light, :on], '2-1' => [:light, :off],
311
+ '02' => [:gripper, :open], '01' => [:gripper, :close],
312
+ '0-2' => [:gripper, :stop], '0-1' => [:gripper, :stop],
313
+ '04' => [:wrist, :up], '08' => [:wrist, :down],
314
+ '0-4' => [:wrist, :stop], '0-8' => [:wrist, :stop],
315
+ '016' => [:elbow, :up], '032' => [:elbow, :down],
316
+ '0-16' => [:elbow, :stop], '0-32' => [:elbow, :stop],
317
+ '064' => [:shoulder, :up], '0128' => [:shoulder, :down],
318
+ '0-64' => [:shoulder, :stop], '0-128' => [:shoulder, :stop],
319
+ '12' => [:base, :left], '11' => [:base, :right],
320
+ '1-2' => [:base, :stop], '1-1' => [:base, :stop]
321
+ }
322
+
323
+ classname, methodname = *commands[switch.to_s + val.to_s]
324
+
325
+ record classname, methodname
326
+ end
186
327
  end
187
328
 
188
329
  # turn off all active signals
189
330
  #
190
- def off()
191
- led.off if led.on?
331
+ def off()
192
332
  stop
193
333
  @handle.release_interface(0)
194
334
  end
195
335
 
196
336
  # stop all robotic movement
197
337
  #
198
- def stop()
338
+ def stop()
199
339
  [wrist, elbow, shoulder, base, grip].each do |motor|
200
340
  motor.stop if motor.moving?
201
341
  end
342
+ led.off if led.on?
343
+ nil
202
344
  end
203
345
 
204
346
  private
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.4
5
+ version: 0.2.0
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-21 00:00:00 Z
13
+ date: 2012-12-24 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: libusb