Skelton of EMG input method program using timer interrupt and thread.
Dependencies: QEI mbed-rtos mbed
Fork of DCmotor by
main.cpp
00001 // Skelton of EMG input method program using timer interrupt and thread. 00002 // ver. 121130a by Kosaka lab. 00003 #include "mbed.h" 00004 #include "rtos.h" 00005 #define PI 3.14159265358979 // def. of PI 00006 /*********** User setting for control parameters (begin) ***************/ 00007 AnalogIn emg(p20); // *3.3 [V], Volt of emg from detection cirquit 00008 #define N_COUNT 5000 // keep N_COUNT data to identify japanese chracter. 00009 #define TS 0.0001 // [s], TS, sampling time[s] to detect emg from AD. 00010 #define TMAX 5 // [s], experiment starts from 0[s] to TMAX[s] 00011 /*********** User setting for control parameters (end) ***************/ 00012 00013 Serial pc(USBTX, USBRX); // Display on tera term in PC 00014 LocalFileSystem local("local"); // save data to mbed USB disk drive in PC 00015 //Semaphore semaphore1(1); // wait and release to protect memories and so on 00016 //Mutex stdio_mutex; // wait and release to protect memories and so on 00017 Ticker timer_interrupt; // Timer interrupt using TIMER3, TS<0.001 is OK. Priority is higher than rtosTimer. 00018 00019 //extern "C" void mbed_reset(); // if called, mbed is resset. 00020 00021 float _emg_data[N_COUNT];// emg raw data 00022 unsigned long _count=0; // sampling number for emg detection. 00023 unsigned long _count2=0; // = _count/N_COUNT 00024 00025 DigitalOut led1(LED1); // for debug 00026 DigitalOut led2(LED2); // for debug 00027 DigitalOut led3(LED3); // for debug 00028 00029 float _char=0; //-------- make japanese character from emg 00030 FILE *fp; // save data to PC 00031 unsigned char _f_req_slow=0; // flag requesting slow() 00032 unsigned char _f_req_slowest=0; // flag requesting slowest() 00033 00034 00035 void disp2PC(){ //-------- display japanese character to tera term on PC 00036 pc.printf(" d %f\r\n",_char); 00037 } 00038 00039 void discriminateEMG(){ //-------- discriminate EMG to make japanese character 00040 int i; 00041 float x; 00042 00043 x = 0; 00044 for( i=0;i<N_COUNT;i++){ 00045 x = x + _emg_data[i]; 00046 } 00047 _char = x; // _char = emg_data[0] + emg_data[1] + emg_data[2] + ... 00048 pc.printf(" s\r\n"); 00049 } 00050 00051 00052 //---------------- from here, timer interrupt and threads --------------- 00053 00054 void slowest(void const *argument) { // thread priority: Low 00055 while(true){ 00056 if( _f_req_slowest == 1 ){ // if slowest() is requested. 00057 led3 = 1; // check calculate time 00058 // function(); 00059 _f_req_slowest = 0; // release to request slowest() 00060 Thread::wait(100); 00061 led3 = 0; // check calculate time 00062 } 00063 } 00064 } 00065 00066 void slow(void const *argument) { // thread priority: below normal 00067 while(true){ 00068 if( _f_req_slow == 1 ){ // if slow() is requested. 00069 led2 = 1; // check calculate time 00070 discriminateEMG(); //-------- discriminate EMG to make japanese character 00071 disp2PC(); //-------- display japanese character to tera term on PC 00072 _f_req_slow = 0; // release to request slow() 00073 _f_req_slowest = 1; // request slowest() 00074 led2 = 0; // check calculate time 00075 } 00076 Thread::wait(1); // wait 1ms to give time with slowest() 00077 } 00078 } 00079 00080 void fastest() { // ticker using TIMER3 interrupt 00081 led1 = 1; // check calculate time 00082 // if( led1==0 ){ led1=1;}else{ led1=0;}// for debug 00083 _emg_data[_count] = emg; 00084 _count = _count + 1; 00085 if( _count==N_COUNT ){ 00086 _count = 0; 00087 _count2 += 1; 00088 _f_req_slow = 1; // request slow() 00089 } 00090 led1 = 0; // check calculate time 00091 } 00092 00093 int main() { 00094 Thread threadSlow(slow,NULL,osPriorityBelowNormal); // call thread slow() 00095 Thread threadSlowest(slowest,NULL,osPriorityLow); // call thread slowest() 00096 00097 pc.printf("Start!!\r\n"); 00098 // if ( NULL == (fp = fopen( "/local/data.csv", "w" )) ){ error( "" );} // open mbed USB drive 00099 timer_interrupt.attach(&fastest, TS ); // start timer interrupt: call fastest() on each TS[s]. 00100 while( _count2 < TMAX/TS/N_COUNT ){ 00101 Thread::wait(1000); // [ms], wait 00102 } 00103 timer_interrupt.detach(); // stop timer interrupt fastest 00104 // fclose( fp ); // release mbed USB drive 00105 pc.printf("Completed!!\r\n\r\n"); 00106 } 00107 // osStatus set_priority(osPriority osPriorityBelowNormal ); 00108 // Priority of Thread (RtosTimer has no priority?) 00109 // osPriorityIdle = -3, ///< priority: idle (lowest)--> then, mbed ERROR!! 00110 // osPriorityLow = -2, ///< priority: low 00111 // osPriorityBelowNormal = -1, ///< priority: below normal 00112 // osPriorityNormal = 0, ///< priority: normal (default) 00113 // osPriorityAboveNormal = +1, ///< priority: above normal 00114 // osPriorityHigh = +2, ///< priority: high 00115 // osPriorityRealtime = +3, ///< priority: realtime (highest) 00116 // osPriorityError = 0x84 ///< system cannot determine priority or thread has illegal priority 00117 00118
Generated on Mon Jul 18 2022 20:28:51 by 1.7.2