cltp(仮), 日本語があるので修正必要。
Dependencies: mbed HEPTA_CDH_lite LPS25HB_I2C
main.cpp@4:36a7b7878738, 2022-08-15 (annotated)
- Committer:
- kosukesuzuki
- Date:
- Mon Aug 15 09:51:46 2022 +0000
- Revision:
- 4:36a7b7878738
- Parent:
- 3:f64ab3adcac5
CLTP-japan
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kosukesuzuki | 4:36a7b7878738 | 1 | #define mC 261.626//ド |
kosukesuzuki | 4:36a7b7878738 | 2 | #define mD 293.665//レ |
kosukesuzuki | 4:36a7b7878738 | 3 | #define mE 329.628//ミ |
kosukesuzuki | 4:36a7b7878738 | 4 | #define mF 349.228//ファ |
kosukesuzuki | 4:36a7b7878738 | 5 | #define mG 391.995//ソ |
kosukesuzuki | 4:36a7b7878738 | 6 | #define mA 440.000//ラ |
kosukesuzuki | 4:36a7b7878738 | 7 | #define mB 493.883//シ |
kosukesuzuki | 4:36a7b7878738 | 8 | |
heptasat2021 | 0:da0f6aca15b8 | 9 | #include "mbed.h" |
heptasat2021 | 1:ddac5ec89167 | 10 | #include "HEPTA_CDH.h" |
kosukesuzuki | 4:36a7b7878738 | 11 | #include "LPS.h" |
heptasat2021 | 3:f64ab3adcac5 | 12 | |
kosukesuzuki | 4:36a7b7878738 | 13 | DigitalOut sp1(A0); |
kosukesuzuki | 4:36a7b7878738 | 14 | Ticker sound; |
heptasat2021 | 3:f64ab3adcac5 | 15 | |
kosukesuzuki | 4:36a7b7878738 | 16 | Timer t; |
kosukesuzuki | 4:36a7b7878738 | 17 | HEPTA_CDH cdh(PB_5, PB_4, PB_3, PA_8, "sd"); //mosi, miso, sclk, nonconnect |
kosukesuzuki | 4:36a7b7878738 | 18 | Serial xbee(D1, D0); // tx, rx |
kosukesuzuki | 4:36a7b7878738 | 19 | Serial pc(USBTX,USBRX,9600); //UART |
kosukesuzuki | 4:36a7b7878738 | 20 | I2C i2c(D4,D5); //sda, scl |
kosukesuzuki | 4:36a7b7878738 | 21 | LPS ps(i2c); |
kosukesuzuki | 4:36a7b7878738 | 22 | int count=0; |
kosukesuzuki | 4:36a7b7878738 | 23 | int j; |
kosukesuzuki | 4:36a7b7878738 | 24 | int oto = 0; |
kosukesuzuki | 4:36a7b7878738 | 25 | |
kosukesuzuki | 4:36a7b7878738 | 26 | void tick(void) |
heptasat2021 | 0:da0f6aca15b8 | 27 | { |
kosukesuzuki | 4:36a7b7878738 | 28 | sp1.write(oto); |
kosukesuzuki | 4:36a7b7878738 | 29 | oto=!oto; |
kosukesuzuki | 4:36a7b7878738 | 30 | } |
kosukesuzuki | 4:36a7b7878738 | 31 | |
kosukesuzuki | 4:36a7b7878738 | 32 | int main(){ |
kosukesuzuki | 4:36a7b7878738 | 33 | //音色 |
kosukesuzuki | 4:36a7b7878738 | 34 | float mm[]={mC*4,mD*4,mE*4,mF*4,mG*4,mA*4,mB*4,mC*8}; |
kosukesuzuki | 4:36a7b7878738 | 35 | |
kosukesuzuki | 4:36a7b7878738 | 36 | |
kosukesuzuki | 4:36a7b7878738 | 37 | //Display from satellite to PC via xbee. |
kosukesuzuki | 4:36a7b7878738 | 38 | pc.printf("settig start\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 39 | xbee.printf("settig start\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 40 | |
kosukesuzuki | 4:36a7b7878738 | 41 | //Create a file to save the data acquired from the sensor to a microSD card |
kosukesuzuki | 4:36a7b7878738 | 42 | //Also, if there is no microSD card, this error statement will be displayed |
heptasat2021 | 1:ddac5ec89167 | 43 | mkdir("/sd/mydir", 0777); |
kosukesuzuki | 4:36a7b7878738 | 44 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); |
heptasat2021 | 1:ddac5ec89167 | 45 | if(fp == NULL) { |
heptasat2021 | 1:ddac5ec89167 | 46 | error("Could not open file for write\r\n"); |
heptasat2021 | 0:da0f6aca15b8 | 47 | } |
kosukesuzuki | 4:36a7b7878738 | 48 | |
kosukesuzuki | 4:36a7b7878738 | 49 | //If there is no Barometric Pressure Sensors, this error statement will appear in teraterm |
kosukesuzuki | 4:36a7b7878738 | 50 | if (!ps.init()){//2 |
kosukesuzuki | 4:36a7b7878738 | 51 | printf("Failed to autodetect pressure sensor!\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 52 | while (1); |
kosukesuzuki | 4:36a7b7878738 | 53 | }//2 |
kosukesuzuki | 4:36a7b7878738 | 54 | ps.enableDefault(); |
kosukesuzuki | 4:36a7b7878738 | 55 | |
kosukesuzuki | 4:36a7b7878738 | 56 | //Display "begin" in teraterm and save it to SDcard |
kosukesuzuki | 4:36a7b7878738 | 57 | pc.printf("begin\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 58 | xbee.printf("begin\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 59 | fprintf(fp,"begin\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 60 | for (;;) {//2 |
kosukesuzuki | 4:36a7b7878738 | 61 | |
kosukesuzuki | 4:36a7b7878738 | 62 | ////Variable definitions for command input |
kosukesuzuki | 4:36a7b7878738 | 63 | //if (pc.readable()) xbee.putc(pc.getc()); |
kosukesuzuki | 4:36a7b7878738 | 64 | if (xbee.readable()) pc.putc(xbee.getc()); |
kosukesuzuki | 4:36a7b7878738 | 65 | |
kosukesuzuki | 4:36a7b7878738 | 66 | wait_us(1); |
kosukesuzuki | 4:36a7b7878738 | 67 | |
kosukesuzuki | 4:36a7b7878738 | 68 | char cmd1 = xbee.getc();//cmd共有 |
kosukesuzuki | 4:36a7b7878738 | 69 | |
kosukesuzuki | 4:36a7b7878738 | 70 | //Input "m", the sensor data is displayed on the teraterm. |
kosukesuzuki | 4:36a7b7878738 | 71 | //And the acquired data is saved to the SD card.(機体の放出後→データを取る) |
kosukesuzuki | 4:36a7b7878738 | 72 | if(cmd1 == 'm'){//3 |
kosukesuzuki | 4:36a7b7878738 | 73 | |
kosukesuzuki | 4:36a7b7878738 | 74 | pc.printf("start\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 75 | xbee.printf("start\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 76 | t.start(); |
kosukesuzuki | 4:36a7b7878738 | 77 | //cmd = 0; |
kosukesuzuki | 4:36a7b7878738 | 78 | pc.printf("p [mbar]\ta [m]\tdeg [C]\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 79 | xbee.printf("p [mbar]\ta [m]\tdeg [C]\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 80 | fprintf(fp,"p [mbar]\ta [m]\tdeg [C]\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 81 | fclose(fp); |
kosukesuzuki | 4:36a7b7878738 | 82 | |
kosukesuzuki | 4:36a7b7878738 | 83 | while(1){//4 |
kosukesuzuki | 4:36a7b7878738 | 84 | //FILE *fp = fopen("/sd/mydir/sdtest.txt", "a"); |
kosukesuzuki | 4:36a7b7878738 | 85 | //if (pc.readable()) xbee.putc(pc.getc()); |
kosukesuzuki | 4:36a7b7878738 | 86 | if (xbee.readable()) pc.putc(xbee.getc()); |
kosukesuzuki | 4:36a7b7878738 | 87 | |
kosukesuzuki | 4:36a7b7878738 | 88 | //The operation of saving the data obtained from 10 data acquired to the SD card is repeated 40 times. |
kosukesuzuki | 4:36a7b7878738 | 89 | for(j=0;j<50;j++){//5 |
kosukesuzuki | 4:36a7b7878738 | 90 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "a"); |
kosukesuzuki | 4:36a7b7878738 | 91 | for(int i=0;i<10;i++){//6 |
kosukesuzuki | 4:36a7b7878738 | 92 | count=count+1; |
kosukesuzuki | 4:36a7b7878738 | 93 | float pressure = ps.readPressureMillibars(); |
kosukesuzuki | 4:36a7b7878738 | 94 | float altitude = ps.pressureToAltitudeMeters(pressure); |
kosukesuzuki | 4:36a7b7878738 | 95 | float temperature = ps.readTemperatureC(); |
kosukesuzuki | 4:36a7b7878738 | 96 | pc.printf("%d %.2f\t \t%.2f \t%.2f \r\n",count,pressure,altitude,temperature); |
kosukesuzuki | 4:36a7b7878738 | 97 | xbee.printf("%d %.2f\t \t%.2f \t%.2f \r\n",count,pressure,altitude,temperature); |
kosukesuzuki | 4:36a7b7878738 | 98 | fprintf(fp,"%d %.2f\t \t%.2f \t%.2f \r\n",count,pressure,altitude,temperature); |
kosukesuzuki | 4:36a7b7878738 | 99 | wait_us(10); |
kosukesuzuki | 4:36a7b7878738 | 100 | }//5 |
kosukesuzuki | 4:36a7b7878738 | 101 | fclose(fp); |
kosukesuzuki | 4:36a7b7878738 | 102 | }//4 |
kosukesuzuki | 4:36a7b7878738 | 103 | |
kosukesuzuki | 4:36a7b7878738 | 104 | //wait_us(10); |
kosukesuzuki | 4:36a7b7878738 | 105 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "a"); |
kosukesuzuki | 4:36a7b7878738 | 106 | pc.printf("end\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 107 | pc.printf("time = %f",t.read()); |
kosukesuzuki | 4:36a7b7878738 | 108 | xbee.printf("end\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 109 | xbee.printf("time = %f",t.read()); |
kosukesuzuki | 4:36a7b7878738 | 110 | fprintf(fp,"end\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 111 | fprintf(fp,"time = %f",t.read()); |
kosukesuzuki | 4:36a7b7878738 | 112 | t.stop(); |
kosukesuzuki | 4:36a7b7878738 | 113 | fclose(fp); |
kosukesuzuki | 4:36a7b7878738 | 114 | //cmd = 0; |
kosukesuzuki | 4:36a7b7878738 | 115 | char cmd2 = xbee.getc(); |
kosukesuzuki | 4:36a7b7878738 | 116 | |
kosukesuzuki | 4:36a7b7878738 | 117 | if(cmd2 == 'n'){//5 |
kosukesuzuki | 4:36a7b7878738 | 118 | while(1){//6 |
kosukesuzuki | 4:36a7b7878738 | 119 | if (xbee.readable()) pc.putc(xbee.getc()); |
kosukesuzuki | 4:36a7b7878738 | 120 | char cmd3 = xbee.getc();//cmd共有 |
kosukesuzuki | 4:36a7b7878738 | 121 | |
kosukesuzuki | 4:36a7b7878738 | 122 | for(j=0;j < sizeof(mm)/sizeof(mm[0]);j++) {//7 |
kosukesuzuki | 4:36a7b7878738 | 123 | sound.attach(&tick,1.0/mm[j]/2.0); //並列プログラムによるえらーなので,無視 |
kosukesuzuki | 4:36a7b7878738 | 124 | wait(0.5f); |
kosukesuzuki | 4:36a7b7878738 | 125 | }//7 |
kosukesuzuki | 4:36a7b7878738 | 126 | |
kosukesuzuki | 4:36a7b7878738 | 127 | sound.detach(); |
kosukesuzuki | 4:36a7b7878738 | 128 | j = 0; |
kosukesuzuki | 4:36a7b7878738 | 129 | |
kosukesuzuki | 4:36a7b7878738 | 130 | if(cmd3 == 'k'){//7 |
kosukesuzuki | 4:36a7b7878738 | 131 | pc.printf("end\r\n"); |
kosukesuzuki | 4:36a7b7878738 | 132 | sound.detach(); |
kosukesuzuki | 4:36a7b7878738 | 133 | //return 0;//end |
kosukesuzuki | 4:36a7b7878738 | 134 | }//7 |
kosukesuzuki | 4:36a7b7878738 | 135 | }//6 |
kosukesuzuki | 4:36a7b7878738 | 136 | }//5 |
kosukesuzuki | 4:36a7b7878738 | 137 | }//4 |
kosukesuzuki | 4:36a7b7878738 | 138 | }//3 |
kosukesuzuki | 4:36a7b7878738 | 139 | }//2 |
kosukesuzuki | 4:36a7b7878738 | 140 | }//1 |