TBT電装 メインプログラム 2017/12/8

Dependencies:   HMC5883L SDFileSystem TextOLED mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //H29/1208
00002 
00003 #include "mbed.h"
00004 #include "TextOLED.h"
00005 #include "SDFileSystem.h"
00006 #include "stdio.h"
00007 
00008 
00009 #define ON 1 
00010 #define OFF 0
00011 
00012 AnalogIn alt(p20);        //高度計読み取り
00013 InterruptIn speedcount(p17); //機速用のフォトインタラプタ読み取り
00014 InterruptIn rpmcount(p18);  //回転数用のフォトインタラプタ読み取り
00015 InterruptIn Log_Switch(p26);//ログスイッチ読み取り
00016 
00017 
00018 Serial pc(USBTX, USBRX);
00019 Serial control_com(p28, p27);//制御基板との通信用
00020 Serial android_com(p13, p14);               //Androidとの通信用txrx
00021 
00022 TextOLED pilot_oled(p29, p30,p22, p23, p24,p25); // RS, E, DB4, DB5, DB6, DB7  有機EL
00023 SDFileSystem sd(p5, p6, p7, p8, "sd");//SDpinの番号 3,7,5,2 //1pin--Vout,4pin--Vout,6pin--GND,8pin--Vout
00024 
00025 
00026 DigitalOut led1(LED1);
00027 DigitalOut led2(LED2);
00028 DigitalOut led3(LED3);
00029 DigitalOut led4(LED4);
00030 //DigitalOut soundsystem(p21);//音システム用
00031 
00032 //タイマー割り込み宣言
00033 Ticker ticker;//updateのタイマー割り込み
00034 Timer t,t1;
00035 
00036 int speedcounter = 0, rpmcounter = 0, min = 0, sec = 0, i;
00037 int oldspeed = 0, oldrpm = 0;
00038 int log_count = 0;
00039 int SD_count = 0;
00040 int up2=0,up1=0,down1=0,down2=0,E_neut=0;
00041 int left1=0,left2=0,right1=0,right2=0,R_neut=0;
00042 int send_compass=0;
00043 
00044 
00045 double time_s = 0,time_ms=0,time1=0,time2=0, speed = 0, rpm = 0, height = 0,compass = 0;
00046 
00047 char ele;
00048 char rud;
00049 char e_trim='e',r_trim='r';
00050 char compass_array[4] = "000";
00051 char data;
00052 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};
00053 
00054 
00055 unsigned char Log_flag = OFF;
00056 unsigned char SD_flag = OFF;
00057 
00058 //プロトタイプ宣言
00059 
00060 void SD_log();
00061 void OLEDdisplay_print();
00062 void spd_f();
00063 void rpm_f();
00064 void spd_c();
00065 void rpm_c();
00066 void control_communication();
00067 void android_communication();
00068 void update();
00069 //================//
00070 void xbee();
00071 //================//
00072 void altitude_f();
00073 void compass_f();
00074 void Log_Switch_f();
00075 void CGRAM_1();
00076 void CGRAM_2();
00077 void CGRAM_3();  
00078 void CGRAM_4();
00079 void CGRAM_5();
00080 
00081 FILE* fp;
00082 
00083 
00084 int main() {
00085     
00086     pilot_oled.cls();
00087     
00088 //    init();
00089 
00090     pc.printf("furst  TBT!!\n");
00091 
00092      led1=1;
00093     led2=1;
00094     led3=1;
00095     led4=1;
00096     
00097 //    myled(1,1,0,0);
00098     wait(2);
00099     
00100     
00101    fp = fopen("/sd/log_data.txt", "w");
00102     if(fp == NULL) {
00103         pilot_oled.locate(0, 0);
00104          pilot_oled.printf("There is no sd.");
00105         error("Could not open file for write\n");
00106     }
00107     
00108     pc.printf("Start writing!\n");
00109     
00110     fclose(fp);
00111     fp = fopen("/sd/log_data.txt", "w");
00112     fprintf(fp, "SD_count, min, sec, rpm, speed, height,compass,up2,up1,E_neut,down1,down2,left2,left1,R_neut,right1,right2\r\n");
00113     fclose(fp);
00114     fp = fopen("/sd/log_data.txt", "w");
00115 
00116     android_com.baud(115200);   
00117     android_com.format(8,Serial::None,1); 
00118     
00119     control_com.attach(control_communication, Serial::RxIrq);
00120     control_com.baud(115200); 
00121 
00122     t.start();
00123     t1.start();
00124 
00125     Log_Switch.rise(&Log_Switch_f);
00126     speedcount.rise(&spd_c);
00127     rpmcount.rise(&rpm_c);
00128     ticker.attach(update,1);//1s更新
00129 
00130     while (1) {
00131         
00132 
00133         led3=!led3;
00134         if(min >=30) { //30分経過時、タイマーリセット
00135             t.reset();
00136         }
00137         if(Log_flag == OFF) { //log取るごとにリセット
00138             t.stop();
00139             t.reset();
00140         } else {
00141             t.start();
00142         }
00143         time_s = t.read();//時間取得
00144         min = (int)(time_s) / 60;
00145         sec = (int)time_s % 60;
00146         wait(0.2);
00147     }
00148 }
00149 
00150 
00151 void xbee(){
00152     //================================//
00153     struct{
00154     char highbyte;
00155     char lowbyte;
00156     short intdat;
00157     }datax;
00158 struct{
00159     char highbyte;
00160     char lowbyte;
00161     short intdat;
00162     }datax2;
00163  struct{
00164     char highbyte;
00165     char lowbyte;
00166     short intdat;
00167     }datax3;       
00168 
00169 //===========================//
00170     
00171     
00172     datax.intdat = rpm;
00173     datax2.intdat = height*10.0;
00174     datax3.intdat = speed*10.0;
00175     
00176     datax.highbyte=datax.intdat/256;
00177     datax.lowbyte=datax.intdat%256;
00178     
00179     datax2.highbyte=datax2.intdat/256;
00180     datax2.lowbyte=datax2.intdat%256;
00181     
00182     datax3.highbyte=datax3.intdat/256;
00183     datax3.lowbyte=datax3.intdat%256;
00184     
00185     control_com.putc('H');//ヘッダーを送信
00186     control_com.putc(datax.highbyte);
00187     control_com.putc(datax.lowbyte);
00188     
00189     control_com.putc(datax2.highbyte);
00190     control_com.putc(datax2.lowbyte);
00191      
00192     
00193     control_com.putc(datax3.highbyte);
00194     control_com.putc(datax3.lowbyte);
00195     
00196     }
00197     
00198 
00199 void SD_log()   //SDログ関数
00200 {
00201     //  __disable_irq();
00202     led2 = 1;
00203     if (Log_flag == ON) {
00204         FILE *fp;
00205         fp = fopen("/sd/log_data.txt", "a");
00206         if (NULL == fp) {
00207             pilot_oled.locate(0, 0);
00208             pilot_oled.printf("There is no SD!!");
00209             pc.printf("There is no SD!!\n");
00210             
00211             pilot_oled.locate(0, 0);
00212          pilot_oled.printf("There is no sd.");
00213             
00214             error("Could not open file for write \n");
00215         } else {
00216             
00217             pc.printf("Now logging!!\r\n");
00218             fprintf(fp, "NO:%d,%02d:%02d,%1.0f,%0.3f,%0.3f,%0.3f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", SD_count, min, sec, rpm, speed, height,compass,up2,up1,E_neut,down1,down2,left2,left1,R_neut,right1,right2);
00219             fclose(fp);                                                                                                              //操舵データ
00220         }
00221     }
00222     led2 = 0;
00223     //   __enable_irq();
00224 }
00225 
00226 void Log_Switch_f()//ログスイッチ
00227 {
00228     log_count++;
00229     if (log_count == 1) {
00230         Log_flag = ON;
00231         SD_count++;
00232     } else if (log_count == 2) {
00233         Log_flag = OFF;
00234         log_count = 0;
00235     }
00236 }
00237 
00238 void OLEDdisplay_print()  //有機EL表示用関数
00239 {
00240     led3!=led3;
00241    
00242     pilot_oled.locate(0, 1);
00243     pilot_oled.printf("r%3.0f s%4.1f h%4.1f", rpm,speed,height);//センサーの値
00244     wait_ms(0.7);
00245 
00246     pilot_oled.locate(4, 0);
00247     pilot_oled.printf("%1d", send_compass);
00248     pilot_oled.locate(11, 0);
00249     if (Log_flag == ON) pilot_oled.printf("ON ");
00250     else pilot_oled.printf("OFF");
00251     
00252 //        pilot_oled.locate(0, 0);
00253   //      pilot_oled.printf("count:%d",rpmcounter);//時間表示
00254     wait_ms(0.7);
00255 // __enable_irq();
00256 }
00257 
00258 void compass_f()//高度の変換
00259 {
00260     compass=atoi(compass_array);
00261     
00262     if(compass<22.5&&compass>=0||compass<360&&compass>=337.5)send_compass=1;//北
00263     else if(compass<67.5&&compass>=22.5)send_compass=2;//
00264     else if(compass<112.5&&compass>=67.5)send_compass=3;//東
00265     else if(compass<157.5&&compass>=112.5)send_compass=4;//
00266     else if(compass<202.5&&compass>=157.5)send_compass=5;//南
00267     else if(compass<247.5&&compass>=202.5)send_compass=6;//
00268     else if(compass<292.5&&compass>=247.5)send_compass=7;//西
00269     else if(compass<337.5&&compass>=292.5)send_compass=8;//
00270 
00271         
00272     
00273 }
00274 
00275 void altitude_f()//高度の変換
00276 {
00277     height=alt.read()*3.3*1000/1.6/100;
00278 }
00279 
00280 void rpm_c()  //回転数のフォトインタラプタ
00281 {
00282     rpmcounter++;
00283 }
00284 
00285 void rpm_f()  //回転数計測
00286 {
00287     rpm = (double)(rpmcounter * 60000/time_ms/48);
00288     rpmcounter = 0;
00289 }
00290 
00291 void spd_c()  //機速のフォトインタラプタ
00292 {
00293     speedcounter++;
00294 }
00295 
00296 void spd_f()
00297 {
00298     int x = 0;
00299     x = (double)(speedcounter * 60000/time_ms/8);
00300     speed = (double)(0.0014*x + 0.9815);
00301     //最初の機速校正時の式は、0.0015*x + 0.6063       
00302     if(speed<=0.9815) {
00303         speed = 0;
00304     }
00305     speedcounter = 0;
00306 }
00307 
00308 
00309 void control_communication()  //操舵基板との通信用
00310 {
00311 //    myled(9,9,1,9);
00312 
00313     // __disable_irq();
00314     // t1.stop();
00315     for (int i = 0; i <3; i++) {
00316         compass_array[i] = control_com.getc();//高度の受信
00317     }
00318     ele = control_com.getc();//舵角の受信
00319     rud = control_com.getc();//舵角の受信
00320     e_trim = control_com.getc();//trimの受信
00321     r_trim = control_com.getc();//trimの受信
00322    
00323     pilot_oled.locate(0, 0);
00324     pilot_oled.printf("%1c%1c", e_trim,r_trim);
00325     pilot_oled.locate(14, 0);
00326     pilot_oled.printf("%2d", SD_count);
00327     wait_ms(0.7);
00328     ///////////エレベータログデータ条件分岐////////////////////////////
00329     switch(ele) {
00330         case 'U':
00331             pilot_oled.locate(6, 0);
00332 //            pilot_oled.putc(0x02);//
00333             pilot_oled.printf("U");//
00334             up2 = 5 ;
00335             break;
00336         case 'u':
00337             pilot_oled.locate(6, 0);
00338 //            pilot_oled.putc(0x5E);//^
00339             pilot_oled.printf("u");//
00340             up1 = 4 ;
00341             break;
00342         case 'n':
00343             pilot_oled.locate(6, 0);
00344             pilot_oled.putc(0x2D);//-
00345             E_neut = 3 ;
00346             break;
00347         case 'd':
00348             pilot_oled.locate(6, 0);
00349 //            pilot_oled.putc(0x00);//vみたいな文字
00350             pilot_oled.printf("d");//
00351             down1 = 2 ;
00352             break;
00353         case 'D':
00354             pilot_oled.locate(6, 0);
00355 //            pilot_oled.putc(0x01);
00356             pilot_oled.printf("D");//
00357             down2 = 1;
00358             break;
00359         default:
00360             break;
00361     }
00362     /////////ラダーログデータ条件分岐/////////////////////////////////////
00363     switch(rud) {
00364         case 'L':
00365             pilot_oled.locate(8, 0);
00366 //            pilot_oled.putc(0x03);
00367             pilot_oled.printf("L");//
00368             left2 =1;
00369             break;
00370         case 'l':
00371             pilot_oled.locate(8, 0);
00372             pilot_oled.putc(0x3C);//<
00373             left1 =2;
00374             break;
00375         case 'N':
00376             pilot_oled.locate(8, 0);
00377             pilot_oled.putc(0x7C);//|
00378             R_neut=3;
00379             break;
00380         case 'r':
00381             pilot_oled.locate(8, 0);
00382             pilot_oled.putc(0x3E);//>
00383             right1=4;
00384             break;
00385         case 'R':
00386             pilot_oled.locate(8, 0);
00387  //           pilot_oled.putc(0x04);
00388              pilot_oled.printf("R");//
00389            right2=5;
00390             break;
00391         default:
00392             break;
00393     }
00394     //  t1.start();
00395     //__enable_irq();
00396 }
00397 
00398 void android_communication()
00399 {
00400 //    myled(9,9,9,1);
00401 
00402     int srpm,sspeed,sheight;
00403     srpm=(int)rpm;
00404     sspeed=(int)speed*10;
00405     sheight=(int)height*10;    
00406     if(srpm<100)srpm+=900;
00407     if(sspeed<100)sspeed+=900;
00408     if(sheight<100)sheight+=900;
00409     
00410     snprintf(send_buff1,12,"s%3d%3d%3d%d",srpm,sheight,sspeed,send_compass);    
00411     for(i=0;i<12;i++){
00412 //        pc.putc(send_buff1[i]); 
00413         android_com.putc(send_buff1[i]);         
00414     }
00415 }
00416 
00417 void update()   //データ更新。最優先関数、割り込み禁止絶対入れろ。
00418 {
00419 //    myled(1,0,0,0);
00420     __disable_irq();
00421     //time2 = t1.read_ms();
00422     time_ms = t1.read_ms();//周期時間読み取り
00423     t1.stop();
00424     t1.reset();
00425     led4 =! led4;
00426     //time_ms =time2-time1;
00427     rpm_f();
00428     spd_f();
00429     altitude_f();
00430     compass_f();
00431      SD_log();
00432     OLEDdisplay_print();
00433     android_communication();
00434     //=============================//
00435     xbee();
00436     //==============================//
00437     pc.printf("NO:%d,%02d:%02d,R:%1.0f,S:%0.3f,H:%0.3f,compass:%3.0f,%c%c,e:%c,r:%c,et:%c,rt:%c,%.3f\r\n", SD_count, min, sec, rpm, speed, height,compass,compass_array[0],compass_array[1], ele, rud,e_trim,r_trim,time_ms);
00438 //    pc.printf("TBT\n");
00439     up2 = 0;
00440     up1 = 0;
00441     E_neut = 0;
00442     down1=0;
00443     down2=0;
00444     left1= 0;
00445     left2 =0;
00446     R_neut = 0;
00447     right1=0;
00448     right2=0;
00449     t1.start();
00450     // time1 = t1.read_ms();
00451     __enable_irq();
00452 }