2021_Colours_ModelRocket / Mbed 2 deprecated Chigusa_L432_beta

Dependencies:   BufferedSerial SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  */
00005 
00006 /*センサーにコマンドを送信するのはとりあえず後回し
00007 GPSのデータを受信するのも後回し*/
00008 
00009 #include "mbed.h"
00010 #include "SDFileSystem.h"
00011 #include "BufferedSerial.h"
00012 
00013 BufferedSerial jy901(PA_9,PA_10);
00014 //BufferedSerial jy901(PA_9,PA_10);
00015 SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd");  //mosi, miso, sck, cs
00016 //BufferedSerial f303(PA_2,PA_3,38400);
00017 //Serial f303(PA_2,PA_3,38400);
00018 DigitalIn F2L_1(PA_3);
00019 DigitalIn F2L_2(PA_2);
00020 
00021 DigitalOut led1(LED1);
00022 DigitalOut led2(LED2);
00023 //Serial pc(USBTX, USBRX,38400);//ボーレートを落とすと,USB側からのデータが正確に出力されない.
00024 
00025 int sig=0;
00026 FILE *fp;
00027 Ticker comm_1;
00028 Ticker comm_2;
00029 Ticker comm_3;
00030 
00031 char str[10];
00032 char NichromeCom[11]={0x55,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
00033 //Timeout FlightPin;
00034 Timeout fileClose;
00035 
00036 int getSignal();    //f303からのコマンドを受け取る関数
00037 //void JY901();   //JY901が取得した生データをSDに書き込む関数
00038 void getGPS();  //GPSデータを取得し、f303に送信する関数
00039 
00040 void StandbyPin();
00041 void StandbyCommand();
00042 void StandbyNichrome();
00043 void StandbyBuzzer();
00044 void MakeFile();
00045 void writeNichrome();
00046 void close();
00047 
00048 int main()
00049 {
00050     unsigned char CalibGyroAcc[5]={0xFF,0xAA,0x01,0x01,0x00};
00051     unsigned char CalibMag[5]={0xFF,0xAA,0x01,0x02,0x00};
00052     unsigned char SetHeight[5]={0xFF,0xAA,0x01,0x03,0x00};
00053     unsigned char ExitCalib[5]={0xFF,0xAA,0x01,0x00,0x00};
00054     comm_1.attach(StandbyPin,0.1);    //割り込みで1秒ごとにf303からのコマンドを取得
00055 
00056     /**********************
00057     //センサーのsleepモードを終わらせて、キャリブレーションを開始する関数
00058     *****************/
00059         
00060     jy901.write(CalibGyroAcc,5);
00061     wait(45);
00062     jy901.write(CalibMag,5);
00063     wait(30);
00064     jy901.write(SetHeight,5);
00065     wait(30);
00066     jy901.write(ExitCalib,5);   //キャリブレーションモード終了
00067     
00068     while(sig==4) {
00069         fputc(jy901.getc(),fp);
00070     }   
00071 }
00072 
00073 void StandbyPin()
00074 {
00075     //printf("StandbyCommand start\r\n");
00076     if(F2L_1 == 1 && F2L_2 == 0){
00077         sig=4;
00078         comm_1.detach();
00079         
00080         led2=1; //2回点滅
00081         wait(0.1);
00082         led2=0;
00083         wait(0.1);
00084         led2=1;
00085         wait(0.1);
00086         led2=0;
00087         
00088         MakeFile();
00089         printf("FlightPin\r\n");
00090         comm_2.attach(StandbyNichrome,0.4);
00091     }
00092 }
00093 
00094 void StandbyNichrome(){
00095     if(F2L_1 == 1 && F2L_2 == 1){
00096         comm_2.detach();
00097         fwrite(&NichromeCom,sizeof(NichromeCom[0]),sizeof(NichromeCom),fp);
00098         
00099         led2=1; //1回点滅
00100         wait(0.1);
00101         led2=0;
00102         
00103         printf("1\r\n");
00104         fileClose.attach(close,70);
00105         printf("Nichrome start\r\n");   
00106         comm_3.attach(StandbyBuzzer,1);
00107     }
00108 }
00109 
00110 void StandbyBuzzer(){
00111     if(F2L_1 == 0 && F2L_2 == 1){
00112         comm_3.detach();
00113         
00114         led2=1; //3回点滅
00115         wait(0.1);
00116         led2=0;
00117         wait(0.1);
00118         led2=1;
00119         wait(0.1);
00120         led2=0;
00121         wait(0.1);
00122         led2=1;
00123         wait(0.1);
00124         led2=0;
00125         printf("3\r\n");
00126         
00127         close();
00128         
00129         while(1) {
00130             getGPS();
00131         }
00132     }   
00133 }
00134 
00135 void MakeFile(){
00136     mkdir("/sd/2021MR", 0777);
00137     FILE *fp = fopen("/sd/2021MR/chigusa.bin", "wb");
00138     if(fp == NULL) {
00139         error("Could not open file for write\n");
00140     }
00141 }
00142 
00143 void getGPS(){
00144     char buf[10000],data[10000];
00145     int ucRxCnt,j=0;
00146     //string GPSStatus="";
00147     /*FILE *fp = fopen("/sd/2021MR/ground.bin", "w");
00148     if(fp == NULL) {
00149         error("Could not open file for write\n");
00150     }*/
00151 
00152     for(int i=0; i<10000; i++) {
00153         buf[i]=jy901.getc();
00154     }
00155 
00156     while(j<=int(sizeof(buf)/sizeof(buf[0]))) {
00157         data[ucRxCnt++]=buf[j];  //Store the received data in the buffer
00158         if (data[0]!=0x55) { //The data header is not correct, then restart to find the 0x55 data header
00159             ucRxCnt=0;
00160             // printf("not start sig\n");
00161         } else if(ucRxCnt<11) {
00162             //printf("data is less than 11\n\r");
00163         }//If the data is less than 11, alert.
00164         else {
00165             //printf("switch\n\r");
00166             switch(data[1]) { //Determine what kind of data the data is, and then copy it to the corresponding structure. Some data packets need to open the corresponding output through the upper computer before receiving the data of this data packet.
00167                 /* case 0x50:    memcpy(&stcTime,&ucRxBuffer[2],8);break;//memcpy is a memory copy function that comes with the compiler. You need to reference "string.h" to copy the characters of the receive buffer into the data structure to achieve data parsing.
00168                  case 0x51:
00169                      printf("case 0x51 worked!");
00170                      memcpy(&stcAcc,&data[2],8);
00171                      fprintf(facc,"Acc,%.3f,%.3f,%.3f\r\n",(float)stcAcc.a[0]/32768*16,(float)stcAcc.a[1]/32768*16,(float)stcAcc.a[2]/32768*16);
00172                      break;
00173                  case 0x52:  memcpy(&stcGyro,&ucRxBuffer[2],8);break;
00174                  case 0x53:  memcpy(&stcAngle,&ucRxBuffer[2],8);break;
00175                  case 0x54:  memcpy(&stcMag,&ucRxBuffer[2],8);break;
00176                  case 0x55:  memcpy(&stcDStatus,&ucRxBuffer[2],8);break;
00177                  case 0x56:  memcpy(&stcPress,&ucRxBuffer[2],8);break;
00178                  case 0x57:  memcpy(&stcLonLat,&ucRxBuffer[2],8);break;*/
00179                 case 0x58:
00180                     //memcpy(&stcGPSV,&data[2],8);
00181                     //sprintf(GPSStatus,"GPSHeight:%.1fm GPSYaw:%.1fDeg GPSV:%.3fkm/h\r\n",(float)stcGPSV.sGPSHeight/10,(float)stcGPSV.sGPSYaw/10,(float)stcGPSV.lGPSVelocity/1000);
00182                     //f303.printf("%s",GPSStatus);
00183                     //printf("come a GPS data!!");
00184                     break;
00185                     //case 0x59:  memcpy(&stcQ,&ucRxBuffer[2],8);break;
00186             }
00187             ucRxCnt=0;//Clear the cache area
00188         }
00189         j++;
00190     }
00191 }
00192 
00193 void close(){
00194     printf("finished\r\n");
00195     fclose(fp);
00196 }