Class: Denko::Motor::Servo
Instance Attribute Summary
#interrupts_enabled, #thread
#mode, #pin
#board
Instance Method Summary
collapse
#enable_interrupts, included, #stop, #stop_thread, #threaded, #threaded_loop
#initialize, #micro_delay
#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
|
#attach ⇒ Object
14
15
16
|
# File 'lib/denko/motor/servo.rb', line 14
def attach
board.servo_toggle(pin, :on, min: @min, max: @max)
end
|
#detach ⇒ Object
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
|