Sha256: f37a1a13c457ad1720e0c890cd61addad5c626d8824c37ed4ed1a428bcc91ee3
Contents?: true
Size: 1.18 KB
Versions: 7
Compression:
Stored size: 1.18 KB
Contents
module Denko module Motor class Servo include Behaviors::SinglePin include Behaviors::Threaded def after_initialize(options={}) super(options) @min = options[:min] || 544 @max = options[:max] || 2400 attach end def attach board.servo_toggle(pin, :on, min: @min, max: @max) end def detach board.servo_toggle(pin, :off) end 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 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 alias :angle= :position= alias :angle :state alias :position :state alias :speed :state def write_microseconds(value) raise 'invalid microsecond value' if value > @max || value < @min board.servo_write(pin, value) end end end end
Version data entries
7 entries across 7 versions & 1 rubygems