Class: WallE::Servo

Inherits:
Object
  • Object
show all
Defined in:
lib/wall_e/components/servo.rb

Constant Summary collapse

OutOfBoundsError =
Class.new(StandardError)

Instance Method Summary collapse

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, options = {})
  @pin = pin
  @pin.set_mode(Pin::SERVO)
  @range = options.fetch(:range) { 0..180 }
end

Instance Method Details

#centerObject

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

#maxObject

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

Returns:

  • (Boolean)


48
49
50
# File 'lib/wall_e/components/servo.rb', line 48

def maxed?
  position == @range.max
end

#minObject

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.

Raises:



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

#positionObject



52
53
54
# File 'lib/wall_e/components/servo.rb', line 52

def position
  @pin.value
end