artoo-arduino 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +15 -17
- data/README.md +8 -63
- data/artoo-arduino.gemspec +4 -3
- data/examples/analog_sensor.rb +31 -0
- data/examples/{firmata_blink_led.rb → blink_led.rb} +1 -1
- data/examples/{firmata_blink_led_with_toggle.rb → blink_led_with_toggle.rb} +1 -1
- data/examples/{firmata_button_and_led.rb → button_and_led.rb} +0 -0
- data/examples/{firmata_dc_motor_speed.rb → dc_motor_speed.rb} +1 -1
- data/examples/{firmata_dc_motor_speed_h-bridge_2_pins.rb → dc_motor_speed_h-bridge_2_pins.rb} +1 -1
- data/examples/{firmata_dc_motor_switch.rb → dc_motor_switch.rb} +1 -1
- data/examples/{firmata_dc_motor_switch_h-bridge_2_pins.rb → dc_motor_switch_h-bridge_2_pins.rb} +1 -1
- data/examples/{firmata_led_brightness.rb → led_brightness.rb} +1 -1
- data/examples/led_brightness_with_analog_input.rb +29 -0
- data/examples/{firmata_led_with_button_toggle.rb → led_with_button_toggle.rb} +1 -1
- data/examples/{firmata_maxbotix.rb → maxbotix.rb} +2 -2
- data/examples/{firmata_motor.rb → motor.rb} +1 -1
- data/examples/motor_speed_with_analog_input.rb +30 -0
- data/examples/{firmata_servo.rb → servo.rb} +1 -1
- data/examples/{firmata_wiichuck.rb → wiichuck.rb} +0 -0
- data/lib/artoo-arduino/version.rb +1 -1
- data/lib/artoo/adaptors/firmata.rb +51 -9
- data/test/adaptors/firmata_test.rb +30 -0
- metadata +43 -48
- data/examples/littlewire_blink_led_with_toggle.rb +0 -17
- data/examples/littlewire_button_and_led.rb +0 -16
- data/examples/littlewire_dc_motor_speed.rb +0 -31
- data/examples/littlewire_maxbotix.rb +0 -19
- data/lib/artoo/adaptors/littlewire.rb +0 -63
- data/lib/artoo/drivers/board.rb +0 -10
- data/lib/artoo/drivers/button.rb +0 -43
- data/lib/artoo/drivers/led.rb +0 -59
- data/lib/artoo/drivers/maxbotix.rb +0 -44
- data/lib/artoo/drivers/motor.rb +0 -167
- data/lib/artoo/drivers/servo.rb +0 -49
- data/lib/artoo/drivers/wiichuck.rb +0 -61
- data/lib/artoo/drivers/wiiclassic.rb +0 -139
- data/lib/artoo/drivers/wiidriver.rb +0 -103
- data/test/adaptors/littlewire_test.rb +0 -25
- data/test/drivers/firmata_board_test.rb +0 -21
- data/test/drivers/led_test.rb +0 -92
- data/test/drivers/maxbotix_test.rb +0 -25
- data/test/drivers/motor_test.rb +0 -204
- data/test/drivers/servo_test.rb +0 -43
- data/test/drivers/wiichuck_test.rb +0 -11
- data/test/drivers/wiiclassic_test.rb +0 -11
- data/test/drivers/wiidriver_test.rb +0 -54
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'artoo/drivers/driver'
|
2
|
-
|
3
|
-
module Artoo
|
4
|
-
module Drivers
|
5
|
-
# Maxbotix ultrasonic range finder driver behaviors for Firmata
|
6
|
-
class Maxbotix < Driver
|
7
|
-
COMMANDS = [:range].freeze
|
8
|
-
|
9
|
-
attr_accessor :last_reading
|
10
|
-
|
11
|
-
def initialize(params={})
|
12
|
-
@last_reading = 0.0
|
13
|
-
super
|
14
|
-
end
|
15
|
-
|
16
|
-
# @return [float] last range reading in inches
|
17
|
-
def range
|
18
|
-
return ( 254.0 / 1024.0 ) * 2.0 * last_reading
|
19
|
-
end
|
20
|
-
|
21
|
-
# @return [float] last range reading in cm
|
22
|
-
def range_cm
|
23
|
-
return (last_reading / 2.0) * 2.54
|
24
|
-
end
|
25
|
-
|
26
|
-
# Sets values to read from ultrasonic range finder
|
27
|
-
# and starts driver
|
28
|
-
def start_driver
|
29
|
-
every(interval) do
|
30
|
-
update(connection.analog_read(pin))
|
31
|
-
end
|
32
|
-
|
33
|
-
super
|
34
|
-
end
|
35
|
-
|
36
|
-
# Publishes events according to the ultrasonic rangefinder value
|
37
|
-
def update(value)
|
38
|
-
last_reading = value
|
39
|
-
publish(event_topic_name("update"), "range", range)
|
40
|
-
publish(event_topic_name("range"), range)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
data/lib/artoo/drivers/motor.rb
DELETED
@@ -1,167 +0,0 @@
|
|
1
|
-
require 'artoo/drivers/driver'
|
2
|
-
|
3
|
-
module Artoo
|
4
|
-
module Drivers
|
5
|
-
class Motor < Driver
|
6
|
-
|
7
|
-
COMMANDS = [:stop, :start, :on?, :off?, :toggle, :speed, :min, :max, :forward, :backward, :current_speed].freeze
|
8
|
-
|
9
|
-
attr_reader :speed_pin, :switch_pin, :current_speed
|
10
|
-
|
11
|
-
def initialize(params={})
|
12
|
-
super
|
13
|
-
|
14
|
-
additional_params = params[:additional_params]
|
15
|
-
@speed_pin = additional_params[:speed_pin]
|
16
|
-
@switch_pin = additional_params[:switch_pin] if additional_params[:switch_pin]
|
17
|
-
|
18
|
-
@forward_pin = additional_params[:forward_pin]
|
19
|
-
@backward_pin = additional_params[:backward_pin]
|
20
|
-
|
21
|
-
@current_state = :low
|
22
|
-
@current_speed = 0
|
23
|
-
|
24
|
-
# digital: just to switch the motor on or off, no speed control
|
25
|
-
# analog: speed control
|
26
|
-
@current_mode = :digital
|
27
|
-
|
28
|
-
@current_direction = :forward
|
29
|
-
|
30
|
-
@@modules_to_include = modules_to_include
|
31
|
-
|
32
|
-
class << self
|
33
|
-
@@modules_to_include.each do |m|
|
34
|
-
include m
|
35
|
-
end if @@modules_to_include
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def digital?
|
41
|
-
@current_mode == :digital
|
42
|
-
end
|
43
|
-
|
44
|
-
def analog?
|
45
|
-
@current_mode == :analog
|
46
|
-
end
|
47
|
-
|
48
|
-
def stop
|
49
|
-
if digital?
|
50
|
-
change_state(:low)
|
51
|
-
else
|
52
|
-
speed(0)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def start
|
57
|
-
if digital?
|
58
|
-
change_state(:high)
|
59
|
-
else
|
60
|
-
speed(@current_speed.zero? ? 255 : @current_speed)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def min
|
65
|
-
stop
|
66
|
-
end
|
67
|
-
|
68
|
-
def max
|
69
|
-
speed(255)
|
70
|
-
end
|
71
|
-
|
72
|
-
def on?
|
73
|
-
if digital?
|
74
|
-
@current_state == :high
|
75
|
-
else
|
76
|
-
@current_speed > 0
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def off?
|
81
|
-
!on?
|
82
|
-
end
|
83
|
-
|
84
|
-
def toggle
|
85
|
-
on? ? stop : start
|
86
|
-
end
|
87
|
-
|
88
|
-
def change_state(state)
|
89
|
-
@current_state = state
|
90
|
-
@current_speed = state == :low ? 0 : 255
|
91
|
-
connection.digital_write(@speed_pin, state)
|
92
|
-
end
|
93
|
-
|
94
|
-
# Set motor speed
|
95
|
-
# @param [Integer] value (must be an integer between 0-255)
|
96
|
-
def speed(value)
|
97
|
-
@current_mode = :analog
|
98
|
-
raise "Motor speed must be an integer between 0-255" unless (value.is_a?(Numeric) && value >= 0 && value <= 255)
|
99
|
-
@current_speed = value
|
100
|
-
connection.pwm_write(speed_pin, value)
|
101
|
-
end
|
102
|
-
|
103
|
-
private
|
104
|
-
def modules_to_include
|
105
|
-
if @forward_pin and @backward_pin
|
106
|
-
[BidirectionalWithForwardBackwardPins]
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
module BidirectionalWithForwardBackwardPins
|
112
|
-
|
113
|
-
# Sets movement forward
|
114
|
-
# @param [Integer] speed
|
115
|
-
def forward(speed = nil)
|
116
|
-
direction(:forward)
|
117
|
-
speed ? self.speed(speed) : start
|
118
|
-
end
|
119
|
-
|
120
|
-
# Sets movement backward
|
121
|
-
# @param [Integer] speed
|
122
|
-
def backward(speed = nil)
|
123
|
-
direction(:backward)
|
124
|
-
speed ? self.speed(speed) : start
|
125
|
-
end
|
126
|
-
|
127
|
-
def forward?
|
128
|
-
@current_direction == :forward
|
129
|
-
end
|
130
|
-
|
131
|
-
def backward?
|
132
|
-
(not forward?)
|
133
|
-
end
|
134
|
-
|
135
|
-
private
|
136
|
-
def change_state(state)
|
137
|
-
@current_state = state
|
138
|
-
@current_speed = state.zero? ? 0 : 255
|
139
|
-
if state == :high
|
140
|
-
direction(@current_direction)
|
141
|
-
speed(@current_speed) if speed_pin
|
142
|
-
elsif state == :low
|
143
|
-
direction(:none)
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
def direction(direction)
|
148
|
-
@current_direction = direction
|
149
|
-
case direction
|
150
|
-
when :forward
|
151
|
-
forward_pin_level = :high
|
152
|
-
backward_pin_level = :low
|
153
|
-
when :backward
|
154
|
-
forward_pin_level = :low
|
155
|
-
backward_pin_level = :high
|
156
|
-
when :none
|
157
|
-
forward_pin_level = :low
|
158
|
-
backward_pin_level = :low
|
159
|
-
end
|
160
|
-
|
161
|
-
connection.digital_write(@forward_pin, forward_pin_level)
|
162
|
-
connection.digital_write(@backward_pin, backward_pin_level)
|
163
|
-
end
|
164
|
-
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
data/lib/artoo/drivers/servo.rb
DELETED
@@ -1,49 +0,0 @@
|
|
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, :current_angle].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
|
-
# Moves to specified angle
|
19
|
-
# @param [Integer] angle must be between 0-180
|
20
|
-
def move(angle)
|
21
|
-
raise "Servo angle must be an integer between 0-180" unless (angle.is_a?(Numeric) && angle >= 0 && angle <= 180)
|
22
|
-
|
23
|
-
@current_angle = angle
|
24
|
-
connection.servo_write(pin, angle_to_span(angle))
|
25
|
-
end
|
26
|
-
|
27
|
-
# Moves to min position
|
28
|
-
def min
|
29
|
-
move(0)
|
30
|
-
end
|
31
|
-
|
32
|
-
# Moves to center position
|
33
|
-
def center
|
34
|
-
move(90)
|
35
|
-
end
|
36
|
-
|
37
|
-
# Moves to max position
|
38
|
-
def max
|
39
|
-
move(180)
|
40
|
-
end
|
41
|
-
|
42
|
-
# converts an angle to a span between 0-255
|
43
|
-
# @param [Integer] angle
|
44
|
-
def angle_to_span(angle)
|
45
|
-
(angle * 255 / 180).to_i
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,61 +0,0 @@
|
|
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
|
@@ -1,139 +0,0 @@
|
|
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
|