Class: WallE::Servo
- Inherits:
-
Object
- Object
- WallE::Servo
- Defined in:
- lib/wall_e/components/servo.rb
Constant Summary collapse
- OutOfBoundsError =
Class.new(StandardError)
Instance Method Summary collapse
-
#center ⇒ Object
Public: Move the servo to the center degree.
-
#initialize(pin, options = {}) ⇒ Servo
constructor
Public: Initialize a Servo.
-
#max ⇒ Object
Public: Move the servo to the maximum degree.
- #maxed? ⇒ Boolean
-
#min ⇒ Object
Public: Move the servo the the minimum degree.
-
#move_to(degrees) ⇒ Object
Public: Move the servo.
- #position ⇒ Object
Constructor Details
#initialize(pin, options = {}) ⇒ Servo
Public: Initialize a Servo
pin - the Pin the servo is attached to. options - the Hash options to configure the servo (default: {}):
:range - the Range to restrict movement (default 0..180).
10 11 12 13 14 |
# File 'lib/wall_e/components/servo.rb', line 10 def initialize(pin, = {}) @pin = pin @pin.set_mode(Pin::SERVO) @range = .fetch(:range) { 0..180 } end |
Instance Method Details
#center ⇒ Object
Public: Move the servo to the center degree.
Returns nothing.
33 34 35 |
# File 'lib/wall_e/components/servo.rb', line 33 def center move_to ((@range.min + @range.max) / 2).abs end |
#max ⇒ Object
Public: Move the servo to the maximum degree.
Returns nothing.
26 27 28 |
# File 'lib/wall_e/components/servo.rb', line 26 def max move_to @range.max end |
#maxed? ⇒ Boolean
48 49 50 |
# File 'lib/wall_e/components/servo.rb', line 48 def maxed? position == @range.max end |
#min ⇒ Object
Public: Move the servo the the minimum degree.
Returns nothing.
19 20 21 |
# File 'lib/wall_e/components/servo.rb', line 19 def min move_to @range.min end |
#move_to(degrees) ⇒ Object
Public: Move the servo.
degrees - the Integer degrees to move to.
Returns nothing. Raises OutOfBoundsError if the servo cannot move to the degree.
43 44 45 46 |
# File 'lib/wall_e/components/servo.rb', line 43 def move_to(degrees) raise OutOfBoundsError unless @range.include?(degrees) @pin.servo_write(degrees) end |
#position ⇒ Object
52 53 54 |
# File 'lib/wall_e/components/servo.rb', line 52 def position @pin.value end |