Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
deadprogram committed May 20, 2013
0 parents commit f626c90
Show file tree
Hide file tree
Showing 24 changed files with 891 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.rvmrc
.rbx
pkg
1 change: 1 addition & 0 deletions .rvmrc.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvm use rbx-2.0.0-rc1@artoo-arduino --create
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "http://rubygems.org"

# Specify your gem's dependencies in artoo-sphero.gemspec
gemspec

# these are for specific hardware, they have to be here for tests
gem "hybridgroup-firmata"
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2012, 2013 The Hybrid Group

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Artoo Adaptor For Arduino

This repository contains the Artoo (http://artoo.io/) adaptor and drivers for the Arduino microcontroller using the firmata protocol.

Artoo is a open source micro-framework for robotics using Ruby.

For more information abut Artoo, check out our repo at https://github.com/hybridgroup/artoo
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'bundler'
Bundler::GemHelper.install_tasks

require 'rake/testtask'

Rake::TestTask.new do |t|
t.pattern = "test/**/*_test.rb"
end

task :default => :test
26 changes: 26 additions & 0 deletions artoo-arduino.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "artoo-arduino/version"

Gem::Specification.new do |s|
s.name = "artoo-arduino"
s.version = Artoo::Arduino::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Ron Evans", "Adrian Zankich"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/hybridgroup/artoo-arduino"
s.summary = %q{Artoo adaptor and driver for Arduino}
s.description = %q{Artoo adaptor and driver for Arduino}

s.rubyforge_project = "artoo-arduino"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_runtime_dependency 'artoo', '~> 0.5.0'
s.add_development_dependency 'minitest', '~> 5.0'
s.add_development_dependency 'minitest-happy'
s.add_development_dependency 'mocha', '~> 0.14.0'
end
7 changes: 7 additions & 0 deletions lib/artoo-arduino.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'lib/artoo/adaptors/firmata'

require 'lib/artoo/drivers/ardrone'
require 'lib/artoo/drivers/ardrone_navigation'
require 'lib/artoo/drivers/ardrone_video'

require 'lib/artoo-arduino/version'
5 changes: 5 additions & 0 deletions lib/artoo-arduino/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Artoo
module Arduino
VERSION = '0.1.0'
end
end
33 changes: 33 additions & 0 deletions lib/artoo/adaptors/firmata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'artoo/adaptors/adaptor'

module Artoo
module Adaptors
# Connect to Arduino using Firmata
# @see http://rubydoc.info/gems/hybridgroup-firmata/0.3.0/Firmata/Board HybridGroup Firmata Documentation
class Firmata < Adaptor
attr_reader :firmata

# Creates connection with firmata board
# @return [Boolean]
def connect
require 'firmata' unless defined?(::Firmata)
@firmata = ::Firmata::Board.new(connect_to)
@firmata.connect
super
return true
end

# Closes connection with firmata board
# @return [Boolean]
def disconnect
super
end

# Uses method missing to call Firmata Board methods
# @see http://rubydoc.info/gems/hybridgroup-firmata/0.3.0/Firmata/Board Firmata Board Documentation
def method_missing(method_name, *arguments, &block)
firmata.send(method_name, *arguments, &block)
end
end
end
end
59 changes: 59 additions & 0 deletions lib/artoo/drivers/button.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'artoo/drivers/driver'

module Artoo
module Drivers
# Button driver behaviors for Firmata
class Button < Driver
COMMANDS = [:is_pressed?].freeze

DOWN = 1
UP = 0

# @return [Boolean] True if pressed
def is_pressed?
(@is_pressed ||= false) == true
end

# Sets values to read and write from button
# and starts driver
def start_driver
connection.set_pin_mode(pin, Firmata::Board::INPUT)
connection.toggle_pin_reporting(pin)

every(interval) do
connection.read_and_process
handle_events
end

super
end

def handle_events
while i = find_event("digital-read-#{pin}") do
update(events.slice!(i).data.first)
end
end

def find_event(name)
events.index {|e| e.name == name}
end

def events
connection.async_events
end

# Publishes events according to the button feedback
def update(value)
if value == DOWN
@is_pressed = true
publish(event_topic_name("update"), "push", value)
publish(event_topic_name("push"), value)
else
@is_pressed = false
publish(event_topic_name("update"), "release", value)
publish(event_topic_name("release"), value)
end
end
end
end
end
48 changes: 48 additions & 0 deletions lib/artoo/drivers/led.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'artoo/drivers/driver'

module Artoo
module Drivers
# The LED driver behaviors
class Led < Driver

COMMANDS = [:on, :off, :toggle, :brightness].freeze

# @return [Boolean] True if on
def is_on?
(@is_on ||= false) == true
end

# @return [Boolean] True if off
def is_off?
(@is_on ||= false) == false
end

# Sets led to on status
def on
@is_on = true
connection.set_pin_mode(pin, Firmata::Board::OUTPUT)
connection.digital_write(pin, Firmata::Board::HIGH)
end

# Sets led to off status
def off
@is_on = false
connection.set_pin_mode(pin, Firmata::Board::OUTPUT)
connection.digital_write(pin, Firmata::Board::LOW)
end

# Toggle status
# @example on > off, off > on
def toggle
is_off? ? on : off
end

# Change brightness level
# @param [Integer] level
def brightness(level=0)
connection.set_pin_mode(pin, Firmata::Board::PWM)
connection.analog_write(pin, level)
end
end
end
end
69 changes: 69 additions & 0 deletions lib/artoo/drivers/motor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'artoo/drivers/driver'

module Artoo
module Drivers
# L293 or other H-bridge style motor driver behaviors for Firmata
class Motor < Driver
COMMANDS = [:forward, :backward, :stop, :speed, :current_speed].freeze

attr_reader :leg1_pin, :leg2_pin, :speed_pin, :current_speed

# Create new Motor
def initialize(params={})
super

raise "Invalid pins, please pass an array in format [leg1, leg2, speed]" unless (pin && pin.is_a?(Array) && pin.size == 3)
@leg1_pin = pin[0]
@leg2_pin = pin[1]
@speed_pin = pin[2]
@current_speed = 0
end

# Starts connection to read and process and driver
def start_driver
every(interval) do
connection.read_and_process
end

super
end

# Sets movement forward
# @param [Integer] s speed
def forward(s)
set_legs(Firmata::Board::LOW, Firmata::Board::HIGH)
speed(s)
end

# Sets movement backward
# @param [Integer] s speed
def backward(s)
set_legs(Firmata::Board::HIGH, Firmata::Board::LOW)
speed(s)
end

# Stops
def stop
speed(0)
end

# Set motor speed
# @param [Integer] s speed (must be an integer between 0-255)
def speed(s)
raise "Motor speed must be an integer between 0-255" unless (s.is_a?(Numeric) && s >= 0 && s <= 255)
@current_speed = s
connection.set_pin_mode(speed_pin, Firmata::Board::PWM)
connection.analog_write(speed_pin, s)
end

private

def set_legs(l1, l2)
connection.set_pin_mode(leg1_pin, Firmata::Board::OUTPUT)
connection.digital_write(leg1_pin, l1)
connection.set_pin_mode(leg2_pin, Firmata::Board::OUTPUT)
connection.digital_write(leg2_pin, l2)
end
end
end
end
59 changes: 59 additions & 0 deletions lib/artoo/drivers/servo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'artoo/drivers/driver'

module Artoo
module Drivers
# Servo behaviors for Firmata
class Servo < Driver
COMMANDS = [:move, :min, :center, :max].freeze

attr_reader :current_angle

# Create new Servo with angle=0
def initialize(params={})
super

@current_angle = 0
end

# Starts connection to read and process and driver
def start_driver
every(interval) do
connection.read_and_process
end

super
end

# Moves to specified angle
# @param [Integer] angle must be between 0-180
def move(angle)
raise "Servo angle must be an integer between 0-180" unless (angle.is_a?(Numeric) && angle >= 0 && angle <= 180)

@current_angle = angle
connection.set_pin_mode(pin, Firmata::Board::SERVO)
connection.analog_write(pin, angle_to_span(angle))
end

# Moves to min position
def min
move(0)
end

# Moves to center position
def center
move(90)
end

# Moves to max position
def max
move(180)
end

# converts an angle to a span between 0-255
# @param [Integer] angle
def angle_to_span(angle)
(angle * 255 / 180).to_i
end
end
end
end
Loading

0 comments on commit f626c90

Please sign in to comment.