基于mindwave脑电波进行疲劳检测算法的设计(2)

  上文讲到的是保证硬件的接通。接下来是用C语言在它提供的API接口进行连接。

  在网盘中下载MindSet Development Tools这个开发包。这个目录下MindSet Development Tools\ThinkGear Communications Driver\win32有一些文件是要用到的。

  我采用的MinGW这个库。这个库的好处是绿色版,一般情况下在没有开发环境的电脑,都可以运行。(我们都知道一个VS安装要一个多小时)。MinGW各个开发库基本没有版本要求。好了我们先在MinGW这个目录下创建一个ThinkGear目录,这个目录跟bin目录是同级的。然后进入ThinkGear目录复制上面提到的win32目录下的thinkgear_testapp.c thinkgear.h和thinkgear.dll,然后还要复制thinkgear.dll文件到MinGW\bin目录用作动态库。现在文件就都准备好了,现在我们先写一个Makefile。

1 LDFLAGS= -L ..\bin
2 LIBS= -lthinkgear
3 main:thinkgear.h thinkgear_testapp.c
4     gcc $(LDFLAGS) $(LIBS) thinkgear_testapp.c -o main.exe

  然后命令行执行make , main.exe执行程序

  现在目录下的文件有

  \ThinkGear

         main.exe  makefile thinkgear.dll  thinkgear.h  thinkgear_testapp.c

  下面将对源代码进行部分注解

  1 #include <stdlib.h>
  2 #include <stdio.h>
  3 
  4 #include "thinkgear.h"
  5 
  6 void wait() 
  7 {
  8     printf( "\n" );
  9     printf( "Press the ENTER key...\n" );
 10     fflush( stdout );
 11     getc( stdin );
 12 }
 13 
 14 /**
 15  * Program which prints ThinkGear EEG_POWERS values to stdout.
 16  */
 17 int main( void ) {
 18 
 19     char *comPortName = NULL;
 20     int   dllVersion = 0; //动态库版本
 21     int   connectionId = 0;//连接ID
 22     int   packetsRead = 0; //包数量
 23     int   errCode = 0; //错误码
 24 
 25     /* Print driver version number */
 26     dllVersion = TG_GetDriverVersion();
 27     printf( "ThinkGear DLL version: %d\n", dllVersion ); //打印当前动态库版本
 28 
 29     /* Get a connection ID handle to ThinkGear */
 30     connectionId = TG_GetNewConnectionId(); //获取连接ID 跟文件描述符类似的功能
 31     if( connectionId < 0 ) {
 32         fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n", 
 33                 connectionId );
 34         wait();
 35         exit( EXIT_FAILURE );
 36     }
 37 
 38     /* Set/open stream (raw bytes) log file for connection */
 39     // 原始数据日志 用于高级分析
 40     errCode = TG_SetStreamLog( connectionId, "streamLog.txt" );
 41     if( errCode < 0 ) {
 42         fprintf( stderr, "ERROR: TG_SetStreamLog() returned %d.\n", errCode );
 43         wait();
 44         exit( EXIT_FAILURE );
 45     }
 46 
 47     /* Set/open data (ThinkGear values) log file for connection */
 48     //ThinkGear数据日志 一种动态库已经封装好的数据 
 49     errCode = TG_SetDataLog( connectionId, "dataLog.txt" );
 50     if( errCode < 0 ) {
 51         fprintf( stderr, "ERROR: TG_SetDataLog() returned %d.\n", errCode );
 52         wait();
 53         exit( EXIT_FAILURE );
 54     }
 55 
 56     /* Attempt to connect the connection ID handle to serial port "COM5" */
 57     comPortName = "\\\\.\\COM3"; //这个是要连接的COM端口 这个在那个计算机管理工具里面看 里面的COM端口多少这里就多少
 58     errCode = TG_Connect( connectionId, 
 59             comPortName, 
 60             TG_BAUD_9600, 
 61             TG_STREAM_PACKETS );
 62     if( errCode < 0 ) {
 63         fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
 64         wait();
 65         exit( EXIT_FAILURE );
 66     }
 67 
 68     if( 0==TG_EnableBlinkDetection(connectionId,1)) //启动眨眼检测
 69     {
 70         printf("Success enable blink\n");
 71     }
 72 
 73     /* 不停的读取数据 */
 74     packetsRead = 0;
 75     while(1) 
 76     {
 77         /* 读一个报文 */
 78         errCode = TG_ReadPackets( connectionId, 1 );
 79         /* 如果这个报文读取成功 */
 80         if( errCode == 1 )
 81         {
 82             int att, det, sig;
 83             if(( errCode = TG_GetValueStatus(connectionId, TG_DATA_ATTENTION)) != 0 ) 
 84             {
 85                 att = TG_GetValue(connectionId, TG_DATA_ATTENTION) ;
 86                 det = TG_GetValue(connectionId, TG_DATA_MEDITATION);
 87                 sig = TG_GetValue(connectionId, TG_DATA_POOR_SIGNAL);
 88                 printf("attentin = %d, meditation=%d, signal=%d\n", att, det, sig);
 89             }
 90         } 
 91         else
 92         {
 93             printf("ReadPackets:errcode=%d\n", errCode);
 94         }
 95     } 
 96      //关闭连接
 97     TG_FreeConnection( connectionId );
 98     wait();
 99     return( EXIT_SUCCESS );
100 }

先编译连接,运行一下这个程序,至此软硬件的环境都弄好了。剩下的就是算法设计了。

 

========================我是分割线=========================

  接下来的一段时间我都会更新关于这个基于脑电波疲劳检测的博客,在看博客的你,如果有更好的检测算法,可以在评论下给出一些想法。并能给出一定的实现思路。我现在的想法是在它提供的那几个波段(上面提到的)中对强度进行判断。然后还有一个就是统计几个波段,然后计算对应的方差。还有就是通过里面有一个判断眨眼的函数来判断眨眼。我们可以根据眨眼的时间差来判断。不过不同的人眨眼的时间是不同的。所以还有让系统先预处理一下,保存当前这个人的眨眼时间差。现在我就想到了这些。具体怎么做还有一段时间。

===========================================================

 

参考资料:

http://www.neurosky.com.cn/news_ny.aspx?NewsID=58

posted @ 2014-04-21 11:31  无脑仔的小明  阅读(2500)  评论(0编辑  收藏  举报