Class: Robolove::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/robolove/bot.rb

Constant Summary collapse

DEFAULTS =
{
  :brick => nil,
  :left_motor => :a,
  :right_motor => :b,
  :speed => 100
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Bot

Returns a new instance of Bot.



13
14
15
16
17
18
19
20
# File 'lib/robolove/bot.rb', line 13

def initialize(options = {})
  options = DEFAULTS.merge options

  @brick = options[:brick] || LegoNXT::LowLevel.connect
  @left_motor = options[:left_motor] || DEFAULTS[:left_motor]
  @right_motor = options[:right_motor] || DEFAULTS[:right_motor]
  @speed = options[:speed] || DEFAULTS[:speed]
end

Instance Attribute Details

#brickObject (readonly)

Returns the value of attribute brick.



11
12
13
# File 'lib/robolove/bot.rb', line 11

def brick
  @brick
end

#left_motorObject (readonly)

Returns the value of attribute left_motor.



11
12
13
# File 'lib/robolove/bot.rb', line 11

def left_motor
  @left_motor
end

#right_motorObject (readonly)

Returns the value of attribute right_motor.



11
12
13
# File 'lib/robolove/bot.rb', line 11

def right_motor
  @right_motor
end

#speedObject (readonly)

Returns the value of attribute speed.



11
12
13
# File 'lib/robolove/bot.rb', line 11

def speed
  @speed
end

Instance Method Details

#backward(duration = 1) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/robolove/bot.rb', line 30

def backward(duration = 1)
  self.brick.run_motor(self.left_motor, -self.speed)
  self.brick.run_motor(self.right_motor, -self.speed)
  sleep duration
  self.brick.stop_motor(self.left_motor)
  self.brick.stop_motor(self.right_motor)
end

#forward(duration = 1) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/robolove/bot.rb', line 22

def forward(duration = 1)
  self.brick.run_motor(self.left_motor, self.speed)
  self.brick.run_motor(self.right_motor, self.speed)
  sleep duration
  self.brick.stop_motor(self.left_motor)
  self.brick.stop_motor(self.right_motor)
end

#left(duration = 0.5) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/robolove/bot.rb', line 46

def left(duration = 0.5)
  self.brick.run_motor(self.right_motor, self.speed)
  self.brick.run_motor(self.left_motor, -self.speed)
  sleep duration
  self.brick.stop_motor(self.right_motor)
  self.brick.stop_motor(self.left_motor)
end

#right(duration = 0.5) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/robolove/bot.rb', line 38

def right(duration = 0.5)
  self.brick.run_motor(self.left_motor, self.speed)
  self.brick.run_motor(self.right_motor, -self.speed)
  sleep duration
  self.brick.stop_motor(self.left_motor)
  self.brick.stop_motor(self.right_motor)
end