Class: Artoo::Drivers::Motor

Inherits:
Driver
  • Object
show all
Defined in:
lib/artoo/drivers/motor.rb

Overview

L293 or other H-bridge style motor driver behaviors for Firmata

Instance Attribute Summary collapse

Attributes inherited from Driver

#parent

Instance Method Summary collapse

Methods inherited from Driver

#connection, #event_topic_name, #interval, #method_missing, #pin

Methods included from Celluloid

#timers

Constructor Details

#initialize(params = {}) ⇒ Motor

Returns a new instance of Motor.



9
10
11
12
13
14
15
16
17
# File 'lib/artoo/drivers/motor.rb', line 9

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Artoo::Drivers::Driver

Instance Attribute Details

#current_speedObject (readonly)

Returns the value of attribute current_speed.



7
8
9
# File 'lib/artoo/drivers/motor.rb', line 7

def current_speed
  @current_speed
end

#leg1_pinObject (readonly)

Returns the value of attribute leg1_pin.



7
8
9
# File 'lib/artoo/drivers/motor.rb', line 7

def leg1_pin
  @leg1_pin
end

#leg2_pinObject (readonly)

Returns the value of attribute leg2_pin.



7
8
9
# File 'lib/artoo/drivers/motor.rb', line 7

def leg2_pin
  @leg2_pin
end

#speed_pinObject (readonly)

Returns the value of attribute speed_pin.



7
8
9
# File 'lib/artoo/drivers/motor.rb', line 7

def speed_pin
  @speed_pin
end

Instance Method Details

#backward(s) ⇒ Object



32
33
34
35
# File 'lib/artoo/drivers/motor.rb', line 32

def backward(s)
  set_legs(Firmata::Board::HIGH, Firmata::Board::LOW)
  speed(s)
end

#forward(s) ⇒ Object



27
28
29
30
# File 'lib/artoo/drivers/motor.rb', line 27

def forward(s)
  set_legs(Firmata::Board::LOW, Firmata::Board::HIGH)
  speed(s)
end

#speed(s) ⇒ Object



41
42
43
44
45
46
# File 'lib/artoo/drivers/motor.rb', line 41

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

#start_driverObject



19
20
21
22
23
24
25
# File 'lib/artoo/drivers/motor.rb', line 19

def start_driver
  every(interval) do
    connection.read_and_process
  end

  super
end

#stopObject



37
38
39
# File 'lib/artoo/drivers/motor.rb', line 37

def stop
  speed(0)
end