class Artoo::Drivers::Motor

Constants

COMMANDS

Attributes

current_speed[R]
speed_pin[R]
switch_pin[R]

Public Class Methods

new(params={}) click to toggle source
Calls superclass method
# File lib/artoo/drivers/motor.rb, line 11
def initialize(params={})
  super
  
  additional_params = params[:additional_params]
  @speed_pin = additional_params[:speed_pin]
  @switch_pin = additional_params[:switch_pin] if additional_params[:switch_pin]

  @forward_pin = additional_params[:forward_pin]
  @backward_pin = additional_params[:backward_pin]

  @current_state = :low
  @current_speed = 0

  # digital: just to switch the motor on or off, no speed control
  # analog: speed control
  @current_mode = :digital

  @current_direction = :forward

  @@modules_to_include = modules_to_include

  class << self
    @@modules_to_include.each do |m|
      include m
    end if @@modules_to_include
  end

end

Public Instance Methods

analog?() click to toggle source
# File lib/artoo/drivers/motor.rb, line 44
def analog?
  @current_mode == :analog
end
change_state(state) click to toggle source
# File lib/artoo/drivers/motor.rb, line 88
def change_state(state)
  @current_state = state
  @current_speed = state == :low ? 0 : 255
  connection.digital_write(@speed_pin, state)
end
digital?() click to toggle source
# File lib/artoo/drivers/motor.rb, line 40
def digital?
  @current_mode == :digital
end
max() click to toggle source
# File lib/artoo/drivers/motor.rb, line 68
def max
  speed(255)
end
min() click to toggle source
# File lib/artoo/drivers/motor.rb, line 64
def min
  stop
end
off?() click to toggle source
# File lib/artoo/drivers/motor.rb, line 80
def off?
  !on?
end
on?() click to toggle source
# File lib/artoo/drivers/motor.rb, line 72
def on?
  if digital?
    @current_state == :high
  else
    @current_speed > 0
  end
end
speed(value) click to toggle source

Set motor speed @param [Integer] value (must be an integer between 0-255)

# File lib/artoo/drivers/motor.rb, line 96
def speed(value)
  @current_mode = :analog
  raise "Motor speed must be an integer between 0-255" unless (value.is_a?(Numeric) && value >= 0 && value <= 255)
  @current_speed = value
  connection.pwm_write(speed_pin, value)
end
start() click to toggle source
# File lib/artoo/drivers/motor.rb, line 56
def start
  if digital?
    change_state(:high)
  else
    speed(@current_speed.zero? ? 255 : @current_speed)
  end
end
stop() click to toggle source
# File lib/artoo/drivers/motor.rb, line 48
def stop
  if digital?
    change_state(:low)
  else
    speed(0)
  end
end
toggle() click to toggle source
# File lib/artoo/drivers/motor.rb, line 84
def toggle
  on? ? stop : start
end

Private Instance Methods

modules_to_include() click to toggle source
# File lib/artoo/drivers/motor.rb, line 104
def modules_to_include
  if @forward_pin and @backward_pin
    [BidirectionalWithForwardBackwardPins]
  end
end