gps test 161222
Dependencies: SDFileSystem mbed
main.cpp@0:6cb9ece2cb7e, 2016-12-22 (annotated)
- Committer:
- Nike3221
- Date:
- Thu Dec 22 07:43:26 2016 +0000
- Revision:
- 0:6cb9ece2cb7e
GPS?test161222
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nike3221 | 0:6cb9ece2cb7e | 1 | #include "mbed.h" |
Nike3221 | 0:6cb9ece2cb7e | 2 | #include "SDFileSystem.h" |
Nike3221 | 0:6cb9ece2cb7e | 3 | |
Nike3221 | 0:6cb9ece2cb7e | 4 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
Nike3221 | 0:6cb9ece2cb7e | 5 | //sdFileSystem sd("sd"); |
Nike3221 | 0:6cb9ece2cb7e | 6 | DigitalOut led1(LED1); |
Nike3221 | 0:6cb9ece2cb7e | 7 | DigitalIn cds(p29); |
Nike3221 | 0:6cb9ece2cb7e | 8 | PwmOut l_moter(p22); |
Nike3221 | 0:6cb9ece2cb7e | 9 | PwmOut r_moter(p21); |
Nike3221 | 0:6cb9ece2cb7e | 10 | PwmOut servo(p24); |
Nike3221 | 0:6cb9ece2cb7e | 11 | Serial gps(p13,p14); |
Nike3221 | 0:6cb9ece2cb7e | 12 | Serial gps2(p9,p10); |
Nike3221 | 0:6cb9ece2cb7e | 13 | Serial pc(USBTX,USBRX); |
Nike3221 | 0:6cb9ece2cb7e | 14 | |
Nike3221 | 0:6cb9ece2cb7e | 15 | time_t seconds = time(NULL) + (60 * 60 * 9); // JST |
Nike3221 | 0:6cb9ece2cb7e | 16 | struct tm *t = localtime(&seconds); |
Nike3221 | 0:6cb9ece2cb7e | 17 | |
Nike3221 | 0:6cb9ece2cb7e | 18 | char gps_c[250]; |
Nike3221 | 0:6cb9ece2cb7e | 19 | int a,b,c,d,e,f,g; |
Nike3221 | 0:6cb9ece2cb7e | 20 | float h,i,j,k,l,m,n,servo_pul; |
Nike3221 | 0:6cb9ece2cb7e | 21 | char o,p; |
Nike3221 | 0:6cb9ece2cb7e | 22 | |
Nike3221 | 0:6cb9ece2cb7e | 23 | int sd_write(){ |
Nike3221 | 0:6cb9ece2cb7e | 24 | a=0; |
Nike3221 | 0:6cb9ece2cb7e | 25 | pc.printf("sequence2\n"); |
Nike3221 | 0:6cb9ece2cb7e | 26 | |
Nike3221 | 0:6cb9ece2cb7e | 27 | while(a <= 159) |
Nike3221 | 0:6cb9ece2cb7e | 28 | { |
Nike3221 | 0:6cb9ece2cb7e | 29 | led1=1; |
Nike3221 | 0:6cb9ece2cb7e | 30 | servo_pul = servo_pul+0.0001; |
Nike3221 | 0:6cb9ece2cb7e | 31 | servo.pulsewidth(servo_pul); |
Nike3221 | 0:6cb9ece2cb7e | 32 | if(servo_pul >= 0.0022) |
Nike3221 | 0:6cb9ece2cb7e | 33 | { servo_pul=0.0008;} |
Nike3221 | 0:6cb9ece2cb7e | 34 | gps_c[a] = gps.getc(); |
Nike3221 | 0:6cb9ece2cb7e | 35 | pc.printf("%d %c\n",a,gps_c[a]); |
Nike3221 | 0:6cb9ece2cb7e | 36 | |
Nike3221 | 0:6cb9ece2cb7e | 37 | if(gps_c[a] == '\n')//改行まできたとき |
Nike3221 | 0:6cb9ece2cb7e | 38 | { gps_c[a+1] = '\0'; |
Nike3221 | 0:6cb9ece2cb7e | 39 | a=0; |
Nike3221 | 0:6cb9ece2cb7e | 40 | pc.printf("gps_c = NULL\n"); |
Nike3221 | 0:6cb9ece2cb7e | 41 | |
Nike3221 | 0:6cb9ece2cb7e | 42 | |
Nike3221 | 0:6cb9ece2cb7e | 43 | if(memcmp(gps_c,"$GPRMC",6) == 0) |
Nike3221 | 0:6cb9ece2cb7e | 44 | { sscanf(gps_c,"$GPRMC,%f,%c,%f,N,%f,E,%f,%f,%f,%f,W",&h,&o,&i,&j,&k,&l,&m,&n); |
Nike3221 | 0:6cb9ece2cb7e | 45 | pc.printf("$GPRMC,%f,%c,%f,N,%f,E,%f,%f,%f,%f,W \n",h,o,i,j,k,l,m,n); |
Nike3221 | 0:6cb9ece2cb7e | 46 | pc.printf("%04d/%02d/%02d %02d:%02d:%02d\n", |
Nike3221 | 0:6cb9ece2cb7e | 47 | t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec); |
Nike3221 | 0:6cb9ece2cb7e | 48 | |
Nike3221 | 0:6cb9ece2cb7e | 49 | //mkdir("/sd/gps", 0777); |
Nike3221 | 0:6cb9ece2cb7e | 50 | FILE *fp = fopen("/sd/gps.txt", "a"); |
Nike3221 | 0:6cb9ece2cb7e | 51 | if(fp == NULL) { |
Nike3221 | 0:6cb9ece2cb7e | 52 | error("Could not open file for write\n"); |
Nike3221 | 0:6cb9ece2cb7e | 53 | } |
Nike3221 | 0:6cb9ece2cb7e | 54 | |
Nike3221 | 0:6cb9ece2cb7e | 55 | fprintf(fp,"$GPRMC,%f,%c,%f,N,%f,E,%f,%f,%f,%f,W \n",h,o,i,j,k,l,m,n); |
Nike3221 | 0:6cb9ece2cb7e | 56 | fprintf(fp,"%04d/%02d/%02d %02d:%02d:%02d\n", |
Nike3221 | 0:6cb9ece2cb7e | 57 | t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec); |
Nike3221 | 0:6cb9ece2cb7e | 58 | |
Nike3221 | 0:6cb9ece2cb7e | 59 | fclose(fp); |
Nike3221 | 0:6cb9ece2cb7e | 60 | } |
Nike3221 | 0:6cb9ece2cb7e | 61 | |
Nike3221 | 0:6cb9ece2cb7e | 62 | if(memcmp(gps_c,"$GPGGA",6) == 0) |
Nike3221 | 0:6cb9ece2cb7e | 63 | { sscanf(gps_c,"$GPGGA,%f,%f,N,%f,E,%d,%d,%f,%f,M,%f,M",&h,&i,&j,&b,&c,&k,&l,&m); |
Nike3221 | 0:6cb9ece2cb7e | 64 | pc.printf("$GPGGA,%f,%f,N,%f,E,%d,%d,%f,%f,M,%f,M\n",h,i,j,b,c,k,l,m); |
Nike3221 | 0:6cb9ece2cb7e | 65 | pc.printf("%04d/%02d/%02d %02d:%02d:%02d\n", |
Nike3221 | 0:6cb9ece2cb7e | 66 | t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec); |
Nike3221 | 0:6cb9ece2cb7e | 67 | |
Nike3221 | 0:6cb9ece2cb7e | 68 | // mkdir("/sd/gps", 0777); |
Nike3221 | 0:6cb9ece2cb7e | 69 | FILE *fp = fopen("/sd/gps.txt", "a"); |
Nike3221 | 0:6cb9ece2cb7e | 70 | if(fp == NULL) { |
Nike3221 | 0:6cb9ece2cb7e | 71 | error("Could not open file for write\n"); |
Nike3221 | 0:6cb9ece2cb7e | 72 | } |
Nike3221 | 0:6cb9ece2cb7e | 73 | |
Nike3221 | 0:6cb9ece2cb7e | 74 | fprintf(fp,"$GPGGA,%f,%f,N,%f,E,%d,%d,%f,%f,M,%f,M",h,i,j,b,c,k,l,m); |
Nike3221 | 0:6cb9ece2cb7e | 75 | fprintf(fp,"%04d/%02d/%02d %02d:%02d:%02d\n", |
Nike3221 | 0:6cb9ece2cb7e | 76 | t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec); |
Nike3221 | 0:6cb9ece2cb7e | 77 | |
Nike3221 | 0:6cb9ece2cb7e | 78 | fclose(fp); |
Nike3221 | 0:6cb9ece2cb7e | 79 | }} |
Nike3221 | 0:6cb9ece2cb7e | 80 | |
Nike3221 | 0:6cb9ece2cb7e | 81 | |
Nike3221 | 0:6cb9ece2cb7e | 82 | |
Nike3221 | 0:6cb9ece2cb7e | 83 | else if(gps_c[a] == '$') |
Nike3221 | 0:6cb9ece2cb7e | 84 | { a=1; |
Nike3221 | 0:6cb9ece2cb7e | 85 | gps_c[0] = '$'; |
Nike3221 | 0:6cb9ece2cb7e | 86 | pc.printf("gps_c = $\n"); |
Nike3221 | 0:6cb9ece2cb7e | 87 | } |
Nike3221 | 0:6cb9ece2cb7e | 88 | |
Nike3221 | 0:6cb9ece2cb7e | 89 | else |
Nike3221 | 0:6cb9ece2cb7e | 90 | { a++; } |
Nike3221 | 0:6cb9ece2cb7e | 91 | } |
Nike3221 | 0:6cb9ece2cb7e | 92 | led1=0; |
Nike3221 | 0:6cb9ece2cb7e | 93 | return(0); |
Nike3221 | 0:6cb9ece2cb7e | 94 | } |
Nike3221 | 0:6cb9ece2cb7e | 95 | |
Nike3221 | 0:6cb9ece2cb7e | 96 | int main(void){ |
Nike3221 | 0:6cb9ece2cb7e | 97 | servo.period(0.020); |
Nike3221 | 0:6cb9ece2cb7e | 98 | servo_pul=0; |
Nike3221 | 0:6cb9ece2cb7e | 99 | pc.printf("%04d/%02d/%02d %02d:%02d:%02d\n", |
Nike3221 | 0:6cb9ece2cb7e | 100 | t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec); |
Nike3221 | 0:6cb9ece2cb7e | 101 | |
Nike3221 | 0:6cb9ece2cb7e | 102 | FILE *fp = fopen("/sd/gps.txt", "a"); |
Nike3221 | 0:6cb9ece2cb7e | 103 | if(fp == NULL) {error("Could not open file for write\n");} |
Nike3221 | 0:6cb9ece2cb7e | 104 | fprintf(fp,"start %04d/%02d/%02d %02d:%02d:%02d\n", |
Nike3221 | 0:6cb9ece2cb7e | 105 | t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec); |
Nike3221 | 0:6cb9ece2cb7e | 106 | fclose(fp); |
Nike3221 | 0:6cb9ece2cb7e | 107 | |
Nike3221 | 0:6cb9ece2cb7e | 108 | while(1){ |
Nike3221 | 0:6cb9ece2cb7e | 109 | |
Nike3221 | 0:6cb9ece2cb7e | 110 | if(cds==0) |
Nike3221 | 0:6cb9ece2cb7e | 111 | { |
Nike3221 | 0:6cb9ece2cb7e | 112 | while(1){ |
Nike3221 | 0:6cb9ece2cb7e | 113 | pc.printf("sequence1 %04d/%02d/%02d %02d:%02d:%02d\n", |
Nike3221 | 0:6cb9ece2cb7e | 114 | t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec); |
Nike3221 | 0:6cb9ece2cb7e | 115 | |
Nike3221 | 0:6cb9ece2cb7e | 116 | |
Nike3221 | 0:6cb9ece2cb7e | 117 | sd_write(); |
Nike3221 | 0:6cb9ece2cb7e | 118 | wait(0.1); |
Nike3221 | 0:6cb9ece2cb7e | 119 | } |
Nike3221 | 0:6cb9ece2cb7e | 120 | } |
Nike3221 | 0:6cb9ece2cb7e | 121 | } |
Nike3221 | 0:6cb9ece2cb7e | 122 | |
Nike3221 | 0:6cb9ece2cb7e | 123 | } |