rapiro_wrapper 0.1.0

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 (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +138 -0
  9. data/Rakefile +5 -0
  10. data/bin/rapiro_wrapper +147 -0
  11. data/lib/rapiro_wrapper.rb +23 -0
  12. data/lib/rapiro_wrapper/body.rb +43 -0
  13. data/lib/rapiro_wrapper/command.rb +19 -0
  14. data/lib/rapiro_wrapper/commander.rb +28 -0
  15. data/lib/rapiro_wrapper/led.rb +4 -0
  16. data/lib/rapiro_wrapper/led/eyes.rb +23 -0
  17. data/lib/rapiro_wrapper/servo_motor.rb +27 -0
  18. data/lib/rapiro_wrapper/servo_motor/head.rb +21 -0
  19. data/lib/rapiro_wrapper/servo_motor/left_foot_pitch.rb +21 -0
  20. data/lib/rapiro_wrapper/servo_motor/left_foot_yaw.rb +21 -0
  21. data/lib/rapiro_wrapper/servo_motor/left_hand_grip.rb +21 -0
  22. data/lib/rapiro_wrapper/servo_motor/left_sholder_pitch.rb +18 -0
  23. data/lib/rapiro_wrapper/servo_motor/left_sholder_roll.rb +18 -0
  24. data/lib/rapiro_wrapper/servo_motor/right_foot_pitch.rb +21 -0
  25. data/lib/rapiro_wrapper/servo_motor/right_foot_yaw.rb +21 -0
  26. data/lib/rapiro_wrapper/servo_motor/right_hand_grip.rb +21 -0
  27. data/lib/rapiro_wrapper/servo_motor/right_sholder_pitch.rb +18 -0
  28. data/lib/rapiro_wrapper/servo_motor/right_sholder_roll.rb +14 -0
  29. data/lib/rapiro_wrapper/servo_motor/waist.rb +21 -0
  30. data/lib/rapiro_wrapper/version.rb +4 -0
  31. data/rapiro_wrapper.gemspec +27 -0
  32. data/spec/integration/base_spec.rb +577 -0
  33. data/spec/rapiro_wrapper/body_spec.rb +77 -0
  34. data/spec/rapiro_wrapper/command_spec.rb +54 -0
  35. data/spec/rapiro_wrapper/commander_spec.rb +111 -0
  36. data/spec/rapiro_wrapper/led/eyes_spec.rb +38 -0
  37. data/spec/rapiro_wrapper/servo_moter/head_spec.rb +248 -0
  38. data/spec/rapiro_wrapper/servo_moter/left_foot_pitch_spec.rb +261 -0
  39. data/spec/rapiro_wrapper/servo_moter/left_foot_yaw_spec.rb +256 -0
  40. data/spec/rapiro_wrapper/servo_moter/left_hand_grip_spec.rb +256 -0
  41. data/spec/rapiro_wrapper/servo_moter/left_sholder_pitch_spec.rb +134 -0
  42. data/spec/rapiro_wrapper/servo_moter/left_sholder_roll_spec.rb +150 -0
  43. data/spec/rapiro_wrapper/servo_moter/right_foot_pitch_spec.rb +261 -0
  44. data/spec/rapiro_wrapper/servo_moter/right_foot_yaw_spec.rb +256 -0
  45. data/spec/rapiro_wrapper/servo_moter/right_hand_grip_spec.rb +256 -0
  46. data/spec/rapiro_wrapper/servo_moter/right_sholder_pitch_spec.rb +134 -0
  47. data/spec/rapiro_wrapper/servo_moter/right_sholder_roll_spec.rb +150 -0
  48. data/spec/rapiro_wrapper/servo_moter/waist_spec.rb +248 -0
  49. data/spec/rapiro_wrapper/servo_moter_spec.rb +64 -0
  50. data/spec/rapiro_wrapper/version_spec.rb +11 -0
  51. data/spec/spec_helper.rb +4 -0
  52. metadata +199 -0
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe RapiroWrapper::Body do
4
+ before(:all) do
5
+ DummyClass = Class.new
6
+ end
7
+ let(:commander) { double(RapiroWrapper::Commander) }
8
+ let(:sequence) { double }
9
+ let(:serial_result) { double }
10
+ let(:dummy_class) { DummyClass }
11
+ let(:dummy_motor) { double(DummyClass) }
12
+ before do
13
+ allow(RapiroWrapper::Commander).to receive(:new) { commander }
14
+ allow(commander).to receive(:sequences) { sequence }
15
+ allow(commander).to receive(:write) { serial_result }
16
+ allow(RapiroWrapper::ServoMotor).to receive(:find_servos) { [dummy_class] }
17
+ allow(dummy_class).to receive(:new) { dummy_motor }
18
+ end
19
+
20
+ context 'constructor' do
21
+ context 'instance variables' do
22
+ context '@commander' do
23
+ subject { super().instance_variable_get(:@commander) }
24
+ it 'should be an instance of RapiroWrapper::Commander' do
25
+ is_expected.to eq commander
26
+ end
27
+ after { subject }
28
+ end
29
+ context '@servos' do
30
+ subject { super().instance_variable_get(:@servos) }
31
+ it { is_expected.to be_instance_of Hash }
32
+ end
33
+ end
34
+ end
35
+
36
+ context '#sequences' do
37
+ subject { super().sequences }
38
+ it { is_expected.to eq sequence }
39
+ end
40
+
41
+ it 'should respond to dynamic setter' do
42
+ is_expected.to respond_to :dummy_class=
43
+ end
44
+
45
+ context 'dynamic setter' do
46
+ let(:motor) { double }
47
+ subject { super().dummy_class = motor }
48
+ it 'should eq argument for setter' do
49
+ is_expected.to eq motor
50
+ end
51
+ it "call servo's sub class's constructor with args" do
52
+ expect(dummy_class).to receive(:new).with(motor)
53
+ subject
54
+ end
55
+ context 'then' do
56
+ context '@dummy_class' do
57
+ subject do
58
+ sub = described_class.new
59
+ sub.dummy_class = motor
60
+ sub.instance_variable_get(:@dummy_class)
61
+ end
62
+ it { is_expected.to eq dummy_motor }
63
+ end
64
+ end
65
+ end
66
+
67
+ it 'should respond to dynamic getter' do
68
+ is_expected.to respond_to :dummy_class
69
+ end
70
+
71
+ context 'dynamic getter' do
72
+ subject { super().dummy_class }
73
+ it "should be respond to instance of servo's sub class" do
74
+ is_expected.to eq dummy_motor
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe RapiroWrapper::Command do
4
+ context 'constructor' do
5
+ context 'without arguments' do
6
+ subject { described_class.new }
7
+ it { expect { subject }.to_not raise_error }
8
+ context '@sequences' do
9
+ subject { super().instance_variable_get :@sequences }
10
+ it { is_expected.to eq [] }
11
+ end
12
+ end
13
+ context 'with args' do
14
+ let(:args) { [double, double, double] }
15
+ subject { described_class.new(args) }
16
+ it { expect { subject }.to_not raise_error }
17
+ context '@sequences' do
18
+ subject { super().instance_variable_get :@sequences }
19
+ it 'should eq args' do
20
+ is_expected.to eq args
21
+ end
22
+ end
23
+ end
24
+ end
25
+ context '#to_sequence' do
26
+ context 'without args, with @sequences = []' do
27
+ subject { described_class.new.to_sequence }
28
+ it do
29
+ is_expected.to eq '#PS00A090S01A090S02A000S03A130S04A090S05A180' \
30
+ 'S06A050S07A090S08A090S09A090S10A090S11A090R000G000B128T010'
31
+ end
32
+ end
33
+ context 'without args, with @sequences = [RapiroWrapper::Head.new(left:' \
34
+ '10), RapiroWrapper::Waist.new(left:10)]' do
35
+ subject do
36
+ described_class.new([
37
+ RapiroWrapper::Head.new(left: 10),
38
+ RapiroWrapper::Waist.new(left: 10)
39
+ ]).to_sequence
40
+ end
41
+ it do
42
+ is_expected.to eq '#PS00A100S01A100S02A000S03A130S04A090S05A180' \
43
+ 'S06A050S07A090S08A090S09A090S10A090S11A090R000G000B128T010'
44
+ end
45
+ end
46
+ context 'with 128, with @sequences = []' do
47
+ subject { described_class.new.to_sequence(128) }
48
+ it do
49
+ is_expected.to eq '#PS00A090S01A090S02A000S03A130S04A090S05A180' \
50
+ 'S06A050S07A090S08A090S09A090S10A090S11A090R000G000B128T128'
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe RapiroWrapper::Commander do
4
+ let(:serial_port) { double }
5
+ before do
6
+ allow(SerialPort).to receive_messages(new: serial_port)
7
+ end
8
+ context 'constructor' do
9
+ it { expect { subject }.to_not raise_error }
10
+ context '@serial' do
11
+ subject { super().instance_variable_get(:@serial) }
12
+ it { is_expected.to eq serial_port }
13
+ end
14
+ context 'without arguments, ' do
15
+ context 'SerialPort' do
16
+ it 'is called with "/dev/ttyAMA0" and 57600' do
17
+ expect(SerialPort).to receive(:new).with('/dev/ttyAMA0', 57_600)
18
+ subject
19
+ end
20
+ end
21
+ end
22
+ context 'with "/dev/tty.usbserial-DA00HMG6"' do
23
+ context 'SerialPort' do
24
+ it 'is called with "/dev/tty.usbserial-DA00HMG6" and 57600' do
25
+ expect(SerialPort).to receive(:new).with('/dev/ttyAMA0', 57_600)
26
+ subject
27
+ end
28
+ end
29
+ end
30
+ end
31
+ context '#execute!' do
32
+ context 'without arguments' do
33
+ subject { super().execute! }
34
+ it { expect { subject }.to raise_error ArgumentError }
35
+ end
36
+ context 'with [RapiroWrapper::Waist.new(left:10)] and 100,' do
37
+ let(:commands) { [RapiroWrapper::Waist.new(left: 10)] }
38
+ let(:duration) { 100 }
39
+ let(:commander) do
40
+ commander = described_class.new
41
+ commander.instance_variable_set(:@serial, serial)
42
+ commander
43
+ end
44
+ subject { commander.execute!(commands, duration) }
45
+ let(:result) { double }
46
+ let(:to_sequence_result) { double }
47
+ let(:command) { double(to_sequence: to_sequence_result) }
48
+ let(:serial) { double(write: result) }
49
+ before do
50
+ # allow(RapiroWrapper::Command).to receive_messages(new: command)
51
+ allow(commander).to receive(:sequences) { to_sequence_result }
52
+ end
53
+ it 'should eq result for RapiroWrapper::Command.new([RapiroWrapper::' \
54
+ 'Waist.new(left:10)], 100).to_sequence()' do
55
+ is_expected.to eq result
56
+ end
57
+ it '@serial#write is called with result for #sequences' do
58
+ expect(serial).to receive(:write).with(to_sequence_result)
59
+ subject
60
+ end
61
+ it '@serial#write is called with "#M0" when @serial is raised some ' \
62
+ 'errors' do
63
+ allow(serial).to(
64
+ receive(:write).with(to_sequence_result).and_raise(RuntimeError))
65
+ expect(serial).to receive(:write).with('#M0')
66
+ subject
67
+ end
68
+ end
69
+
70
+ context 'with [RapiroWrapper::Head.new(left:10), RapiroWrapper::' \
71
+ 'Waist.new(left:10)],' do
72
+ let(:commands) do
73
+ [RapiroWrapper::Head.new(left: 10), RapiroWrapper::Waist.new(left: 10)]
74
+ end
75
+ subject do
76
+ commander = super()
77
+ commander.instance_variable_set(:@serial, serial)
78
+ commander.execute!(commands)
79
+ end
80
+ let(:result) { double }
81
+ let(:to_sequence_result) { double }
82
+ let(:command) { double(to_sequence: to_sequence_result) }
83
+ let(:serial) { double(write: result) }
84
+ before do
85
+ allow(RapiroWrapper::Command).to receive_messages(new: command)
86
+ end
87
+ it 'should eq result for RapiroWrapper::Command.new([RapiroWrapper::Hea' \
88
+ 'd.new(left:10), RapiroWrapper::Waist.new(left:10)]).to_sequence()' do
89
+ is_expected.to eq result
90
+ end
91
+ it '@serial#write is called with result for @sequences' do
92
+ expect(serial).to receive(:write).with(to_sequence_result)
93
+ subject
94
+ end
95
+ it '@serial#write is called with "#M0" when @serial is raised some ' \
96
+ 'errors' do
97
+ allow(serial).to(
98
+ receive(:write).with(to_sequence_result).and_raise(RuntimeError))
99
+ expect(serial).to receive(:write).with('#M0')
100
+ subject
101
+ end
102
+ it '' do
103
+ expect(RapiroWrapper::Command).to receive(:new).with(commands)
104
+ subject
105
+ end
106
+ end
107
+ end
108
+
109
+ context '#sequences' do
110
+ end
111
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe RapiroWrapper::Eyes do
4
+ context 'constructor' do
5
+ context 'without params' do
6
+ context '@value' do
7
+ subject { described_class.new.instance_variable_get :@value }
8
+ it { is_expected.to eq '#000080' }
9
+ end
10
+ end
11
+ context 'with "#123456"' do
12
+ context '@value' do
13
+ subject { described_class.new('#123456').instance_variable_get :@value }
14
+ it { is_expected.to eq '#123456' }
15
+ end
16
+ end
17
+ context 'with { red: 255, green: 128, blue: 0 }' do
18
+ context '@value' do
19
+ subject do
20
+ described_class.new(red: 255, green: 128, blue: 0)
21
+ .instance_variable_get :@value
22
+ end
23
+ it { is_expected.to eq '#ff8000' }
24
+ end
25
+ end
26
+ end
27
+
28
+ context '#to_code' do
29
+ context 'with @value = "#ff8000"' do
30
+ subject { described_class.new('#ff8000').to_code }
31
+ it { is_expected.to eq 'R255G128B000' }
32
+ end
33
+ context 'with @value = "#aabbcc"' do
34
+ subject { described_class.new('#aabbcc').to_code }
35
+ it { is_expected.to eq 'R170G187B204' }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,248 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe RapiroWrapper::Head do
4
+ context 'constants' do
5
+ context 'NO' do
6
+ subject { described_class.const_get(:NO) }
7
+ it { is_expected.to eq 0 }
8
+ end
9
+ context 'DEFAULT' do
10
+ subject { described_class.const_get(:DEFAULT) }
11
+ it { is_expected.to eq 90 }
12
+ end
13
+ context 'MIN' do
14
+ subject { described_class.const_get(:MIN) }
15
+ it { is_expected.to eq 0 }
16
+ end
17
+ context 'MAX' do
18
+ subject { described_class.const_get(:MAX) }
19
+ it { is_expected.to eq 180 }
20
+ end
21
+ end
22
+
23
+ context '.code' do
24
+ context 'without arguments' do
25
+ subject { described_class.code }
26
+ it { is_expected.to eq 'S00A090' }
27
+ end
28
+
29
+ context 'with both right and left' do
30
+ subject { described_class.code(option) }
31
+ let(:option) { { right: 0, left: 0 } }
32
+ it { expect { subject }.to raise_error ArgumentError }
33
+ end
34
+
35
+ context 'with right' do
36
+ subject { described_class.code(option) }
37
+
38
+ context '-180' do
39
+ let(:option) { { right: -180 } }
40
+ it { expect { subject }.to raise_error ArgumentError }
41
+ end
42
+
43
+ context '-91' do
44
+ let(:option) { { right: -91 } }
45
+ it { expect { subject }.to raise_error ArgumentError }
46
+ end
47
+
48
+ context '-90' do
49
+ let(:option) { { right: -90 } }
50
+ it { is_expected.to eq 'S00A180' }
51
+ end
52
+
53
+ context '0' do
54
+ let(:option) { { right: 0 } }
55
+ it { is_expected.to eq 'S00A090' }
56
+ end
57
+
58
+ context '90' do
59
+ let(:option) { { right: 90 } }
60
+ it { is_expected.to eq 'S00A000' }
61
+ end
62
+
63
+ context '91' do
64
+ let(:option) { { right: 91 } }
65
+ it { expect { subject }.to raise_error ArgumentError }
66
+ end
67
+
68
+ context '180' do
69
+ let(:option) { { right: 180 } }
70
+ it { expect { subject }.to raise_error ArgumentError }
71
+ end
72
+ end
73
+
74
+ context 'with left' do
75
+ subject { described_class.code(option) }
76
+
77
+ context '-180' do
78
+ let(:option) { { left: -180 } }
79
+ it { expect { subject }.to raise_error ArgumentError }
80
+ end
81
+
82
+ context '-91' do
83
+ let(:option) { { left: -91 } }
84
+ it { expect { subject }.to raise_error ArgumentError }
85
+ end
86
+
87
+ context '-90' do
88
+ let(:option) { { left: -90 } }
89
+ it { is_expected.to eq 'S00A000' }
90
+ end
91
+
92
+ context '0' do
93
+ let(:option) { { left: 0 } }
94
+ it { is_expected.to eq 'S00A090' }
95
+ end
96
+
97
+ context '90' do
98
+ let(:option) { { left: 90 } }
99
+ it { is_expected.to eq 'S00A180' }
100
+ end
101
+
102
+ context '91' do
103
+ let(:option) { { left: 91 } }
104
+ it { expect { subject }.to raise_error ArgumentError }
105
+ end
106
+
107
+ context '180' do
108
+ let(:option) { { left: 180 } }
109
+ it { expect { subject }.to raise_error ArgumentError }
110
+ end
111
+ end
112
+ end
113
+
114
+ context 'constructor' do
115
+ context 'without arguments' do
116
+ subject { described_class.new }
117
+ it { expect { subject }.to_not raise_error }
118
+ context '@value' do
119
+ subject { described_class.new.instance_variable_get(:@value) }
120
+ it { is_expected.to eq 90 }
121
+ end
122
+ context '#to_code' do
123
+ subject { described_class.new.to_code }
124
+ it { is_expected.to eq 'S00A090' }
125
+ end
126
+ end
127
+
128
+ context 'with both right and left' do
129
+ subject { described_class.new(option).instance_variable_get(:@value) }
130
+ let(:option) { { right: 0, left: 0 } }
131
+ it { expect { subject }.to raise_error ArgumentError }
132
+ end
133
+
134
+ context 'with right' do
135
+ subject { described_class.new(option).instance_variable_get(:@value) }
136
+
137
+ context '-180' do
138
+ let(:option) { { right: -180 } }
139
+ it { expect { subject }.to raise_error ArgumentError }
140
+ end
141
+
142
+ context '-91' do
143
+ let(:option) { { right: -91 } }
144
+ it { expect { subject }.to raise_error ArgumentError }
145
+ end
146
+
147
+ context '-90' do
148
+ let(:option) { { right: -90 } }
149
+ context '@value' do
150
+ it { is_expected.to eq 180 }
151
+ end
152
+ context '#to_code' do
153
+ subject { described_class.new(option).to_code }
154
+ it { is_expected.to eq 'S00A180' }
155
+ end
156
+ end
157
+
158
+ context '0' do
159
+ let(:option) { { right: 0 } }
160
+ context '@value' do
161
+ it { is_expected.to eq 90 }
162
+ end
163
+ context '#to_code' do
164
+ subject { described_class.new(option).to_code }
165
+ it { is_expected.to eq 'S00A090' }
166
+ end
167
+ end
168
+
169
+ context '90' do
170
+ let(:option) { { right: 90 } }
171
+ context '@value' do
172
+ it { is_expected.to eq 0 }
173
+ end
174
+ context '#to_code' do
175
+ subject { described_class.new(option).to_code }
176
+ it { is_expected.to eq 'S00A000' }
177
+ end
178
+ end
179
+
180
+ context '91' do
181
+ let(:option) { { right: 91 } }
182
+ it { expect { subject }.to raise_error ArgumentError }
183
+ end
184
+
185
+ context '180' do
186
+ let(:option) { { right: 180 } }
187
+ it { expect { subject }.to raise_error ArgumentError }
188
+ end
189
+ end
190
+
191
+ context 'with left' do
192
+ subject { described_class.new(option).instance_variable_get(:@value) }
193
+
194
+ context '-180' do
195
+ let(:option) { { left: -180 } }
196
+ it { expect { subject }.to raise_error ArgumentError }
197
+ end
198
+
199
+ context '-91' do
200
+ let(:option) { { left: -91 } }
201
+ it { expect { subject }.to raise_error ArgumentError }
202
+ end
203
+
204
+ context '-90' do
205
+ let(:option) { { left: -90 } }
206
+ context '@value' do
207
+ it { is_expected.to eq 0 }
208
+ end
209
+ context '#to_code' do
210
+ subject { described_class.new(option).to_code }
211
+ it { is_expected.to eq 'S00A000' }
212
+ end
213
+ end
214
+
215
+ context '0' do
216
+ let(:option) { { left: 0 } }
217
+ context '@value' do
218
+ it { is_expected.to eq 90 }
219
+ end
220
+ context '#to_code' do
221
+ subject { described_class.new(option).to_code }
222
+ it { is_expected.to eq 'S00A090' }
223
+ end
224
+ end
225
+
226
+ context '90' do
227
+ let(:option) { { left: 90 } }
228
+ context '@value' do
229
+ it { is_expected.to eq 180 }
230
+ end
231
+ context '#to_code' do
232
+ subject { described_class.new(option).to_code }
233
+ it { is_expected.to eq 'S00A180' }
234
+ end
235
+ end
236
+
237
+ context '91' do
238
+ let(:option) { { left: 91 } }
239
+ it { expect { subject }.to raise_error ArgumentError }
240
+ end
241
+
242
+ context '180' do
243
+ let(:option) { { left: 180 } }
244
+ it { expect { subject }.to raise_error ArgumentError }
245
+ end
246
+ end
247
+ end
248
+ end