EmoEngine() { if (EE_EngineConnect() != EDK_OK) { throw std::runtime_error("Emotiv Engine start up failed."); } _stateCode = EDK_OK; _event = EE_EmoEngineEventCreate(); _state = EE_EmoStateCreate(); }
EpocHandler::EpocHandler() { userID = 0; secs = 1; datarate = 0; readytocollect = false; state = 0; connected = false; updated = false; eEvent = EE_EmoEngineEventCreate(); eState = EE_EmoStateCreate(); }
BOOL EmotivRead::EB_OpenDevice(int usbportnumber) { m_nErrorCode = EE_EngineConnect(); if(m_nErrorCode!=EDK_OK) return FALSE; m_neEvent = EE_EmoEngineEventCreate(); m_neState = EE_EmoStateCreate(); m_hData = EE_DataCreate(); m_nState = EE_EngineGetNextEvent(m_neEvent); return TRUE; }
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 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); }
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(); } }