Class: Artoo::Drivers::Motor

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

Constant Summary collapse

COMMANDS =
[:stop, :start, :on?, :off?, :toggle, :speed, :min, :max, :forward, :backward, :current_speed].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Motor

Returns a new instance of Motor.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

Instance Attribute Details

#current_speedObject (readonly)

Returns the value of attribute current_speed.



9
10
11
# File 'lib/artoo/drivers/motor.rb', line 9

def current_speed
  @current_speed
end

#speed_pinObject (readonly)

Returns the value of attribute speed_pin.



9
10
11
# File 'lib/artoo/drivers/motor.rb', line 9

def speed_pin
  @speed_pin
end

#switch_pinObject (readonly)

Returns the value of attribute switch_pin.



9
10
11
# File 'lib/artoo/drivers/motor.rb', line 9

def switch_pin
  @switch_pin
end

Instance Method Details

#analog?Boolean

Returns:

  • (Boolean)


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

def analog?
  @current_mode == :analog
end

#change_state(state) ⇒ Object



88
89
90
91
92
# 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?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/artoo/drivers/motor.rb', line 40

def digital?
  @current_mode == :digital
end

#maxObject



68
69
70
# File 'lib/artoo/drivers/motor.rb', line 68

def max
  speed(255)
end

#minObject



64
65
66
# File 'lib/artoo/drivers/motor.rb', line 64

def min
  stop
end

#off?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/artoo/drivers/motor.rb', line 80

def off?
  !on?
end

#on?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/artoo/drivers/motor.rb', line 72

def on?
  if digital?
    @current_state == :high
  else
    @current_speed > 0
  end
end

#speed(value) ⇒ Object

Set motor speed

Parameters:

  • value (Integer)

    (must be an integer between 0-255)



96
97
98
99
100
101
# 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

#startObject



56
57
58
59
60
61
62
# 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

#stopObject



48
49
50
51
52
53
54
# File 'lib/artoo/drivers/motor.rb', line 48

def stop
  if digital?
    change_state(:low)
  else
    speed(0)
  end
end

#toggleObject



84
85
86
# File 'lib/artoo/drivers/motor.rb', line 84

def toggle
  on? ? stop : start
end