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, #underscore

Constructor Details

#initialize(params = {}) ⇒ Robot

Create new robot

Parameters:

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

Options Hash (params):

  • :name (String)
  • :connections (Collection)
  • :devices (Collection)


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

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.



41
42
43
# File 'lib/artoo/robot.rb', line 41

def api_host
  @api_host
end

.api_portObject

Returns the value of attribute api_port.



41
42
43
# File 'lib/artoo/robot.rb', line 41

def api_port
  @api_port
end

.device_typesObject

Returns the value of attribute device_types.



41
42
43
# File 'lib/artoo/robot.rb', line 41

def device_types
  @device_types
end

.runningObject

Returns the value of attribute running.



41
42
43
# File 'lib/artoo/robot.rb', line 41

def running
  @running
end

.use_apiObject

Returns the value of attribute use_api.



41
42
43
# File 'lib/artoo/robot.rb', line 41

def use_api
  @use_api
end

.working_codeObject

Returns the value of attribute working_code.



41
42
43
# File 'lib/artoo/robot.rb', line 41

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'



83
84
85
86
87
88
# File 'lib/artoo/robot.rb', line 83

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 True if cli env.

Returns:

  • (Boolean)

    True if cli env



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

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

Examples:

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

Parameters:

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


52
53
54
55
# File 'lib/artoo/robot.rb', line 52

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

.connection_typesObject



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

def connection_types
  @@connection_types ||= []
end

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

Device that uses a connection to communicate

Examples:

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

Parameters:

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


61
62
63
64
65
# File 'lib/artoo/robot.rb', line 61

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 True if it's running.

Returns:

  • (Boolean)

    True if it's running



136
137
138
139
# File 'lib/artoo/robot.rb', line 136

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

.masterObject

Master actor



121
122
123
# File 'lib/artoo/robot.rb', line 121

def master
  Celluloid::Actor[:master]
end

.prepare_robots(robot = nil) ⇒ Object

Prepare master robots for work



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/artoo/robot.rb', line 108

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 True if test env.

Returns:

  • (Boolean)

    True if test env



126
127
128
# File 'lib/artoo/robot.rb', line 126

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

.work(&block) ⇒ Object

The work that needs to be performed

Examples:

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

Parameters:

  • work (block)


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

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

Parameters:

  • robot (Robot) (defaults to: nil)


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/artoo/robot.rb', line 95

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
    master.start_work
    self.running = true
    sleep # sleep main thread, and let the work commence!
  end
end

Instance Method Details

#api_hostString

Returns Api Host.

Returns:

  • (String)

    Api Host



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

def api_host
  self.class.api_host
end

#api_portString

Returns Api Port.

Returns:

  • (String)

    Api Port



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

def api_port
  self.class.api_port
end

#as_jsonJSON

Returns robot.

Returns:

  • (JSON)

    robot



213
214
215
# File 'lib/artoo/robot.rb', line 213

def as_json
  MultiJson.dump(to_hash)
end

#connection_typesCollection

Returns connection types.

Returns:

  • (Collection)

    connection types



188
189
190
# File 'lib/artoo/robot.rb', line 188

def connection_types
  current_class.connection_types
end

#continue_workObject

continue with the work



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

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

#default_connectionConnection

Returns default connection.

Returns:



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

def default_connection
  connections.values.first
end

#device_typesCollection

Returns device types.

Returns:

  • (Collection)

    device types



193
194
195
196
# File 'lib/artoo/robot.rb', line 193

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

#disconnectObject

Terminate all connections



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

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

#inspectString

Returns robot.

Returns:

  • (String)

    robot



218
219
220
# File 'lib/artoo/robot.rb', line 218

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

#pause_workObject

pause the work



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

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

#safe_nameString

Returns Name without spaces and downcased.

Returns:

  • (String)

    Name without spaces and downcased



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

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

#to_hashHash

Returns robot.

Returns:

  • (Hash)

    robot



204
205
206
207
208
209
210
# File 'lib/artoo/robot.rb', line 204

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



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

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_codeProc

Returns current working code.

Returns:

  • (Proc)

    current working code



199
200
201
# File 'lib/artoo/robot.rb', line 199

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