駆動信号発生器用プログラム

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 int mode = 1;                               //0=電気信号発生器 1=駆動信号発生器
00004 int revision = 1;
00005 
00006 //Serial Port Setting
00007 Serial pc(p13, p14); // シリアルポートのインスタンス
00008 
00009 //PWM Port Setting
00010 PwmOut signal_1(p23);
00011 PwmOut signal_2(p22);
00012 PwmOut signal_3(p21);
00013 
00014 //Ticker
00015 Ticker timer;
00016 
00017 //Digital Out
00018 DigitalOut ledch1(LED1);
00019 
00020 //cos Wave
00021 float coswave[256];
00022 
00023 //status flug
00024 int   status1 =0;                                   //suatus1 : 0=波形出力停止中, 1=波形出力中
00025 
00026 //User set Wave Paramater 初期値
00027 int   sample_dt             = 100;                  //microsec 10kHz
00028 float hb_carr_freq          = 8.7;                  //Hz       
00029 float hb_mod_freq           = 1;                    //Hz
00030 float hb_carr_level         = 0.06;                //min=0 max=0.5 0.5以上ではMOD 50%以上で振幅が飽和する
00031 float hb_mod_ratio          = 60;                   //0-100 %
00032 float resp_freq             = 0.3;                  //Hz
00033 float resp_level            = 0.23;                 //min=0 max=1
00034 float snore_freq            = 150;                  //Hz
00035 float snore_level           = 0.045;                 //min=0 max=1 
00036 int snore_oncycle           = 10000;                //On Time = snore_oncycle * sample_dt, snore_oncycle < snore_allcycle;
00037 int snore_allcycle          = 33333;                //OFF Time = (allcycle - oncycle) * sample_dt
00038 int snore_onoff             = 1;                    //0: ALL Time ON, 1:On Time only
00039 
00040 //Set wave paramater
00041 //HB_carr
00042 float hb_carr_period        = 1000000*1/hb_carr_freq;  //microSec  
00043 float hb_carr_n             = 0;
00044 float hb_carr_n_sample      = 0;                    //The maximum number of sampling points in the signal
00045 float hb_carr_tend          = 0;                    //The remainder of sampling points in the signal
00046 float hb_carr_tstart        = 0;                    //start offset of sampling time
00047 float hb_carr_t_samplepoint = 0;        
00048 float hb_carr_table_dt      = hb_carr_period/256;
00049 float hb_carr_n_samplepoint = 0;
00050 float hb_carr_v_samplepoint = 0;
00051 
00052 //HB_mod
00053 float hb_mod_period        = 1000000*1/hb_mod_freq;
00054 float hb_mod_n             = 0;
00055 float hb_mod_n_sample      = 0;                    //The maximum number of sampling points in the signal
00056 float hb_mod_tend          = 0;                    //The remainder of sampling points in the signal
00057 float hb_mod_tstart        = 0;                    //start offset of sampling time
00058 float hb_mod_t_samplepoint = 0;        
00059 float hb_mod_table_dt      = hb_mod_period/256;
00060 float hb_mod_n_samplepoint = 0;
00061 float hb_mod_v_samplepoint = 0;
00062 
00063 float hb_mod_ra             = hb_mod_ratio * 0.01;
00064 float hb_signal             = 0;
00065 float mod_signal            = 0;
00066 
00067 //RESP
00068 float resp_period           = 1000000*1/resp_freq;  //microSec      
00069 float resp_n                = 0;
00070 float resp_n_sample         = 0;                    //The maximum number of sampling points in the signal
00071 float resp_tend             = 0;                    //The remainder of sampling points in the signal
00072 float resp_tstart           = 0;                    //start offset of sampling time
00073 float resp_t_samplepoint    = 0;           
00074 float resp_table_dt         = resp_period/256;                                            
00075 float resp_n_samplepoint    = 0;
00076 float resp_v_samplepoint    = 0;
00077 float resp_signal           = 0;
00078 
00079 //SNORE Paramater
00080 float snore_period          = 1000000*1/snore_freq;  //microSec
00081 float snore_n               = 0;                    
00082 float snore_n_sample        = 0;                    //The maximum number of sampling points in the signal
00083 float snore_tend            = 0;                    //The remainder of sampling points in the signal
00084 float snore_tstart          = 0;                    //start offset of sampling time
00085 float snore_t_samplepoint   = 0;        
00086 float snore_table_dt        = snore_period/256;     //2016/12/12 255->256
00087 float snore_n_samplepoint   = 0;
00088 float snore_v_samplepoint   = 0;
00089 int   snore_cycle           = 0;
00090 float snore_signal          = 0;
00091 
00092 int   pwidth_1 = 0;
00093 int   pwidth_2 = 0;
00094 int   pwidth_3 = 0;
00095 
00096 // Debug use only   ----
00097 int dbcount = 0;
00098 
00099 // End of debug use ----
00100 
00101 //Signal Culcurate
00102 void signal_culc(){
00103        
00104 //HB section
00105 //HB_carr
00106         hb_carr_n_sample = (hb_carr_period - hb_carr_tstart) / sample_dt;
00107         hb_carr_tend = fmod((hb_carr_period - hb_carr_tstart) , sample_dt);  
00108         if(hb_carr_tend == 0){
00109             hb_carr_n_sample = hb_carr_n_sample-1;
00110             }   
00111         hb_carr_t_samplepoint = hb_carr_tstart + sample_dt * hb_carr_n; //time of sampling point
00112         hb_carr_n_samplepoint = hb_carr_t_samplepoint / hb_carr_table_dt;
00113         if(hb_carr_n_samplepoint >= 256){
00114             hb_carr_n_samplepoint = 0;
00115             }    
00116         hb_carr_v_samplepoint = coswave[(int)hb_carr_n_samplepoint];
00117         if(hb_carr_n > (int)hb_carr_n_sample) {
00118             hb_carr_n =0;
00119             hb_carr_tstart = sample_dt - hb_carr_tend;
00120             }          
00121         else {
00122             hb_carr_n++;           
00123             }
00124 //HB_mod
00125 //A(1+macosωmt)*cosωct     Acosωct = fcarr(t),ma = modulation ratio,
00126         hb_mod_n_sample = (hb_mod_period - hb_mod_tstart) / sample_dt;
00127         hb_mod_tend = fmod((hb_mod_period - hb_mod_tstart) , sample_dt); 
00128         if(hb_mod_tend == 0){
00129             hb_mod_n_sample = hb_mod_n_sample-1;
00130             }    
00131         hb_mod_t_samplepoint = hb_mod_tstart + sample_dt * hb_mod_n; //time of sampling point    
00132         hb_mod_n_samplepoint = hb_mod_t_samplepoint / hb_mod_table_dt;
00133         if(hb_mod_n_samplepoint >= 256){
00134             hb_mod_n_samplepoint = 0;
00135             }
00136         hb_mod_v_samplepoint = hb_mod_ra*(coswave[(int)hb_mod_n_samplepoint]);  
00137         if(hb_mod_n > (int)hb_mod_n_sample) {
00138             hb_mod_n =0;
00139             hb_mod_tstart = sample_dt - hb_mod_tend;
00140             }
00141         else {
00142             hb_mod_n++;           
00143             }
00144         
00145 //HB_AM MOD 
00146         
00147         hb_signal = 0.5*(hb_carr_level*(1+hb_mod_v_samplepoint)*hb_carr_v_samplepoint)+0.5;//0-1 -> 0V-3.3V
00148  
00149 //debug part      
00150 /*
00151         if(dbcount>=100){
00152             pc.printf("%f, %f, %f\n",hb_signal,mod_signal,hb_mod_v_samplepoint);
00153             dbcount = 0;
00154             }
00155         dbcount++;
00156 */     
00157 //end debug part
00158         
00159 //RESP section
00160         resp_n_sample = (resp_period - resp_tstart) / sample_dt;
00161         resp_tend = fmod((resp_period - resp_tstart) , sample_dt);
00162         if(resp_tend == 0){
00163             resp_n_sample = resp_n_sample-1;
00164             }
00165         resp_t_samplepoint = resp_tstart + sample_dt * resp_n; //time of sampling point
00166         resp_n_samplepoint = resp_t_samplepoint / resp_table_dt;
00167         if(resp_n_samplepoint >= 256){
00168             resp_n_samplepoint = 0;
00169             }
00170         resp_v_samplepoint = resp_level*(coswave[(int)resp_n_samplepoint]);
00171         resp_signal = 0.5*resp_v_samplepoint+0.5;     //0-1 -> 0V-3.3V
00172         if(resp_n > (int)resp_n_sample) {
00173             resp_n =0;
00174             resp_tstart = sample_dt - resp_tend;
00175             }      
00176         else {
00177             resp_n++;         
00178             }
00179             
00180 //SNORE section
00181         snore_n_sample = (snore_period - snore_tstart) / sample_dt;       //
00182         snore_tend = fmod((snore_period - snore_tstart) , sample_dt);
00183         if(snore_tend == 0){
00184             snore_n_sample = snore_n_sample-1;
00185             }
00186         else {
00187             }
00188         snore_t_samplepoint = snore_tstart + sample_dt * snore_n; //time of sampling point
00189         snore_n_samplepoint = snore_t_samplepoint / snore_table_dt;
00190         if(snore_n_samplepoint >= 256){
00191             snore_n_samplepoint = 0;
00192             }
00193         snore_v_samplepoint = snore_level*(coswave[(int)snore_n_samplepoint]);
00194         if(snore_n > (int)snore_n_sample) {
00195             snore_n =0;             //2016/12/12 1->0
00196             snore_tstart = sample_dt - snore_tend;
00197             }
00198         else {
00199             snore_n++;          
00200             } 
00201         if(snore_onoff==0){
00202             }
00203         else if(snore_onoff==1){
00204             if (snore_cycle <= snore_oncycle) {
00205                 snore_cycle++;
00206                 }
00207             else if (snore_cycle>snore_oncycle && snore_cycle <= snore_allcycle){
00208                 snore_v_samplepoint = 0;
00209                 snore_cycle++;
00210                 }
00211             else {
00212                 snore_cycle = 0;
00213                 }
00214             }    
00215         snore_signal = 0.5*snore_v_samplepoint+0.5;   //0-1 -> 0V-3.3V
00216             
00217 //PWM Output Pulse width
00218         pwidth_1 = hb_signal * sample_dt;
00219         pwidth_2 = resp_signal * sample_dt;
00220         pwidth_3 = snore_signal * sample_dt;
00221         
00222         signal_1.pulsewidth_us(pwidth_1);
00223         signal_2.pulsewidth_us(pwidth_2);
00224         signal_3.pulsewidth_us(pwidth_3); 
00225 }
00226 
00227 //チッカー割り込みハンドラ
00228 void attime(){
00229     if(status1==1){
00230         ledch1=0;
00231         signal_culc();
00232         }
00233     else{
00234         ledch1=!ledch1;
00235         }
00236     }
00237 
00238 //Serial 受信割り込みハンドラ
00239 void isrRx() {
00240         
00241     /*  UART受信コマンド
00242     start:      波形出力スタート
00243     stop:       波形出力ストップ
00244     St:         割り込み周期[μsec]
00245     carrf:      脈波AM変調波搬送波周波数[Hz]
00246     modf:       脈波AM変調波変調周波数[Hz]
00247     carrl:      脈波振幅
00248     modr:       脈波変調波変調率[%]
00249     respf:      呼吸周波数[Hz]
00250     respl:      呼吸振幅
00251     snorf:      体動・イビキ周波数[Hz]
00252     snorl:      体動・イビキ振幅
00253     snon:       体動・イビキONサイクル      
00254     snall:      体動・イビキALLサイクル 
00255     sncn:       体動・イビキ断続制御       
00256     */
00257     
00258     char scan[10];
00259     float para;
00260 
00261     pc.scanf("%s",scan);                            // 文字列受信バッファより取り出し
00262 //    pc.printf("read = %s\n",scan);
00263   
00264     if(strcmp(scan,"start")==0){
00265         pc.printf("go\n");
00266         status1=1;                                  //波形出力状態へ遷移
00267         }
00268     else if(strcmp(scan,"stop")==0){
00269         pc.printf("end\n");
00270         status1=0;                                  //波形出力停止へ遷移
00271         }
00272     else if(strcmp(scan,"dt")==0){    
00273         pc.scanf("%f",&para); 
00274         sample_dt       =   para;
00275         pc.printf("dt = %f\n",sample_dt);
00276         } 
00277     else if(strcmp(scan,"carrf")==0){    
00278         pc.scanf("%f",&para);
00279         hb_carr_freq          = para;
00280         hb_carr_period        = 1000000*1/hb_carr_freq;  //microSec
00281         hb_carr_n             = 0;
00282         hb_carr_n_sample      = 0;                  //The maximum number of sampling points in the signal
00283         hb_carr_tend          = 0;                  //The remainder of sampling points in the signal
00284         hb_carr_tstart        = 0;                  //start offset of sampling time
00285         hb_carr_t_samplepoint = 0;        
00286         hb_carr_table_dt      = hb_carr_period/256;
00287         hb_carr_n_samplepoint = 0;
00288         hb_carr_v_samplepoint = 0;
00289         pc.printf("carrf = %f\n",hb_carr_freq);
00290         hb_mod_period        = 1000000*1/hb_mod_freq;
00291         hb_mod_n             = 0;
00292         hb_mod_n_sample      = 0;                  //The maximum number of sampling points in the signal
00293         hb_mod_tend          = 0;                  //The remainder of sampling points in the signal
00294         hb_mod_tstart        = 0;                  //start offset of sampling time
00295         hb_mod_t_samplepoint = 0;        
00296         hb_mod_table_dt      = hb_mod_period/256;
00297         hb_mod_n_samplepoint = 0;
00298         hb_mod_v_samplepoint = 0;
00299         }
00300     else if(strcmp(scan,"modf")==0){    
00301         pc.scanf("%f",&para);  
00302         hb_mod_freq           = para;
00303         hb_mod_period        = 1000000*1/hb_mod_freq;
00304         hb_mod_n             = 0;
00305         hb_mod_n_sample      = 0;                  //The maximum number of sampling points in the signal
00306         hb_mod_tend          = 0;                  //The remainder of sampling points in the signal
00307         hb_mod_tstart        = 0;                  //start offset of sampling time
00308         hb_mod_t_samplepoint = 0;        
00309         hb_mod_table_dt      = hb_mod_period/256;
00310         hb_mod_n_samplepoint = 0;
00311         hb_mod_v_samplepoint = 0;
00312         pc.printf("modf = %f\n",hb_mod_freq);
00313         hb_carr_n             = 0;
00314         hb_carr_n_sample      = 0;                  //The maximum number of sampling points in the signal
00315         hb_carr_tend          = 0;                  //The remainder of sampling points in the signal
00316         hb_carr_tstart        = 0;                  //start offset of sampling time
00317         hb_carr_t_samplepoint = 0;        
00318         hb_carr_table_dt      = hb_carr_period/256;
00319         hb_carr_n_samplepoint = 0;
00320         hb_carr_v_samplepoint = 0;
00321         }
00322     else if(strcmp(scan,"carrl")==0){    
00323         pc.scanf("%f",&para);  
00324         hb_carr_level         = para;
00325         pc.printf("carrl = %f\n",hb_carr_level);
00326         }    
00327     else if(strcmp(scan,"modr")==0){    
00328         pc.scanf("%f",&para); 
00329         hb_mod_ratio          = para;
00330         hb_mod_ra             = hb_mod_ratio *0.01;
00331         pc.printf("modr = %f\n",hb_mod_ra);
00332         } 
00333     else if(strcmp(scan,"respf")==0){    
00334         pc.scanf("%f",&para); 
00335         resp_freq             = para; 
00336         resp_period           = 1000000*1/resp_freq;//microSec      
00337         resp_n                = 0;
00338         resp_n_sample         = 0;                  //The maximum number of sampling points in the signal
00339         resp_tend             = 0;                  //The remainder of sampling points in the signal
00340         resp_tstart           = 0;                  //start offset of sampling time
00341         resp_t_samplepoint    = 0;           
00342         resp_table_dt         = resp_period/256;                                            
00343         resp_n_samplepoint    = 0;
00344         resp_v_samplepoint    = 0;
00345         pc.printf("respf = %f\n",resp_freq);  
00346         } 
00347     else if(strcmp(scan,"respl")==0){    
00348         pc.scanf("%f",&para); 
00349         resp_level            = para;
00350         pc.printf("respl = %f\n",resp_level); 
00351         } 
00352     else if(strcmp(scan,"snorf")==0){    
00353         pc.scanf("%f",&para);   
00354         snore_freq            = para; 
00355         snore_period          = 1000000*1/snore_freq;  //microSec
00356         snore_n               = 0;                  //2016/12/12 1->0
00357         snore_n_sample        = 0;                  //The maximum number of sampling points in the signal
00358         snore_tend            = 0;                  //The remainder of sampling points in the signal
00359         snore_tstart          = 0;                  //start offset of sampling time
00360         snore_t_samplepoint   = 0;        
00361         snore_table_dt        = snore_period/256;
00362         snore_n_samplepoint   = 0;
00363         snore_v_samplepoint   = 0;  
00364         snore_cycle           = 0;
00365         pc.printf("snorf = %f\n",snore_freq);     
00366     } 
00367     else if(strcmp(scan,"snorl")==0){    
00368         pc.scanf("%f",&para); 
00369         snore_level           = para;
00370         pc.printf("snorl = %f\n",snore_level); 
00371     } 
00372     else if(strcmp(scan,"snon")==0){    
00373         pc.scanf("%f",&para); 
00374         snore_oncycle         = (int)para; 
00375         pc.printf("snon = %d\n",snore_oncycle);     
00376     } 
00377     else if(strcmp(scan,"snall")==0){    
00378         pc.scanf("%f",&para); 
00379         snore_allcycle        = (int)para; 
00380         pc.printf("snall = %d\n",snore_allcycle);   
00381     } 
00382     else if(strcmp(scan,"sncn")==0){    
00383         pc.scanf("%f",&para); 
00384         snore_onoff           = (int)para;
00385         pc.printf("sncn = %d\n",snore_onoff);  
00386     } 
00387     else if(strcmp(scan,"ack")==0){
00388         pc.printf("carrf = %f\n",hb_carr_freq);
00389         pc.printf("carrl = %f\n",hb_carr_level);
00390         pc.printf("modf = %f\n",hb_mod_freq);
00391         pc.printf("modr = %f\n",hb_mod_ra);
00392         pc.printf("respf = %f\n",resp_freq);  
00393         pc.printf("respl = %f\n",resp_level); 
00394         pc.printf("snorf = %f\n",snore_freq);     
00395         pc.printf("snorl = %f\n",snore_level); 
00396         pc.printf("snon = %d\n",snore_oncycle);     
00397         pc.printf("snall = %d\n",snore_allcycle);   
00398         pc.printf("sncn = %d\n",snore_onoff); 
00399     }
00400     else if(strcmp(scan,"mr")==0){
00401         pc.printf("mode = %d\n",mode);
00402         pc.printf("revision = %d\n",revision);
00403     }
00404     else {
00405         pc.printf("Command Error\n");
00406     }   
00407 }
00408 
00409 int main() {
00410     pc.printf("Start Up\n");
00411     
00412 //Make cos wave
00413     int i;
00414     for(i=0;i<=255;i++){                         
00415         coswave[i]=cos(2.0*3.1415*i/256);           //2017/1/5  0.5*(cos(2.0*3.1415*i/256));                         
00416     }
00417     i = 0;
00418 
00419     signal_1.period_us(sample_dt);                  //Pulse_cycle = 100usec = 10kHz
00420     signal_2.period_us(sample_dt);
00421     signal_3.period_us(sample_dt);
00422     
00423     int j;
00424     j = sample_dt;
00425     timer.attach_us(&attime,j);                     //⇒UART割り込み”START”に移動     
00426     
00427     pc.attach(isrRx, Serial::RxIrq);                // 割込みハンドラ登録
00428     NVIC_SetPriority(UART1_IRQn,1);
00429 
00430     while(1) {                  
00431         }
00432 }