Class: Artoo::Drivers::Motor
- Inherits:
-
Driver
- Object
- Driver
- Artoo::Drivers::Motor
- 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
-
#current_speed ⇒ Object
readonly
Returns the value of attribute current_speed.
-
#speed_pin ⇒ Object
readonly
Returns the value of attribute speed_pin.
-
#switch_pin ⇒ Object
readonly
Returns the value of attribute switch_pin.
Instance Method Summary collapse
- #analog? ⇒ Boolean
- #change_state(state) ⇒ Object
- #digital? ⇒ Boolean
-
#initialize(params = {}) ⇒ Motor
constructor
A new instance of Motor.
- #max ⇒ Object
- #min ⇒ Object
- #off? ⇒ Boolean
- #on? ⇒ Boolean
-
#speed(value) ⇒ Object
Set motor speed.
- #start ⇒ Object
- #stop ⇒ Object
- #toggle ⇒ Object
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_speed ⇒ Object (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_pin ⇒ Object (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_pin ⇒ Object (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
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
40 41 42 |
# File 'lib/artoo/drivers/motor.rb', line 40 def digital? @current_mode == :digital end |
#max ⇒ Object
68 69 70 |
# File 'lib/artoo/drivers/motor.rb', line 68 def max speed(255) end |
#min ⇒ Object
64 65 66 |
# File 'lib/artoo/drivers/motor.rb', line 64 def min stop end |
#off? ⇒ Boolean
80 81 82 |
# File 'lib/artoo/drivers/motor.rb', line 80 def off? !on? end |
#on? ⇒ 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
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 |
#start ⇒ Object
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 |
#stop ⇒ Object
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 |
#toggle ⇒ Object
84 85 86 |
# File 'lib/artoo/drivers/motor.rb', line 84 def toggle on? ? stop : start end |