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

Dependencies:   BufferedSerial SDFileSystem mbed

main.cpp

Committer:
MatsumotoKouki
Date:
2021-09-24
Revision:
0:d34ad1a628e4
Child:
1:3f26e434ae82

File content as of revision 0:d34ad1a628e4:

/* mbed Microcontroller Library
 * Copyright (c) 2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
//#include "platform/mbed_thread.h"
#include "SDFileSystem.h"

BufferedSerial jy901(D1,D0);
SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd");
DigitalIn mcu_1(PA_8);
DigitalIn mcu_2(PA_11);
Serial f303(,);

int sig=0;
Ticker timer;

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

void StandbyCommand(){
    sig=getSignal();
    switch(sig){
      case 1:
      //Timer.detach();
      MakeFile();
      break;
      
      case 2:
      timer.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を返す関数。
    char mcuCom[10];
    f303.read(mcuCom,4);    //mcuComに4byte(適当)のデータを取得しますよ
    if(mcuCom[0]=="s"&&mcuCom[1]=="t"){ //startのsとt
        return 1;
    }
    if(mcuCom[0]=="G"&&mcuCom[1]=="P"){ //GPSのGP
        return 2;
    }
    
    return 0;
}

void getGPS(){
    char buf[10000],data[10000];
    int ucRxCnt;
    //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(int 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,&ucRxBuffer[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);
                break;
                //case 0x59:  memcpy(&stcQ,&ucRxBuffer[2],8);break;
            }
            ucRxCnt=0;//Clear the cache area
        }
        j++;
    }
    
    
}