/** * @brief idle */ void idle() { double deltaT = (double)TIMER_MS; optotrak.updateMarkers(deltaT); markers = optotrak.getAllMarkers(); // Coordinates picker allVisiblePlatform = isVisible(markers.at(15).p) && isVisible(markers.at(16).p); allVisibleThumb = isVisible(markers.at(11).p) && isVisible(markers.at(12).p) && isVisible(markers.at(13).p); allVisibleIndex = isVisible(markers.at(7).p) && isVisible(markers.at(8).p) && isVisible(markers.at(9).p); allVisibleFingers = allVisibleThumb && allVisibleIndex; allVisiblePatch = isVisible(markers.at(1).p) && isVisible(markers.at(2).p) && isVisible(markers.at(3).p); allVisibleHead = allVisiblePatch && isVisible(markers.at(17).p) && isVisible(markers.at(18).p); //if ( allVisiblePatch ) headEyeCoords.update(markers.at(1).p,markers.at(2).p,markers.at(3).p,deltaT); // update thumb coordinates thumbCoords.update(markers.at(11).p,markers.at(12).p,markers.at(13).p,deltaT); // update index coordinates indexCoords.update(markers.at(7).p, markers.at(8).p, markers.at(9).p,deltaT); // Compute the coordinates of visual thumb thumb = thumbCoords.getP1().p; index = indexCoords.getP1().p; eyeLeft = headEyeCoords.getLeftEye().p; eyeRight = headEyeCoords.getRightEye().p; rigidCurrentThumb.setRigidBody(markers.at(11).p,markers.at(12).p,markers.at(13).p); rigidCurrentIndex.setRigidBody(markers.at(7).p,markers.at(8).p,markers.at(9).p); rigidStartThumb.computeTransformation(rigidCurrentThumb); rigidStartIndex.computeTransformation(rigidCurrentIndex); if (headCalibrationDone==3 && fingerCalibrationDone==4) { if ( !allVisibleIndex || !allVisibleThumb || !isVisible(markers.at(6).p)) boost::thread invisibleBeep( beepInvisible); Vector2d thumbProjection = cam.computeProjected(thumb); thumbProjectedInside = ( thumbProjection.x() >= 0 && thumbProjection.x() <= SCREEN_WIDTH ) && ( thumbProjection.y() >= 0 && thumbProjection.y() <= SCREEN_HEIGHT ); Vector2d indexProjection = cam.computeProjected(index); indexProjectedInside = ( indexProjection.x() >= 0 && indexProjection.x() <= SCREEN_WIDTH ) && ( indexProjection.y() >= 0 && indexProjection.y() <= SCREEN_HEIGHT ); trialFrame++; // Work on fingers coordinates double wThumb=0.5, wIndex=0.5; Vector3d m = (wThumb*thumb+wIndex*index)/(wThumb+wIndex); Vector3d d = thumb-index; visualThumb = thumb; visualIndex = index; double clamp= 2*factors.getCurrent().at("StimulusRadius"); double gain = 1.0;//in quello di Bob era invece: factors.getCurrent().at("Gain"); if ( d.norm() > clamp ) { visualThumb = m + 0.5*d.normalized()*(gain*d.blueNorm() - clamp*(gain-1)); visualIndex = m - 0.5*d.normalized()*(gain*d.blueNorm() - clamp*(gain-1)); } // Check when to advance trial double stimRadius = factors.getCurrent().at("StimulusRadius"); double radiusTolerance = util::str2num<double>(parameters.find("RadiusTolerance")); fingNow = fingersInSphericalVolume(thumb,index, visualStimCenter, stimRadius-radiusTolerance, stimRadius+radiusTolerance, selectedFinger ); // SubjectName\tFingerDist\tTrialNumber\tTrialFrame\tTotTime\tVisualStimX\tVisualStimY\tVisualStimZ\tfStimulusRadius\tfDistances\tfGain\tEyeLeftXraw\tEyeLeftYraw\tEyeLeftZraw\tEyeRightXraw\tEyeRightYraw\tEyeRightZraw\tWristXraw\tWristYraw\tWristZraw\tThumbXraw\tThumbYraw\tThumbZraw\tIndexXraw\tIndexYraw\tIndexZraw\tVisualThumbXraw\tVisualThumbYraw\tVisualThumbZraw\tVisualIndexXraw\tVisualIndexYraw\tVisualIndexZraw\tIsDrawing\tIsThumbProjected\tIsIndexProjected if (!isSaving) return; RowVector3d junk(9999,9999,9999); markersFile << fixed << setprecision(3) << parameters.find("SubjectName") << "\t" << fingerDistance << "\t" << totalTrialNumber << "\t" << trialFrame << "\t" << globalTimer.getElapsedTimeInMilliSec() << "\t" << visualStimCenter.x() << "\t" << visualStimCenter.y() << "\t" << visualStimCenter.z() << "\t" << factors.getCurrent().at("StimulusRadius") << "\t" << factors.getCurrent().at("Distances") << "\t" << factors.getCurrent().at("Gain") << "\t" << factors.getCurrent().at("DisappearRadius") << "\t" << ( isVisible(eyeLeft) ? eyeLeft.transpose() : junk ) << "\t" << ( isVisible(eyeRight) ? eyeRight.transpose() : junk ) << "\t" << ( isVisible(markers.at(6).p) ? markers.at(6).p.transpose() : junk ) << "\t" << ( isVisible(thumb) ? thumb.transpose() : junk ) << "\t" << ( isVisible(index) ? index.transpose() : junk ) << "\t" << ( isVisible(visualThumb) ? visualThumb.transpose() : junk ) << "\t" << ( isVisible(visualIndex) ? visualIndex.transpose() : junk ) << "\t" << isDrawing << "\t" << thumbProjectedInside << "\t" << indexProjectedInside << "\t" << fingNow << endl; // Switch to this function if you want cylindrical stimulus //fingersInCylindricalVolume( thumb,index, Vector3d(0,0,factors.getCurrent().at("Distances")), util::str2num<double>(parameters.find("StimulusHeight")), stimRadius,stimRadius+20, selectedFinger ); if (trialMode == STIMULUSMODE ) { if ( !fingNow ) // always starts counting the time if the finger is outside { fingersTimer.start(); } // only if the finger is inside the volume the counter has counted more that 1 second if ( fingersTimer.getElapsedTimeInMilliSec() > util::str2num<double>(parameters.find("LeaningTime")) ) { plato_write(PLATO_LEFT_RIGHT_CLOSED); advanceTrial(); plato_write(PLATO_LEFT_RIGHT_OPEN); } trialTimer.start(); // continue keeping it starting during stimulus drawingTrialFrame++; invisibleIndexFrames+=!allVisibleIndex; invisibleThumbFrames+=!allVisibleThumb; invisibleWristFrames+=!isVisible(markers.at(6).p); } else { if ( trialTimer.getElapsedTimeInMilliSec() > util::str2num<double>(parameters.find("IntervalTime")) ) { boost::thread trialBeep( beepTrial ); trialMode=STIMULUSMODE; } } isDrawing = (trialMode == STIMULUSMODE); } }
void idle() { // Set the time during which the stimulus is drawn, it depents on the phase (adaption or test) double drawStimTime = 0; if ( block.at("Phase") == 1 ) drawStimTime = str2num<double>(parameters.find("AdaptStimulusDuration")); else drawStimTime = str2num<double>(parameters.find("TestStimulusDuration")); double deltaT = (double)TIMER_MS; optotrak->updateMarkers(deltaT); markers = optotrak->getAllMarkers(); // Coordinates picker allVisiblePlatform = isVisible(markers.at(15).p) && isVisible(markers.at(16).p); allVisibleThumb = isVisible(markers.at(11).p) && isVisible(markers.at(12).p) && isVisible(markers.at(13).p); allVisibleIndex = isVisible(markers.at(7).p) && isVisible(markers.at(8).p) && isVisible(markers.at(9).p); allVisibleFingers = allVisibleThumb && allVisibleIndex; allVisiblePatch = isVisible(markers.at(1).p) && isVisible(markers.at(2).p) && isVisible(markers.at(3).p); allVisibleHead = allVisiblePatch && isVisible(markers.at(17).p) && isVisible(markers.at(18).p); //if ( allVisiblePatch ) headEyeCoords.update(markers.at(1).p,markers.at(2).p,markers.at(3).p,deltaT); // update thumb coordinates thumbCoords.update(markers.at(11).p,markers.at(12).p,markers.at(13).p,deltaT); // update index coordinates indexCoords.update(markers.at(7).p, markers.at(8).p, markers.at(9).p,deltaT); eyeLeft = headEyeCoords.getLeftEye().p; eyeRight = headEyeCoords.getRightEye().p; checkBounds(); indexInside[1]=indexInside[0]; // Controlla i frames occlusi, se il dito di test è fuori dalla sfera // ed ha superato una percentuale di tempo maggiore di // TestPercentOccludedFrames rispetto al tempo di presentazione dello // stimolo allora fa un beep if ( headCalibrationDone==3 && fingerCalibrationDone==3 && !indexInside[0] && (int) block.at("Phase")!=1 && isDrawing ) { bool visibleFinger=true; switch ( str2num<int>(parameters.find("TestFinger")) ) { case 0: visibleFinger=isVisible(thumbCoords.getP1().p); break; case 1: visibleFinger=isVisible(indexCoords.getP1().p); break; case 2: visibleFinger=isVisible(thumbCoords.getP1().p) && isVisible(indexCoords.getP1().p); break; } // XXX scommentare per far si che anche il polso venga controllato per missing frames //visibleFinger = visibleFinger && isVisible(markers.at(6).p); if ( !visibleFinger ) { occludedFrames++; if ( str2num<int>(parameters.find("AudioFeedback") ) ) boost::thread invisibleBeep( beepInvisible); } trialOk=true; // mentre disegna lo stimolo } // Qui durante la fase di delay dopo la scomparsa dello stimolo controlla che il numero // di frames occlusi non abbia superato il limite consentito if ( isDrawing==0 && headCalibrationDone==3 && (block.at("Phase")!=1) && !indexInside[0] ) { double percentOccludedFrames = ((double)occludedFrames/(double)drawingTrialFrame )*100.0; trialOk=(percentOccludedFrames < str2num<double>(parameters.find("TestPercentOccludedFrames"))); } // Calcola le coordinate delle dita offsettate durante la fase di adattamento if ( block.at("Phase") == 1 ) { double fingerOffset = adaptOffsets.at(block1TrialNumber) ; switch ( str2num<int>(parameters.find("AdaptFinger")) ) { case 0: //index visualThumb = thumbCoords.getP1().p + Vector3d(0,0,fingerOffset); break; case 1: //thumb visualIndex = indexCoords.getP1().p + Vector3d(0,0,fingerOffset); break; case 2: { visualThumb = thumbCoords.getP1().p + Vector3d(0,0,fingerOffset); visualIndex = indexCoords.getP1().p + Vector3d(0,0,fingerOffset); } break; } drawingTrialFrame=0; occludedFrames=0; } if ( headCalibrationDone==3 && fingerCalibrationDone==3 && trialMode==STIMULUSMODE ) { if ( globalTimer.getElapsedTimeInMilliSec() <= drawStimTime ) { isDrawing=1; delayedTimer.start(); } else { // non disegna nulla if ( delayedTimer.getElapsedTimeInMilliSec() < str2num<int>(parameters.find("DelayTime")) ) isDrawing=0; else { delayedTimer.start(); advanceTrial(); } } } if ( (int) block.at("Phase") != 1 && isDrawing==1 ) { drawingTrialFrame++; } #define WRITE int thisBlockTrialNumber=0; switch ( block.at("Phase") ) { case 0: thisBlockTrialNumber=block0TrialNumber; break; case 1: thisBlockTrialNumber=block1TrialNumber; break; case 2: thisBlockTrialNumber=block2TrialNumber; break; } if ( !isMovingRod ) { RowVector3d junk(9999,9999,9999); // Write to file if ( headCalibrationDone==3 && fingerCalibrationDone==3 ) { if ( totalFrame==0) { markersFile << fixed << setprecision(3) << "SubjectName\tAdaptFinger\tTestFinger\tAdaptOffset\tAdaptHapticFeedback\tFingerDist\tTotalTrial\tPhase\tTrialNumber\tTrialFrame\tdT\tTotTime\tIndexInside\tIsDrawing\tTrialOk\tVisualStimX\tVisualStimY\tVisualStimZ\tHapticRodX\tHapticRodY\tHapticRodZ\tfRelDepth\tfDistances\tDeltaXRods\tEyeLeftXraw\tEyeLeftYraw\tEyeLeftZraw\tEyeRightXraw\tEyeRightYraw\tEyeRightZraw\tWristXraw\tWristYraw\tWristZraw\tThumbXraw\tThumbYraw\tThumbZraw\tIndexXraw\tIndexYraw\tIndexZraw" << endl; totalFrame++; } markersFile << fixed << setprecision(3) << parameters.find("SubjectName") << "\t" << parameters.find("AdaptFinger") << "\t" << parameters.find("TestFinger") << "\t" << adaptOffsets.at(block1TrialNumber) << "\t" << parameters.find("AdaptHapticFeedback") << "\t" << fingerDistance << "\t" << totalTrialNumber << "\t" << block.at("Phase") << "\t" << thisBlockTrialNumber << "\t" << trialFrame << "\t" << deltaT*1000 << "\t" << totalTime.getElapsedTimeInMilliSec() << "\t" << indexInside[0] << "\t" << isDrawing << "\t" << trialOk << "\t" << visualRodCenter.transpose() << "\t" << hapticRodCenter.transpose() << "\t" << ((block.at("Phase")!=1) ? factors.at("RelDepth") : 9999) << "\t" << ((block.at("Phase")!=1) ? factors.at("Distances") : 9999) << "\t" << deltaXRods << "\t" << ( isVisible(eyeLeft) ? eyeLeft.transpose() : junk ) << "\t" << ( isVisible(eyeRight) ? eyeRight.transpose() : junk ) << "\t" << ( isVisible(markers.at(6).p) ? markers.at(6).p.transpose() : junk ) << "\t" << ( isVisible(thumbCoords.getP1().p) ? thumbCoords.getP1().p.transpose() : junk ) << "\t" << ( isVisible(indexCoords.getP1().p) ? indexCoords.getP1().p.transpose() : junk ) << endl; } trialFrame++; } #ifdef SAVEDATADAT eulerAngles.init( headEyeCoords.getRigidStart().getFullTransformation().rotation() ); // Write datafile to real-time streaming of data ofstream outputfile; outputfile.open("data.dat"); outputfile << "Subject Name: " << parameters.find("SubjectName") << endl; outputfile << "AdaptHapticFeedback: " << parameters.find("AdaptHapticFeedback") << endl; outputfile << "AdaptFinger: " << parameters.find("AdaptFinger") << endl; outputfile << "TestFinger: " << parameters.find("TestFinger") << endl; outputfile << "IndexInside: " << indexInside[0] << endl; //outputfile << "Yaw: " << toDegrees(eulerAngles.getYaw()) << endl <<"Pitch: " << toDegrees(eulerAngles.getPitch()) << endl << "Roll: " << toDegrees(eulerAngles.getRoll()) << endl; outputfile << "EyeLeft: " << headEyeCoords.getLeftEye().p.transpose() << endl; outputfile << "EyeRight: " << headEyeCoords.getRightEye().p.transpose() << endl << endl; outputfile << "Thumb: " << thumbCoords.getP1().p.transpose() << endl; outputfile << "Index: " << indexCoords.getP1().p.transpose() << endl; outputfile << "Wrist: " << markers.at(6).p.transpose() << endl; outputfile << "IsDrawing: " << isDrawing << " TrialOk: " << trialOk << " OccludedFrames: " << occludedFrames << " DrawingTrialFrames: " << drawingTrialFrame << endl; outputfile << "HapticRodCenter: " << hapticRodCenter.transpose() << endl; outputfile << "VisualRodCenter: " << visualRodCenter.transpose() << endl; outputfile << "Markers(5): " << markers.at(5).p.transpose() << endl; outputfile << "AdaptOffset: " << adaptOffsets.at(block1TrialNumber) << endl; outputfile << "Phase: " << block.at("Phase") << endl; outputfile << "TrialNumber: " << thisBlockTrialNumber << endl; outputfile << "Total trials: " << totalTrialNumber << endl; #endif frameTimer.start(); }
void idle() { if (trialNumber >= maxTotalTrials ) exit(0); double elapsedFrameTime = totalTimer.getTimeIntervalInMilliSec(); optotrak.updateMarkers(elapsedFrameTime); markers = optotrak.getAllMarkers(); headEyeCoords.update(markers[1],markers[2],markers[3],TIMER_MS); allVisiblePatch = markers[1].isVisible() && markers[2].isVisible() && markers[3].isVisible(); allVisibleHead = markers[17].isVisible() && markers[18].isVisible() && allVisibleHead; eyeLeft = headEyeCoords.getLeftEye().p; eyeRight = headEyeCoords.getRightEye().p; cyclopeanEye = (eyeLeft+eyeRight)/2.0; projPointEyeRight = getEyeProjectionPoint(); checkBounds(nOscillationsFixation, eyeRight.x(), trialMode, headCalibrationDone, minOscTime, maxOscTime, maxXOscillation, translationTimer, beepOk, tweeter, woofer,tweeter); if ( trialMode == STIMULUSMODE ) deltaT+=TIMER_MS; else deltaT=0; if (headCalibrationDone == 3 && trialMode != PROBEMODE ) { // Questo rende conto del fatto che lo stimolo appare solo quando l'occhio è quasi in centro int actualTrialMode = trialMode; if ( trialMode == STIMULUSMODE && ( eyeRight.x()) > centerTolerance ) actualTrialMode=FIXATIONMODE; markersFile << fixed << trialNumber << " " << actualTrialMode << " " ; markersFile << fixed << setprecision(3) << eyeRight.transpose() << " " << eyeLeft.transpose() << " " << toDegrees(headEyeCoords.getPitch()) << " " << toDegrees(headEyeCoords.getYaw()) << " " << toDegrees(headEyeCoords.getRoll()) << " " ; markersFile << fixed << setprecision(0)<< factors["OmegaY"] << " " << factors["Binocular"] << " " << factors["Tilt"] << " " << factors["Slant"] << " " << totalTimer.getElapsedTimeInMilliSec() << endl; //objectPassiveTransformation.setIdentity(); if ( actualTrialMode == STIMULUSMODE ) { objectPassiveTransformation = getPassiveMatrix(); matrixFile << setw(6) << left << trialNumber << " " ; for ( int i=0; i<3; i++) matrixFile << objectPassiveTransformation.matrix().row(i) << " " ; matrixFile << endl; } if ( actualTrialMode == STIMULUSMODE ) { vector< Vector3d> projPoints = stimDrawer.projectStimulusPoints(objectActiveTransformation,headEyeCoords.getRigidStart().getFullTransformation(),cam,focalDistance,screen,Vector3d(0,0,0),false,false); MatrixXd a1toa6 = stimDrawer.computeOpticFlow(projPoints, focalDistance, elapsedFrameTime/1000); flowsFile << trialNumber << " " << a1toa6.transpose() << endl; } } writeContinuosDataFile(); }