EyeTrackingWithEprime - Meg Wiki
location: EyeTrackingWithEprime

Running an Eye tracking experiment with E-Prime

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.

To be able to use an eye tracker with this toolkit you need to do the following:

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.

2 - At the start of your experiment, add these lines, in an inline object:

' 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

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

3 - You need to calibrate the eye tracker before you can use it. Put this code in an inline object to do so.

' Use 13 targets for calibration

iView_X_SDK_CBU_SetCalMethod 13

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

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.

' Do the validation

Dim DeviationXL as Double
Dim DeviationYL as Double
Dim DeviationXR as Double
Dim DeviationYR as Double

if not iView_X_SDK_CBU_Validate( DeviationXL, DeviationYL, DeviationXR, DeviationYR) Then

        Mouse.ShowCursor True
        MsgBox "Validation Failed: " & iView_X_SDK_CBU_GetErrorString
        iView_X_SDK_CBU_Abort
        End

end if

' Present the results

Mouse.ShowCursor true
MsgBox "MeanDeviation: "& DeviationXL &" " & DeviationYL &" " & DeviationXR &" " & DeviationYR
Mouse.ShowCursor false

5 - Just before you start presenting your stimuli you should start the eye tracking recording

' Start a recording on the Eye Tracking computer

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

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 BeGaze software, as the associations between the trial and image will then be made automatically by the software. For more info, see BeGaze):

' 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.

Display.WaitForVerticalBlank
Sleep 14                         ' Wait until a few ms before the next refresh

if not iView_X_SDK_CBU_SendMessage("msg") then

        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

end if

7 - Finally, at the end of th experiment put these lines in an inline object:

' First stop the current recording

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

' Then save the data. Get the filename that E-Prime uses for its output

dim fullfilename as string
fullfilename = c.datafile.filename

' Split the filename in its parts

dim fileroot as string
fileroot = FileParse$(fullfilename, 4)

' 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.

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

' And finally close the connection with the eye tracker.

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

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.

Running the script

When running the script you should go through these steps:

1 - Switch on the eye tracking hardware and install your subject.

2 - Start the iView X program on the eye tracking computer.

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.

4 - Run your E-Prime script.

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.

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.

Drift correction

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.

' Start drift correction
strData = "ET_RCL" & strENDL
Serial.WriteString strData

' Get the current canvas, set to gray and clear it
Set Cvs = Display.Canvas

Cvs.FillColor = CColor("gray")  ' Yes, 'gray'
Cvs.Clear

Cvs.PenWidth = 8
Cvs.PenColor = CColor("0,0,0")

' Process iView X commands

bReady = False
bWait = false
nLimit = 0

' 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

While Not bReady

        nLimit = nLimit + 1

        if nLimit > 500 then
                MsgBox("Communication failure, time-out after 500 attempts")
                Stop
        end if

        ' Check the keyboard for presses on space, to force the current
        ' point to be accepted. This might be needed, sometimes

        if bPointOnShow and KeyBoard.History.Count > 0 then

                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

                Keyboard.History.RemoveAll

        end if

        nRead = Serial.ReadString(strRead)

        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

                nPos = InStr(strBuf, strENDL)

                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

                        Debug.print strComLine

                        strCom = Item$(strComLine, 1, 1, " ")   ' Extract command itself

                        if  strCom = "ET_FIN" then
                                ' Calibration finished
                                bReady = True
                        end if
                end if
        end if
Wend

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.

CbuMeg: EyeTrackingWithEprime (last edited 2013-04-04 15:33:37 by MaartenVanCasteren)