mygps.c : Updated command line usage to be compliant with Luceor
authorBenoit Papillault <benoit.papillault@free.fr>
Thu, 14 Jul 2011 06:51:07 +0000 (08:51 +0200)
committerBenoit Papillault <benoit.papillault@free.fr>
Thu, 14 Jul 2011 06:51:07 +0000 (08:51 +0200)
specifications and to allow GPS position logging

mygps.c

diff --git a/mygps.c b/mygps.c
index d5e5e97..146142d 100644 (file)
--- a/mygps.c
+++ b/mygps.c
@@ -5,6 +5,9 @@
 
   In order to compile :
   # aptitude install libgps-dev
+
+  Warning: on google maps, point are : latitude,longitude (not
+    longitude,latitude)
 */
 
 #include <stdio.h>
@@ -196,96 +199,148 @@ struct gps_handler_t {
 #define sizetab(tab) (sizeof(tab)/sizeof(tab[0]))
 
 void usage() {
-       fprintf(stderr,"usage: mygps [--data]\n");
+       printf("usage: mygps [-set longitude,latitude] [-get|-log]\n");
        exit (-1);
 }
 
 int main(int argc, const char *argv[]) {
        int i;
        struct gps_data_t gpsdata, *gps = &gpsdata;
-       int data_mode = 0;
+       int get_mode = 0, get_mode_done = 0;
+       int log_mode = 0;
+       int set_mode = 0;
+       double ref_longitude, ref_latitude;
+       int debug_mode = 0;
 
        for (i=1; i<argc; i++) {
-               if (strcmp(argv[i], "--data")==0) {
-                       data_mode = 1;
-               } else {
-                       usage();
-               }
+         if (strcmp(argv[i],"-get")==0) {
+           get_mode = 1;
+         } else if (strcmp(argv[i],"-log")==0) {
+           log_mode = 1;
+         } else if (strcmp(argv[i],"-set")==0 && i+1<argc) {
+           set_mode = 1;
+           if (sscanf(argv[i+1],"%lf,%lf", &ref_longitude, &ref_latitude) != 2)
+             usage();
+           i++;
+         } else if (strcmp(argv[i],"-debug")==0) {
+           debug_mode = 1;
+         } else {
+           usage();
+         }
        }
 
-       printf("Compiled libgps API version %d.%d\n",
-              GPSD_API_MAJOR_VERSION, GPSD_API_MINOR_VERSION);
+       if (!get_mode && !log_mode)
+         usage();
 
-       for (;;) {
+       if (debug_mode) {
+         printf("Compiled libgps API version %d.%d\n",
+                GPSD_API_MAJOR_VERSION, GPSD_API_MINOR_VERSION);
+       }
 
-               /* NULL, NULL matches localhost and the default GPSD port */
+       do {
+         
+         /* NULL, NULL matches localhost and the default GPSD port */
 #if GPS_API_MAJOR_VERSION < 5
-               if ((gps = gps_open(NULL, NULL)) == NULL) {
+         if ((gps = gps_open(NULL, NULL)) == NULL) {
 #endif
 #if GPSD_API_MAJOR_VERSION == 5
-               if (gps_open(NULL, NULL, &gpsdata) != 0) {
+         if (gps_open(NULL, NULL, &gpsdata) != 0) {
 #endif
-                       fprintf(stderr, "gps_open: %s\n", gps_errstr(errno));
-                       return -1;
-               }
-
-               if (gps_stream(gps, WATCH_ENABLE, NULL) != 0) {
-                       fprintf(stderr, "gps_stream() failed\n");
-                       return -1;
+           fprintf(stderr, "gps_open: %s\n", gps_errstr(errno));
+           return -1;
+         }
+         
+         if (gps_stream(gps, WATCH_ENABLE, NULL) != 0) {
+           fprintf(stderr, "gps_stream() failed\n");
+           return -1;
+         }
+         
+         do {
+           int i, r;
+           fd_set fdr;
+           struct timeval tv = { 10, 0 };
+           gps_mask_t set;
+           
+           /* wait for data of gps->gps_fd */
+           
+           FD_ZERO(&fdr);
+           FD_SET(gps->gps_fd, &fdr);
+           
+           r = select(FD_SETSIZE, &fdr, NULL, NULL, &tv);
+           if (r == 0) {
+             fprintf(stderr,"Time-out reading from GPS daemon\n");
+             break;
+           }
+           
+           /* Depending on the version of libgps, you need to
+              call gps_read() or gps_poll() */
+           if (gps_read(gps) < 0) {
+             perror("gps_read");
+             break;
+           }
+           
+           gettimeofday(&tv_now, NULL);
+           
+           if (debug_mode) {
+             printf("--- %lu.%06lu ---\n", 
+                    tv_now.tv_sec, tv_now.tv_usec);
+             
+             set = gps->set;
+             for (i=0; i<sizetab(gps_handlers); i++) {
+               if (set & gps_handlers[i].set &&
+                   gps_handlers[i].handler) {
+                 set &= ~gps_handlers[i].set;
+                 gps_handlers[i].handler(gps);
                }
-
-               for (;;) {
-                       int i, r;
-                       fd_set fdr;
-                       struct timeval tv = { 10, 0 };
-                       gps_mask_t set;
-
-                       /* wait for data of gps->gps_fd */
-
-                       FD_ZERO(&fdr);
-                       FD_SET(gps->gps_fd, &fdr);
-
-                       r = select(FD_SETSIZE, &fdr, NULL, NULL, &tv);
-                       if (r == 0) {
-                               printf("Time-out reading from GPS daemon\n");
-                               break;
-                       }
-
-                       /* Depending on the version of libgps, you need to
-                          call gps_read() or gps_poll() */
-                       if (gps_read(gps) < 0) {
-                               perror("gps_read");
-                               break;
-                       }
-
-                       gettimeofday(&tv_now, NULL);
-                       printf("--- %lu.%06lu ---\n", 
-                              tv_now.tv_sec, tv_now.tv_usec);
-
-                       set = gps->set;
-                       for (i=0; i<sizetab(gps_handlers); i++) {
-                               if (set & gps_handlers[i].set &&
-                                   gps_handlers[i].handler) {
-                                       set &= ~gps_handlers[i].set;
-                                       gps_handlers[i].handler(gps);
-                               }
-                       }
-
-                       if (data_mode) {
-                               printf("data %u.%06lu\n",
-                                      tv_now.tv_sec, tv_now.tv_usec);
-                       }
-
-                       fflush(stdout);
-
-                       if (set) {
-                               printf("=> gps->set not decoded : 0x%x\n",
-                                      set);
-                       }
-               }
-
-               gps_close(gps);
-       }
-
+             }
+             if (set) {
+               printf("=> gps->set not decoded : 0x%x\n",
+                      set);
+             }
+           }
+           
+           if ((log_mode || get_mode) &&
+               (gps->set & LATLON_SET) &&
+               (gps->set & ALTITUDE_SET) &&
+               (gps->set & HERR_SET) &&
+               (gps->set & VERR_SET)) {
+
+             /* <time recv>
+                <longitude in °> <latitude in °> <altitude in m>
+                <distance with reference point or 0 >
+                <longitude error in m> <latitude error in m> 
+                <altitude error in m>
+                <horizontal speed in m/s> <vertical speed in m/s>
+                <horizontal speed error in m/s>
+                <vertical speed error in m/s>
+             */
+             printf("%u.%06lu-%f %f %f %f %f %f %f %f %f %f %f %f\n",
+                    /* time recv */
+                    tv_now.tv_sec, tv_now.tv_usec, gps->fix.time,
+                    /* longitude in °, latitude in °, altitude in m */
+                    gps->fix.longitude, gps->fix.latitude, gps->fix.altitude,
+                    /* distance with ref point */
+                    set_mode ? earth_distance(gps->fix.latitude, gps->fix.longitude,
+                                              ref_latitude, ref_longitude) : 0.0,
+                    /* longitude error in m, latitude error in m, altitude error in m */
+                    gps->fix.epx, gps->fix.epy, gps->fix.epv,
+                    /* horizontal speed in m/s */
+                    gps->set & SPEED_SET ? gps->fix.speed : 0.0,
+                    /* vertical speed in m/s */
+                    gps->set & CLIMB_SET ? gps->fix.climb : 0.0,
+                    /* horizontal speed error in m/s */
+                    gps->set & SPEEDERR_SET ? gps->fix.eps : 0.0,
+                    /* vertical speed error in m/s */
+                    gps->set & CLIMBERR_SET ? gps->fix.epc : 0.0);
+             fflush(stdout);
+             if (get_mode)
+               get_mode_done = 1;
+           }
+         } while (!get_mode_done);
+           
+         gps_close(gps);
+
+       } while (!get_mode_done);
+       
        return 0;
 }