Class: BrickPi::Bot

Inherits:
Object
  • Object
show all
Includes:
Configuration
Defined in:
lib/brick_pi/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration

#color_sensor, #configure, #motor, #sensor, #touch_sensor, #ultrasonic_sensor

Constructor Details

#initializeBot

Returns a new instance of Bot.



12
13
14
15
16
17
# File 'lib/brick_pi/bot.rb', line 12

def initialize
  Native.BrickPiSetup()
  Native::Address[0] = 1
  Native::Address[1] = 2
  @queue = Queue.new
end

Instance Attribute Details

#motor_AObject

Returns the value of attribute motor_A.



9
10
11
# File 'lib/brick_pi/bot.rb', line 9

def motor_A
  @motor_A
end

#motor_BObject

Returns the value of attribute motor_B.



9
10
11
# File 'lib/brick_pi/bot.rb', line 9

def motor_B
  @motor_B
end

#motor_CObject

Returns the value of attribute motor_C.



9
10
11
# File 'lib/brick_pi/bot.rb', line 9

def motor_C
  @motor_C
end

#motor_DObject

Returns the value of attribute motor_D.



9
10
11
# File 'lib/brick_pi/bot.rb', line 9

def motor_D
  @motor_D
end

#sensor_1Object

Returns the value of attribute sensor_1.



10
11
12
# File 'lib/brick_pi/bot.rb', line 10

def sensor_1
  @sensor_1
end

#sensor_2Object

Returns the value of attribute sensor_2.



10
11
12
# File 'lib/brick_pi/bot.rb', line 10

def sensor_2
  @sensor_2
end

#sensor_3Object

Returns the value of attribute sensor_3.



10
11
12
# File 'lib/brick_pi/bot.rb', line 10

def sensor_3
  @sensor_3
end

#sensor_4Object

Returns the value of attribute sensor_4.



10
11
12
# File 'lib/brick_pi/bot.rb', line 10

def sensor_4
  @sensor_4
end

Instance Method Details

#schedule(&block) ⇒ Object



53
54
55
# File 'lib/brick_pi/bot.rb', line 53

def schedule(&block)
  @queue.push block
end

#startObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/brick_pi/bot.rb', line 19

def start
  @stop = false
  Native.ClearTick()
  Native.Timeout = 50
  Native.BrickPiSetTimeout()
  Thread.new do
    until @stop do
      Native.BrickPiUpdateValues()
      sleep 50/1000
    end
  end
  Thread.new do
    until @stop do
      code = @queue.pop
      begin
        code.call
      rescue => e
        $stderr.puts e.message
        $stderr.puts e.backtrace.join("\n")
      end
    end
  end
  Thread.new do
    yield
  end.join
  stop
end

#stopObject



47
48
49
50
51
# File 'lib/brick_pi/bot.rb', line 47

def stop
  schedule do
    @stop = true
  end
end