kk
Dependencies: SDFileSystem mbed
Fork of cansat_kk by
main.cpp@3:efaa785283a6, 2016-02-29 (annotated)
- Committer:
- mosukro
- Date:
- Mon Feb 29 18:36:45 2016 +0000
- Revision:
- 3:efaa785283a6
- Parent:
- 2:be9046fb5859
- Child:
- 4:cfaf33b2c97a
sample plogram 160301
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nike3221 | 0:649fc30be6ec | 1 | #include "mbed.h" |
Nike3221 | 0:649fc30be6ec | 2 | #include "SDFileSystem.h" |
Nike3221 | 0:649fc30be6ec | 3 | |
Nike3221 | 0:649fc30be6ec | 4 | Ticker timer; |
Nike3221 | 0:649fc30be6ec | 5 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
mosukro | 1:fa44a6246bcc | 6 | Serial gps(p28, p27); // tx, rx |
Nike3221 | 0:649fc30be6ec | 7 | Serial pc(USBTX, USBRX); // tx, rx |
Nike3221 | 0:649fc30be6ec | 8 | PwmOut moterl(p21);//左モーター |
Nike3221 | 0:649fc30be6ec | 9 | PwmOut moterr(p22);//右モーター |
Nike3221 | 0:649fc30be6ec | 10 | DigitalOut led1(LED1); |
Nike3221 | 0:649fc30be6ec | 11 | DigitalOut fet3(p23);//ニクロム線 |
Nike3221 | 0:649fc30be6ec | 12 | DigitalOut led2(LED2); |
Nike3221 | 0:649fc30be6ec | 13 | DigitalOut led3(LED3); |
Nike3221 | 0:649fc30be6ec | 14 | DigitalOut led4(LED4); |
Nike3221 | 0:649fc30be6ec | 15 | DigitalOut janpa1(p19);//パラシュートの開きを検知 |
Nike3221 | 0:649fc30be6ec | 16 | DigitalIn janpa2(p20);//パラシュートの開きを検知 |
Nike3221 | 0:649fc30be6ec | 17 | |
mosukro | 1:fa44a6246bcc | 18 | char gps_data[256]; |
mosukro | 1:fa44a6246bcc | 19 | float longitude,latitude,gpstime,knot,angle; |
Nike3221 | 0:649fc30be6ec | 20 | int i=0; |
mosukro | 2:be9046fb5859 | 21 | char *gps_target = "$GPRMC,174813.000,A,3606.9349,N,13927.4196,E,0.42,221.65,280216,,,A*6D"; |
mosukro | 2:be9046fb5859 | 22 | float longitude_target,latitude_target,gpstime_target,knot_target,angle_target; |
mosukro | 3:efaa785283a6 | 23 | int count=0; |
Nike3221 | 0:649fc30be6ec | 24 | |
mosukro | 1:fa44a6246bcc | 25 | void gps_rx()//GPSデータ受信割り込み |
Nike3221 | 0:649fc30be6ec | 26 | { |
mosukro | 1:fa44a6246bcc | 27 | gps_data[i] = gps.getc(); |
mosukro | 1:fa44a6246bcc | 28 | if( gps_data[i] == '$' ) |
mosukro | 2:be9046fb5859 | 29 | { |
mosukro | 1:fa44a6246bcc | 30 | //$から受信データを保持する |
mosukro | 1:fa44a6246bcc | 31 | gps_data[0] = '$'; |
mosukro | 1:fa44a6246bcc | 32 | i = 1; |
mosukro | 1:fa44a6246bcc | 33 | } |
mosukro | 1:fa44a6246bcc | 34 | else if( gps_data[i-1] == '\r' && gps_data[i] == '\n' ) |
mosukro | 1:fa44a6246bcc | 35 | { |
mosukro | 1:fa44a6246bcc | 36 | // 改行コードまでのデータを解析する |
mosukro | 1:fa44a6246bcc | 37 | gps_data[i+1] = '\0'; |
mosukro | 1:fa44a6246bcc | 38 | if(memcmp(gps_data, "$GPRMC",6) == 0) |
mosukro | 1:fa44a6246bcc | 39 | { |
mosukro | 1:fa44a6246bcc | 40 | // $GPRMCで始ってればデータを分けて格納 |
mosukro | 1:fa44a6246bcc | 41 | sscanf(gps_data,"$GPRMC,%f,A,%f,N,%f,E,%f,%f",&gpstime,&longitude,&latitude,&knot,&angle); |
mosukro | 1:fa44a6246bcc | 42 | } |
mosukro | 1:fa44a6246bcc | 43 | //データをSDに書き込み |
mosukro | 1:fa44a6246bcc | 44 | mkdir("/sd/mydir", 0777); |
mosukro | 1:fa44a6246bcc | 45 | FILE *fp = fopen("/sd/mydir/gpsdata.txt", "a"); |
mosukro | 1:fa44a6246bcc | 46 | if(fp == NULL) |
mosukro | 1:fa44a6246bcc | 47 | { |
mosukro | 1:fa44a6246bcc | 48 | error("Could not open file for write\n"); |
mosukro | 1:fa44a6246bcc | 49 | } |
mosukro | 1:fa44a6246bcc | 50 | fprintf(fp,"%s \n",gps_data); |
mosukro | 1:fa44a6246bcc | 51 | fclose(fp); |
Nike3221 | 0:649fc30be6ec | 52 | |
mosukro | 1:fa44a6246bcc | 53 | //PCにデータ送信 |
mosukro | 1:fa44a6246bcc | 54 | pc.printf("%s \n",gps_data); |
mosukro | 1:fa44a6246bcc | 55 | i = 0; |
mosukro | 1:fa44a6246bcc | 56 | } |
mosukro | 1:fa44a6246bcc | 57 | else |
mosukro | 1:fa44a6246bcc | 58 | { |
mosukro | 1:fa44a6246bcc | 59 | //改行コードが来るまでカウントを続ける |
mosukro | 1:fa44a6246bcc | 60 | i++; |
mosukro | 2:be9046fb5859 | 61 | if(i==255) |
mosukro | 2:be9046fb5859 | 62 | { |
mosukro | 1:fa44a6246bcc | 63 | pc.printf("*** Error! ***\n"); |
mosukro | 1:fa44a6246bcc | 64 | } |
mosukro | 1:fa44a6246bcc | 65 | } |
Nike3221 | 0:649fc30be6ec | 66 | |
Nike3221 | 0:649fc30be6ec | 67 | } |
Nike3221 | 0:649fc30be6ec | 68 | |
mosukro | 1:fa44a6246bcc | 69 | void move() |
Nike3221 | 0:649fc30be6ec | 70 | { |
mosukro | 1:fa44a6246bcc | 71 | //動作 |
mosukro | 2:be9046fb5859 | 72 | switch (count) |
mosukro | 2:be9046fb5859 | 73 | { |
mosukro | 2:be9046fb5859 | 74 | case 0: |
mosukro | 3:efaa785283a6 | 75 | //パラシュートを焼き切るまで待つ |
mosukro | 3:efaa785283a6 | 76 | //焼き切ったらcount+1 |
mosukro | 2:be9046fb5859 | 77 | break; |
mosukro | 2:be9046fb5859 | 78 | case 1: |
mosukro | 3:efaa785283a6 | 79 | //落ちてパラシュートを焼き切った後 |
mosukro | 3:efaa785283a6 | 80 | if( (angle_target+3)>=angle || (angle_target-3)<=angle )//3は適当な数字。誤差範囲 |
mosukro | 3:efaa785283a6 | 81 | { |
mosukro | 3:efaa785283a6 | 82 | //角度が合ったらcount+1 |
mosukro | 3:efaa785283a6 | 83 | } |
mosukro | 3:efaa785283a6 | 84 | else |
mosukro | 3:efaa785283a6 | 85 | { |
mosukro | 3:efaa785283a6 | 86 | //角度が合うまで回転 |
mosukro | 3:efaa785283a6 | 87 | if(angle_target>=angle) |
mosukro | 3:efaa785283a6 | 88 | { |
mosukro | 3:efaa785283a6 | 89 | //differ_angle=angle_target-angle; |
mosukro | 3:efaa785283a6 | 90 | //moterlを動かす |
mosukro | 3:efaa785283a6 | 91 | } |
mosukro | 3:efaa785283a6 | 92 | else if(angle>=angle_target) |
mosukro | 3:efaa785283a6 | 93 | { |
mosukro | 3:efaa785283a6 | 94 | //differ_angle=angle-angle_target; |
mosukro | 3:efaa785283a6 | 95 | //moterrを動かす |
mosukro | 3:efaa785283a6 | 96 | } |
mosukro | 3:efaa785283a6 | 97 | } |
mosukro | 2:be9046fb5859 | 98 | break; |
mosukro | 2:be9046fb5859 | 99 | case 2: |
mosukro | 3:efaa785283a6 | 100 | //目標地点の近くまで走行 |
mosukro | 3:efaa785283a6 | 101 | //目標地点に近づいたらcount+1 |
mosukro | 3:efaa785283a6 | 102 | break; |
mosukro | 3:efaa785283a6 | 103 | case 3: |
mosukro | 3:efaa785283a6 | 104 | //動作を停止。割り込みも停止させ、回収待ち |
mosukro | 3:efaa785283a6 | 105 | //moterl=moterr=0; |
mosukro | 3:efaa785283a6 | 106 | //ngps.detach(); |
mosukro | 2:be9046fb5859 | 107 | break; |
mosukro | 2:be9046fb5859 | 108 | default: |
mosukro | 2:be9046fb5859 | 109 | break; |
mosukro | 3:efaa785283a6 | 110 | } |
Nike3221 | 0:649fc30be6ec | 111 | } |
mosukro | 1:fa44a6246bcc | 112 | |
mosukro | 1:fa44a6246bcc | 113 | int main() |
mosukro | 1:fa44a6246bcc | 114 | { |
mosukro | 1:fa44a6246bcc | 115 | gps.baud(9600); |
mosukro | 1:fa44a6246bcc | 116 | gps.attach(gps_rx, Serial::RxIrq);//GPS割り込み |
mosukro | 2:be9046fb5859 | 117 | sscanf(gps_target,"$GPRMC,%f,A,%f,N,%f,E,%f,%f",&gpstime_target,&longitude_target,&latitude_target,&knot_target,&angle_target); |
mosukro | 1:fa44a6246bcc | 118 | |
Nike3221 | 0:649fc30be6ec | 119 | while(1) |
Nike3221 | 0:649fc30be6ec | 120 | { |
mosukro | 1:fa44a6246bcc | 121 | move(); |
Nike3221 | 0:649fc30be6ec | 122 | } |
mosukro | 1:fa44a6246bcc | 123 | |
Nike3221 | 0:649fc30be6ec | 124 | } |