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 =
[:roll, :stop, :detect_collisions, :messages, :set_color, :color].freeze

Instance Method Summary collapse

Instance Method Details

#color(*colors) ⇒ Object

Retrieves color

Parameters:

  • colors (Collection)


61
62
63
64
65
66
67
68
69
70
# File 'lib/artoo/drivers/sphero.rb', line 61

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

Detects collisions

Parameters:

  • params (Hash) (defaults to: {})


49
50
51
# File 'lib/artoo/drivers/sphero.rb', line 49

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

#handle_message_eventsObject



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

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

Set color

Parameters:

  • colors (Collection)


55
56
57
# File 'lib/artoo/drivers/sphero.rb', line 55

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

#start_driverObject

Starts drives and required connections



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/artoo/drivers/sphero.rb', line 17

def start_driver
  begin
    detect_collisions

    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