Module: Rumba::Dsl

Included in:
Rumba
Defined in:
lib/rumba/dsl.rb

Constant Summary collapse

DEFAULT_SPEED =

Remember, Roomba speeds are defined in mm/s (max is 200)

200
RADIUS =

Radius of an average Roomba, used for calculating rotation

165.1

Instance Method Summary collapse

Instance Method Details

#backward(distance, speed: DEFAULT_SPEED) ⇒ Object Also known as: backwards

distance is in mm!



31
32
33
# File 'lib/rumba/dsl.rb', line 31

def backward(distance, speed: DEFAULT_SPEED)
  straight_distance(distance, speed: -speed)
end

#forward(distance, speed: DEFAULT_SPEED) ⇒ Object Also known as: forwards

distance is in mm!



26
27
28
# File 'lib/rumba/dsl.rb', line 26

def forward(distance, speed: DEFAULT_SPEED)
  straight_distance(distance, speed: speed)
end

#rotate(direction, speed: DEFAULT_SPEED) ⇒ Object Also known as: turn

Direction can either be a Fixnum for number of degrees, or a symbol for the direction (:left, :right)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rumba/dsl.rb', line 37

def rotate(direction, speed: DEFAULT_SPEED)
  # handle symbols...
  # note that counter-clockwise is positive
  case direction
    when :left
      direction = 90
    when :right
      direction = -90
  end

  direction > 0 ? spin_right(speed) : spin_left(speed)

  total = 0
  goal  = direction.abs / 2
  loop do
    raw_angle = get_sensor(:angle)

    # taken from the official docs to convert output to degrees...
    degrees = (360 * raw_angle)/(258 * Math::PI)
    total += degrees.abs
    break if total >= goal
  end

  halt
end

#straight_distance(distance, speed: DEFAULT_SPEED) ⇒ Object

move both wheels at the same speed in a certain direction! NOTE THAT THIS BLOCKS UNTIL COMPLETE



14
15
16
17
18
19
20
21
22
23
# File 'lib/rumba/dsl.rb', line 14

def straight_distance(distance, speed: DEFAULT_SPEED)
  total = 0
  straight(speed)
  loop do
    total += get_sensor(:distance).abs
    break if total >= distance
  end

  halt
end