예제 #1
0
UINT DataProcess(LPVOID lprarm)				//数据文件读取线程
{
	EmotivRead *pemo = (EmotivRead*)lprarm;
	if(pemo==NULL)
		return 1;
	int state;
	bool readytocollect = false;
	unsigned int nSamplesTaken=0;
	unsigned int channelCount = pemo->GetChangnum();
	double ** buffer = new double*[channelCount];
	while(!pemo->m_bExit)
	{
		state = EE_EngineGetNextEvent(pemo->m_neEvent);
		if(EDK_OK==state)
		{
			EE_Event_t eventType = EE_EmoEngineEventGetType(pemo->m_neEvent);
			EE_EmoEngineEventGetUserId(pemo->m_neEvent, &pemo->m_nUserID);
			if (eventType == EE_UserAdded) {
				EE_DataAcquisitionEnable(pemo->m_nUserID,true);
				readytocollect = true;
			}
		}
		if(readytocollect)
		{
			EE_DataUpdateHandle(0, pemo->m_hData);
			EE_DataGetNumberOfSample(pemo->m_hData,&nSamplesTaken);

			if(nSamplesTaken==0)
			{
				Sleep(1);
				continue;
			}
			for (size_t i=0; i<channelCount; i++)
				buffer[i] = new double[nSamplesTaken];
			EE_DataGetMultiChannels(pemo->m_hData, pemo->m_nChans, channelCount, buffer, nSamplesTaken);
			double *pt = new double[channelCount*nSamplesTaken];
			for (size_t i=0;i<nSamplesTaken;i++)
			{
				for (size_t j=0;j<channelCount;j++)
				{
					pt[channelCount*i+j] = buffer[j][i];
				}
			}
			pemo->SaveDatatoBuf(pt , channelCount*nSamplesTaken);
			delete pt;
			for (size_t i=0; i<channelCount; i++)		//realse;
				delete buffer[i];
		}
	}
	delete buffer;
	buffer = NULL;
	return 0;
}
int main() {
	EmoEngine engine;

	while(1) {
        while(engine.retrieveEvent()) {
            EmoEngineEventHandle event = engine.event();

            unsigned userID;
            EE_Event_t eventType = EE_EmoEngineEventGetType(event);
			EE_EmoEngineEventGetUserId(event, &userID);

			if(eventType == EE_EmoStateUpdated) {
                EmoStateHandle state = engine.eventState();
                std::cout << extractExpression(state) << std::endl;
			}
        }

		Sleep(1);
	}
}
예제 #3
0
파일: PPRA.cpp 프로젝트: gamingrobot/PPRA
int main(int argc, char** argv) {

	comPort=openPort(portS);//***PUT YOUR COM PORT NUMBER HERE!***

	// location of the machine running the 3D motion cube
	std::string receiverHost = "localhost";
	
	if (argc > 2) {
		std::cout << "Usage: " << argv[0] << " <hostname>" << std::endl;
		std::cout << "The arguments specify the host of the motion cube (Default: localhost)" << std::endl;
		return 1;
	}

	if (argc > 1) {
		receiverHost = std::string(argv[1]);
	}

	EmoEngineEventHandle eEvent	= EE_EmoEngineEventCreate();
	EmoStateHandle eState		= EE_EmoStateCreate();
	unsigned int userID			= 0;
	
	try {

//		if (EE_EngineConnect() != EDK_OK) {
		if (EE_EngineRemoteConnect("127.0.0.1", 3008) != EDK_OK) {
			throw std::exception("Emotiv Engine start up failed.");
		}
		else {
			std::cout << "Emotiv Engine started!" << std::endl;
			
				neutral();//send all servos to their neutral positions
		}

		int startSendPort = 6868;
		std::map<unsigned int, SocketClient> socketMap;
		promptUser();
		
		while (true) {
			
			// Handle the user input
			if (_kbhit()) {
				if (!handleUserInput()) {
					break;
				}
			}

				
			if(paused == true){
				int state = EE_EngineGetNextEvent(eEvent);

				// New event needs to be handled
				if (state == EDK_OK) {

					EE_Event_t eventType = EE_EmoEngineEventGetType(eEvent);
					EE_EmoEngineEventGetUserId(eEvent, &userID);

					switch (eventType) {

						// New headset connected, create a new socket to send the animation
						case EE_UserAdded:
						{
							std::cout << std::endl << "New user " << userID << " added, sending Cognitiv animation to ";
							std::cout << receiverHost << ":" << startSendPort << "..." << std::endl;
							promptUser();

							socketMap.insert(std::pair<unsigned int, SocketClient>(
								userID, SocketClient(receiverHost, startSendPort, UDP)));
							
							startSendPort++;
							break;
						}
					
						// Headset disconnected, remove the existing socket
						case EE_UserRemoved:
						{
							std::cout << std::endl << "User " << userID << " has been removed." << std::endl;
							promptUser();

							std::map<unsigned int, SocketClient>::iterator iter;
							iter = socketMap.find(userID);
							if (iter != socketMap.end()) {
								socketMap.erase(iter);
							}
							break;
						}
						
						// Send the Cognitiv animation if EmoState has been updated
						case EE_EmoStateUpdated:
						{
							//std::cout << "New EmoState from user " << userID << "..." << std::endl;
							EE_EmoEngineEventGetEmoState(eEvent, eState);

							std::map<unsigned int, SocketClient>::iterator iter;
							iter = socketMap.find(userID);
							if (iter != socketMap.end()) {
								sendCognitiv(eState);
							}
							break;
						}

						// Handle Cognitiv training related event
						case EE_CognitivEvent:
						{
							handleCognitivEvent(std::cout, eEvent);
							break;
						}

						default:
							break;
					}
				}
				else if (state != EDK_NO_EVENT) {
					std::cout << "Internal error in Emotiv Engine!" << std::endl;
					break;
				}
			}
			Sleep(1);
		}
	}
	catch (const std::exception& e) {
		std::cerr << e.what() << std::endl;
		std::cout << "Press any keys to exit..." << std::endl;
		getchar();
	}

	EE_EngineDisconnect();
	EE_EmoStateFree(eState);
	EE_EmoEngineEventFree(eEvent);

	return 0;
}
예제 #4
0
파일: PPRA.cpp 프로젝트: gamingrobot/PPRA
void handleCognitivEvent(std::ostream& os, EmoEngineEventHandle cognitivEvent) {

	unsigned int userID = 0;
	EE_EmoEngineEventGetUserId(cognitivEvent, &userID);
	EE_CognitivEvent_t eventType = EE_CognitivEventGetType(cognitivEvent);


	switch (eventType) {

		case EE_CognitivTrainingStarted:
		{
			os << std::endl << "Cognitiv training for user " << userID << " STARTED!" << std::endl;
			break;
		}

		case EE_CognitivTrainingSucceeded:
		{
			os << std::endl << "Cognitiv training for user " << userID << " SUCCEEDED!" << std::endl;
			break;
		}

		case EE_CognitivTrainingFailed:
		{
			os << std::endl << "Cognitiv training for user " << userID << " FAILED!" << std::endl;
			break;
		}

		case EE_CognitivTrainingCompleted:
		{
			os << std::endl << "Cognitiv training for user " << userID << " COMPLETED!" << std::endl;
			break;
		}

		case EE_CognitivTrainingDataErased:
		{
			os << std::endl << "Cognitiv training data for user " << userID << " ERASED!" << std::endl;
			break;
		}

		case EE_CognitivTrainingRejected:
		{
			os << std::endl << "Cognitiv training for user " << userID << " REJECTED!" << std::endl;
			break;
		}

		case EE_CognitivTrainingReset:
		{
			os << std::endl << "Cognitiv training for user " << userID << " RESET!" << std::endl;
			break;
		}

		case EE_CognitivAutoSamplingNeutralCompleted:
		{
			os << std::endl << "Cognitiv auto sampling neutral for user " << userID << " COMPLETED!" << std::endl;
			break;
		}

		case EE_CognitivSignatureUpdated:
		{
			os << std::endl << "Cognitiv signature for user " << userID << " UPDATED!" << std::endl;
			break;
		}

		case EE_CognitivNoEvent:
			break;

		default:
			//@@ unhandled case
			assert(0);
			break;
	}
    promptUser();
}
예제 #5
0
void emotiv::init(){

	EE_DataChannel_t targetChannelList[] = {
		ED_COUNTER,
		ED_AF3, ED_F7, ED_F3, ED_FC5, ED_T7, 
		ED_P7, ED_O1, ED_O2, ED_P8, ED_T8, 
		ED_FC6, ED_F4, ED_F8, ED_AF4, ED_GYROX, ED_GYROY, ED_TIMESTAMP, 
		ED_FUNC_ID, ED_FUNC_VALUE, ED_MARKER, ED_SYNC_SIGNAL
	};

const char header[] = "COUNTER,AF3,F7,F3, FC5, T7, P7, O1, O2,P8" 
                      ", T8, FC6, F4,F8, AF4,GYROX, GYROY, TIMESTAMP, "   
                      "FUNC_ID, FUNC_VALUE, MARKER, SYNC_SIGNAL,";
const char affectivSuitesName[] = "Engagement,Frustration,Meditation,Excitement,";

	EmoEngineEventHandle eEvent			= EE_EmoEngineEventCreate();
	EmoStateHandle eState				= EE_EmoStateCreate();
	unsigned int userID					= 0;
	const unsigned short composerPort	= 1726;
	float secs							= 1;
	unsigned int datarate				= 0;
	bool readytocollect					= false;
	int option							= 0;
	int state							= 0;


	
	if (EE_EngineConnect() != EDK_OK) {
		throw std::exception("Emotiv Engine start up failed.");
	}
				
		
		std::cout << "Start receiving EEG Data and affectiv data! Press any key to stop logging...\n" << std::endl;
    	std::ofstream ofs("../bin/EEG_Data.csv",std::ios::trunc);
		ofs << header << std::endl;
		std::ofstream ofs2("../bin/Affectiv_Data.csv",std::ios::trunc);
		ofs2 << affectivSuitesName << std::endl;
		
		DataHandle hData = EE_DataCreate();
		EE_DataSetBufferSizeInSec(secs);

		std::cout << "Buffer size in secs:" << secs << std::endl;

		while (!_kbhit()) {

			state = EE_EngineGetNextEvent(eEvent);
			EE_Event_t eventType;

			if (state == EDK_OK) {

				eventType = EE_EmoEngineEventGetType(eEvent);
				EE_EmoEngineEventGetUserId(eEvent, &userID);
				EE_EmoEngineEventGetEmoState(eEvent, eState);

				// Log the EmoState if it has been updated
				if (eventType == EE_UserAdded) {
					std::cout << "User added";
					EE_DataAcquisitionEnable(userID,true);
					readytocollect = true;
				}
			

			if (readytocollect && (eventType == EE_EmoStateUpdated)) {
												
						EE_DataUpdateHandle(0, hData);

						unsigned int nSamplesTaken=0;
						EE_DataGetNumberOfSample(hData,&nSamplesTaken);
		
						std::cout << "Updated " << nSamplesTaken << std::endl;
						
						if (nSamplesTaken != 0  ) {

							double* data = new double[nSamplesTaken];
							for (int sampleIdx=0 ; sampleIdx<(int)nSamplesTaken ; ++ sampleIdx) {
								for (int i = 0 ; i<sizeof(targetChannelList)/sizeof(EE_DataChannel_t) ; i++) {

									EE_DataGet(hData, targetChannelList[i], data, nSamplesTaken);
									ofs << data[sampleIdx] << ",";
								}	
								ofs << std::endl;
							}
							delete[] data;							
						}
						
						float affEngegement = ES_AffectivGetEngagementBoredomScore(eState);
						float affFrus = ES_AffectivGetFrustrationScore(eState);
						float affMed = ES_AffectivGetMeditationScore(eState);
						float affExcitement = ES_AffectivGetExcitementShortTermScore(eState);
						printf("Engagement: %f, Frustration: %f, ...\n",affEngegement,affFrus);
						ofs2 <<affEngegement<<","<<	affFrus<<","<<affMed<<","<<affExcitement<<","<<std::endl;

			}
			}
			Sleep(100);
		}

		ofs.close();
		ofs2.close();
		EE_DataFree(hData);

}
예제 #6
0
int EpocHandler::updateData()
{
	//if(debug)
	//	std::cout << "1" <<std::endl;
	if(!connected){
		useradded = false;
		if(connect())
		{
			if(debug)
				std::cout << "Successfully connected" <<std::endl;
		}else
		{
			if(debug)
				std::cout << "Did not Connect Properly" << std::endl;
		}
	}
	if(debug)
		std::cout << "2" <<std::endl;
	DataHandle hData = EE_DataCreate();
	EE_DataSetBufferSizeInSec(secs);
	EE_Event_t eventType;
	state = EE_EngineGetNextEvent(eEvent);
	eventType = EE_EmoEngineEventGetType(eEvent); 
	while(!useradded){
		if ((state) == EDK_OK)
		{
			
			EE_EmoEngineEventGetUserId(eEvent, &userID);			
			// Log the EmoState if it has been updated
			if (eventType == EE_UserAdded) {
				if(debug)
					std::cout << "User added" << std::endl;
				useradded = true;
				EE_DataAcquisitionEnable(userID, true);
				readytocollect = true;
			}else{
				if(debug)
					std::cout << "No user added" << std::endl;
			}
		}
		state = EE_EngineGetNextEvent(eEvent);
		eventType = EE_EmoEngineEventGetType(eEvent);
	}
	
	if (readytocollect)
	{
		if(debug)
			std::cout << "3" <<std::endl;
		//alte arrays löschen
		if(updated){
			for(it_type iterator = eegDataMap.begin(); iterator != eegDataMap.end(); iterator++) {
				delete[] iterator->second;
			}
			updated = false;
		}

		EE_DataUpdateHandle(0, hData);
		EE_EmoEngineEventGetEmoState(eEvent, eState);
		EE_DataGetNumberOfSample(hData, &nSamplesTaken);
		if (debug)
			std::cout << "Updated " << nSamplesTaken << std::endl;
		if (nSamplesTaken != 0) {
			if(debug)
				std::cout << "EmoState read " <<std::endl;
			if(debug)
				std::cout << " , " << (eventType == EE_EmoStateUpdated) << std::endl;
			pEngagement =  ES_AffectivGetEngagementBoredomScore(eState);
			pFrustration = ES_AffectivGetFrustrationScore(eState);
			pMeditation = ES_AffectivGetMeditationScore(eState);
			pExcitement = ES_AffectivGetExcitementShortTermScore(eState);
			if(debug)
				std::cout << "EmoState read " << pEngagement << ", " << pFrustration << ", " << pMeditation<<std::endl;
			
			if(debug)
				std::cout << "datachannels size " << sizeof(dataChannels)/sizeof(dataChannels[0]) << std::endl;
			if(debug)
				std::cout << "Map Size : "<< eegDataMap.size() <<std::endl;
			
			//Map füllen
			for(int i = 0; i < sizeof(dataChannels)/sizeof(dataChannels[0]); ++i){
				double* data = new double[nSamplesTaken];
				if(debug)
					std::cout << "check!\t";
				EE_DataGet(hData, dataChannels[i],data, nSamplesTaken);
				eegDataMap[dataChannels[i]] = data;

			} 
			if(debug)
				std::cout << "4" <<std::endl;
			updated = true;
			if(debug)
				std::cout << "sizeof Map: " << eegDataMap.size() <<std::endl;

		}

	}
	//disconnect();

	return nSamplesTaken;
}
예제 #7
0
int main(int argc, char** argv) {

    EmoEngineEventHandle eEvent			= EE_EmoEngineEventCreate();
    EmoStateHandle eState				= EE_EmoStateCreate();
    unsigned int userID					= 0;    
    float secs							= 1;
    unsigned int datarate				= 0;
    bool readytocollect					= false;
    int state							= 0;
    bool connected                      = true;
    long long count                     = 0;

    // Connect to the EE_Engine.
    if (EE_EngineConnect() != EDK_OK) {
        std::cout << "Could not connect to EEG" << std::endl;
        return 0;
    }

    std::string directory = argv[2];
    std::string filename = argv[1];
    filename = directory + filename + "-EEGLogger.csv";

    std::cout << filename << std::endl; // were the file will be saved

    std::cout << "Start receiving EEG Data! Press any key to stop logging...\n" << std::endl;
    std::ofstream ofs(filename.c_str(),std::ios::trunc);
    ofs << header << std::endl;

    if(connected){
        DataHandle hData = EE_DataCreate();
        EE_DataSetBufferSizeInSec(secs);

        std::cout << "Buffer size in secs:" << secs << std::endl;

        while (!kbhit()){
            state = EE_EngineGetNextEvent(eEvent);
          
          if(!printFace(count)){ break; } // print new face to make every 20 seconds
            
            if (state == EDK_OK){

                EE_Event_t eventType = EE_EmoEngineEventGetType(eEvent);
                EE_EmoEngineEventGetUserId(eEvent, &userID);

                // Log the EmoState if it has been updated
                if (eventType == EE_UserAdded){
                    std::cout << "User added" << std::endl;
                    EE_DataAcquisitionEnable(userID,true);
                    readytocollect = true;
                }
            }
            if (readytocollect){
                EE_DataUpdateHandle(0, hData);

                unsigned int nSamplesTaken=0;
                EE_DataGetNumberOfSample(hData,&nSamplesTaken);

                if (nSamplesTaken != 0) {
                      double* data = new double[nSamplesTaken];
                      for (int sampleIdx=0 ; sampleIdx<(int)nSamplesTaken ; ++ sampleIdx) {
                           for (int i = 0 ; i<sizeof(targetChannelList)/sizeof(EE_DataChannel_t) ; i++) {
                               EE_DataGet(hData, targetChannelList[i], data, nSamplesTaken);
                               ofs << data[sampleIdx] << ",";
                            }
                            ofs << std::endl;
                            count++;
                       }
                       delete[] data;
                }
            }
        }

        ofs.close();
        EE_DataFree(hData);
    }

    std::cout << "Disconnected" << std::endl;

    EE_EngineDisconnect();
    EE_EmoStateFree(eState);
    EE_EmoEngineEventFree(eEvent);

    return 0;
}
//--------------------------------------------------------------
void testApp::setup(){
	
	EmoEngineEventHandle eEvent			= EE_EmoEngineEventCreate();
	EmoStateHandle eState				= EE_EmoStateCreate();
	unsigned int userID					= 0;
	const unsigned short composerPort	= 1726;
	float secs							= 1;
	unsigned int datarate				= 0;
	bool readytocollect					= false;
	int option							= 0;
	int state							= 0;


	std::string input;

	try {

		//if (argc != 2) {
			//throw std::exception("Please supply the log file name.\nUsage: EEGLogger [log_file_name].");
		//}

		std::cout << "===================================================================" << std::endl;
		std::cout << "Example to show how to log EEG Data and Affectiv Data from EmoEngine/EmoComposer."	   << std::endl;
		std::cout << "===================================================================" << std::endl;
		std::cout << "Press '1' to start and connect to the EmoEngine                    " << std::endl;
		std::cout << "Press '2' to connect to the EmoComposer                            " << std::endl;
		std::cout << ">> ";

		std::getline(std::cin, input, '\n');
		option = atoi(input.c_str());

		switch (option) {
			case 1:
			{
				if (EE_EngineConnect() != EDK_OK) {
					throw std::exception("Emotiv Engine start up failed.");
				}
				break;
			}
			case 2:
			{
				std::cout << "Target IP of EmoComposer? [127.0.0.1] ";
				std::getline(std::cin, input, '\n');

				if (input.empty()) {
					input = std::string("127.0.0.1");
				}

				if (EE_EngineRemoteConnect(input.c_str(), composerPort) != EDK_OK) {
					std::string errMsg = "Cannot connect to EmoComposer on [" + input + "]";
					throw std::exception(errMsg.c_str());
				}
				break;
			}
			default:
				throw std::exception("Invalid option...");
				break;
		}
		
		
		std::cout << "Start receiving EEG Data and affectiv data! Press any key to stop logging...\n" << std::endl;
    	std::ofstream ofs("../bin/EEG_Data.csv",std::ios::trunc);
		ofs << header << std::endl;
		std::ofstream ofs2("../bin/Affectiv_Data.csv",std::ios::trunc);
		ofs2 << affectivSuitesName << std::endl;
		
		DataHandle hData = EE_DataCreate();
		EE_DataSetBufferSizeInSec(secs);

		std::cout << "Buffer size in secs:" << secs << std::endl;

		while (!_kbhit()) {

			state = EE_EngineGetNextEvent(eEvent);
			EE_Event_t eventType;

			if (state == EDK_OK) {

				eventType = EE_EmoEngineEventGetType(eEvent);
				EE_EmoEngineEventGetUserId(eEvent, &userID);
				EE_EmoEngineEventGetEmoState(eEvent, eState);

				// Log the EmoState if it has been updated
				if (eventType == EE_UserAdded) {
					std::cout << "User added";
					EE_DataAcquisitionEnable(userID,true);
					readytocollect = true;
				}
			

			if (readytocollect && (eventType == EE_EmoStateUpdated)) {
												
						EE_DataUpdateHandle(0, hData);

						unsigned int nSamplesTaken=0;
						EE_DataGetNumberOfSample(hData,&nSamplesTaken);
		
						std::cout << "Updated " << nSamplesTaken << std::endl;
						

						if (nSamplesTaken != 0  ) {

							double* data = new double[nSamplesTaken];
							for (int sampleIdx=0 ; sampleIdx<(int)nSamplesTaken ; ++ sampleIdx) {
								for (int i = 0 ; i<sizeof(targetChannelList)/sizeof(EE_DataChannel_t) ; i++) {

									EE_DataGet(hData, targetChannelList[i], data, nSamplesTaken);
									ofs << data[sampleIdx] << ",";
								}	
								ofs << std::endl;
							}
							delete[] data;							
						}
						
						float affEngegement = ES_AffectivGetEngagementBoredomScore(eState);
						float affFrus = ES_AffectivGetFrustrationScore(eState);
						float affMed = ES_AffectivGetMeditationScore(eState);
						float affExcitement = ES_AffectivGetExcitementShortTermScore(eState);
						printf("Engagement: %f, Frustration: %f, ...\n",affEngegement,affFrus);
						ofs2 <<affEngegement<<","<<	affFrus<<","<<affMed<<","<<affExcitement<<","<<std::endl;

			}
			}
			Sleep(100);
		}

		ofs.close();
		ofs2.close();
		EE_DataFree(hData);

	}
	catch (const std::exception& e) {
		std::cerr << e.what() << std::endl;
		std::cout << "Press any key to exit..." << std::endl;
		getchar();
	}
	
}