Class: Artoo::Drivers::Servo
- Inherits:
-
Driver
- Object
- Driver
- Artoo::Drivers::Servo
- Defined in:
- lib/artoo/drivers/servo.rb
Overview
Servo behaviors for Firmata
Constant Summary collapse
- COMMANDS =
[:move, :min, :center, :max, :current_angle].freeze
Instance Attribute Summary collapse
-
#current_angle ⇒ Object
readonly
Returns the value of attribute current_angle.
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.
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 |
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
44 45 46 |
# File 'lib/artoo/drivers/servo.rb', line 44 def angle_to_span(angle) (angle * 255 / 180).to_i end |
#center ⇒ Object
Moves to center position
33 34 35 |
# File 'lib/artoo/drivers/servo.rb', line 33 def center move(90) end |
#max ⇒ Object
Moves to max position
38 39 40 |
# File 'lib/artoo/drivers/servo.rb', line 38 def max move(180) end |
#min ⇒ Object
Moves to min position
28 29 30 |
# File 'lib/artoo/drivers/servo.rb', line 28 def min move(0) end |
#move(angle) ⇒ Object
Moves to specified angle
20 21 22 23 24 25 |
# File 'lib/artoo/drivers/servo.rb', line 20 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.servo_write(pin, angle_to_span(angle)) end |