Class: Artoo::Robot

Inherits:
Object
  • Object
show all
Extended by:
Basic
Includes:
Events, Utility, Celluloid, Celluloid::Notifications
Defined in:
lib/artoo/robot.rb

Overview

The most important class used by Artoo is Robot. This represents the primary interface for interacting with a collection of physical computing capabilities.

Direct Known Subclasses

MainRobot

Constant Summary

Constants included from Basic

Basic::CALLERS_TO_IGNORE

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Basic

caller_files, set

Methods included from Events

#create_proxy_method, #on, #proxy_method_name

Methods included from Utility

#classify, #constantize, #current_class, #current_instance, #random_string

Methods included from Celluloid

#timers

Constructor Details

#initialize(params = {}) ⇒ Robot

Returns a new instance of Robot.



29
30
31
32
33
# File 'lib/artoo/robot.rb', line 29

def initialize(params={})
  @name = params[:name] || "Robot #{random_string}"
  initialize_connections(params[:connections] || {})
  initialize_devices(params[:devices] || {})
end

Class Attribute Details

.api_hostObject

Returns the value of attribute api_host.



36
37
38
# File 'lib/artoo/robot.rb', line 36

def api_host
  @api_host
end

.api_portObject

Returns the value of attribute api_port.



36
37
38
# File 'lib/artoo/robot.rb', line 36

def api_port
  @api_port
end

.device_typesObject

Returns the value of attribute device_types.



36
37
38
# File 'lib/artoo/robot.rb', line 36

def device_types
  @device_types
end

.runningObject

Returns the value of attribute running.



36
37
38
# File 'lib/artoo/robot.rb', line 36

def running
  @running
end

.use_apiObject

Returns the value of attribute use_api.



36
37
38
# File 'lib/artoo/robot.rb', line 36

def use_api
  @use_api
end

.working_codeObject

Returns the value of attribute working_code.



36
37
38
# File 'lib/artoo/robot.rb', line 36

def working_code
  @working_code
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



27
28
29
# File 'lib/artoo/robot.rb', line 27

def connections
  @connections
end

#devicesObject (readonly)

Returns the value of attribute devices.



27
28
29
# File 'lib/artoo/robot.rb', line 27

def devices
  @devices
end

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/artoo/robot.rb', line 27

def name
  @name
end

Class Method Details

.api(params = {}) ⇒ Object

activate RESTful api Example:

api :host => '127.0.0.1', :port => '1234'


75
76
77
78
79
80
# File 'lib/artoo/robot.rb', line 75

def api(params = {})
  Celluloid::Logger.info "Registering api..."
  self.use_api = true
  self.api_host = params[:host] || '127.0.0.1'
  self.api_port = params[:port] || '4321'
end

.cli?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/artoo/robot.rb', line 114

def cli?
  ENV["ARTOO_CLI"] == 'true'
end

.connection(name, params = {}) ⇒ Object

connection to some hardware that has one or more devices via some specific protocol Example:

connection :arduino, :adaptor => :firmata, :port => '/dev/tty.usbmodemxxxxx'


46
47
48
49
# File 'lib/artoo/robot.rb', line 46

def connection(name, params = {})
  Celluloid::Logger.info "Registering connection '#{name}'..."
  self.connection_types << {:name => name}.merge(params)
end

.connection_typesObject



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

def connection_types
  @@connection_types ||= []
end

.device(name, params = {}) ⇒ Object

device that uses a connection to communicate Example:

device :collision_detect, :driver => :switch, :pin => 3


54
55
56
57
58
# File 'lib/artoo/robot.rb', line 54

def device(name, params = {})
  Celluloid::Logger.info "Registering device '#{name}'..."
  self.device_types ||= []
  self.device_types << {:name => name}.merge(params)
end

.is_running?Boolean

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/artoo/robot.rb', line 118

def is_running?
  self.running ||= false
  self.running == true
end

.prepare_robots(robot = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/artoo/robot.rb', line 98

def prepare_robots(robot=nil)
  if robot.respond_to?(:work)
    robots = [robot]
  elsif robot.kind_of?(Array)
    robots = robot
  else
    robots = [self.new]
  end

  Celluloid::Actor[:master] = Master.new(robots)
end

.test?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/artoo/robot.rb', line 110

def test?
  ENV["ARTOO_TEST"] == 'true'
end

.work(&block) ⇒ Object

the work that needs to be performed Example:

work do 
  every(10.seconds) do
    puts "hello, world"
  end
end


67
68
69
70
# File 'lib/artoo/robot.rb', line 67

def work(&block)
  Celluloid::Logger.info "Preparing work..."
  self.working_code = block if block_given?
end

.work!(robot = nil) ⇒ Object

work can be performed by either:

an existing instance
an array of existing instances
or, a new instance can be created


86
87
88
89
90
91
92
93
94
95
96
# File 'lib/artoo/robot.rb', line 86

def work!(robot=nil)
  return if !test? && is_running?
  prepare_robots(robot)

  unless cli?
    Celluloid::Actor[:api] = Api.new(self.api_host, self.api_port) if self.use_api
    Celluloid::Actor[:master].start_work
    self.running = true
    sleep # sleep main thread, and let the work commence!
  end
end

Instance Method Details

#api_hostObject



128
129
130
# File 'lib/artoo/robot.rb', line 128

def api_host
  self.class.api_host
end

#api_portObject



132
133
134
# File 'lib/artoo/robot.rb', line 132

def api_port
  self.class.api_port
end

#as_jsonObject



182
183
184
# File 'lib/artoo/robot.rb', line 182

def as_json
  MultiJson.dump(to_hash)
end

#connection_typesObject



162
163
164
# File 'lib/artoo/robot.rb', line 162

def connection_types
  current_class.connection_types
end

#continue_workObject



149
150
151
152
# File 'lib/artoo/robot.rb', line 149

def continue_work
  Logger.info "Continuing work..."
  current_instance.timers.continue
end

#default_connectionObject



158
159
160
# File 'lib/artoo/robot.rb', line 158

def default_connection
  connections.values.first
end

#device_typesObject



166
167
168
169
# File 'lib/artoo/robot.rb', line 166

def device_types
  current_class.device_types ||= []
  current_class.device_types
end

#disconnectObject



154
155
156
# File 'lib/artoo/robot.rb', line 154

def disconnect
  connections.each {|k, c| c.async.disconnect}
end

#inspectObject



186
187
188
# File 'lib/artoo/robot.rb', line 186

def inspect
  "#<Robot #{object_id}>"
end

#pause_workObject



144
145
146
147
# File 'lib/artoo/robot.rb', line 144

def pause_work
  Logger.info "Pausing work..."
  current_instance.timers.pause
end

#safe_nameObject



124
125
126
# File 'lib/artoo/robot.rb', line 124

def safe_name
  name.gsub(' ', '_').downcase
end

#to_hashObject



175
176
177
178
179
180
# File 'lib/artoo/robot.rb', line 175

def to_hash
  {:name => name,
   :connections => connections.each_value.collect {|c|c.to_hash},
   :devices => devices.each_value.collect {|d|d.to_hash}
  }
end

#workObject

start doing the work



137
138
139
140
141
142
# File 'lib/artoo/robot.rb', line 137

def work
  Logger.info "Starting work..."
  execute_startup(connections) {|c| c.future.connect}
  execute_startup(devices) {|d| d.future.start_device}
  execute_working_code
end

#working_codeObject



171
172
173
# File 'lib/artoo/robot.rb', line 171

def working_code
  current_class.working_code ||= proc {puts "No work defined."}
end