Esempio n. 1
0
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;
}
//--------------------------------------------------------------
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();
	}
	
}