# File lib/commands/motor.rb, line 45
  def start
    @nxt.reset_motor_position(NXTComm.const_get("MOTOR_#{@port.to_s.upcase}"))

    if @direction == :stop
      motor_power = 0
      mode        = NXTComm::COAST
      run_state   = NXTComm::MOTOR_RUN_STATE_IDLE
    else
      @direction == :forward ? motor_power = @power : motor_power = -@power
      mode        = NXTComm::MOTORON | NXTComm::BRAKE
      run_state   = NXTComm::MOTOR_RUN_STATE_RUNNING
    end

    turn_ratio = 0

    if @control_power
      mode |= NXTComm::REGULATED
      reg_mode = NXTComm::REGULATION_MODE_MOTOR_SPEED
    else
      reg_mode = NXTComm::REGULATION_MODE_IDLE
    end
    
    if @duration.class == Hash
      if @duration[:rotations]
        tacho_limit = @duration[:rotations] * 360
      end
    
      if @duration[:degrees]
        tacho_limit = @duration[:degrees]
      end
    
      if @duration[:seconds]
        tacho_limit = 0
      end
    else
      tacho_limit = 0
    end

    if @duration != :unlimited
      if @duration[:degrees] or @duration[:seconds]
        case @action
          when :constant
            run_state = NXTComm::MOTOR_RUN_STATE_RUNNING
          when :ramp_up
            run_state = NXTComm::MOTOR_RUN_STATE_RAMPUP
          when :ramp_down
            run_state = NXTComm::MOTOR_RUN_STATE_RAMPDOWN
        end
      end
    end

    @nxt.set_output_state(
      NXTComm.const_get("MOTOR_#{@port.to_s.upcase}"),
      motor_power,
      mode,
      reg_mode,
      turn_ratio,
      run_state,
      tacho_limit
    )
    
    if @duration != :unlimited or @duration[:seconds] or @wait
      if @duration[:seconds]
        sleep(@duration[:seconds])
      else
        until self.run_state == NXTComm::MOTOR_RUN_STATE_IDLE
          sleep(0.25)
        end
      end
      self.stop
    end
  end