<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>EyeTrackingWithEprime</title><revhistory><revision><revnumber>36</revnumber><date>2013-04-04 15:33:37</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>35</revnumber><date>2013-03-08 10:02:45</date><authorinitials>localhost</authorinitials><revremark>converted to 1.6 markup</revremark></revision><revision><revnumber>34</revnumber><date>2012-11-19 11:24:08</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>33</revnumber><date>2012-11-12 17:13:46</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>32</revnumber><date>2012-10-23 15:58:47</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>31</revnumber><date>2012-10-23 15:57:26</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>30</revnumber><date>2012-10-23 14:03:44</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>29</revnumber><date>2012-10-22 14:08:10</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>28</revnumber><date>2012-10-22 14:06:31</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>27</revnumber><date>2012-10-22 13:56:44</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>26</revnumber><date>2012-10-18 13:52:41</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>25</revnumber><date>2012-02-23 13:14:29</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>24</revnumber><date>2012-02-23 13:13:51</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>23</revnumber><date>2012-02-23 13:13:30</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>22</revnumber><date>2010-07-01 12:38:18</date><authorinitials>test113.mrc-cbu.cam.ac.uk</authorinitials></revision><revision><revnumber>21</revnumber><date>2010-07-01 12:36:15</date><authorinitials>test113.mrc-cbu.cam.ac.uk</authorinitials></revision><revision><revnumber>20</revnumber><date>2010-07-01 12:35:56</date><authorinitials>test113.mrc-cbu.cam.ac.uk</authorinitials></revision><revision><revnumber>19</revnumber><date>2010-07-01 12:28:56</date><authorinitials>test113.mrc-cbu.cam.ac.uk</authorinitials></revision><revision><revnumber>18</revnumber><date>2009-10-15 15:55:39</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>17</revnumber><date>2009-10-12 11:55:36</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>16</revnumber><date>2009-10-12 11:54:35</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>15</revnumber><date>2009-10-12 11:54:06</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>14</revnumber><date>2009-10-12 11:51:31</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>13</revnumber><date>2009-10-12 11:43:53</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>12</revnumber><date>2009-10-08 16:08:15</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>11</revnumber><date>2009-10-01 16:10:13</date><authorinitials>ElisabethvondemHagen</authorinitials></revision><revision><revnumber>10</revnumber><date>2009-06-30 10:22:02</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>9</revnumber><date>2009-06-30 10:21:04</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>8</revnumber><date>2009-06-30 10:14:48</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>7</revnumber><date>2009-06-30 10:13:52</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>6</revnumber><date>2009-06-30 10:12:00</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>5</revnumber><date>2009-06-30 10:10:31</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>4</revnumber><date>2009-06-30 10:08:34</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>3</revnumber><date>2009-06-30 10:07:27</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>2</revnumber><date>2009-06-30 10:03:28</date><authorinitials>MaartenVanCasteren</authorinitials></revision><revision><revnumber>1</revnumber><date>2009-06-30 09:32:43</date><authorinitials>MaartenVanCasteren</authorinitials></revision></revhistory></articleinfo><section><title>Running an Eye tracking experiment with E-Prime</title><para>When using E-Prime with a SMI eye tracker, E-Prime should communicate with iView X, the eye tracking software, to start and stop the recording, indicate the start of each trial, specify the stimulus used and finally save the data. The way SMI prefer to do this is by using an ethernet connection and the UDP protocol, but E-Prime cannot handle this. For this reason SMI have developed a toolkit that can take care of the communication with the eye tracker. The toolkit consists of two parts: first a dll file that contains the actual executable code, and second an E-Prime package file that makes sure the functionality in the dll is available within an E-Prime script in a user friendly way. Here at the CBU we have upgraded the package file somewhat, so we are not using the original SMI package file. </para><para>To be able to use an eye tracker with this toolkit you need to do the following: </para><para>1 - Add the package to your E-Prime script. In the 'structure' window double-click on 'Experiment' and then select the 'Packages' tab. Press 'Add' and select the 'iView_X_SDK_CBU' package. If this is not available you'll have to install it: the file 'iViewXAPI_CBU.epk2' should be in 'Program Files\PST\E-Prime 2.0\Program\Packages'. If the Packages folder doesn't exist, it should be created. </para><para>2 - At the start of your experiment, add these lines, in an inline object: </para><screen><![CDATA[' Connect to the Eye Tracker over the ethernet. The IP adresses are the
' ones we usually use, so this should work at the CBU without modification
]]><![CDATA[
if not iView_X_SDK_CBU_Connect("192.168.1.1", 4444, "192.168.1.2", 5555) then
        Mouse.ShowCursor True
        MsgBox "Connect failed: " & iView_X_SDK_CBU_GetErrorString
        iView_X_SDK_CBU_Abort
        End
end if]]></screen><para>3 - You need to calibrate the eye tracker before you can use it. Put this code in an inline object to do so. </para><screen><![CDATA[' Use 13 targets for calibration
]]><![CDATA[
iView_X_SDK_CBU_SetCalMethod 13
]]><![CDATA[
if not iView_X_SDK_CBU_Calibrate then
        Mouse.ShowCursor True
        MsgBox "Calibration failed: " & iView_X_SDK_CBU_GetErrorString
        iView_X_SDK_CBU_Abort
        End
end if]]></screen><para>4 - You then should check the calibration to see if it was sufficiently precise. This is called validation. You should repeat calibration and validation until you have the required precision. </para><screen><![CDATA[' Do the validation
]]><![CDATA[
Dim DeviationXL as Double
Dim DeviationYL as Double
Dim DeviationXR as Double
Dim DeviationYR as Double
]]><![CDATA[
if not iView_X_SDK_CBU_Validate( DeviationXL, DeviationYL, DeviationXR, DeviationYR) Then
]]><![CDATA[
        Mouse.ShowCursor True
        MsgBox "Validation Failed: " & iView_X_SDK_CBU_GetErrorString
        iView_X_SDK_CBU_Abort
        End
]]><![CDATA[
end if
]]><![CDATA[
' Present the results
]]><![CDATA[
Mouse.ShowCursor true
MsgBox "MeanDeviation: "& DeviationXL &" " & DeviationYL &" " & DeviationXR &" " & DeviationYR
Mouse.ShowCursor false]]></screen><para>5 - Just before you start presenting your stimuli you should start the eye tracking recording </para><screen><![CDATA[' Start a recording on the Eye Tracking computer
]]><![CDATA[
if not iView_X_SDK_CBU_StartRecording then
        Mouse.ShowCursor True
        MsgBox "StartRecording failed: " & iView_X_SDK_CBU_GetErrorString
        iView_X_SDK_CBU_Abort
        End
end if]]></screen><para>6 - At the start of each trial, or just before your main stimulus is being presented, add a 'message line', preferably with your stimulus name as an argument (NB if you are presenting images, sending the name of the image file you are presenting will greatly facilitate analysis using the <ulink url="https://lsr-wiki-02.mrc-cbu.cam.ac.uk/meg/EyeTrackingWithEprime/meg/BeGaze#">BeGaze</ulink> software, as the associations between the trial and image will then be made automatically by the software. For more info, see <ulink url="https://lsr-wiki-02.mrc-cbu.cam.ac.uk/meg/EyeTrackingWithEprime/meg/BeGaze#">BeGaze</ulink>): </para><screen><![CDATA[' Send a message to be included in the Eye Tracking recording.
' This will normally be a trial number, or a condition code or
' something like that.
]]><![CDATA[
Display.WaitForVerticalBlank
Sleep 14                         ' Wait until a few ms before the next refresh
]]><![CDATA[
if not iView_X_SDK_CBU_SendMessage("msg") then
]]><![CDATA[
        Mouse.ShowCursor True
        MsgBox "SendMessage 5 failed: " & iView_X_SDK_CBU_GetErrorString
        iView_X_SDK_CBU_Abort
        List1.Terminate                ' Change List1 to actual list name
]]><![CDATA[
end if]]></screen><para>7 - Finally, at the end of th experiment put these lines in an inline object: </para><screen><![CDATA[' First stop the current recording
]]><![CDATA[
if not iView_X_SDK_CBU_StopRecording then
        Mouse.ShowCursor True
        MsgBox "StopRecording failed: " & iView_X_SDK_CBU_GetErrorString
        iView_X_SDK_CBU_Abort
        End
end if
]]><![CDATA[
' Then save the data. Get the filename that E-Prime uses for its output
]]><![CDATA[
dim fullfilename as string
fullfilename = c.datafile.filename
]]><![CDATA[
' Split the filename in its parts
]]><![CDATA[
dim fileroot as string
fileroot = FileParse$(fullfilename, 4)
]]><![CDATA[
' Eye tracking data will now be saved under the same name as the E-Prime data, in the iViewX folder
' If you want it to go to your own folder then add this to the filerootname.
]]><![CDATA[
if not iView_X_SDK_CBU_SaveData(fileroot, "", "", 1) then
        Mouse.ShowCursor True
        MsgBox "SaveData failed: " & iView_X_SDK_CBU_GetErrorString
        iView_X_SDK_CBU_Abort
        End
end if
]]><![CDATA[
' And finally close the connection with the eye tracker.
]]><![CDATA[
if not iView_X_SDK_CBU_Disconnect then
        Mouse.ShowCursor True
        MsgBox "Disconnect failed: " & iView_X_SDK_CBU_GetErrorString
        iView_X_SDK_CBU_Abort
        End
end if]]></screen><para>Obviously, the filename can be changed to whatever is best for you. Note that iView X cannot save to a directory that doesn't exist, so make sure to either create the correct directory, or write to root. </para></section><section><title>Running the script</title><para>When running the script you should go through these steps: </para><para>1 - Switch on the eye tracking hardware and install your subject. </para><para>2 - Start the iView X program on the eye tracking computer. </para><para>3 - Make sure the eye picture looks good and the eye gaze is correctly tracked. Verify this by asking the subject to look at all 4 corners of the screen. </para><para>4 - Run your E-Prime script. </para><para>The calibration uses the settings specified in iView X, which means that you will have to specify 'auto accept' or other settings there. The only thing that the script sets is the number of calibration points used. This is set to 13, but can easily be changed to another value. </para><para>One warning: this code hasn't been tested much yet, so might not always work as expected. Be careful and please report any problems to Maarten van Casteren. </para></section><section><title>Drift correction</title><para>And you can do drift correction using this script. This is still in the old form, using the serial port. This is NOT compatible with the other code above, but SMI have promised that drift correction will be part of the new version of the SDK, so we will be able to use it soon, hopefully. </para><screen><![CDATA[' Start drift correction
strData = "ET_RCL" & strENDL
Serial.WriteString strData
]]><![CDATA[
' Get the current canvas, set to gray and clear it
Set Cvs = Display.Canvas
]]><![CDATA[
Cvs.FillColor = CColor("gray")  ' Yes, 'gray'
Cvs.Clear
]]><![CDATA[
Cvs.PenWidth = 8
Cvs.PenColor = CColor("0,0,0")
]]><![CDATA[
' Process iView X commands
]]><![CDATA[
bReady = False
bWait = false
nLimit = 0
]]><![CDATA[
' Put calibration point
bPointOnShow = true
'strPnt = points(CInt(Item$(strComLine, 2, 2, " \t")))
strPnt = points(CInt(Item$("ET_CHG 1", 2, 2, " \t")))
Cvs.Clear
Cvs.Circle Cint(Item$(strPnt, 1, 1, " \t")), Cint(Item$(strPnt, 2, 2, " \t")), 8
]]><![CDATA[
While Not bReady
]]><![CDATA[
        nLimit = nLimit + 1
]]><![CDATA[
        if nLimit > 500 then
                MsgBox("Communication failure, time-out after 500 attempts")
                Stop
        end if
]]><![CDATA[
        ' Check the keyboard for presses on space, to force the current
        ' point to be accepted. This might be needed, sometimes
]]><![CDATA[
        if bPointOnShow and KeyBoard.History.Count > 0 then
]]><![CDATA[
                For nIndex = 1 To Keyboard.History.Count
                        ' Retrieve a single ResponseData object from the
                        ' collection of ResponseData objects stored in the
                        ' InputHistoryManager
                        Set theResponseData = Keyboard.History(nIndex)
                        If Not theResponseData Is Nothing Then
                                If theResponseData.RESP = "{SPACE}" Then
                                        strData = "ET_ACC" & strENDL
                                        Serial.WriteString strData
                                End If
                        End If
                Next
]]><![CDATA[
                Keyboard.History.RemoveAll
]]><![CDATA[
        end if
]]><![CDATA[
        nRead = Serial.ReadString(strRead)
]]><![CDATA[
        if nRead = 0 and bWait then
                ' Nothing read from port and nothing in buffer to process
                Sleep(50)
        else
                strBuf = strBuf & strRead       ' Add newly read input to buffer
]]><![CDATA[
                nPos = InStr(strBuf, strENDL)
]]><![CDATA[
                if nPos = 0 then
                        bWait = true
                else
                        bWait = false
                        strComLine = Left(strBuf, nPos - 1)     ' Extract command and arguments
                        strBuf = Mid(strBuf, nPos + 2)          ' Remove from input buffer
]]><![CDATA[
                        Debug.print strComLine
]]><![CDATA[
                        strCom = Item$(strComLine, 1, 1, " ")   ' Extract command itself
]]><![CDATA[
                        if  strCom = "ET_FIN" then
                                ' Calibration finished
                                bReady = True
                        end if
                end if
        end if
Wend]]></screen><para>The main advantage of letting E-Prime take care of the calibration is that you don't have to use WinCal any more. Combine these E-Prime scripts to start the recording, do the calibration, validation, drift correction, mark the trials, stop the recording and save the results to disk. </para></section></article>