brick-pi 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/README.md +75 -41
- data/brick_pi.gemspec +1 -0
- data/examples/motors.rb +24 -0
- data/examples/touch_sensor.rb +17 -0
- data/examples/ultrasonic_sensor.rb +15 -0
- data/lib/brick_pi.rb +1 -0
- data/lib/brick_pi/bot.rb +12 -9
- data/lib/brick_pi/configuration.rb +55 -0
- data/lib/brick_pi/sensor.rb +12 -1
- data/lib/brick_pi/version.rb +1 -1
- data/spec/lib/configuration_spec.rb +75 -0
- data/spec/spec_helper.rb +17 -0
- metadata +26 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1522e9c8dee209658f828bd89f8ccb2d24b21e7
|
4
|
+
data.tar.gz: a9e9ac4ba22253afa10378656f9d28359ab7d6d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e167069b483dfecde1928861fdc5c206558a928eea19a64d6e9d67ea746a98fdc3f77e0a3c113ddb31f02a40cba2eab0db2c2d653f4da955cb19d07e6d09cb5e
|
7
|
+
data.tar.gz: 4aae5d27030ef1ee10ae287b2628b109c6fa177a758c3f8d1dd87fd85c0e14a4c833c3840fccc6c03efb31221e15e39d2b5d8c0631b0a81e43ddbf178b5821d9
|
data/.rspec
ADDED
data/README.md
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
ruby wrappers for the BrickPi Lego Mindstorms C library
|
4
4
|
|
5
|
+
## Issues
|
6
|
+
|
7
|
+
I use HuBoard to manage GitHub issues. It's pretty awesome, check it out here:
|
8
|
+
|
9
|
+
https://huboard.com/tehviking/brick_pi_ruby/
|
10
|
+
|
5
11
|
## What you need:
|
6
12
|
|
7
13
|
You need to have a few things to use this:
|
@@ -28,7 +34,9 @@ Or install it yourself as:
|
|
28
34
|
|
29
35
|
## Usage
|
30
36
|
|
31
|
-
|
37
|
+
### Motors
|
38
|
+
|
39
|
+
Currently, you only get a set of primitives to run motors. Expect a more robust library supporting various configurations (e.g. 2-tracked vehicle) later on.
|
32
40
|
|
33
41
|
The Bot object starts and stops the robot. It contains 4 Motor objects, mapped to port A-D on your BrickPi.
|
34
42
|
|
@@ -37,20 +45,24 @@ Here's a quick & dirty script to spin a motor:
|
|
37
45
|
```ruby
|
38
46
|
require 'brick_pi'
|
39
47
|
|
40
|
-
#
|
41
|
-
bot = BrickPi
|
42
|
-
|
43
|
-
|
44
|
-
bot.motor1.spin 50
|
48
|
+
# Create a bot with a motor on port A
|
49
|
+
bot = BrickPi.create do |bot|
|
50
|
+
bot.motor :port_A
|
51
|
+
end
|
45
52
|
|
46
53
|
# Get this party started
|
47
|
-
bot.
|
54
|
+
bot.run do
|
48
55
|
|
49
|
-
#
|
50
|
-
bot.
|
56
|
+
# Set the speed for a motor, on a scale of 0 - 100
|
57
|
+
bot.motor_A.spin 50
|
51
58
|
|
52
|
-
#
|
53
|
-
|
59
|
+
# Run the motor for 1 second
|
60
|
+
sleep 1
|
61
|
+
|
62
|
+
# Stop a single motor
|
63
|
+
bot.motor_A.stop
|
64
|
+
|
65
|
+
end
|
54
66
|
```
|
55
67
|
|
56
68
|
Once your bot is started, you can change the speed or direction of the motors at any time with the `Motor#spin` method.
|
@@ -64,41 +76,63 @@ require "highline/system_extensions"
|
|
64
76
|
include HighLine::SystemExtensions
|
65
77
|
HighLine::SystemExtensions.raw_no_echo_mode
|
66
78
|
|
67
|
-
bot
|
68
|
-
bot.
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
speed
|
79
|
+
# Create a bot with two motors
|
80
|
+
bot = BrickPi.create do |bot|
|
81
|
+
bot.motor :port_A
|
82
|
+
bot.motor :port_B
|
83
|
+
end
|
84
|
+
|
85
|
+
bot.run do
|
86
|
+
|
87
|
+
speed = 20
|
88
|
+
loop do
|
89
|
+
char = HighLine::SystemExtensions.get_character
|
90
|
+
case char.chr
|
91
|
+
when 'w'
|
92
|
+
bot.motor_A.spin speed
|
93
|
+
bot.motor_B.spin speed
|
94
|
+
when 'd'
|
95
|
+
bot.motor_A.spin speed
|
96
|
+
bot.motor_B.spin 0 - speed
|
97
|
+
when 'a'
|
98
|
+
bot.motor_A.spin 0 - speed
|
99
|
+
bot.motor_B.spin speed
|
100
|
+
when 'x'
|
101
|
+
bot.motor_A.spin 0 - speed
|
102
|
+
bot.motor_B.spin 0 - speed
|
103
|
+
when 'o'
|
104
|
+
if speed >=0 && speed <= 80
|
105
|
+
speed += 20
|
106
|
+
end
|
107
|
+
when 'l'
|
108
|
+
if speed > 20 && speed <= 100
|
109
|
+
speed -= 20
|
110
|
+
end
|
111
|
+
when 'e', 'c', 'z', 'q'
|
112
|
+
bot.motor_A.stop
|
113
|
+
bot.motor_B.stop
|
93
114
|
end
|
94
|
-
|
95
|
-
bot.motor1.stop
|
96
|
-
bot.motor2.stop
|
115
|
+
sleep(5 / 1000)
|
97
116
|
end
|
98
|
-
sleep(5 / 1000)
|
99
117
|
end
|
100
118
|
```
|
101
119
|
|
120
|
+
### Sensors
|
121
|
+
|
122
|
+
You can read values from sensors by doing something like this:
|
123
|
+
|
124
|
+
```
|
125
|
+
bot = BrickPi.create do |bot|
|
126
|
+
bot.touch_sensor :port_1
|
127
|
+
end
|
128
|
+
bot.run do
|
129
|
+
bot.sensor_1.read
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
See the scripts in the `examples` directory for more details.
|
134
|
+
|
135
|
+
|
102
136
|
## Contributing
|
103
137
|
|
104
138
|
1. Fork it ( https://github.com/tehviking/brick_pi/fork )
|
data/brick_pi.gemspec
CHANGED
data/examples/motors.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'brick_pi'
|
4
|
+
|
5
|
+
# Instantiate the Bot
|
6
|
+
bot = BrickPi.create do |bot|
|
7
|
+
bot.motor :port_B
|
8
|
+
bot.motor :port_C
|
9
|
+
end
|
10
|
+
|
11
|
+
# Get this party started
|
12
|
+
bot.run do
|
13
|
+
|
14
|
+
# Half speed on both motors. Max value is 100.
|
15
|
+
bot.motor_B.spin 50
|
16
|
+
bot.motor_C.spin 50
|
17
|
+
|
18
|
+
sleep 5
|
19
|
+
|
20
|
+
# Stop motors
|
21
|
+
bot.motor_B.stop
|
22
|
+
bot.motor_C.stop
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'brick_pi'
|
4
|
+
|
5
|
+
bot = BrickPi.create do |bot|
|
6
|
+
bot.touch_sensor :port_1
|
7
|
+
end
|
8
|
+
|
9
|
+
bot.run do
|
10
|
+
puts "waiting..."
|
11
|
+
|
12
|
+
until bot.sensor_1.touched?
|
13
|
+
# nothing - just wait for a touch
|
14
|
+
end
|
15
|
+
|
16
|
+
puts "touched!"
|
17
|
+
end
|
data/lib/brick_pi.rb
CHANGED
data/lib/brick_pi/bot.rb
CHANGED
@@ -3,18 +3,14 @@ include BrickPi
|
|
3
3
|
|
4
4
|
module BrickPi
|
5
5
|
class Bot
|
6
|
-
|
6
|
+
|
7
|
+
include BrickPi::Configuration
|
8
|
+
|
9
|
+
attr_accessor :motor_A, :motor_B, :motor_C, :motor_D
|
10
|
+
attr_accessor :sensor_1, :sensor_2, :sensor_3, :sensor_4
|
7
11
|
|
8
12
|
def initialize
|
9
13
|
Native.BrickPiSetup()
|
10
|
-
@motor1 = ::BrickPi::Motor.new(Native::PORT_A)
|
11
|
-
@motor2 = ::BrickPi::Motor.new(Native::PORT_B)
|
12
|
-
@motor3 = ::BrickPi::Motor.new(Native::PORT_C)
|
13
|
-
@motor4 = ::BrickPi::Motor.new(Native::PORT_D)
|
14
|
-
@sensor1 = nil
|
15
|
-
@sensor2 = nil
|
16
|
-
@sensor3 = nil
|
17
|
-
@sensor4 = nil
|
18
14
|
Native::Address[0] = 1
|
19
15
|
Native::Address[1] = 2
|
20
16
|
end
|
@@ -36,5 +32,12 @@ module BrickPi
|
|
36
32
|
def stop
|
37
33
|
@stop = true
|
38
34
|
end
|
35
|
+
|
36
|
+
def run
|
37
|
+
start
|
38
|
+
yield
|
39
|
+
stop
|
40
|
+
end
|
41
|
+
|
39
42
|
end
|
40
43
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module BrickPi
|
2
|
+
|
3
|
+
def self.create &block
|
4
|
+
bot = BrickPi::Bot.new
|
5
|
+
bot.configure &block
|
6
|
+
bot
|
7
|
+
end
|
8
|
+
|
9
|
+
module Configuration
|
10
|
+
|
11
|
+
def configure &block
|
12
|
+
yield self
|
13
|
+
end
|
14
|
+
|
15
|
+
def motor(port)
|
16
|
+
case port
|
17
|
+
when :port_A
|
18
|
+
@motor_A = BrickPi::Motor.new(Native::PORT_A)
|
19
|
+
when :port_B
|
20
|
+
@motor_B = BrickPi::Motor.new(Native::PORT_B)
|
21
|
+
when :port_C
|
22
|
+
@motor_C = BrickPi::Motor.new(Native::PORT_C)
|
23
|
+
when :port_D
|
24
|
+
@motor_D = BrickPi::Motor.new(Native::PORT_D)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def sensor(port, sensor_type)
|
29
|
+
case port
|
30
|
+
when :port_1
|
31
|
+
@sensor_1 = BrickPi::Sensor.new(:port_1, sensor_type)
|
32
|
+
when :port_2
|
33
|
+
@sensor_2 = BrickPi::Sensor.new(:port_2, sensor_type)
|
34
|
+
when :port_3
|
35
|
+
@sensor_3 = BrickPi::Sensor.new(:port_3, sensor_type)
|
36
|
+
when :port_4
|
37
|
+
@sensor_4 = BrickPi::Sensor.new(:port_4, sensor_type)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def touch_sensor(port)
|
42
|
+
sensor(port, :touch)
|
43
|
+
end
|
44
|
+
|
45
|
+
def ultrasonic_sensor(port)
|
46
|
+
sensor(port, :ultrasonic)
|
47
|
+
end
|
48
|
+
|
49
|
+
def color_sensor(port)
|
50
|
+
sensor(port, :color)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/lib/brick_pi/sensor.rb
CHANGED
@@ -26,7 +26,18 @@ module BrickPi
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def read
|
29
|
-
Native::Sensor[PORT_MAP[@port]]
|
29
|
+
Native::Sensor[PORT_MAP[@port]] if @port
|
30
30
|
end
|
31
|
+
|
32
|
+
# Nice friendly access methods for different sensors types
|
33
|
+
|
34
|
+
def touched?
|
35
|
+
@sensor_type == :touch && read == 1
|
36
|
+
end
|
37
|
+
|
38
|
+
def distance
|
39
|
+
read if @sensor_type == :ultrasonic
|
40
|
+
end
|
41
|
+
|
31
42
|
end
|
32
43
|
end
|
data/lib/brick_pi/version.rb
CHANGED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'brick_pi/configuration'
|
4
|
+
|
5
|
+
# Stub classes for testing
|
6
|
+
# Needed because otherwise native stuff gets in the way on non-brickpi machines
|
7
|
+
|
8
|
+
class BrickPi::Bot
|
9
|
+
include BrickPi::Configuration
|
10
|
+
attr_accessor :motor_A, :motor_B, :motor_C, :motor_D
|
11
|
+
attr_accessor :sensor_1, :sensor_2, :sensor_3, :sensor_4
|
12
|
+
end
|
13
|
+
module Native
|
14
|
+
PORT_A = 0
|
15
|
+
PORT_B = 1
|
16
|
+
PORT_C = 2
|
17
|
+
PORT_D = 3
|
18
|
+
end
|
19
|
+
class BrickPi::Motor
|
20
|
+
attr_accessor :port
|
21
|
+
def initialize(port)
|
22
|
+
@port = port
|
23
|
+
end
|
24
|
+
end
|
25
|
+
class BrickPi::Sensor
|
26
|
+
attr_accessor :port, :sensor_type
|
27
|
+
def initialize(port, sensor_type)
|
28
|
+
@port = port
|
29
|
+
@sensor_type = sensor_type
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Test configuration block
|
34
|
+
|
35
|
+
describe BrickPi::Configuration do
|
36
|
+
|
37
|
+
it "should configure motors" do
|
38
|
+
|
39
|
+
# Initialise bot using DSL
|
40
|
+
bot = BrickPi.create do |bot|
|
41
|
+
bot.motor :port_B
|
42
|
+
bot.motor :port_C
|
43
|
+
end
|
44
|
+
|
45
|
+
# Check bot was created right
|
46
|
+
bot.should_not be_nil
|
47
|
+
bot.motor_A.should be_nil
|
48
|
+
bot.motor_B.port.should eql Native::PORT_B
|
49
|
+
bot.motor_C.port.should eql Native::PORT_C
|
50
|
+
bot.motor_D.should be_nil
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should configure sensors" do
|
55
|
+
|
56
|
+
# Initialise bot using DSL
|
57
|
+
bot = BrickPi.create do |bot|
|
58
|
+
bot.touch_sensor :port_1
|
59
|
+
bot.ultrasonic_sensor :port_2
|
60
|
+
bot.color_sensor :port_3
|
61
|
+
end
|
62
|
+
|
63
|
+
# Check bot was created right
|
64
|
+
bot.should_not be_nil
|
65
|
+
bot.sensor_1.port.should == :port_1
|
66
|
+
bot.sensor_1.sensor_type.should == :touch
|
67
|
+
bot.sensor_2.port.should == :port_2
|
68
|
+
bot.sensor_2.sensor_type.should == :ultrasonic
|
69
|
+
bot.sensor_3.port.should == :port_3
|
70
|
+
bot.sensor_3.sensor_type.should == :color
|
71
|
+
bot.sensor_4.should be_nil
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brick-pi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Hays
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-05-
|
12
|
+
date: 2014-05-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -39,6 +39,20 @@ dependencies:
|
|
39
39
|
- - '>='
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
42
56
|
description: BrickPi uses a C library to communicate with Lego Mindstorms motors and
|
43
57
|
sensors via GPIO on the Raspberry Pi. This library wraps the C and exposes a nicer,
|
44
58
|
object-oriented interface via Ruby.
|
@@ -50,21 +64,28 @@ extensions:
|
|
50
64
|
extra_rdoc_files: []
|
51
65
|
files:
|
52
66
|
- .gitignore
|
67
|
+
- .rspec
|
53
68
|
- Gemfile
|
54
69
|
- LICENSE
|
55
70
|
- LICENSE.txt
|
56
71
|
- README.md
|
57
72
|
- Rakefile
|
58
73
|
- brick_pi.gemspec
|
74
|
+
- examples/motors.rb
|
75
|
+
- examples/touch_sensor.rb
|
76
|
+
- examples/ultrasonic_sensor.rb
|
59
77
|
- ext/brick_pi/BrickPi.h
|
60
78
|
- ext/brick_pi/extconf.rb
|
61
79
|
- ext/brick_pi/native.c
|
62
80
|
- ext/brick_pi/tick.h
|
63
81
|
- lib/brick_pi.rb
|
64
82
|
- lib/brick_pi/bot.rb
|
83
|
+
- lib/brick_pi/configuration.rb
|
65
84
|
- lib/brick_pi/motor.rb
|
66
85
|
- lib/brick_pi/sensor.rb
|
67
86
|
- lib/brick_pi/version.rb
|
87
|
+
- spec/lib/configuration_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
68
89
|
homepage: ''
|
69
90
|
licenses:
|
70
91
|
- MIT
|
@@ -90,4 +111,6 @@ rubygems_version: 2.2.2
|
|
90
111
|
signing_key:
|
91
112
|
specification_version: 4
|
92
113
|
summary: Ruby library to run the BrickPi drivers for Lego Mindstorms
|
93
|
-
test_files:
|
114
|
+
test_files:
|
115
|
+
- spec/lib/configuration_spec.rb
|
116
|
+
- spec/spec_helper.rb
|