brick-pi 0.1.1 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8cd74fea019cdd42e7ab6e7e4084c8f7bbad4bfc
4
- data.tar.gz: 38583bcd636016e9b2b816f15e9ef8406c75f8aa
3
+ metadata.gz: f1522e9c8dee209658f828bd89f8ccb2d24b21e7
4
+ data.tar.gz: a9e9ac4ba22253afa10378656f9d28359ab7d6d7
5
5
  SHA512:
6
- metadata.gz: f7f070bc9cd604f8122604b7b3ff6a47c9994a7eeabc5c2abe0e3a78a7c9941dbf0cc1cc0c95755be78201eaeebdd6edcb2d59ad0678921668ec0b22f328cde7
7
- data.tar.gz: 42c09cd79e0139e2175d011133e8a56c90bdab271a2cc02eb456d6774c8e26565caaa8f259695682dd757c14d6bcefdfde03bc3b434b893cdae3697dbcee4b29
6
+ metadata.gz: e167069b483dfecde1928861fdc5c206558a928eea19a64d6e9d67ea746a98fdc3f77e0a3c113ddb31f02a40cba2eab0db2c2d653f4da955cb19d07e6d09cb5e
7
+ data.tar.gz: 4aae5d27030ef1ee10ae287b2628b109c6fa177a758c3f8d1dd87fd85c0e14a4c833c3840fccc6c03efb31221e15e39d2b5d8c0631b0a81e43ddbf178b5821d9
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
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
- Currently, you only get a set of primitives to run motors. Expect more functionality around sensors and a more robust library supporting various configurations (e.g. 2-tracked vehicle).
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
- # Instantiate the Bot
41
- bot = BrickPi::Bot.new
42
-
43
- # Set the speed for a motor, on a scale of 0 - 100
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.start
54
+ bot.run do
48
55
 
49
- # Stop a single motor
50
- bot.motor1.stop
56
+ # Set the speed for a motor, on a scale of 0 - 100
57
+ bot.motor_A.spin 50
51
58
 
52
- # Stop all functions for a bot
53
- bot.stop
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 = BrickPi::Bot.new
68
- bot.start
69
-
70
- speed = 20
71
- loop do
72
- char = HighLine::SystemExtensions.get_character
73
- case char.chr
74
- when 'w'
75
- bot.motor1.spin speed
76
- bot.motor2.spin speed
77
- when 'd'
78
- bot.motor1.spin speed
79
- bot.motor2.spin 0 - speed
80
- when 'a'
81
- bot.motor1.spin 0 - speed
82
- bot.motor2.spin speed
83
- when 'x'
84
- bot.motor1.spin 0 - speed
85
- bot.motor2.spin 0 - speed
86
- when 'o'
87
- if speed >=0 && speed <= 80
88
- speed += 20
89
- end
90
- when 'l'
91
- if speed > 20 && speed <= 100
92
- speed -= 20
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
- when 'e', 'c', 'z', 'q'
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
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
  spec.extensions = ["ext/brick_pi/extconf.rb"]
21
21
  spec.add_development_dependency "bundler", "~> 1.6"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
23
24
  end
@@ -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
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'brick_pi'
4
+
5
+ bot = BrickPi.create do |bot|
6
+ bot.ultrasonic_sensor :port_1
7
+ end
8
+
9
+ bot.run do
10
+
11
+ while true
12
+ puts bot.sensor_1.distance
13
+ end
14
+
15
+ end
data/lib/brick_pi.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "brick_pi/version"
2
2
  require "brick_pi/native"
3
+ require "brick_pi/configuration"
3
4
  require "brick_pi/bot"
4
5
  require "brick_pi/motor"
5
6
  require "brick_pi/sensor"
data/lib/brick_pi/bot.rb CHANGED
@@ -3,18 +3,14 @@ include BrickPi
3
3
 
4
4
  module BrickPi
5
5
  class Bot
6
- attr_accessor :motor1, :motor2, :motor3, :motor4, :sensor1, :sensor2, :sensor3, :sensor4
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module BrickPi
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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
@@ -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.1.1
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-01 00:00:00.000000000 Z
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