artoo-arduino 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8a614efd769cd7695db5bb044c4ab4fa6dbdaf6d
4
+ data.tar.gz: d32fa41b3f9af38945542a34a43eb808e49e67cd
5
+ SHA512:
6
+ metadata.gz: 821c652fde64fa4733212924a7e7690ca464f708ea5b60078df52d9ef0617b88908c0f01e4e1dfecf3be70a23f283b5ad7f27e8a6f30ab708a280fa2fe29de83
7
+ data.tar.gz: 9d4e51c327cd33b6982382ade0c7564a1b58ad89bd632fb04e116bb7ac8d63f7b39db8922d87a7d9ed6faf794809bb2e45875b1a9833eb9b0959f7559f1b4348
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .rvmrc
2
+ .rbx
3
+ pkg
data/.rvmrc.example ADDED
@@ -0,0 +1 @@
1
+ rvm use rbx-2.0.0-rc1@artoo-arduino --create
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - jruby-19mode
6
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in artoo-sphero.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ artoo-arduino (1.0.0.pre)
5
+ artoo (~> 1.0.0.pre)
6
+ hybridgroup-firmata (~> 0.4.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ artoo (1.0.0.pre)
12
+ celluloid (~> 0.14.0)
13
+ celluloid-io (~> 0.14.0)
14
+ multi_json (~> 1.6)
15
+ pry (~> 0.9)
16
+ rake (~> 10.0)
17
+ reel (~> 0.4.pre)
18
+ celluloid (0.14.0)
19
+ timers (>= 1.0.0)
20
+ celluloid-io (0.14.0)
21
+ celluloid (>= 0.13.0)
22
+ nio4r (>= 0.4.5)
23
+ certified (0.1.1)
24
+ coderay (1.0.9)
25
+ event_spitter (0.5.0)
26
+ http (0.4.0)
27
+ certified
28
+ http_parser.rb
29
+ http_parser.rb (0.5.3)
30
+ hybridgroup-firmata (0.4.0)
31
+ event_spitter
32
+ metaclass (0.0.1)
33
+ method_source (0.8.1)
34
+ minitest (5.0.1)
35
+ minitest-happy (1.0.0)
36
+ mocha (0.14.0)
37
+ metaclass (~> 0.0.1)
38
+ multi_json (1.7.3)
39
+ nio4r (0.4.5)
40
+ pry (0.9.12.2)
41
+ coderay (~> 1.0.5)
42
+ method_source (~> 0.8)
43
+ slop (~> 3.4)
44
+ rack (1.5.2)
45
+ rake (10.0.4)
46
+ reel (0.4.0.pre)
47
+ celluloid-io (>= 0.8.0)
48
+ http (>= 0.2.0)
49
+ http_parser.rb (>= 0.5.3)
50
+ rack (>= 1.4.0)
51
+ websocket_parser (>= 0.1.2)
52
+ slop (3.4.5)
53
+ timers (1.1.0)
54
+ websocket_parser (0.1.4)
55
+ http
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ artoo-arduino!
62
+ minitest (~> 5.0)
63
+ minitest-happy
64
+ mocha (~> 0.14.0)
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012, 2013 The Hybrid Group
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # Artoo Adaptor For Arduino
2
+
3
+ This repository contains the Artoo (http://artoo.io/) adaptor and drivers for the Arduino microcontroller using the firmata protocol.
4
+
5
+ Artoo is a open source micro-framework for robotics using Ruby.
6
+
7
+ For more information abut Artoo, check out our repo at https://github.com/hybridgroup/artoo
8
+
9
+ [![Code Climate](https://codeclimate.com/github/hybridgroup/artoo-arduino.png)](https://codeclimate.com/github/hybridgroup/artoo-arduino) [![Build Status](https://travis-ci.org/hybridgroup/artoo-arduino.png?branch=master)](https://travis-ci.org/hybridgroup/artoo-arduino)
10
+
11
+ ## Installing
12
+
13
+ ```
14
+ gem install artoo-arduino
15
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "artoo-arduino/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "artoo-arduino"
7
+ s.version = Artoo::Arduino::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Ron Evans", "Adrian Zankich"]
10
+ s.email = ["artoo@hybridgroup.com"]
11
+ s.homepage = "https://github.com/hybridgroup/artoo-arduino"
12
+ s.summary = %q{Artoo adaptor and driver for Arduino}
13
+ s.description = %q{Artoo adaptor and driver for Arduino}
14
+
15
+ s.rubyforge_project = "artoo-arduino"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency 'artoo', '~> 1.0.0.pre'
23
+ s.add_runtime_dependency 'hybridgroup-firmata', '~> 0.4.0'
24
+ s.add_development_dependency 'minitest', '~> 5.0'
25
+ s.add_development_dependency 'minitest-happy'
26
+ s.add_development_dependency 'mocha', '~> 0.14.0'
27
+ end
@@ -0,0 +1,9 @@
1
+ require 'artoo'
2
+
3
+ connection :arduino, :adaptor => :firmata, :port => '127.0.0.1:8023'
4
+ device :led, :driver => :led, :pin => 13
5
+ device :button, :driver => :button, :pin => 2
6
+
7
+ work do
8
+ on button, :push => proc {led.toggle}
9
+ end
@@ -0,0 +1,13 @@
1
+ require 'artoo'
2
+
3
+ connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
4
+ device :board
5
+ device :led, :driver => :led, :pin => 13
6
+
7
+ work do
8
+ puts "Firmware name #{board.firmware_name}"
9
+ puts "Firmata version #{board.version}"
10
+ every 1.second do
11
+ led.toggle
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require 'lib/artoo/adaptors/firmata'
2
+
3
+ require 'lib/artoo/drivers/ardrone'
4
+ require 'lib/artoo/drivers/ardrone_navigation'
5
+ require 'lib/artoo/drivers/ardrone_video'
6
+
7
+ require 'lib/artoo-arduino/version'
@@ -0,0 +1,5 @@
1
+ module Artoo
2
+ module Arduino
3
+ VERSION = '1.0.0.pre'
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ require 'artoo/adaptors/adaptor'
2
+
3
+ module Artoo
4
+ module Adaptors
5
+ # Connect to Arduino using Firmata
6
+ # @see http://rubydoc.info/gems/hybridgroup-firmata/0.3.0/Firmata/Board HybridGroup Firmata Documentation
7
+ class Firmata < Adaptor
8
+ attr_reader :firmata
9
+
10
+ # Creates connection with firmata board
11
+ # @return [Boolean]
12
+ def connect
13
+ require 'firmata' unless defined?(::Firmata)
14
+ @firmata = ::Firmata::Board.new(connect_to)
15
+ @firmata.connect
16
+ super
17
+ return true
18
+ end
19
+
20
+ # Closes connection with firmata board
21
+ # @return [Boolean]
22
+ def disconnect
23
+ super
24
+ end
25
+
26
+ # Uses method missing to call Firmata Board methods
27
+ # @see http://rubydoc.info/gems/hybridgroup-firmata/0.3.0/Firmata/Board Firmata Board Documentation
28
+ def method_missing(method_name, *arguments, &block)
29
+ firmata.send(method_name, *arguments, &block)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,59 @@
1
+ require 'artoo/drivers/driver'
2
+
3
+ module Artoo
4
+ module Drivers
5
+ # Button driver behaviors for Firmata
6
+ class Button < Driver
7
+ COMMANDS = [:is_pressed?].freeze
8
+
9
+ DOWN = 1
10
+ UP = 0
11
+
12
+ # @return [Boolean] True if pressed
13
+ def is_pressed?
14
+ (@is_pressed ||= false) == true
15
+ end
16
+
17
+ # Sets values to read and write from button
18
+ # and starts driver
19
+ def start_driver
20
+ connection.set_pin_mode(pin, Firmata::Board::INPUT)
21
+ connection.toggle_pin_reporting(pin)
22
+
23
+ every(interval) do
24
+ connection.read_and_process
25
+ handle_events
26
+ end
27
+
28
+ super
29
+ end
30
+
31
+ def handle_events
32
+ while i = find_event("digital-read-#{pin}") do
33
+ update(events.slice!(i).data.first)
34
+ end
35
+ end
36
+
37
+ def find_event(name)
38
+ events.index {|e| e.name == name}
39
+ end
40
+
41
+ def events
42
+ connection.async_events
43
+ end
44
+
45
+ # Publishes events according to the button feedback
46
+ def update(value)
47
+ if value == DOWN
48
+ @is_pressed = true
49
+ publish(event_topic_name("update"), "push", value)
50
+ publish(event_topic_name("push"), value)
51
+ else
52
+ @is_pressed = false
53
+ publish(event_topic_name("update"), "release", value)
54
+ publish(event_topic_name("release"), value)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,48 @@
1
+ require 'artoo/drivers/driver'
2
+
3
+ module Artoo
4
+ module Drivers
5
+ # The LED driver behaviors
6
+ class Led < Driver
7
+
8
+ COMMANDS = [:on, :off, :toggle, :brightness].freeze
9
+
10
+ # @return [Boolean] True if on
11
+ def is_on?
12
+ (@is_on ||= false) == true
13
+ end
14
+
15
+ # @return [Boolean] True if off
16
+ def is_off?
17
+ (@is_on ||= false) == false
18
+ end
19
+
20
+ # Sets led to on status
21
+ def on
22
+ @is_on = true
23
+ connection.set_pin_mode(pin, Firmata::Board::OUTPUT)
24
+ connection.digital_write(pin, Firmata::Board::HIGH)
25
+ end
26
+
27
+ # Sets led to off status
28
+ def off
29
+ @is_on = false
30
+ connection.set_pin_mode(pin, Firmata::Board::OUTPUT)
31
+ connection.digital_write(pin, Firmata::Board::LOW)
32
+ end
33
+
34
+ # Toggle status
35
+ # @example on > off, off > on
36
+ def toggle
37
+ is_off? ? on : off
38
+ end
39
+
40
+ # Change brightness level
41
+ # @param [Integer] level
42
+ def brightness(level=0)
43
+ connection.set_pin_mode(pin, Firmata::Board::PWM)
44
+ connection.analog_write(pin, level)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,69 @@
1
+ require 'artoo/drivers/driver'
2
+
3
+ module Artoo
4
+ module Drivers
5
+ # L293 or other H-bridge style motor driver behaviors for Firmata
6
+ class Motor < Driver
7
+ COMMANDS = [:forward, :backward, :stop, :speed, :current_speed].freeze
8
+
9
+ attr_reader :leg1_pin, :leg2_pin, :speed_pin, :current_speed
10
+
11
+ # Create new Motor
12
+ def initialize(params={})
13
+ super
14
+
15
+ raise "Invalid pins, please pass an array in format [leg1, leg2, speed]" unless (pin && pin.is_a?(Array) && pin.size == 3)
16
+ @leg1_pin = pin[0]
17
+ @leg2_pin = pin[1]
18
+ @speed_pin = pin[2]
19
+ @current_speed = 0
20
+ end
21
+
22
+ # Starts connection to read and process and driver
23
+ def start_driver
24
+ every(interval) do
25
+ connection.read_and_process
26
+ end
27
+
28
+ super
29
+ end
30
+
31
+ # Sets movement forward
32
+ # @param [Integer] s speed
33
+ def forward(s)
34
+ set_legs(Firmata::Board::LOW, Firmata::Board::HIGH)
35
+ speed(s)
36
+ end
37
+
38
+ # Sets movement backward
39
+ # @param [Integer] s speed
40
+ def backward(s)
41
+ set_legs(Firmata::Board::HIGH, Firmata::Board::LOW)
42
+ speed(s)
43
+ end
44
+
45
+ # Stops
46
+ def stop
47
+ speed(0)
48
+ end
49
+
50
+ # Set motor speed
51
+ # @param [Integer] s speed (must be an integer between 0-255)
52
+ def speed(s)
53
+ raise "Motor speed must be an integer between 0-255" unless (s.is_a?(Numeric) && s >= 0 && s <= 255)
54
+ @current_speed = s
55
+ connection.set_pin_mode(speed_pin, Firmata::Board::PWM)
56
+ connection.analog_write(speed_pin, s)
57
+ end
58
+
59
+ private
60
+
61
+ def set_legs(l1, l2)
62
+ connection.set_pin_mode(leg1_pin, Firmata::Board::OUTPUT)
63
+ connection.digital_write(leg1_pin, l1)
64
+ connection.set_pin_mode(leg2_pin, Firmata::Board::OUTPUT)
65
+ connection.digital_write(leg2_pin, l2)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,59 @@
1
+ require 'artoo/drivers/driver'
2
+
3
+ module Artoo
4
+ module Drivers
5
+ # Servo behaviors for Firmata
6
+ class Servo < Driver
7
+ COMMANDS = [:move, :min, :center, :max].freeze
8
+
9
+ attr_reader :current_angle
10
+
11
+ # Create new Servo with angle=0
12
+ def initialize(params={})
13
+ super
14
+
15
+ @current_angle = 0
16
+ end
17
+
18
+ # Starts connection to read and process and driver
19
+ def start_driver
20
+ every(interval) do
21
+ connection.read_and_process
22
+ end
23
+
24
+ super
25
+ end
26
+
27
+ # Moves to specified angle
28
+ # @param [Integer] angle must be between 0-180
29
+ def move(angle)
30
+ raise "Servo angle must be an integer between 0-180" unless (angle.is_a?(Numeric) && angle >= 0 && angle <= 180)
31
+
32
+ @current_angle = angle
33
+ connection.set_pin_mode(pin, Firmata::Board::SERVO)
34
+ connection.analog_write(pin, angle_to_span(angle))
35
+ end
36
+
37
+ # Moves to min position
38
+ def min
39
+ move(0)
40
+ end
41
+
42
+ # Moves to center position
43
+ def center
44
+ move(90)
45
+ end
46
+
47
+ # Moves to max position
48
+ def max
49
+ move(180)
50
+ end
51
+
52
+ # converts an angle to a span between 0-255
53
+ # @param [Integer] angle
54
+ def angle_to_span(angle)
55
+ (angle * 255 / 180).to_i
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,61 @@
1
+ require 'artoo/drivers/wiidriver'
2
+
3
+ module Artoo
4
+ module Drivers
5
+ # Wiichuck driver behaviors for Firmata
6
+ class Wiichuck < Wiidriver
7
+
8
+ # Update button and joystick values
9
+ # @param [Object] value
10
+ def update(value)
11
+ begin
12
+ super
13
+
14
+ adjust_origins
15
+ update_buttons
16
+ update_joystick
17
+
18
+ rescue Exception => e
19
+ Logger.error "wiichuck update exception!"
20
+ Logger.error e.message
21
+ Logger.error e.backtrace.inspect
22
+ end
23
+ end
24
+
25
+ # Adjust x, y origin values
26
+ def adjust_origins
27
+ set_joystick_default_value(:sy_origin, data[:sy])
28
+ set_joystick_default_value(:sx_origin, data[:sx])
29
+ end
30
+
31
+ # Publishes events for c and z buttons
32
+ def update_buttons
33
+ publish(event_topic_name("c_button")) if data[:c] == true
34
+ publish(event_topic_name("z_button")) if data[:z] == true
35
+ end
36
+
37
+ # Publishes event for joystick
38
+ def update_joystick
39
+ publish(event_topic_name("joystick"), {:x => calculate_joystick_value(:sx, :sx_origin), :y => calculate_joystick_value(:sy, :sy_origin)})
40
+ end
41
+
42
+ private
43
+
44
+ def get_defaults
45
+ {
46
+ :sy_origin => nil,
47
+ :sx_origin => nil
48
+ }
49
+ end
50
+
51
+ def parse(value)
52
+ return {
53
+ :sx => decode_value(value, 0),
54
+ :sy => decode_value(value, 1),
55
+ :z => generate_bool(decode_value(value, 5) & 0x01),
56
+ :c => generate_bool(decode_value(value, 5) & 0x02)
57
+ }
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,139 @@
1
+ require 'artoo/drivers/wiidriver'
2
+
3
+ module Artoo
4
+ module Drivers
5
+ # Wiiclassic driver behaviors for Firmata
6
+ class Wiiclassic < Wiidriver
7
+
8
+ # Update buttons and joysticks values
9
+ # @param [Object] value
10
+ def update(value)
11
+ begin
12
+ super
13
+
14
+ adjust_origins
15
+ update_buttons
16
+ update_left_joystick
17
+ update_right_joystick
18
+ update_triggers
19
+
20
+ rescue Exception => e
21
+ Logger.error "wiiclassic update exception!"
22
+ Logger.error e.message
23
+ Logger.error e.backtrace.inspect
24
+ end
25
+ end
26
+
27
+ # Adjust all origins
28
+ def adjust_origins
29
+ set_joystick_default_value(:ly_origin, data[:ly])
30
+ set_joystick_default_value(:lx_origin, data[:lx])
31
+ set_joystick_default_value(:ry_origin, data[:ry])
32
+ set_joystick_default_value(:rx_origin, data[:rx])
33
+ set_joystick_default_value(:rt_origin, data[:rt])
34
+ set_joystick_default_value(:lt_origin, data[:lt])
35
+ end
36
+
37
+ # Update button values
38
+ def update_buttons
39
+ update_button("a_button", :a)
40
+ update_button("b_button", :b)
41
+ update_button("x_button", :x)
42
+ update_button("y_button", :y)
43
+ update_button("home_button", :h)
44
+ update_button("start_button", :+)
45
+ update_button("select_button", :-)
46
+ end
47
+
48
+ # Publish button event
49
+ def update_button(name, key)
50
+ publish(event_topic_name(name)) if data[key] == true
51
+ end
52
+
53
+ # Publish left joystick event
54
+ def update_left_joystick
55
+ publish(event_topic_name("left_joystick"), {:x => calculate_joystick_value(:lx, :lx_origin), :y => calculate_joystick_value(:ly, :ly_origin)})
56
+ end
57
+
58
+ # Publish right joystick event
59
+ def update_right_joystick
60
+ publish(event_topic_name("right_joystick"), {:x => calculate_joystick_value(:rx, :rx_origin), :y => calculate_joystick_value(:ry, :ry_origin)})
61
+ end
62
+
63
+ # Publish triggers events
64
+ def update_triggers
65
+ publish(event_topic_name("right_trigger"), calculate_joystick_value(:rt, :rt_origin))
66
+ publish(event_topic_name("left_trigger"), calculate_joystick_value(:lt, :lt_origin))
67
+ end
68
+
69
+ private
70
+
71
+ def get_defaults
72
+ {
73
+ :ry_origin => nil,
74
+ :rx_origin => nil,
75
+ :ly_origin => nil,
76
+ :lx_origin => nil,
77
+ :rt_origin => nil,
78
+ :lt_origin => nil
79
+ }
80
+ end
81
+
82
+ def parse(value)
83
+ return parse_joysticks(value).
84
+ merge(parse_buttons(value)).
85
+ merge(parse_triggers(value)).
86
+ merge(parse_dpad(value)).
87
+ merge(parse_zbuttons(value))
88
+ end
89
+
90
+ def parse_joysticks(value)
91
+ {
92
+ :lx => decode_value(value, 0) & 0x3f,
93
+ :ly => decode_value(value, 1) & 0x3f,
94
+ :rx => ((decode_value(value, 0) & 0xC0) >> 3) | ((decode_value(value, 1) & 0xC0) >> 5) | (decode_value(value, 2)[7]),
95
+ :ry => decode_value(value, 2) & 0x1f
96
+ }
97
+ end
98
+
99
+ def parse_buttons(value)
100
+ {
101
+ :a => get_bool_decoded_value(value, 5, 4),
102
+ :b => get_bool_decoded_value(value, 5, 6),
103
+ :x => get_bool_decoded_value(value, 5, 3),
104
+ :y => get_bool_decoded_value(value, 5, 5),
105
+ :+ => get_bool_decoded_value(value, 4, 2),
106
+ :- => get_bool_decoded_value(value, 4, 4),
107
+ :h => get_bool_decoded_value(value, 4, 3)
108
+ }
109
+ end
110
+
111
+ def parse_triggers(value)
112
+ {
113
+ :lt => ((decode_value(value, 2) & 0x60) >> 3) | ((decode_value(value, 3) & 0xC0) >> 6),
114
+ :rt => decode_value(value, 3) & 0x1f
115
+ }
116
+ end
117
+
118
+ def parse_dpad(value)
119
+ {
120
+ :d_up => get_bool_decoded_value(value, 5, 0),
121
+ :d_down => get_bool_decoded_value(value, 4, 6),
122
+ :d_left => get_bool_decoded_value(value, 5, 1),
123
+ :d_right => get_bool_decoded_value(value, 4, 7)
124
+ }
125
+ end
126
+
127
+ def parse_zbuttons(value)
128
+ {
129
+ :zr => get_bool_decoded_value(value, 5, 2),
130
+ :zl => get_bool_decoded_value(value, 5, 7)
131
+ }
132
+ end
133
+
134
+ def get_bool_decoded_value(value, offset1, offset2)
135
+ generate_bool(decode_value(value, offset1)[offset2])
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,103 @@
1
+ require 'artoo/drivers/driver'
2
+
3
+ module Artoo
4
+ module Drivers
5
+ # Wii-based controller shared driver behaviors for Firmata
6
+ class Wiidriver < Driver
7
+ attr_reader :joystick, :data
8
+
9
+ def address; 0x52; end
10
+
11
+ # Create new Wiidriver
12
+ def initialize(params={})
13
+ @joystick = get_defaults
14
+ @data = {}
15
+ super
16
+ end
17
+
18
+ # Starts drives and required connections
19
+ def start_driver
20
+ begin
21
+ connection.i2c_config(0)
22
+ every(interval) do
23
+ connection.i2c_write_request(address, 0x40, 0x00)
24
+ connection.i2c_write_request(address, 0x00, 0x00)
25
+ connection.i2c_read_request(address, 6)
26
+
27
+ connection.read_and_process
28
+ handle_events
29
+ end
30
+
31
+ super
32
+ rescue Exception => e
33
+ Logger.error "Error starting wii driver!"
34
+ Logger.error e.message
35
+ Logger.error e.backtrace.inspect
36
+ end
37
+ end
38
+
39
+ # Get and update data
40
+ def update(value)
41
+ if encrypted?(value)
42
+ Logger.error "Encrypted bytes from wii device!"
43
+ raise "Encrypted bytes from wii device!"
44
+ end
45
+
46
+ @data = parse(value)
47
+ end
48
+
49
+ def handle_events
50
+ while i = find_event("i2c_reply") do
51
+ event = events.slice!(i)
52
+ update(event.data.first) if !event.nil?
53
+ end
54
+ end
55
+
56
+ protected
57
+
58
+ def find_event(name)
59
+ events.index {|e| e.name == name}
60
+ end
61
+
62
+ def events
63
+ connection.async_events
64
+ end
65
+
66
+ def get_defaults
67
+ {}
68
+ end
69
+
70
+ def parse
71
+ {}
72
+ end
73
+
74
+ def set_joystick_default_value(joystick_axis, default_value)
75
+ joystick[joystick_axis] = default_value if joystick[joystick_axis].nil?
76
+ end
77
+
78
+ def calculate_joystick_value(axis, origin)
79
+ data[axis] - joystick[origin]
80
+ end
81
+
82
+ def encrypted?(value)
83
+ [[0, 1], [2, 3], [4, 5]].all? {|a| get_value(value, a[0]) == get_value(value, a[1]) }
84
+ end
85
+
86
+ def decode(x)
87
+ return ( x ^ 0x17 ) + 0x17
88
+ end
89
+
90
+ def decode_value(value, index)
91
+ decode(get_value(value, index))
92
+ end
93
+
94
+ def get_value(value, index)
95
+ value[:data][index]
96
+ end
97
+
98
+ def generate_bool(value)
99
+ value == 0 ? true : false
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'artoo/adaptors/firmata'
3
+ require 'firmata'
4
+
5
+ describe Artoo::Adaptors::Firmata do
6
+ before do
7
+ @port = Artoo::Port.new('/dev/awesome')
8
+ @adaptor = Artoo::Adaptors::Firmata.new(:port => @port)
9
+ @firmata = mock('firmata')
10
+ Firmata::Board.stubs(:new).returns(@firmata)
11
+ end
12
+
13
+ it 'Artoo::Adaptors::Firmata#connect' do
14
+ @firmata.expects(:connect)
15
+ @adaptor.connect.must_equal true
16
+ end
17
+
18
+ it 'Artoo::Adaptors::Firmata#disconnect' do
19
+ @firmata.stubs(:connect)
20
+ @adaptor.connect
21
+ @adaptor.disconnect
22
+
23
+ @adaptor.connected?.must_equal false
24
+ end
25
+ end
@@ -0,0 +1,56 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'artoo/drivers/led'
3
+
4
+ describe Artoo::Drivers::Led do
5
+ before do
6
+ @device = mock('device')
7
+ @pin = 13
8
+ @device.stubs(:pin).returns(@pin)
9
+ @led = Artoo::Drivers::Led.new(:parent => @device)
10
+ @connection = mock('connection')
11
+ @device.stubs(:connection).returns(@connection)
12
+ end
13
+
14
+ it 'Led#is_on? default' do
15
+ @led.is_on?.must_equal false
16
+ end
17
+
18
+ it 'Led#is_off? default' do
19
+ @led.is_off?.must_equal true
20
+ end
21
+
22
+ it 'Led#on' do
23
+ @connection.expects(:set_pin_mode).with(@pin, Firmata::Board::OUTPUT)
24
+ @connection.expects(:digital_write).with(@pin, Firmata::Board::HIGH)
25
+ @led.on
26
+ @led.is_on?.must_equal true
27
+ end
28
+
29
+ it 'Led#off' do
30
+ @connection.expects(:set_pin_mode).with(@pin, Firmata::Board::OUTPUT)
31
+ @connection.expects(:digital_write).with(@pin, Firmata::Board::LOW)
32
+ @led.off
33
+ @led.is_off?.must_equal true
34
+ end
35
+
36
+ it 'Led#toggle' do
37
+ @connection.stubs(:set_pin_mode)
38
+ @connection.stubs(:digital_write)
39
+ @led.is_off?.must_equal true
40
+ @led.toggle
41
+ @led.is_on?.must_equal true
42
+ @led.toggle
43
+ @led.is_off?.must_equal true
44
+ end
45
+
46
+ it 'Led#brightness' do
47
+ val = 100
48
+ @connection.expects(:set_pin_mode).with(@pin, Firmata::Board::PWM)
49
+ @connection.expects(:analog_write).with(@pin, val)
50
+ @led.brightness(val)
51
+ end
52
+
53
+ it 'Led#commands' do
54
+ @led.commands.must_include :toggle
55
+ end
56
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'artoo/drivers/motor'
3
+
4
+ describe Artoo::Drivers::Motor do
5
+ before do
6
+ @device = mock('device')
7
+ @device.stubs(:pin).returns([2, 3, 4])
8
+ @motor = Artoo::Drivers::Motor.new(:parent => @device)
9
+
10
+ @connection = mock('connection')
11
+ @connection.stubs(:set_pin_mode)
12
+ @connection.stubs(:analog_write)
13
+ @connection.stubs(:digital_write)
14
+ @device.stubs(:connection).returns(@connection)
15
+ end
16
+
17
+ it 'Motor#speed must be valid' do
18
+ invalid_speed = lambda { @motor2 = Artoo::Drivers::Motor.new(:parent => @device); @motor2.speed("ads") }
19
+ invalid_speed.must_raise RuntimeError
20
+ error = invalid_speed.call rescue $!
21
+ error.message.must_equal 'Motor speed must be an integer between 0-255'
22
+ end
23
+
24
+ it 'Motor#forward' do
25
+ @motor.wrapped_object.expects(:set_legs)
26
+ @motor.forward(100)
27
+ @motor.current_speed.must_equal 100
28
+ end
29
+
30
+ it 'Motor#backward' do
31
+ @motor.wrapped_object.expects(:set_legs)
32
+ @motor.backward(100)
33
+ @motor.current_speed.must_equal 100
34
+ end
35
+
36
+ it 'Motor#stop' do
37
+ @motor.stop
38
+ @motor.current_speed.must_equal 0
39
+ end
40
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'artoo/drivers/servo'
3
+
4
+ describe Artoo::Drivers::Servo do
5
+ before do
6
+ @device = mock('device')
7
+ @device.stubs(:pin).returns(3)
8
+ @servo = Artoo::Drivers::Servo.new(:parent => @device)
9
+
10
+ @connection = mock('connection')
11
+ @connection.stubs(:set_pin_mode)
12
+ @connection.stubs(:analog_write)
13
+ # @connection.stubs(:digital_write)
14
+ @device.stubs(:connection).returns(@connection)
15
+ end
16
+
17
+ it "Servo#angle_to_span" do
18
+ @servo.angle_to_span(0).must_equal 0
19
+ @servo.angle_to_span(30).must_equal 42
20
+ @servo.angle_to_span(90).must_equal 127
21
+ @servo.angle_to_span(180).must_equal 255
22
+ end
23
+
24
+ it 'Servo#move must be valid' do
25
+ invalid_angle = lambda { @servo2 = Artoo::Drivers::Servo.new(:parent => @device); @servo2.move(360) }
26
+ invalid_angle.must_raise RuntimeError
27
+ error = invalid_angle.call rescue $!
28
+ error.message.must_equal 'Servo angle must be an integer between 0-180'
29
+ end
30
+
31
+ it 'Servo#min' do
32
+ @servo.min
33
+ @servo.current_angle.must_equal 0
34
+ end
35
+
36
+ it 'Servo#center' do
37
+ @servo.center
38
+ @servo.current_angle.must_equal 90
39
+ end
40
+
41
+ it 'Servo#max' do
42
+ @servo.max
43
+ @servo.current_angle.must_equal 180
44
+ end
45
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'artoo/drivers/wiichuck'
3
+
4
+ describe Artoo::Drivers::Wiichuck do
5
+ before do
6
+ @device = mock('device')
7
+ @driver = Artoo::Drivers::Wiichuck.new(:parent => @device)
8
+ end
9
+
10
+ it 'must do things'
11
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'artoo/drivers/wiiclassic'
3
+
4
+ describe Artoo::Drivers::Wiiclassic do
5
+ before do
6
+ @device = mock('device')
7
+ @driver = Artoo::Drivers::Wiiclassic.new(:parent => @device)
8
+ end
9
+
10
+ it 'must do things'
11
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'artoo/drivers/wiidriver'
3
+
4
+ class Artoo::Drivers::Wiidriver
5
+ public :set_joystick_default_value, :calculate_joystick_value, :encrypted?,
6
+ :decode, :get_value, :generate_bool
7
+ end
8
+
9
+ describe Artoo::Drivers::Wiidriver do
10
+ before do
11
+ @device = mock('device')
12
+ @driver = Artoo::Drivers::Wiidriver.new(:parent => @device)
13
+ end
14
+
15
+ it 'Artoo::Drivers::Wiidriver#set_joystick_default_value' do
16
+ val = "101"
17
+ @driver.joystick[:test_axis] = nil
18
+ @driver.set_joystick_default_value(:test_axis, val)
19
+ @driver.joystick[:test_axis].must_equal val
20
+ end
21
+
22
+ it 'Artoo::Drivers::Wiidriver#calculate_joystick_value' do
23
+ @driver.data[:test_axis] = 8
24
+ @driver.joystick[:test_origin] = 5
25
+ @driver.calculate_joystick_value(:test_axis, :test_origin).must_equal 3
26
+ end
27
+
28
+ it 'Artoo::Drivers::Wiidriver#encrypted?' do
29
+ value = {:data => [0, 0, 0, 0, 0, 0]}
30
+ @driver.encrypted?(value).must_equal true
31
+ end
32
+
33
+ it 'Artoo::Drivers::Wiidriver#encrypted?' do
34
+ value = {:data => [1, 2, 3, 4, 5, 6]}
35
+ @driver.encrypted?(value).must_equal false
36
+ end
37
+
38
+ it 'Artoo::Drivers::Wiidriver#decode' do
39
+ @driver.decode(22).must_equal 24
40
+ @driver.decode(0).must_equal 46
41
+ @driver.decode(16).must_equal 30
42
+ end
43
+
44
+ it 'Artoo::Drivers::Wiidriver#get_value' do
45
+ value = {:data => [1, 2, 3, 4, 5, 6], :other_data => [10, 20, 30, 40, 50, 60]}
46
+ @driver.get_value(value, 1).must_equal 2
47
+ @driver.get_value(value, 5).must_equal 6
48
+ end
49
+
50
+ it 'Artoo::Drivers::Wiidriver#generate_bool' do
51
+ @driver.generate_bool(0).must_equal true
52
+ @driver.generate_bool(1).must_equal false
53
+ end
54
+ end
@@ -0,0 +1,6 @@
1
+ require 'minitest/autorun'
2
+ require 'mocha/setup'
3
+
4
+ require 'artoo/robot'
5
+
6
+ Celluloid.logger = nil
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artoo-arduino
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Ron Evans
8
+ - Adrian Zankich
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: artoo
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: 1.0.0.pre
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: 1.0.0.pre
28
+ - !ruby/object:Gem::Dependency
29
+ name: hybridgroup-firmata
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 0.4.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: 0.4.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: '5.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '5.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: minitest-happy
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: mocha
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.14.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: 0.14.0
84
+ description: Artoo adaptor and driver for Arduino
85
+ email:
86
+ - artoo@hybridgroup.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .rvmrc.example
93
+ - .travis.yml
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE
97
+ - README.md
98
+ - Rakefile
99
+ - artoo-arduino.gemspec
100
+ - examples/button_and_led.rb
101
+ - examples/firmata.rb
102
+ - lib/artoo-arduino.rb
103
+ - lib/artoo-arduino/version.rb
104
+ - lib/artoo/adaptors/firmata.rb
105
+ - lib/artoo/drivers/button.rb
106
+ - lib/artoo/drivers/led.rb
107
+ - lib/artoo/drivers/motor.rb
108
+ - lib/artoo/drivers/servo.rb
109
+ - lib/artoo/drivers/wiichuck.rb
110
+ - lib/artoo/drivers/wiiclassic.rb
111
+ - lib/artoo/drivers/wiidriver.rb
112
+ - test/adaptors/firmata_test.rb
113
+ - test/drivers/led_test.rb
114
+ - test/drivers/motor_test.rb
115
+ - test/drivers/servo_test.rb
116
+ - test/drivers/wiichuck_test.rb
117
+ - test/drivers/wiiclassic_test.rb
118
+ - test/drivers/wiidriver_test.rb
119
+ - test/test_helper.rb
120
+ homepage: https://github.com/hybridgroup/artoo-arduino
121
+ licenses: []
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>'
135
+ - !ruby/object:Gem::Version
136
+ version: 1.3.1
137
+ requirements: []
138
+ rubyforge_project: artoo-arduino
139
+ rubygems_version: 2.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Artoo adaptor and driver for Arduino
143
+ test_files: []