2021千草のl432(センサー)側プログラム

Dependencies:   BufferedSerial SDFileSystem mbed

main.cpp

Committer:
MatsumotoKouki
Date:
23 months ago
Revision:
2:4c7d64e27929
Parent:
1:3f26e434ae82
Child:
3:c1456d673aaf

File content as of revision 2:4c7d64e27929:

/* mbed Microcontroller Library
 * Copyright (c) 2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */
 
 /*センサーにコマンドを送信するのはとりあえず後回し
 GPSのデータを受信するのも後回し*/

#include "mbed.h"
#include "SDFileSystem.h"
//#include "BufferedSerial.h"

//BufferedSerial jy901(PA_9,PA_10);
Serial jy901(PA_9,PA_10);
SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd");  //mosi, miso, sck, cs
//BufferedSerial f303(PA_2,PA_3,38400);
Serial f303(PA_2,PA_3,38400);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
//Serial pc(USBTX, USBRX,38400);//ボーレートを落とすと,USB側からのデータが正確に出力されない.

int sig=0;
Ticker comm;
char str[10];

int getSignal();    //f303からのコマンドを受け取る関数
//void JY901();   //JY901が取得した生データをSDに書き込む関数
void getGPS();  //GPSデータを取得し、f303に送信する関数
void StandbyCommand();
void MakeFile();
 
int main()
{
    comm.attach(StandbyCommand,1);    //割り込みで1秒ごとにf303からのコマンドを取得
    
    /**********************
    //センサーのsleepモードを終わらせて、キャリブレーションを開始する関数
    *****************/
    
    /*while(sig==1){//次のシグナルがくるまでの間
        JY901();
    }
    
    while(sig==2){
        getGPS();
    }*/
}

void StandbyCommand(){
    //printf("StandbyCommand start\r\n");
    sig=getSignal();
    switch(sig){
      case 1:
      //comm.detach();
      MakeFile();
      break;
      
      case 2:
      comm.detach();
      while(1){
          getGPS();
      }
      break;
    }
}

void MakeFile(){
    mkdir("/sd/2021MR", 0777);   
    FILE *fp = fopen("/sd/2021MR/chigusa.bin", "w");
    if(fp == NULL) {
        error("Could not open file for write\n");
    }
    
    while(sig!=2){
        fputc(jy901.getc(),fp);
        //JY901();
    }
    fclose(fp);
}

/*void JY901(){
    
}*/

int getSignal(){    //センサー起動のタイミングで1を返し、センサー終了してGPS送信するタイミングで2を返す関数。
    
    //while(1){   //s:撮影終了、p:フライトピン作動,c:撮影終了・GPS送信
    //printf("getSignal start\r\n");
    int i=0;
    char temp;
    while(temp != '\n') { //読み込み文字が改行で無い場合(順番では\r\n)
        //printf("in a while\r\n");
        if(f303.readable()) { //f303からのデータがある場合
                //printf("readable\r\n");
                led1=1;
                wait(0.1);
                led1=0;                
                
                char temp = f303.getc();//一文字読み込む
                //printf("%c\r\n",temp);
                str[i++] = temp;
            } //else if(temp == '\n') { //読み込み文字が改行の場合
        }
                //printf("get Command\r\n");
                if(str[i-1]=='s'){  //手動でカメラの動作が停止された場合
                    //printf("get s!!\r\n");
                    led2=1;
                    wait(0.1);
                    led2=0;
                    return 1;
                }
                else if(str[i-1]=='p'){ //フライトピン作動
                    //printf("get p\r\n");
                    led2=1;
                    wait(0.1);
                    led2=0;  
                    wait(0.1);                  
                    led2=1;
                    wait(0.1);
                    led2=0;
                    return 2;
                }
                else if(str[i-1]=='c'){ //ブザー作動後、センサー記録を停止
                    //printf("get c\r\n");     
                    led2=1;
                    wait(0.1);
                    led2=0;  
                    wait(0.1);                  
                    led2=1;
                    wait(0.1);
                    led2=0;
                    wait(0.1);
                    led2=1;
                    wait(0.1);
                    led2=0;
                    return 3;
                }  
}

void getGPS(){
    char buf[10000],data[10000];
    int ucRxCnt,j=0;
    //string GPSStatus="";
    /*FILE *fp = fopen("/sd/2021MR/ground.bin", "w");
    if(fp == NULL) {
        error("Could not open file for write\n");
    }*/
    
    for(int i=0;i<10000;i++){
        buf[i]=jy901.getc();
    }
    
    while(j<=int(sizeof(buf)/sizeof(buf[0]))){        
        data[ucRxCnt++]=buf[j];  //Store the received data in the buffer
        if (data[0]!=0x55){ //The data header is not correct, then restart to find the 0x55 data header
            ucRxCnt=0;
           // printf("not start sig\n");
        }
        else if(ucRxCnt<11) {
           //printf("data is less than 11\n\r");
        }//If the data is less than 11, alert.
        else{
            //printf("switch\n\r");
            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.
            {
               /* 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.
                case 0x51:  
                    printf("case 0x51 worked!");
                    memcpy(&stcAcc,&data[2],8);
                    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);
                    break;
                case 0x52:  memcpy(&stcGyro,&ucRxBuffer[2],8);break;
                case 0x53:  memcpy(&stcAngle,&ucRxBuffer[2],8);break;
                case 0x54:  memcpy(&stcMag,&ucRxBuffer[2],8);break;
                case 0x55:  memcpy(&stcDStatus,&ucRxBuffer[2],8);break;
                case 0x56:  memcpy(&stcPress,&ucRxBuffer[2],8);break;
                case 0x57:  memcpy(&stcLonLat,&ucRxBuffer[2],8);break;*/
                case 0x58:  
                //memcpy(&stcGPSV,&data[2],8);
                //sprintf(GPSStatus,"GPSHeight:%.1fm GPSYaw:%.1fDeg GPSV:%.3fkm/h\r\n",(float)stcGPSV.sGPSHeight/10,(float)stcGPSV.sGPSYaw/10,(float)stcGPSV.lGPSVelocity/1000);
                //f303.printf("%s",GPSStatus);
                //printf("come a GPS data!!");
                break;
                //case 0x59:  memcpy(&stcQ,&ucRxBuffer[2],8);break;
            }
            ucRxCnt=0;//Clear the cache area
        }
        j++;
    }
    
    
}