gps test 161222

Dependencies:   SDFileSystem mbed

main.cpp

Committer:
Nike3221
Date:
2016-12-22
Revision:
0:6cb9ece2cb7e

File content as of revision 0:6cb9ece2cb7e:

#include "mbed.h"
#include "SDFileSystem.h"

SDFileSystem sd(p5, p6, p7, p8, "sd");
//sdFileSystem sd("sd");
DigitalOut led1(LED1);
DigitalIn cds(p29);
PwmOut l_moter(p22);
PwmOut r_moter(p21);
PwmOut servo(p24);
Serial gps(p13,p14);
Serial gps2(p9,p10);
Serial pc(USBTX,USBRX);

time_t seconds = time(NULL) + (60 * 60 * 9); // JST
struct tm *t = localtime(&seconds);

char gps_c[250];
int a,b,c,d,e,f,g;
float h,i,j,k,l,m,n,servo_pul;
char o,p;

int sd_write(){
               a=0;
               pc.printf("sequence2\n");
               
               while(a <= 159)
                {
                   led1=1;
                   servo_pul = servo_pul+0.0001;
                   servo.pulsewidth(servo_pul);
                   if(servo_pul >= 0.0022)
                    { servo_pul=0.0008;}
                   gps_c[a] = gps.getc();
                   pc.printf("%d %c\n",a,gps_c[a]);
                
                   if(gps_c[a] == '\n')//改行まできたとき
                     { gps_c[a+1] = '\0';
                       a=0;
                       pc.printf("gps_c = NULL\n");
                     
                   
                       if(memcmp(gps_c,"$GPRMC",6) == 0)
                         { sscanf(gps_c,"$GPRMC,%f,%c,%f,N,%f,E,%f,%f,%f,%f,W",&h,&o,&i,&j,&k,&l,&m,&n);
                           pc.printf("$GPRMC,%f,%c,%f,N,%f,E,%f,%f,%f,%f,W \n",h,o,i,j,k,l,m,n);
                           pc.printf("%04d/%02d/%02d %02d:%02d:%02d\n",
                           t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec);
                     
                           //mkdir("/sd/gps", 0777);
                           FILE *fp = fopen("/sd/gps.txt", "a");
                           if(fp == NULL) {
                                error("Could not open file for write\n");
                                           } 

                           fprintf(fp,"$GPRMC,%f,%c,%f,N,%f,E,%f,%f,%f,%f,W \n",h,o,i,j,k,l,m,n);
                           fprintf(fp,"%04d/%02d/%02d %02d:%02d:%02d\n",
                           t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec);
                           
                           fclose(fp);
                        }
                        
                        if(memcmp(gps_c,"$GPGGA",6) == 0)
                         { sscanf(gps_c,"$GPGGA,%f,%f,N,%f,E,%d,%d,%f,%f,M,%f,M",&h,&i,&j,&b,&c,&k,&l,&m);
                           pc.printf("$GPGGA,%f,%f,N,%f,E,%d,%d,%f,%f,M,%f,M\n",h,i,j,b,c,k,l,m);
                           pc.printf("%04d/%02d/%02d %02d:%02d:%02d\n",
                           t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec);
                     
                          // mkdir("/sd/gps", 0777);
                           FILE *fp = fopen("/sd/gps.txt", "a");
                           if(fp == NULL) {
                                error("Could not open file for write\n");
                                           } 

                           fprintf(fp,"$GPGGA,%f,%f,N,%f,E,%d,%d,%f,%f,M,%f,M",h,i,j,b,c,k,l,m);
                           fprintf(fp,"%04d/%02d/%02d %02d:%02d:%02d\n",
                           t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec);
                           
                           fclose(fp);
                        }}
                        
                     
                   
                   else if(gps_c[a] == '$')
                     { a=1;
                       gps_c[0] = '$';
                       pc.printf("gps_c = $\n");
                     }
               
                   else
                   { a++; }
               }
               led1=0;
               return(0);
}

int main(void){
               servo.period(0.020);
               servo_pul=0;
               pc.printf("%04d/%02d/%02d %02d:%02d:%02d\n",
               t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec);
               
               FILE *fp = fopen("/sd/gps.txt", "a");
               if(fp == NULL) {error("Could not open file for write\n");} 
               fprintf(fp,"start %04d/%02d/%02d %02d:%02d:%02d\n",
               t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec);
               fclose(fp);            
               
               while(1){ 
                        
                        if(cds==0)
                         {
                           while(1){
                                     pc.printf("sequence1 %04d/%02d/%02d %02d:%02d:%02d\n",
                                     t->tm_year +1900, t->tm_mon+12, t->tm_mday+7, t->tm_hour+15, t->tm_min-54,t->tm_sec);

                                     
                                     sd_write();
                                     wait(0.1);
                                   }
                         }
                       } 
                  
}