Class: Robolove::Bot
- Inherits:
-
Object
- Object
- Robolove::Bot
- Defined in:
- lib/robolove/bot.rb
Constant Summary collapse
- DEFAULTS =
{ :brick => nil, :left_motor => :a, :right_motor => :b, :speed => 100 }
Instance Attribute Summary collapse
-
#brick ⇒ Object
readonly
Returns the value of attribute brick.
-
#left_motor ⇒ Object
readonly
Returns the value of attribute left_motor.
-
#right_motor ⇒ Object
readonly
Returns the value of attribute right_motor.
-
#speed ⇒ Object
readonly
Returns the value of attribute speed.
Instance Method Summary collapse
- #backward(duration = 1) ⇒ Object
- #forward(duration = 1) ⇒ Object
-
#initialize(options = {}) ⇒ Bot
constructor
A new instance of Bot.
- #left(duration = 0.5) ⇒ Object
- #right(duration = 0.5) ⇒ Object
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( = {}) = DEFAULTS.merge @brick = [:brick] || LegoNXT::LowLevel.connect @left_motor = [:left_motor] || DEFAULTS[:left_motor] @right_motor = [:right_motor] || DEFAULTS[:right_motor] @speed = [:speed] || DEFAULTS[:speed] end |
Instance Attribute Details
#brick ⇒ Object (readonly)
Returns the value of attribute brick.
11 12 13 |
# File 'lib/robolove/bot.rb', line 11 def brick @brick end |
#left_motor ⇒ Object (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_motor ⇒ Object (readonly)
Returns the value of attribute right_motor.
11 12 13 |
# File 'lib/robolove/bot.rb', line 11 def right_motor @right_motor end |
#speed ⇒ Object (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 |