Class: Artoo::Drivers::Servo
- Defined in:
- lib/artoo/drivers/servo.rb
Overview
Servo behaviors for Firmata
Constant Summary collapse
- COMMANDS =
[:move, :min, :center, :max].freeze
Instance Attribute Summary collapse
-
#current_angle ⇒ Object
readonly
Returns the value of attribute current_angle.
Attributes inherited from Driver
Instance Method Summary collapse
-
#angle_to_span(angle) ⇒ Object
converts an angle to a span between 0-255.
-
#center ⇒ Object
Moves to center position.
-
#initialize(params = {}) ⇒ Servo
constructor
Create new Servo with angle=0.
-
#max ⇒ Object
Moves to max position.
-
#min ⇒ Object
Moves to min position.
-
#move(angle) ⇒ Object
Moves to specified angle.
-
#start_driver ⇒ Object
Starts connection to read and process and driver.
Methods inherited from Driver
#command, #commands, #connection, #event_topic_name, #interval, #known_command?, #method_missing, #pin
Constructor Details
#initialize(params = {}) ⇒ Servo
Create new Servo with angle=0
12 13 14 15 16 |
# File 'lib/artoo/drivers/servo.rb', line 12 def initialize(params={}) super @current_angle = 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_angle ⇒ Object (readonly)
Returns the value of attribute current_angle.
9 10 11 |
# File 'lib/artoo/drivers/servo.rb', line 9 def current_angle @current_angle end |
Instance Method Details
#angle_to_span(angle) ⇒ Object
converts an angle to a span between 0-255
54 55 56 |
# File 'lib/artoo/drivers/servo.rb', line 54 def angle_to_span(angle) (angle * 255 / 180).to_i end |
#center ⇒ Object
Moves to center position
43 44 45 |
# File 'lib/artoo/drivers/servo.rb', line 43 def center move(90) end |
#max ⇒ Object
Moves to max position
48 49 50 |
# File 'lib/artoo/drivers/servo.rb', line 48 def max move(180) end |
#min ⇒ Object
Moves to min position
38 39 40 |
# File 'lib/artoo/drivers/servo.rb', line 38 def min move(0) end |
#move(angle) ⇒ Object
Moves to specified angle
29 30 31 32 33 34 35 |
# File 'lib/artoo/drivers/servo.rb', line 29 def move(angle) raise "Servo angle must be an integer between 0-180" unless (angle.is_a?(Numeric) && angle >= 0 && angle <= 180) @current_angle = angle connection.set_pin_mode(pin, Firmata::Board::SERVO) connection.analog_write(pin, angle_to_span(angle)) end |
#start_driver ⇒ Object
Starts connection to read and process and driver
19 20 21 22 23 24 25 |
# File 'lib/artoo/drivers/servo.rb', line 19 def start_driver every(interval) do connection.read_and_process end super end |