# File lib/motor.rb, line 141
  def run(options)
    debug(options, :run)
  
    if options[:power]
      power = options[:power].to_i.abs
    else
      power = 25
    end
    
    time = options[:time] || nil
    regulate = options[:regulate] || true
    regulation_mode = options[:regulation_mode] || "speed"
    degrees = options[:degrees] || 0
    ratio = options[:ratio] || self.ratio
    direction = options[:direction] || 1 # 1 is forward, -1 is backward

    brake_on_stop = options.has_key?(:time) || options.has_key?(:degrees)
    wait_until_complete = options.has_key?(:time) || options.has_key?(:degrees)
    
    brake_on_stop = options[:brake_on_stop] if options.has_key?(:brake_on_stop)

    wait_until_complete = options[:wait_until_complete] if options.has_key?(:wait_until_complete)
    # FIXME: wait_until_complete MUST be true if a time period is specified, otherwise we have no way of
    #                           enforcing the time limit (this is a problem with the way threading is implemented...)
    wait_until_complete = true if options.has_key?(:time)
    
    power = direction * power
    
    mode = NXTComm::MOTORON
    mode |= NXTComm::BRAKE if brake_on_stop
    mode |= NXTComm::REGULATED if regulate
    
    if regulate
      case regulation_mode
              when "idle"
                regulation_mode = NXTComm::REGULATION_MODE_IDLE
        when "speed"
            regulation_mode = NXTComm::REGULATION_MODE_MOTOR_SPEED
              when "sync"
                regulation_mode = NXTComm::REGULATION_MODE_MOTOR_SYNC
      end
    else
        regulation_mode = NXTComm::REGULATION_MODE_IDLE
    end

    @log.debug(:run) {"sending run command"}
    @nxt.set_output_state(@port, power, mode, regulation_mode, ratio, NXTComm::MOTOR_RUN_STATE_RUNNING, degrees)
  
    if time.nil?
        if wait_until_complete
              @log.debug(:run) {"sleeping until run_state is idle"}
              until read_state[:run_state] == NXTComm::MOTOR_RUN_STATE_IDLE
                sleep(POLL_INTERVAL)
                @log.debug(:run) {"checking run_state again"}
              end
              @log.debug(:run) {"run_state is idle"}
        end
    else
      @log.debug(:run) {"waiting #{time} seconds until stop"}
      sleep(time)
      @log.debug(:run) {"stopping"}
      self.stop
      @log.debug(:run) {"stopped"}
    end
  end