Class: Artoo::Drivers::Sphero

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

Overview

The Sphero driver behaviors

Constant Summary collapse

RED =
[255, 0,   0]
GREEN =
[0,   255, 0]
YELLOW =
[255, 255, 0]
BLUE =
[0,   0,   255]
WHITE =
[255, 255, 255]
COMMANDS =
[:ping, :version, :bluetooth_info, :auto_reconnect,
:disable_auto_reconnect, :power_state, :sphero_sleep, :roll,
:stop, :heading, :stabilization, :color, :rgb, :set_color,
:back_led_output, :rotation_rate, :set_power_notification,
:set_data_streaming, :detect_collisions,
:handle_message_events, :get_rgb, :start_calibration,
:finish_calibration].freeze

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Sphero

Returns a new instance of Sphero.



22
23
24
25
# File 'lib/artoo/drivers/sphero.rb', line 22

def initialize(params={})
  super
  require_interface(:sphero)
end

Instance Method Details

#color(*colors) ⇒ Object

Public: You can pass a color as a symbol or an array containing RGB colors from 0 to 255 ([255, 0, 0] == red), if passed a symbol returns the array of RGB corresponding to the color, if passed an array of colors it returns the same array (used when setting colors to the sphero).

:color_symbol - color red - params green - params blue -params

Returns true | nil.



105
106
107
108
109
110
111
112
113
114
# File 'lib/artoo/drivers/sphero.rb', line 105

def color(*colors)
  case colors.first
  when :red    then RED
  when :green  then GREEN
  when :yellow then YELLOW
  when :blue   then BLUE
  when :white  then WHITE
  else colors
  end
end

#detect_collisions(params = {}) ⇒ Object

Public: Sets the sphero to detect collisions and report them.

Returns true | nil.



72
73
74
# File 'lib/artoo/drivers/sphero.rb', line 72

def detect_collisions(params={})
  connection.configure_collision_detection 0x01, 0x20, 0x20, 0x20, 0x20, 0x50
end

#finish_calibrationObject

Finishes calibration process by setting the new heading, turning off back LED, and turning back on auto-stabilization



136
137
138
139
140
# File 'lib/artoo/drivers/sphero.rb', line 136

def finish_calibration
  connection.heading = 0
  connection.back_led_output = 0
  connection.stabilization = true
end

#get_rgbObject

Public: You can retrieve current sphero color

Returns array of rgb values



121
122
123
124
125
# File 'lib/artoo/drivers/sphero.rb', line 121

def get_rgb
  rgb = connection.user_led

  [rgb.r, rgb.g, rgb.b] if rgb
end

#handle_message_eventsObject

Public: Handles different message events.

Returns sphero_event.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/artoo/drivers/sphero.rb', line 52

def handle_message_events
  while not connection.messages.empty? do
    evt = connection.messages.pop
    case 
    when evt.is_a?(::Sphero::Response::CollisionDetected)
      handle_collision_detected(evt)
    when evt.is_a?(::Sphero::Response::PowerNotification)
      handle_power_notification(evt)
    when evt.is_a?(::Sphero::Response::SensorData)
      handle_sensor_data(evt)
    end
  end
end

#set_color(*colors) ⇒ Object

Public: You can either use it in tandem with color to set the color of the sphero or just pass an array containing RGB colors on the range 0 to 255 ([255, 0, 0] == red).

red - params green - params blue -params

Returns true | nil.



88
89
90
# File 'lib/artoo/drivers/sphero.rb', line 88

def set_color(*colors)
  connection.rgb(*color(*colors), true)
end

#start_calibrationObject

Starts calibration process by turning on back LED and turning off auto-stabilization



129
130
131
132
# File 'lib/artoo/drivers/sphero.rb', line 129

def start_calibration
  connection.back_led_output = 127
  connection.stabilization = false
end

#start_driverObject

Public: Starts the driver.

Returns null.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/artoo/drivers/sphero.rb', line 32

def start_driver
  begin
    detect_collisions
    enable_stop_on_disconnect

    every(interval) do
      handle_message_events
    end

    super
  rescue Exception => e
    Logger.error "Error starting Sphero driver!"
    Logger.error e.message
    Logger.error e.backtrace.inspect
  end
end