Class: Artoo::Drivers::Servo
- Defined in:
- lib/artoo/drivers/servo.rb
Overview
Servo behaviors for Firmata
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
-
#initialize(params = {}) ⇒ Servo
constructor
A new instance of Servo.
- #max ⇒ Object
- #min ⇒ Object
- #move(angle) ⇒ Object
- #start_driver ⇒ Object
Methods inherited from Driver
#connection, #event_topic_name, #interval, #method_missing, #pin
Methods included from Celluloid
Constructor Details
#initialize(params = {}) ⇒ Servo
9 10 11 12 13 |
# File 'lib/artoo/drivers/servo.rb', line 9 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.
7 8 9 |
# File 'lib/artoo/drivers/servo.rb', line 7 def current_angle @current_angle end |
Instance Method Details
#angle_to_span(angle) ⇒ Object
converts an angle to a span between 0-255
44 45 46 |
# File 'lib/artoo/drivers/servo.rb', line 44 def angle_to_span(angle) (angle * 255 / 180).to_i end |
#center ⇒ Object
35 36 37 |
# File 'lib/artoo/drivers/servo.rb', line 35 def center move(90) end |
#max ⇒ Object
39 40 41 |
# File 'lib/artoo/drivers/servo.rb', line 39 def max move(180) end |
#min ⇒ Object
31 32 33 |
# File 'lib/artoo/drivers/servo.rb', line 31 def min move(0) end |
#move(angle) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/artoo/drivers/servo.rb', line 23 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
15 16 17 18 19 20 21 |
# File 'lib/artoo/drivers/servo.rb', line 15 def start_driver every(interval) do connection.read_and_process end super end |