Class: Artoo::Drivers::Motor
- Defined in:
- lib/artoo/drivers/motor.rb
Overview
L293 or other H-bridge style motor driver behaviors for Firmata
Constant Summary collapse
- COMMANDS =
[:forward, :backward, :stop, :speed, :current_speed].freeze
Instance Attribute Summary collapse
-
#current_speed ⇒ Object
readonly
Returns the value of attribute current_speed.
-
#leg1_pin ⇒ Object
readonly
Returns the value of attribute leg1_pin.
-
#leg2_pin ⇒ Object
readonly
Returns the value of attribute leg2_pin.
-
#speed_pin ⇒ Object
readonly
Returns the value of attribute speed_pin.
Attributes inherited from Driver
Instance Method Summary collapse
-
#backward(s) ⇒ Object
Sets movement backward.
-
#forward(s) ⇒ Object
Sets movement forward.
-
#initialize(params = {}) ⇒ Motor
constructor
Create new Motor.
-
#speed(s) ⇒ Object
Set motor speed.
-
#start_driver ⇒ Object
Starts connection to read and process and driver.
-
#stop ⇒ Object
Stops.
Methods inherited from Driver
#command, #commands, #connection, #event_topic_name, #interval, #known_command?, #method_missing, #pin
Constructor Details
#initialize(params = {}) ⇒ Motor
Create new Motor
12 13 14 15 16 17 18 19 20 |
# File 'lib/artoo/drivers/motor.rb', line 12 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_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 |
#leg1_pin ⇒ Object (readonly)
Returns the value of attribute leg1_pin.
9 10 11 |
# File 'lib/artoo/drivers/motor.rb', line 9 def leg1_pin @leg1_pin end |
#leg2_pin ⇒ Object (readonly)
Returns the value of attribute leg2_pin.
9 10 11 |
# File 'lib/artoo/drivers/motor.rb', line 9 def leg2_pin @leg2_pin 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 |
Instance Method Details
#backward(s) ⇒ Object
Sets movement backward
40 41 42 43 |
# File 'lib/artoo/drivers/motor.rb', line 40 def backward(s) set_legs(Firmata::Board::HIGH, Firmata::Board::LOW) speed(s) end |
#forward(s) ⇒ Object
Sets movement forward
33 34 35 36 |
# File 'lib/artoo/drivers/motor.rb', line 33 def forward(s) set_legs(Firmata::Board::LOW, Firmata::Board::HIGH) speed(s) end |
#speed(s) ⇒ Object
Set motor speed
52 53 54 55 56 57 |
# File 'lib/artoo/drivers/motor.rb', line 52 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_driver ⇒ Object
Starts connection to read and process and driver
23 24 25 26 27 28 29 |
# File 'lib/artoo/drivers/motor.rb', line 23 def start_driver every(interval) do connection.read_and_process end super end |
#stop ⇒ Object
Stops
46 47 48 |
# File 'lib/artoo/drivers/motor.rb', line 46 def stop speed(0) end |