Class: Denko::Motor::Servo

Inherits:
Object
  • Object
show all
Includes:
Behaviors::SinglePin, Behaviors::Threaded
Defined in:
lib/denko/motor/servo.rb

Instance Attribute Summary

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::SinglePin

#mode, #pin

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Threaded

#enable_interrupts, included, #stop, #stop_thread, #threaded, #threaded_loop

Methods included from Behaviors::Component

#initialize, #micro_delay

Methods included from Behaviors::State

#initialize, #state

Instance Method Details

#after_initialize(options = {}) ⇒ Object



7
8
9
10
11
12
# File 'lib/denko/motor/servo.rb', line 7

def after_initialize(options={})
  super(options)
  @min = options[:min] || 544
  @max = options[:max] || 2400
  attach
end

#attachObject



14
15
16
# File 'lib/denko/motor/servo.rb', line 14

def attach
  board.servo_toggle(pin, :on, min: @min, max: @max)
end

#detachObject



18
19
20
# File 'lib/denko/motor/servo.rb', line 18

def detach
  board.servo_toggle(pin, :off)
end

#position=(value) ⇒ Object Also known as: angle=



22
23
24
25
26
27
28
29
# File 'lib/denko/motor/servo.rb', line 22

def position=(value)
  value = value % 180 unless value == 180

  microseconds = ((value.to_f / 180) * (@max - @min)) + @min
  board.servo_write(pin, microseconds.round)

  @state = value
end

#speed=(value) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/denko/motor/servo.rb', line 31

def speed=(value)
  raise 'invalid speed value' if value > 100 || value < -100

  microseconds = (((value.to_f + 100) / 200) * (@max - @min)) + @min
  board.servo_write(pin, microseconds.round)

  @state = value
end

#write_microseconds(value) ⇒ Object



45
46
47
48
# File 'lib/denko/motor/servo.rb', line 45

def write_microseconds(value)
  raise 'invalid microsecond value' if value > @max || value < @min
  board.servo_write(pin, value)
end