.. gallery-section: general Experiments example 6: Current Designs dial (fORP 905) ------------------------------------------------------ The MEG Lab has a fiber-optic rotary dial (Current Designs ``HHSC-1x1-DIAL``) that participants can turn left or right inside the MSR, for rating scales, continuous tracking or adjustment tasks. It reaches the stimulus PC through the Current Designs ``FIU-905`` interface, which plugs in by USB. The hardware, ports and cabling are described in :ref:`the KIT system specification `. This page is about using the dial from Psychtoolbox. Scripts can be found under :github-file:`experiments/psychtoolbox/dial_test/`. How the dial arrives in MATLAB ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The 905 behaves like a USB keyboard. It decodes the dial itself and types one key per step: .. list-table:: :header-rows: 1 :widths: 30 70 * - Dial movement - What the 905 sends * - one step clockwise - a short tap (about 1 ms) of the key ``t`` * - one step counter-clockwise - a short tap of the key ``b`` * - at rest - usually nothing; sometimes alternating ``t b t b ...`` taps (see *Rest chatter* below) Nominally one step is 1 degree, i.e. 360 steps per turn. Check it once with ``dial_dot_demo`` (below) and pass the measured number to ``dial_open`` if it differs. Because every step is a key tap, you read the dial with a Psychtoolbox **keyboard queue** (``KbQueueCreate`` / ``KbEventGet``), which collects taps in the background so none are lost while a frame is drawn. The position is simply the number of ``t`` taps minus the number of ``b`` taps. Do not use ``KbCheck``: it only sees keys that are down at the moment you call it, and each tap lasts about 1 ms. .. warning:: The 905 **sets its optical gain at the moment its USB cable is plugged in**. If the dial is not moving at that moment, the gain is wrong for a rotary device and the 905 mis-decodes the direction (about as many ``b`` as ``t`` in one turn). Always: 1. connect the dial (and the fiber bundle, if used) to the 905 first, 2. have someone turn the dial continuously, 3. plug in the 905 USB cable while the dial is turning, keep turning for a few seconds, 4. run ``dial_dot_demo`` to confirm that one turn moves the dot once round. This has to be repeated whenever the 905 loses USB power (cable unplugged, PC cold boot), not after every MATLAB session. Rest chatter ^^^^^^^^^^^^ When the dial comes to rest exactly on an encoder edge, the 905 can keep sending alternating ``t b t b ...`` taps at more than 100 per second, and the TRIGGER and B1 LEDs on its front panel blink continuously. The taps cancel out in the ``t - b`` count, so the measured position does not drift. Two practical consequences: - while an experiment is not running, the chatter is typed into whichever window has focus. Unplug the 905 USB when it is not in use (and redo the gain procedure when plugging it back in), or nudge the dial by a degree; - ``dial_poll`` may report a ``delta`` of +1 or -1 on consecutive frames while the dial is at rest. If a trial should end "when the dial stops moving", use a tolerance of a few steps rather than exactly zero. Test scripts ^^^^^^^^^^^^ ``dial_dot_demo``: a dot on a circle follows the dial. Use it to check the cabling, the gain procedure and the number of steps per turn (press SPACE at 12 o'clock, turn the dial exactly once, read the step count on screen). .. code-block:: matlab cd experiments/psychtoolbox/dial_test dial_dot_demo % 360 steps per turn, on the last screen dial_dot_demo(360, 1) % on screen 1 .. literalinclude:: /../../experiments/psychtoolbox/dial_test/dial_dot_demo.m :language: matlab ``dial_probe``: records every raw key event from the 905 while you turn one full turn each way, saves them to a CSV and reports counts, net steps and chatter. Use it when the dial seems to misbehave, and attach the CSV when asking for help. .. code-block:: matlab dial_probe % 15 s per turn, 10 s to walk to the dial Using the dial in your experiment ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add ``experiments/psychtoolbox/dial_test`` to the MATLAB path (or copy the four ``dial_*.m`` functions into your experiment folder). Then the whole integration is four calls: .. code-block:: matlab dial = dial_open(); % once, after opening the PTB window (360 steps/turn) dial = dial_reset(dial); % at the start of each trial: current position = 0 while ~done dial = dial_poll(dial); % once per frame % dial.delta : steps since the previous frame (+ clockwise, - counter-clockwise) % dial.steps : net steps since dial_reset % dial.angle : dial.steps in degrees, 0..360 % dial.otherKeys : any other keys pressed, e.g. any(dial.otherKeys == KbName('ESCAPE')) ... Screen('Flip', win); end dial_close(dial); % once, at the end (also in your catch block) Converting steps to a rating, a position or an angle is one line, for example a 0-100 scale where 200 steps (just over half a turn) span the whole scale: .. code-block:: matlab rating = min(100, max(0, rating + 0.5 * dial.delta)); Things to keep in mind: - there is one keyboard queue per keyboard. ``dial_open`` takes the default one (all keyboards on Windows). If your experiment already uses ``KbQueueCreate`` on the default keyboard, read your own keys from ``dial.otherKeys`` instead, or call ``dial_open`` after releasing your queue; - the operator keyboard's ``t`` and ``b`` keys count as dial steps too; - the dial has no button. To confirm a rating, use a response box (:doc:`responsebox`), a hold time, or a timeout; - ``dial_open`` suppresses keyboard input to the MATLAB console (``ListenChar(-1)``) and ``dial_close`` restores it. The functions: .. literalinclude:: /../../experiments/psychtoolbox/dial_test/dial_open.m :language: matlab .. literalinclude:: /../../experiments/psychtoolbox/dial_test/dial_poll.m :language: matlab .. literalinclude:: /../../experiments/psychtoolbox/dial_test/dial_reset.m :language: matlab .. literalinclude:: /../../experiments/psychtoolbox/dial_test/dial_close.m :language: matlab Complete example: a rating task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``dial_example_rating`` shows a 0-100 scale with a marker driven by the dial. A trial ends when the marker has not moved for 2 s (or after 15 s), and the rating, the time of the first movement and the dial steps are returned. The lines marked ``COPY`` are the ones to take into your own task. .. code-block:: matlab results = dial_example_rating(3) % three trials .. literalinclude:: /../../experiments/psychtoolbox/dial_test/dial_example_rating.m :language: matlab