Class: Artoo::Drivers::Servo

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

Overview

Servo behaviors for Firmata

Constant Summary collapse

COMMANDS =
[:move, :min, :center, :max].freeze

Instance Attribute Summary collapse

Attributes inherited from Driver

#parent

Instance Method Summary collapse

Methods inherited from Driver

#command, #commands, #connection, #event_topic_name, #interval, #known_command?, #method_missing, #pin

Constructor Details

#initialize(params = {}) ⇒ Servo

Create new Servo with angle=0



12
13
14
15
16
# File 'lib/artoo/drivers/servo.rb', line 12

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.



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

def current_angle
  @current_angle
end

Instance Method Details

#angle_to_span(angle) ⇒ Object

converts an angle to a span between 0-255

Parameters:



54
55
56
# File 'lib/artoo/drivers/servo.rb', line 54

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

#centerObject

Moves to center position



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

def center
  move(90)
end

#maxObject

Moves to max position



48
49
50
# File 'lib/artoo/drivers/servo.rb', line 48

def max
  move(180)
end

#minObject

Moves to min position



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

def min
  move(0)
end

#move(angle) ⇒ Object

Moves to specified angle

Parameters:

  • angle (Integer)

    must be between 0-180



29
30
31
32
33
34
35
# File 'lib/artoo/drivers/servo.rb', line 29

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

Starts connection to read and process and driver



19
20
21
22
23
24
25
# File 'lib/artoo/drivers/servo.rb', line 19

def start_driver
  every(interval) do
    connection.read_and_process
  end

  super
end