basyo matsuo / Mbed 2 deprecated GPS_Test05

Dependencies:   HMC5883L SDFileSystem TextOLED mbed

Fork of 2bk0123_GPS_Test05 by Atsumi Toda

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //GPS GT-720F Test05
00002 
00003 
00004 #include "mbed.h"
00005 #include "TextOLED.h"
00006 #include "SDFileSystem.h"
00007 #include "stdio.h"
00008 
00009 
00010 #define ON 1
00011 #define OFF 0
00012 
00013 
00014 AnalogIn alt(p20);        //高度計読み取り
00015 InterruptIn speedcount(p17); //機速用のフォトインタラプタ読み取り
00016 InterruptIn rpmcount(p21);  //回転数用のフォトインタラプタ読み取り
00017 InterruptIn Log_Switch(p26);//ログスイッチ読み取り
00018 
00019 
00020 Serial pc(USBTX, USBRX);
00021 Serial control_com(p28, p27);//制御基板との通信用
00022 Serial android_com(p9, p10);               //Androidとの通信用txrx!!
00023 
00024 Serial gps(p13,p14);// tx, rx
00025 
00026 TextOLED pilot_oled(p11, p12 ,p30 ,p29 ,p15, p16); // RS, E, DB4, DB5, DB6, DB7  有機EL!!
00027 SDFileSystem sd(p5, p6, p7, p8, "sd");//SDpinの番号 3,7,5,2 //1pin--Vout,4pin--Vout,6pin--GND,8pin--Vout
00028 
00029 
00030 DigitalOut led1(LED1);
00031 DigitalOut led2(LED2);
00032 DigitalOut led3(LED3);
00033 DigitalOut led4(LED4);
00034 
00035 
00036 //音システム用
00037 DigitalOut soundsystem1(p19);
00038 DigitalOut soundsystem2(p24);
00039 DigitalOut soundsystem3(p25);
00040 
00041 DigitalOut option(p22);//toGPS and jailo
00042 
00043 
00044 //タイマー割り込み宣言
00045 Ticker ticker;//updateのタイマー割り込み
00046 Timer t,t1;
00047 
00048 int speedcounter = 0, rpmcounter = 0, min = 0, sec = 0, i;
00049 int oldspeed = 0, oldrpm = 0;
00050 int log_count = 0;
00051 int SD_count = 0;
00052 int up2=0,up1=0,down1=0,down2=0,E_neut=0;
00053 int left1=0,left2=0,right1=0,right2=0,R_neut=0;
00054 int send_compass=0;
00055 
00056 
00057 double time_s = 0,time_ms=0,time1=0,time2=0, speed = 0, rpm = 0, height = 0,compass = 0;
00058 
00059 char ele;
00060 char rud;
00061 char e_trim='e',r_trim='r';
00062 char compass_array[4] = "000";
00063 char data;
00064 char send_buff1[24]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
00065 
00066 
00067 unsigned char Log_flag = OFF;
00068 unsigned char SD_flag = OFF;
00069       
00070       
00071 
00072 //プロトタイプ宣言
00073 //void init();
00074 //void myled(int a,int b,int c,int d);
00075 void SD_log();
00076 void OLEDdisplay_print();
00077 void spd_f();
00078 void rpm_f();
00079 void spd_c();
00080 void rpm_c();
00081 void control_communication();
00082 void android_communication();
00083 void update();
00084 void altitude_f();
00085 void compass_f();
00086 void Log_Switch_f();
00087 void CGRAM_1();
00088 void CGRAM_2();
00089 void CGRAM_3();
00090 void CGRAM_4();
00091 void CGRAM_5();
00092 
00093 
00094 //=======================================================================//
00095 
00096 
00097 
00098 LocalFileSystem local("local");
00099 
00100 
00101 FILE *fp;
00102 
00103 int main() {
00104 
00105       char c;
00106       int i3,rlock,stn;
00107       char gps_data[256];
00108       char ns,ew;
00109       float time,hokui,tokei;
00110       float g_hokui,g_tokei;
00111       float d_hokui,m_hokui,d_tokei,m_tokei;
00112  
00113       int h_time=0,m_time=0,s_time=0;
00114       
00115  //mkdir("/sd/gps_data", 0777);//まずsdのフォルダをつくる。
00116  
00117       gps.baud(9600);
00118 
00119   pc.printf("Start TBT!!\n");
00120 
00121      led1=1;
00122     led2=1;
00123     led3=1;
00124     led4=1;
00125     wait(0.01);
00126     pilot_oled.cls();
00127     wait(0.001);
00128     CGRAM_1();
00129     CGRAM_2();
00130     CGRAM_3();
00131     CGRAM_4();
00132     CGRAM_5();
00133 //    myled(1,1,0,0);
00134     wait(2);
00135    mkdir("/sd/mydir", 0777);
00136     android_com.baud(115200);   
00137     android_com.format(8,Serial::None,1); 
00138     
00139     control_com.attach(control_communication, Serial::RxIrq);
00140     control_com.baud(115200); 
00141 
00142     t.start();
00143     t1.start();
00144 
00145     Log_Switch.rise(&Log_Switch_f);
00146     speedcount.rise(&spd_c);
00147     rpmcount.rise(&rpm_c);
00148     ticker.attach(update,1);//1s更新
00149     //time1 = t1.read_ms();
00150     
00151 
00152 
00153     while (1) {
00154       
00155             
00156 //        pc.printf("There is TBT!!\n");
00157         led3=!led3;
00158         if(min >=30) { //30分経過時、タイマーリセット
00159             t.reset();
00160         }
00161         if(Log_flag == OFF) { //log取るごとにリセット
00162             t.stop();
00163             t.reset();
00164         } else {
00165             t.start();
00166         }
00167         time_s = t.read();//時間取得
00168         min = (int)(time_s) / 60;
00169         sec = (int)time_s % 60;
00170         wait(0.0002);
00171     
00172     
00173       i3=0;
00174       while(gps.getc()!='$'){
00175       }
00176       
00177       while( (gps_data[i3]=gps.getc()) != '\r'){
00178         i3++;
00179         if(i3==256){
00180          
00181            i3=255;
00182            break;
00183          }
00184       }
00185       gps_data[i3]='\0';
00186       
00187       //test
00188       /* Test data
00189        rlock=1;
00190        hokui=3532.25024; //=>35.537502
00191        tokei=13751.86820;//=>137.864471
00192       */  
00193       if( sscanf(gps_data, "GPGGA,%f,%f,%c,%f,%c,%d,%d",&time,&hokui,&ns,&tokei,&ew,&rlock,&stn) >= 1){
00194         if(rlock >= 1){
00195           
00196           //time set
00197           h_time=int(time/10000);
00198           m_time=int((time-h_time*10000)/100);
00199           s_time=int(time-h_time*10000-m_time*100);
00200           h_time=h_time+9;//UTC =>JST
00201           
00202           //hokui
00203           d_hokui=int(hokui/100);
00204           m_hokui=(hokui-d_hokui*100)/60;
00205           g_hokui=d_hokui+m_hokui;
00206           //tokei
00207           d_tokei=int(tokei/100);
00208           m_tokei=(tokei-d_tokei*100)/60;
00209           g_tokei=d_tokei+m_tokei;
00210           //g_hokui=int(hokui/100)+(hokui-int(hokui/100))/60;
00211           //g_tokei=int(tokei/100)+(tokei-int(tokei/100))/60;
00212         
00213           
00214           //==================================================//
00215          
00216          pc.printf("%2d,%2d,%2d,%4.6f,,%3.6f,\r\n",h_time,m_time,s_time,g_tokei,g_hokui);
00217          
00218      //    fp = fopen("/sd/gps_data/log.txt", "a");
00219     
00220         //  fprintf(fp,"%2d,%2d,%2d,%4.6f,,%3.6f,\r\n",h_time,m_time,s_time,g_tokei,g_hokui);
00221        // fprintf(fp,"%4.6f,%3.6f,\r\n",g_tokei,g_hokui);
00222        //   fclose(fp);
00223           
00224           //==================================================//
00225           
00226           
00227          
00228         //}
00229         }
00230       }//if
00231     }//while
00232 }//main
00233 
00234 
00235 void SD_log()   //SDログ関数
00236 {
00237     //  __disable_irq();
00238     led2 = 1;
00239     if (Log_flag == ON) {
00240         FILE *fp;
00241         /*
00242          fp = fopen("/sd/log.txt", "a");
00243     
00244          fprintf(fp,"%2d,%2d,%2d,%4.6f,,%3.6f,\r\n",h_time,m_time,s_time,g_tokei,g_hokui);
00245         fprintf(fp,"%4.6f,%3.6f,\r\n",g_tokei,g_hokui);
00246           fclose(fp);   
00247           */
00248           
00249         fp = fopen("/sd/mydir/TP.txt", "a");
00250         
00251         if (NULL == fp) {
00252             pilot_oled.locate(0, 0);
00253             pilot_oled.printf("There is no SD!!");
00254             pc.printf("There is no SD!!\n");
00255             error("Could not open file for write \n");
00256         } else {
00257             fprintf(fp, "NO:%d,%02d:%02d,%1.0f,%0.3f,%0.3f,%0.3f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
00258                          SD_count, min, sec, rpm, speed, height,compass,up2,up1,E_neut,down1,down2,left2,left1,R_neut,right1,right2);
00259                    //      fprintf(fp,"%4.6f,;%3.6f,;\r\n",g_tokei,g_hokui);
00260             fclose(fp);     
00261                                                                                                                 //操舵データ
00262         }
00263     }
00264     led2 = 0;
00265     //   __enable_irq();
00266 }
00267 
00268 void Log_Switch_f()//ログスイッチ
00269 {
00270     log_count++;
00271     if (log_count == 1) {
00272         Log_flag = ON;
00273         SD_count++;
00274     } else if (log_count == 2) {
00275         Log_flag = OFF;
00276         log_count = 0;
00277     }
00278 }
00279 
00280 void OLEDdisplay_print()  //有機EL表示用関数
00281 {
00282     led3!=led3;
00283     //__disable_irq();
00284 /*    if(Log_flag == OFF) {
00285         wait_ms(0.7);
00286         pilot_oled.locate(0, 0);
00287         pilot_oled.printf("log");//時間表示
00288         pilot_oled.locate(3, 0);
00289         pilot_oled.putc(0xB5);//オ
00290         pilot_oled.locate(4, 0);
00291         pilot_oled.putc(0xBE);//セ
00292         pilot_oled.locate(5, 0);
00293         pilot_oled.putc(0x21);//!
00294     } else {
00295         pilot_oled.locate(0, 0);
00296         pilot_oled.printf("%02d:%02d ", min, sec);//時間表示
00297     }
00298     wait_ms(0.7);*/
00299     pilot_oled.locate(0, 1);
00300     pilot_oled.printf("r%3.0f s%4.1f h%4.1f", rpm,speed,height);//センサーの値
00301     wait_ms(0.7);
00302 
00303     pilot_oled.locate(4, 0);
00304     pilot_oled.printf("%1d", send_compass);
00305     pilot_oled.locate(11, 0);
00306     if (Log_flag == ON) pilot_oled.printf("ON ");
00307     else pilot_oled.printf("OFF");
00308     
00309 //        pilot_oled.locate(0, 0);
00310   //      pilot_oled.printf("count:%d",rpmcounter);//時間表示
00311     wait_ms(0.7);
00312 // __enable_irq();
00313 }
00314 
00315 void compass_f()//高度の変換
00316 {
00317     compass=atoi(compass_array);
00318     
00319     if(compass<22.5&&compass>=0||compass<360&&compass>=337.5)send_compass=1;//北
00320     else if(compass<67.5&&compass>=22.5)send_compass=2;//
00321     else if(compass<112.5&&compass>=67.5)send_compass=3;//東
00322     else if(compass<157.5&&compass>=112.5)send_compass=4;//
00323     else if(compass<202.5&&compass>=157.5)send_compass=5;//南
00324     else if(compass<247.5&&compass>=202.5)send_compass=6;//
00325     else if(compass<292.5&&compass>=247.5)send_compass=7;//西
00326     else if(compass<337.5&&compass>=292.5)send_compass=8;//
00327 
00328         
00329     
00330 }
00331 
00332 void altitude_f()//高度の変換
00333 {
00334     height=alt.read()*3.3*1000/1.6/100;
00335 }
00336 
00337 void rpm_c()  //回転数のフォトインタラプタ
00338 {
00339     rpmcounter++;
00340 }
00341 
00342 void rpm_f()  //回転数計測
00343 {
00344 //    rpm = (double)(rpmcounter * 60000/time_ms/48);
00345     rpm=(double)(rpmcounter);
00346     rpmcounter = 0;
00347 }
00348 
00349 void spd_c()  //機速のフォトインタラプタ
00350 {
00351     speedcounter++;
00352 }
00353 
00354 void spd_f()
00355 {
00356     int x = 0;
00357     x = (double)(speedcounter * 60000/time_ms/8);
00358     speed = (double)(0.0014*x + 0.9815);
00359     //最初の機速校正時の式は、0.0015*x + 0.6063       
00360     if(speed<=0.9815) {
00361         speed = 0;
00362     }
00363     speedcounter = 0;
00364 }
00365 
00366 
00367 void control_communication()  //操舵基板との通信用
00368 {
00369 //    myled(9,9,1,9);
00370 
00371     // __disable_irq();
00372     // t1.stop();
00373     for (int i = 0; i <3; i++) {
00374         compass_array[i] = control_com.getc();//高度の受信
00375     }
00376     ele = control_com.getc();//舵角の受信
00377     rud = control_com.getc();//舵角の受信
00378     e_trim = control_com.getc();//trimの受信
00379     r_trim = control_com.getc();//trimの受信
00380     //////////////ER操舵データ表示////////////////////
00381     /*   pilot_oled.locate(6, 0);
00382        pilot_oled.printf("%c", ele);
00383        wait_ms(0.7);
00384        pilot_oled.locate(8, 0);
00385        pilot_oled.printf("%c", rud);
00386        wait_ms(0.7);*/
00387     pilot_oled.locate(0, 0);
00388     pilot_oled.printf("%1c%1c", e_trim,r_trim);
00389     pilot_oled.locate(14, 0);
00390     pilot_oled.printf("%2d", SD_count);
00391     wait_ms(0.7);
00392     ///////////エレベータログデータ条件分岐////////////////////////////
00393     switch(ele) {
00394         case 'U':
00395             pilot_oled.locate(6, 0);
00396             pilot_oled.putc(0x02);//
00397 //            pilot_oled.printf("U");//
00398             up2 = 5 ;
00399             break;
00400         case 'u':
00401             pilot_oled.locate(6, 0);
00402             pilot_oled.putc(0x5E);//^
00403 //            pilot_oled.printf("u");//
00404             up1 = 4 ;
00405             break;
00406         case 'n':
00407             pilot_oled.locate(6, 0);
00408             pilot_oled.putc(0x2D);//-
00409             E_neut = 3 ;
00410             break;
00411         case 'd':
00412             pilot_oled.locate(6, 0);
00413             pilot_oled.putc(0x00);//vみたいな文字
00414 //            pilot_oled.printf("d");//
00415             down1 = 2 ;
00416             break;
00417         case 'D':
00418             pilot_oled.locate(6, 0);
00419             pilot_oled.putc(0x01);
00420 //            pilot_oled.printf("D");//
00421             down2 = 1;
00422             break;
00423         default:
00424             break;
00425     }
00426     /////////ラダーログデータ条件分岐/////////////////////////////////////
00427     switch(rud) {
00428         case 'L':
00429             pilot_oled.locate(8, 0);
00430             pilot_oled.putc(0x03);
00431 //            pilot_oled.printf("L");//
00432             left2 =1;
00433             break;
00434         case 'l':
00435             pilot_oled.locate(8, 0);
00436             pilot_oled.putc(0x3C);//<
00437             left1 =2;
00438             break;
00439         case 'N':
00440             pilot_oled.locate(8, 0);
00441             pilot_oled.putc(0x7C);//|
00442             R_neut=3;
00443             break;
00444         case 'r':
00445             pilot_oled.locate(8, 0);
00446             pilot_oled.putc(0x3E);//>
00447             right1=4;
00448             break;
00449         case 'R':
00450             pilot_oled.locate(8, 0);
00451             pilot_oled.putc(0x04);
00452 //             pilot_oled.printf("R");//
00453            right2=5;
00454             break;
00455         default:
00456             break;
00457     }
00458     //  t1.start();
00459     //__enable_irq();
00460 }
00461 
00462 void android_communication()
00463 {
00464 //    myled(9,9,9,1);
00465 
00466     int srpm,sspeed,sheight;
00467     srpm=(int)rpm;
00468     sspeed=(int)(speed*10);
00469     sheight=(int)(height*10);    
00470  //   if(srpm<100)srpm+=900;
00471  //   if(sspeed<100)sspeed+=900;
00472 //    if(sheight<100)sheight+=900;
00473     
00474     snprintf(send_buff1,10,"r%xh%xv%x",srpm,sheight,sspeed);    
00475     for(i=0;i<10;i++){
00476 //        pc.putc(send_buff1[i]); 
00477         android_com.putc(send_buff1[i]);         
00478     }
00479 }
00480 
00481 void update()   //データ更新。最優先関数、割り込み禁止絶対入れろ。
00482 {
00483 //    myled(1,0,0,0);
00484     __disable_irq();
00485     //time2 = t1.read_ms();
00486     time_ms = t1.read_ms();//周期時間読み取り
00487     t1.stop();
00488     t1.reset();
00489     led4 =! led4;
00490     //time_ms =time2-time1;
00491     rpm_f();
00492     spd_f();
00493     altitude_f();
00494     compass_f();
00495    SD_log();
00496     OLEDdisplay_print();
00497     android_communication();
00498     pc.printf("NO:%d,%02d:%02d,R:%1.0f,S:%0.3f,H:%0.3f,%c%c%c,compass:%3.0f,%c%c,e:%c,r:%c,et:%c,rt:%c,%.3f\r\n", SD_count, min, sec, rpm, speed, height,send_buff1[4],send_buff1[5],send_buff1[6],compass,compass_array[0],compass_array[1], ele, rud,e_trim,r_trim,time_ms);
00499 //    pc.printf("TBT\n");
00500     up2 = 0;
00501     up1 = 0;
00502     E_neut = 0;
00503     down1=0;
00504     down2=0;
00505     left1= 0;
00506     left2 =0;
00507     R_neut = 0;
00508     right1=0;
00509     right2=0;
00510     t1.start();
00511     // time1 = t1.read_ms();
00512     __enable_irq();
00513 }
00514 
00515 
00516 void CGRAM_1()
00517 {
00518     // CGRAMアドレスの指定
00519     // CGRAM(1)にビット・パターンを登録します。
00520     pilot_oled.writeCommand(0x40) ;
00521     // 40μ秒待ちます。
00522     wait_us(40);          
00523 
00524     // ここから文字のビット・パターンを1行ずつ設定していきます。
00525     pilot_oled.writeData((int)0x00) ;
00526     pilot_oled.writeData((int)0x00) ;
00527     pilot_oled.writeData((int)0x00) ;
00528     pilot_oled.writeData((int)0x00) ;
00529     pilot_oled.writeData((int)0x00) ;
00530     pilot_oled.writeData((int)0x11) ;//10001
00531     pilot_oled.writeData((int)0x0A) ;//01010
00532     pilot_oled.writeData((int)0x04) ;//00100
00533     wait_us(40);
00534     pilot_oled.cls();
00535 }
00536 
00537 void CGRAM_2()
00538 {
00539     // CGRAMアドレスの指定
00540     // CGRAM(2)にビット・パターンを登録します。
00541     pilot_oled.writeCommand(0x48) ;
00542     // 40μ秒待ちます。
00543     wait_us(40);          
00544 
00545     // ここから文字のビット・パターンを1行ずつ設定していきます。
00546     pilot_oled.writeData((int)0x00) ;
00547     pilot_oled.writeData((int)0x00) ;
00548     pilot_oled.writeData((int)0x00) ;
00549     pilot_oled.writeData((int)0x00) ;
00550     pilot_oled.writeData((int)0x00) ;
00551     pilot_oled.writeData((int)0x1F) ;//11111
00552     pilot_oled.writeData((int)0x0E) ;//01110
00553     pilot_oled.writeData((int)0x04) ;//00100
00554     wait_us(40);
00555     pilot_oled.cls();
00556 }
00557 
00558 void CGRAM_3()
00559 {
00560     // CGRAMアドレスの指定
00561     // CGRAM(3)にビット・パターンを登録します。
00562     pilot_oled.writeCommand(0x50) ;
00563     // 40μ秒待ちます。
00564     wait_us(40);          
00565 
00566     // ここから文字のビット・パターンを1行ずつ設定していきます。
00567     pilot_oled.writeData((int)0x04) ;//00100
00568     pilot_oled.writeData((int)0x0E) ;//01110
00569     pilot_oled.writeData((int)0x1F) ;//11111
00570     pilot_oled.writeData((int)0x00) ;
00571     pilot_oled.writeData((int)0x00) ;
00572     pilot_oled.writeData((int)0x00) ;
00573     pilot_oled.writeData((int)0x00) ;
00574     pilot_oled.writeData((int)0x00) ;
00575     wait_us(40);
00576     pilot_oled.cls();
00577 }
00578 
00579 void CGRAM_4()
00580 {
00581     // CGRAMアドレスの指定
00582     // CGRAM(4)にビット・パターンを登録します。
00583     pilot_oled.writeCommand(0x58) ;
00584     // 40μ秒待ちます。
00585     wait_us(40);          
00586 
00587     // ここから文字のビット・パターンを1行ずつ設定していきます。
00588     pilot_oled.writeData((int)0x02) ;//00010
00589     pilot_oled.writeData((int)0x06) ;//00110
00590     pilot_oled.writeData((int)0x0E) ;//01110
00591     pilot_oled.writeData((int)0x1E) ;//11110
00592     pilot_oled.writeData((int)0x0E) ;//01110
00593     pilot_oled.writeData((int)0x06) ;//00110
00594     pilot_oled.writeData((int)0x02) ;//00010
00595     pilot_oled.writeData((int)0x00) ;//00000
00596     wait_us(40);
00597     pilot_oled.cls();
00598 }
00599 
00600 void CGRAM_5()
00601 {
00602     // CGRAMアドレスの指定
00603     // CGRAM(5)にビット・パターンを登録します。
00604     pilot_oled.writeCommand(0x60) ;
00605     // 40μ秒待ちます。
00606     wait_us(40);          
00607 
00608     // ここから文字のビット・パターンを1行ずつ設定していきます。
00609     pilot_oled.writeData((int)0x08) ;//01000
00610     pilot_oled.writeData((int)0x0C) ;//01100
00611     pilot_oled.writeData((int)0x0E) ;//01110
00612     pilot_oled.writeData((int)0x0F) ;//01111
00613     pilot_oled.writeData((int)0x0E) ;//01110
00614     pilot_oled.writeData((int)0x0C) ;//01100
00615     pilot_oled.writeData((int)0x08) ;//01000
00616     pilot_oled.writeData((int)0x00) ;//00000
00617     wait_us(40);
00618     pilot_oled.cls();
00619 }
00620 
00621 
00622