Class: Artoo::Drivers::Servo

Inherits:
Driver
  • Object
show all
Defined in:
lib/artoo/drivers/servo.rb

Overview

Servo behaviors for Firmata

Instance Attribute Summary collapse

Attributes inherited from Driver

#parent

Instance Method Summary collapse

Methods inherited from Driver

#connection, #event_topic_name, #interval, #method_missing, #pin

Methods included from Celluloid

#timers

Constructor Details

#initialize(params = {}) ⇒ Servo



9
10
11
12
13
# File 'lib/artoo/drivers/servo.rb', line 9

def initialize(params={})
  super

  @current_angle = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Artoo::Drivers::Driver

Instance Attribute Details

#current_angleObject (readonly)

Returns the value of attribute current_angle.



7
8
9
# File 'lib/artoo/drivers/servo.rb', line 7

def current_angle
  @current_angle
end

Instance Method Details

#angle_to_span(angle) ⇒ Object

converts an angle to a span between 0-255



44
45
46
# File 'lib/artoo/drivers/servo.rb', line 44

def angle_to_span(angle)
  (angle * 255 / 180).to_i
end

#centerObject



35
36
37
# File 'lib/artoo/drivers/servo.rb', line 35

def center
  move(90)
end

#maxObject



39
40
41
# File 'lib/artoo/drivers/servo.rb', line 39

def max
  move(180)
end

#minObject



31
32
33
# File 'lib/artoo/drivers/servo.rb', line 31

def min
  move(0)
end

#move(angle) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/artoo/drivers/servo.rb', line 23

def move(angle)
  raise "Servo angle must be an integer between 0-180" unless (angle.is_a?(Numeric) && angle >= 0 && angle <= 180)
  
  @current_angle = angle
  connection.set_pin_mode(pin, Firmata::Board::SERVO)
  connection.analog_write(pin, angle_to_span(angle))
end

#start_driverObject



15
16
17
18
19
20
21
# File 'lib/artoo/drivers/servo.rb', line 15

def start_driver
  every(interval) do
    connection.read_and_process
  end

  super
end