#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

# [solved] Touch Screen Corsi Task - Borrowing a little code!!

edited April 2014

Hi I have recently begun my PhD and I'm quite new to programming / code etc. I used the below piece of code which I found in another stream as I am developing what I hope will be a touch screen Corsi task. In this participants are required to watch a block sequence and then tap this out on screen in the same order. Sequence lengths vary from 2 to 9. I'm doing it forwards and then backwards, harder than it sounds believe me.
Therefore I need opensesame to regcognise a touch response within the square and not anywhere else on screen and then log the co-ordinate of the touch. To be more specific I need it to do this for each tough, so if the sequence is 5 blocks long, I need SPSS to record 5 taps, whether correct / incorrect and coordinates. I also want reaction time but I'm not sure if this is a later question I might have to post. I used the below code therefore and I think I have it working, of sorts, but I was just wondering if anyone knew, for write up / understanding purposes how exactly the code calculates acceptable distance, how far is too far in essense from the square to be considered correct? I'm sorry if this is completely out of context from the original post as well, I'm relatively new to forums also.

Thank you , Katherine.

``````from math import sqrt
# The maximum error from the target center
maxClickErr = 100
# Use Pythagoras to determine the click error and set
# the variable `correct` to 1 (correct) or 0 (incorrect)
# depending on whether the click error is small enough.
# Because the cursor coordinates have 0,0 at the top-left
# and the sketchpad coordinates have 0,0 at the center,
# we need to compensate for the display size.
xc = self.get('width') / 2
yc = self.get('height') / 2
# Determine x and y error
dx = self.get('cursor_x')- xc - self.get('xTarget')
dy = self.get('cursor_y')- yc - self.get('yTarget')
# Determine error
clickErr = sqrt(dx**2 + dy**2)
# Determine `correct` variable
if clickErr <= maxClickErr:
exp.set('correct', 1)
else:
exp.set('correct', 0)
``````

• edited 4:51PM

Hi Katherine,

Welcome to the forum!

The variable `maxClickErr` in this script indicates the maximum distance in pixels that a touch can be from a specific target point, indicated by `xTarget` and `yTarget`. So the acceptable area is a circle around the target point with a radius of `maxClickErr` pixels.

Cheers,
Sebastiaan

• edited 4:51PM

Thank you, yes of sorts, I think my problem is that I've used this code thinking it would be appropriate for my task. Basically the corsi blocks task uses squares as targets. To this extent I've used the above code so that now it draws an acceptable target circle around the square which is largely fine as the margin for error is so small outside of the target, but is there a way that I could adapt the above code specifically for squares?

Thank you,
Katherine.

• edited April 2014

Hi Katherina,

Sure, the `dx` and `dy` variables indicate respectively the horizontal and vertical deviation from the target center. So defining an acceptable square would be something like this:

Update: Corrected script

``````# Determine `correct` variable
if clickErr >= abs(dx) and clickErr >= abs(dy):
exp.set('correct', 1)
else:
exp.set('correct', 0)
``````

Cheers,
Sebastiaan

• edited 4:51PM

Hi Sebastiann,

thank you for your help so far. I now might need a little more though, I've changed my design slightly so that it uses rectangles as stimuli which are 151 x 117 pixels in size and like before I need them to be targets for the touchscreen version. I'm currently trying to adapt the first piece of code on this thread to use rectangles, trying to work it out considering that you explained how I would make it relevant for squares. However I am yet as able to do that. I was wondering if you would be able to give me the full code for using rectangles as targets? Would I need to remove the square root part of the original code in this thread?

Sorry,

Thank you,
Katherine x

• edited April 2014

Hi Katherine,

I now notice that there is a mistake in my previous post. You should not compare `clickErr` against the horizontal (`dx`) and vertical (`dy`) deviation, but `maxClickErr`. And if you want to use a rectangle instead of a square, you need to use different maximum errors for the horizontal and vertical. For example like so:

Update: Corrected script

``````from math import sqrt
# The maximum error from the target center
maxClickErrX = 115/2
maxClickErrY = 117/2
# Use Pythagoras to determine the click error and set
# the variable `correct` to 1 (correct) or 0 (incorrect)
# depending on whether the click error is small enough.
# Because the cursor coordinates have 0,0 at the top-left
# and the sketchpad coordinates have 0,0 at the center,
# we need to compensate for the display size.
xc = self.get('width') / 2
yc = self.get('height') / 2
# Determine x and y error
dx = self.get('cursor_x')- xc - self.get('xTarget')
dy = self.get('cursor_y')- yc - self.get('yTarget')
# Determine `correct` variable
if maxClickErrX >= abs(dx) and maxClickErrY >= abs(dy):
exp.set('correct', 1)
else:
exp.set('correct', 0)
``````

So you basically just check if the horizontal and vertical deviation are smaller than or equal to the maximum click error. Pretty straightforward, really. Does that make sense?

Cheers,
Sebastiaan

• edited 4:51PM

Hi Sebastiaan,

thank you for your help, I pasted that code in t replace what I had before and changed the 115 to 151 because that's the width of my rectangle, but it now isn't registering any clicks as correct at all. I'm not sure why. No matter how close to the centre the results just keep registering 0 correct. I will continue to have a look at this, any ideas though?

Thank you again for your help,
Katherine.

• edited 4:51PM

Also what does abs stand for? The rest I think I understand. Thank you, Katherine.

• edited 4:51PM

Wait I think I may have done it, the arrows should be the other way around? >=abs(dx)?

thankyou

• edited 4:51PM

Hi Katherine,

The function `abs()` is to calculate the absolute value of a number. This is defined as the square root of the square of the original number, which basically comes down to the same number as you put in, but without the minus sign. Examples: `abs(3) == 3`, and `abs(-3) == 3.`

It seems the "smaller than or equal" sign should have indeed been a "greater than or equal". Sorry about that!

• edited 4:51PM

It seems the "smaller than or equal" sign should have indeed been a "greater than or equal". Sorry about that!

Oops 8-}

• edited 4:51PM

Its o.k, thank you both lots, you've been a massive help and I think I'm one step closer now to understanding programming, she sais . . I think I finally have my Corsi task up and running though, stopping rule and touch screen response working, thank you. I will spread the word!!!!

thanks, kat x