class BirdbrainFinch

Constants

BACKWARD
FORWARD
LEFT
MOVE_START_WAIT_SECONDS
MOVE_TIMEOUT_SECONDS
VALID_LED_PORTS
VALID_MOVE_DIRECTION
VALID_SENSOR_PORTS
VALID_SERVO_PORTS
VALID_TAIL_PORTS
VALID_TRILED_PORTS
VALID_TURN_DIRECTION

Attributes

move_start_time[RW]
move_start_wait_seconds[RW]
move_timeout_seconds[RW]

Public Class Methods

new(device = DEFAULT_DEVICE) click to toggle source
Calls superclass method BirdbrainDevice::new
# File lib/birdbrain/birdbrain_finch.rb, line 23
def initialize(device = DEFAULT_DEVICE)
  super
  self.move_start_wait_seconds = MOVE_START_WAIT_SECONDS # seconds to allow finch to start moving
  self.move_timeout_seconds = MOVE_TIMEOUT_SECONDS # maximum number of seconds to wait for finch moving
  self.move_start_time = 0 # after move records how long it took the startup to complete for tuning
end

Public Instance Methods

accelerometer() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 54
def accelerometer
  BirdbrainFinchInput.accelerometer(device) if connected?
end
beak(r_intensity, g_intensity, b_intensity) click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 94
def beak(r_intensity, g_intensity, b_intensity)
  BirdbrainHummingbirdOutput.tri_led(device, 1, r_intensity, g_intensity, b_intensity) if connected?
end
compass() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 58
def compass
  BirdbrainFinchInput.compass(device) if connected?
end
distance() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 42
def distance
  BirdbrainFinchInput.distance(device) if connected?
end
encoder(direction) click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 50
def encoder(direction)
  BirdbrainFinchInput.encoder(device, direction) if connected?
end
light(direction) click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 38
def light(direction)
  BirdbrainFinchInput.light(device, direction) if connected?
end
line(direction) click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 46
def line(direction)
  BirdbrainFinchInput.line(device, direction) if connected?
end
magnetometer() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 62
def magnetometer
  BirdbrainFinchInput.magnetometer(device) if connected?
end
motors(left_speed, right_speed) click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 161
def motors(left_speed, right_speed)
  BirdbrainFinchOutput.motors(device, left_speed, right_speed) if connected?
end
move(direction, distance, speed, wait_to_finish_movement = true) click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 137
def move(direction, distance, speed, wait_to_finish_movement = true)
  return nil unless connected_and_valid?(direction, VALID_MOVE_DIRECTION)

  unless (response = BirdbrainFinchOutput.move(device, direction, distance, speed))
    return response
  end

  wait_until_movement_and_wait if wait_to_finish_movement

  true
end
moving?() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 34
def moving?
  BirdbrainFinchInput.moving?(device) if connected?
end
orientation() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 66
def orientation
  BirdbrainFinchInput.orientation(device) if connected?
end
orientation_beak_down?() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 74
def orientation_beak_down?
  BirdbrainFinchInput.orientation_beak_down?(device) if connected?
end
orientation_beak_up?() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 70
def orientation_beak_up?
  BirdbrainFinchInput.orientation_beak_up?(device) if connected?
end
orientation_level?() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 86
def orientation_level?
  BirdbrainFinchInput.orientation_level?(device) if connected?
end
orientation_tilt_left?() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 78
def orientation_tilt_left?
  BirdbrainFinchInput.orientation_tilt_left?(device) if connected?
end
orientation_tilt_right?() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 82
def orientation_tilt_right?
  BirdbrainFinchInput.orientation_tilt_right?(device) if connected?
end
orientation_upside_down?() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 90
def orientation_upside_down?
  BirdbrainFinchInput.orientation_upside_down?(device) if connected?
end
play_note(note, beats) click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 108
def play_note(note, beats)
  BirdbrainHummingbirdOutput.play_note(device, note, beats) if connected?
end
reset_encoders() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 169
def reset_encoders
  BirdbrainFinchOutput.reset_encoders(device) if connected?
end
stop() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 165
def stop
  BirdbrainFinchOutput.stop(device) if connected?
end
tail(port, r_intensity, g_intensity, b_intensity) click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 98
def tail(port, r_intensity, g_intensity, b_intensity)
  return nil unless connected_and_valid?(port, VALID_TAIL_PORTS)

  if port.to_s == 'all'
    (2..5).each { |each_port| BirdbrainHummingbirdOutput.tri_led(device, each_port, r_intensity, g_intensity, b_intensity) }
  else
    BirdbrainHummingbirdOutput.tri_led(device, port + 1, r_intensity, g_intensity, b_intensity)
  end
end
turn(direction, angle, speed, wait_to_finish_movement = true) click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 149
def turn(direction, angle, speed, wait_to_finish_movement = true)
  return nil unless connected_and_valid?(direction, VALID_TURN_DIRECTION)

  unless (response = BirdbrainFinchOutput.turn(device, direction, angle, speed))
    return response
  end

  wait_until_movement_and_wait if wait_to_finish_movement

  true
end
valid_device_type?() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 30
def valid_device_type?
  finch?
end
wait() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 122
def wait
  start_time = Time.now

  sleep(0.01) while moving? && ((Time.now - start_time) < move_timeout_seconds)

  true
end
wait_until_movement() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 112
def wait_until_movement
  start_time = Time.now

  sleep(0.01) while !moving? && ((Time.now - start_time) < move_start_wait_seconds) # short wait for finch to start moving

  self.move_start_time = Time.now - start_time # close to amount of time it took the finch to startup for tuning

  true
end
wait_until_movement_and_wait() click to toggle source
# File lib/birdbrain/birdbrain_finch.rb, line 130
def wait_until_movement_and_wait
  wait_until_movement
  wait

  true
end