![](/media/cache/group/mebd_thumbnail.jpg.50x50_q85.jpg)
千草L432用のプログラムベータ版。フロー自体はおそらく正常だが、センサーデータ取得ができるかは未確認
Dependencies: BufferedSerial SDFileSystem mbed
main.cpp@0:a485504ce221, 23 months ago (annotated)
- Committer:
- MatsumotoKouki
- Date:
- Tue Sep 13 07:12:43 2022 +0000
- Revision:
- 0:a485504ce221
beta_ver don't know whether sensor works
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MatsumotoKouki | 0:a485504ce221 | 1 | /* mbed Microcontroller Library |
MatsumotoKouki | 0:a485504ce221 | 2 | * Copyright (c) 2019 ARM Limited |
MatsumotoKouki | 0:a485504ce221 | 3 | * SPDX-License-Identifier: Apache-2.0 |
MatsumotoKouki | 0:a485504ce221 | 4 | */ |
MatsumotoKouki | 0:a485504ce221 | 5 | |
MatsumotoKouki | 0:a485504ce221 | 6 | /*センサーにコマンドを送信するのはとりあえず後回し |
MatsumotoKouki | 0:a485504ce221 | 7 | GPSのデータを受信するのも後回し*/ |
MatsumotoKouki | 0:a485504ce221 | 8 | |
MatsumotoKouki | 0:a485504ce221 | 9 | #include "mbed.h" |
MatsumotoKouki | 0:a485504ce221 | 10 | #include "SDFileSystem.h" |
MatsumotoKouki | 0:a485504ce221 | 11 | #include "BufferedSerial.h" |
MatsumotoKouki | 0:a485504ce221 | 12 | |
MatsumotoKouki | 0:a485504ce221 | 13 | BufferedSerial jy901(PA_9,PA_10); |
MatsumotoKouki | 0:a485504ce221 | 14 | //BufferedSerial jy901(PA_9,PA_10); |
MatsumotoKouki | 0:a485504ce221 | 15 | SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd"); //mosi, miso, sck, cs |
MatsumotoKouki | 0:a485504ce221 | 16 | //BufferedSerial f303(PA_2,PA_3,38400); |
MatsumotoKouki | 0:a485504ce221 | 17 | //Serial f303(PA_2,PA_3,38400); |
MatsumotoKouki | 0:a485504ce221 | 18 | DigitalIn F2L_1(PA_3); |
MatsumotoKouki | 0:a485504ce221 | 19 | DigitalIn F2L_2(PA_2); |
MatsumotoKouki | 0:a485504ce221 | 20 | |
MatsumotoKouki | 0:a485504ce221 | 21 | DigitalOut led1(LED1); |
MatsumotoKouki | 0:a485504ce221 | 22 | DigitalOut led2(LED2); |
MatsumotoKouki | 0:a485504ce221 | 23 | //Serial pc(USBTX, USBRX,38400);//ボーレートを落とすと,USB側からのデータが正確に出力されない. |
MatsumotoKouki | 0:a485504ce221 | 24 | |
MatsumotoKouki | 0:a485504ce221 | 25 | int sig=0; |
MatsumotoKouki | 0:a485504ce221 | 26 | FILE *fp; |
MatsumotoKouki | 0:a485504ce221 | 27 | Ticker comm_1; |
MatsumotoKouki | 0:a485504ce221 | 28 | Ticker comm_2; |
MatsumotoKouki | 0:a485504ce221 | 29 | Ticker comm_3; |
MatsumotoKouki | 0:a485504ce221 | 30 | |
MatsumotoKouki | 0:a485504ce221 | 31 | char str[10]; |
MatsumotoKouki | 0:a485504ce221 | 32 | char NichromeCom[11]={0x55,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
MatsumotoKouki | 0:a485504ce221 | 33 | //Timeout FlightPin; |
MatsumotoKouki | 0:a485504ce221 | 34 | Timeout fileClose; |
MatsumotoKouki | 0:a485504ce221 | 35 | |
MatsumotoKouki | 0:a485504ce221 | 36 | int getSignal(); //f303からのコマンドを受け取る関数 |
MatsumotoKouki | 0:a485504ce221 | 37 | //void JY901(); //JY901が取得した生データをSDに書き込む関数 |
MatsumotoKouki | 0:a485504ce221 | 38 | void getGPS(); //GPSデータを取得し、f303に送信する関数 |
MatsumotoKouki | 0:a485504ce221 | 39 | |
MatsumotoKouki | 0:a485504ce221 | 40 | void StandbyPin(); |
MatsumotoKouki | 0:a485504ce221 | 41 | void StandbyCommand(); |
MatsumotoKouki | 0:a485504ce221 | 42 | void StandbyNichrome(); |
MatsumotoKouki | 0:a485504ce221 | 43 | void StandbyBuzzer(); |
MatsumotoKouki | 0:a485504ce221 | 44 | void MakeFile(); |
MatsumotoKouki | 0:a485504ce221 | 45 | void writeNichrome(); |
MatsumotoKouki | 0:a485504ce221 | 46 | void close(); |
MatsumotoKouki | 0:a485504ce221 | 47 | |
MatsumotoKouki | 0:a485504ce221 | 48 | int main() |
MatsumotoKouki | 0:a485504ce221 | 49 | { |
MatsumotoKouki | 0:a485504ce221 | 50 | unsigned char CalibGyroAcc[5]={0xFF,0xAA,0x01,0x01,0x00}; |
MatsumotoKouki | 0:a485504ce221 | 51 | unsigned char CalibMag[5]={0xFF,0xAA,0x01,0x02,0x00}; |
MatsumotoKouki | 0:a485504ce221 | 52 | unsigned char SetHeight[5]={0xFF,0xAA,0x01,0x03,0x00}; |
MatsumotoKouki | 0:a485504ce221 | 53 | unsigned char ExitCalib[5]={0xFF,0xAA,0x01,0x00,0x00}; |
MatsumotoKouki | 0:a485504ce221 | 54 | comm_1.attach(StandbyPin,0.1); //割り込みで1秒ごとにf303からのコマンドを取得 |
MatsumotoKouki | 0:a485504ce221 | 55 | |
MatsumotoKouki | 0:a485504ce221 | 56 | /********************** |
MatsumotoKouki | 0:a485504ce221 | 57 | //センサーのsleepモードを終わらせて、キャリブレーションを開始する関数 |
MatsumotoKouki | 0:a485504ce221 | 58 | *****************/ |
MatsumotoKouki | 0:a485504ce221 | 59 | |
MatsumotoKouki | 0:a485504ce221 | 60 | jy901.write(CalibGyroAcc,5); |
MatsumotoKouki | 0:a485504ce221 | 61 | wait(45); |
MatsumotoKouki | 0:a485504ce221 | 62 | jy901.write(CalibMag,5); |
MatsumotoKouki | 0:a485504ce221 | 63 | wait(30); |
MatsumotoKouki | 0:a485504ce221 | 64 | jy901.write(SetHeight,5); |
MatsumotoKouki | 0:a485504ce221 | 65 | wait(30); |
MatsumotoKouki | 0:a485504ce221 | 66 | jy901.write(ExitCalib,5); //キャリブレーションモード終了 |
MatsumotoKouki | 0:a485504ce221 | 67 | |
MatsumotoKouki | 0:a485504ce221 | 68 | while(sig==4) { |
MatsumotoKouki | 0:a485504ce221 | 69 | fputc(jy901.getc(),fp); |
MatsumotoKouki | 0:a485504ce221 | 70 | } |
MatsumotoKouki | 0:a485504ce221 | 71 | } |
MatsumotoKouki | 0:a485504ce221 | 72 | |
MatsumotoKouki | 0:a485504ce221 | 73 | void StandbyPin() |
MatsumotoKouki | 0:a485504ce221 | 74 | { |
MatsumotoKouki | 0:a485504ce221 | 75 | //printf("StandbyCommand start\r\n"); |
MatsumotoKouki | 0:a485504ce221 | 76 | if(F2L_1 == 1 && F2L_2 == 0){ |
MatsumotoKouki | 0:a485504ce221 | 77 | sig=4; |
MatsumotoKouki | 0:a485504ce221 | 78 | comm_1.detach(); |
MatsumotoKouki | 0:a485504ce221 | 79 | |
MatsumotoKouki | 0:a485504ce221 | 80 | led2=1; //2回点滅 |
MatsumotoKouki | 0:a485504ce221 | 81 | wait(0.1); |
MatsumotoKouki | 0:a485504ce221 | 82 | led2=0; |
MatsumotoKouki | 0:a485504ce221 | 83 | wait(0.1); |
MatsumotoKouki | 0:a485504ce221 | 84 | led2=1; |
MatsumotoKouki | 0:a485504ce221 | 85 | wait(0.1); |
MatsumotoKouki | 0:a485504ce221 | 86 | led2=0; |
MatsumotoKouki | 0:a485504ce221 | 87 | |
MatsumotoKouki | 0:a485504ce221 | 88 | MakeFile(); |
MatsumotoKouki | 0:a485504ce221 | 89 | printf("FlightPin\r\n"); |
MatsumotoKouki | 0:a485504ce221 | 90 | comm_2.attach(StandbyNichrome,0.4); |
MatsumotoKouki | 0:a485504ce221 | 91 | } |
MatsumotoKouki | 0:a485504ce221 | 92 | } |
MatsumotoKouki | 0:a485504ce221 | 93 | |
MatsumotoKouki | 0:a485504ce221 | 94 | void StandbyNichrome(){ |
MatsumotoKouki | 0:a485504ce221 | 95 | if(F2L_1 == 1 && F2L_2 == 1){ |
MatsumotoKouki | 0:a485504ce221 | 96 | comm_2.detach(); |
MatsumotoKouki | 0:a485504ce221 | 97 | fwrite(&NichromeCom,sizeof(NichromeCom[0]),sizeof(NichromeCom),fp); |
MatsumotoKouki | 0:a485504ce221 | 98 | |
MatsumotoKouki | 0:a485504ce221 | 99 | led2=1; //1回点滅 |
MatsumotoKouki | 0:a485504ce221 | 100 | wait(0.1); |
MatsumotoKouki | 0:a485504ce221 | 101 | led2=0; |
MatsumotoKouki | 0:a485504ce221 | 102 | |
MatsumotoKouki | 0:a485504ce221 | 103 | printf("1\r\n"); |
MatsumotoKouki | 0:a485504ce221 | 104 | fileClose.attach(close,70); |
MatsumotoKouki | 0:a485504ce221 | 105 | printf("Nichrome start\r\n"); |
MatsumotoKouki | 0:a485504ce221 | 106 | comm_3.attach(StandbyBuzzer,1); |
MatsumotoKouki | 0:a485504ce221 | 107 | } |
MatsumotoKouki | 0:a485504ce221 | 108 | } |
MatsumotoKouki | 0:a485504ce221 | 109 | |
MatsumotoKouki | 0:a485504ce221 | 110 | void StandbyBuzzer(){ |
MatsumotoKouki | 0:a485504ce221 | 111 | if(F2L_1 == 0 && F2L_2 == 1){ |
MatsumotoKouki | 0:a485504ce221 | 112 | comm_3.detach(); |
MatsumotoKouki | 0:a485504ce221 | 113 | |
MatsumotoKouki | 0:a485504ce221 | 114 | led2=1; //3回点滅 |
MatsumotoKouki | 0:a485504ce221 | 115 | wait(0.1); |
MatsumotoKouki | 0:a485504ce221 | 116 | led2=0; |
MatsumotoKouki | 0:a485504ce221 | 117 | wait(0.1); |
MatsumotoKouki | 0:a485504ce221 | 118 | led2=1; |
MatsumotoKouki | 0:a485504ce221 | 119 | wait(0.1); |
MatsumotoKouki | 0:a485504ce221 | 120 | led2=0; |
MatsumotoKouki | 0:a485504ce221 | 121 | wait(0.1); |
MatsumotoKouki | 0:a485504ce221 | 122 | led2=1; |
MatsumotoKouki | 0:a485504ce221 | 123 | wait(0.1); |
MatsumotoKouki | 0:a485504ce221 | 124 | led2=0; |
MatsumotoKouki | 0:a485504ce221 | 125 | printf("3\r\n"); |
MatsumotoKouki | 0:a485504ce221 | 126 | |
MatsumotoKouki | 0:a485504ce221 | 127 | close(); |
MatsumotoKouki | 0:a485504ce221 | 128 | |
MatsumotoKouki | 0:a485504ce221 | 129 | while(1) { |
MatsumotoKouki | 0:a485504ce221 | 130 | getGPS(); |
MatsumotoKouki | 0:a485504ce221 | 131 | } |
MatsumotoKouki | 0:a485504ce221 | 132 | } |
MatsumotoKouki | 0:a485504ce221 | 133 | } |
MatsumotoKouki | 0:a485504ce221 | 134 | |
MatsumotoKouki | 0:a485504ce221 | 135 | void MakeFile(){ |
MatsumotoKouki | 0:a485504ce221 | 136 | mkdir("/sd/2021MR", 0777); |
MatsumotoKouki | 0:a485504ce221 | 137 | FILE *fp = fopen("/sd/2021MR/chigusa.bin", "wb"); |
MatsumotoKouki | 0:a485504ce221 | 138 | if(fp == NULL) { |
MatsumotoKouki | 0:a485504ce221 | 139 | error("Could not open file for write\n"); |
MatsumotoKouki | 0:a485504ce221 | 140 | } |
MatsumotoKouki | 0:a485504ce221 | 141 | } |
MatsumotoKouki | 0:a485504ce221 | 142 | |
MatsumotoKouki | 0:a485504ce221 | 143 | void getGPS(){ |
MatsumotoKouki | 0:a485504ce221 | 144 | char buf[10000],data[10000]; |
MatsumotoKouki | 0:a485504ce221 | 145 | int ucRxCnt,j=0; |
MatsumotoKouki | 0:a485504ce221 | 146 | //string GPSStatus=""; |
MatsumotoKouki | 0:a485504ce221 | 147 | /*FILE *fp = fopen("/sd/2021MR/ground.bin", "w"); |
MatsumotoKouki | 0:a485504ce221 | 148 | if(fp == NULL) { |
MatsumotoKouki | 0:a485504ce221 | 149 | error("Could not open file for write\n"); |
MatsumotoKouki | 0:a485504ce221 | 150 | }*/ |
MatsumotoKouki | 0:a485504ce221 | 151 | |
MatsumotoKouki | 0:a485504ce221 | 152 | for(int i=0; i<10000; i++) { |
MatsumotoKouki | 0:a485504ce221 | 153 | buf[i]=jy901.getc(); |
MatsumotoKouki | 0:a485504ce221 | 154 | } |
MatsumotoKouki | 0:a485504ce221 | 155 | |
MatsumotoKouki | 0:a485504ce221 | 156 | while(j<=int(sizeof(buf)/sizeof(buf[0]))) { |
MatsumotoKouki | 0:a485504ce221 | 157 | data[ucRxCnt++]=buf[j]; //Store the received data in the buffer |
MatsumotoKouki | 0:a485504ce221 | 158 | if (data[0]!=0x55) { //The data header is not correct, then restart to find the 0x55 data header |
MatsumotoKouki | 0:a485504ce221 | 159 | ucRxCnt=0; |
MatsumotoKouki | 0:a485504ce221 | 160 | // printf("not start sig\n"); |
MatsumotoKouki | 0:a485504ce221 | 161 | } else if(ucRxCnt<11) { |
MatsumotoKouki | 0:a485504ce221 | 162 | //printf("data is less than 11\n\r"); |
MatsumotoKouki | 0:a485504ce221 | 163 | }//If the data is less than 11, alert. |
MatsumotoKouki | 0:a485504ce221 | 164 | else { |
MatsumotoKouki | 0:a485504ce221 | 165 | //printf("switch\n\r"); |
MatsumotoKouki | 0:a485504ce221 | 166 | 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. |
MatsumotoKouki | 0:a485504ce221 | 167 | /* 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. |
MatsumotoKouki | 0:a485504ce221 | 168 | case 0x51: |
MatsumotoKouki | 0:a485504ce221 | 169 | printf("case 0x51 worked!"); |
MatsumotoKouki | 0:a485504ce221 | 170 | memcpy(&stcAcc,&data[2],8); |
MatsumotoKouki | 0:a485504ce221 | 171 | 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); |
MatsumotoKouki | 0:a485504ce221 | 172 | break; |
MatsumotoKouki | 0:a485504ce221 | 173 | case 0x52: memcpy(&stcGyro,&ucRxBuffer[2],8);break; |
MatsumotoKouki | 0:a485504ce221 | 174 | case 0x53: memcpy(&stcAngle,&ucRxBuffer[2],8);break; |
MatsumotoKouki | 0:a485504ce221 | 175 | case 0x54: memcpy(&stcMag,&ucRxBuffer[2],8);break; |
MatsumotoKouki | 0:a485504ce221 | 176 | case 0x55: memcpy(&stcDStatus,&ucRxBuffer[2],8);break; |
MatsumotoKouki | 0:a485504ce221 | 177 | case 0x56: memcpy(&stcPress,&ucRxBuffer[2],8);break; |
MatsumotoKouki | 0:a485504ce221 | 178 | case 0x57: memcpy(&stcLonLat,&ucRxBuffer[2],8);break;*/ |
MatsumotoKouki | 0:a485504ce221 | 179 | case 0x58: |
MatsumotoKouki | 0:a485504ce221 | 180 | //memcpy(&stcGPSV,&data[2],8); |
MatsumotoKouki | 0:a485504ce221 | 181 | //sprintf(GPSStatus,"GPSHeight:%.1fm GPSYaw:%.1fDeg GPSV:%.3fkm/h\r\n",(float)stcGPSV.sGPSHeight/10,(float)stcGPSV.sGPSYaw/10,(float)stcGPSV.lGPSVelocity/1000); |
MatsumotoKouki | 0:a485504ce221 | 182 | //f303.printf("%s",GPSStatus); |
MatsumotoKouki | 0:a485504ce221 | 183 | //printf("come a GPS data!!"); |
MatsumotoKouki | 0:a485504ce221 | 184 | break; |
MatsumotoKouki | 0:a485504ce221 | 185 | //case 0x59: memcpy(&stcQ,&ucRxBuffer[2],8);break; |
MatsumotoKouki | 0:a485504ce221 | 186 | } |
MatsumotoKouki | 0:a485504ce221 | 187 | ucRxCnt=0;//Clear the cache area |
MatsumotoKouki | 0:a485504ce221 | 188 | } |
MatsumotoKouki | 0:a485504ce221 | 189 | j++; |
MatsumotoKouki | 0:a485504ce221 | 190 | } |
MatsumotoKouki | 0:a485504ce221 | 191 | } |
MatsumotoKouki | 0:a485504ce221 | 192 | |
MatsumotoKouki | 0:a485504ce221 | 193 | void close(){ |
MatsumotoKouki | 0:a485504ce221 | 194 | printf("finished\r\n"); |
MatsumotoKouki | 0:a485504ce221 | 195 | fclose(fp); |
MatsumotoKouki | 0:a485504ce221 | 196 | } |