Initial source code using libgps (http://gpsd.berlios.de/ and various script
authorBenoit Papillault <benoit.papillault@free.fr>
Sun, 12 Jun 2011 12:22:57 +0000 (14:22 +0200)
committerBenoit Papillault <benoit.papillault@free.fr>
Sun, 12 Jun 2011 12:22:57 +0000 (14:22 +0200)
used to display data with gnuplot

Makefile [new file with mode: 0644]
mygps.c [new file with mode: 0644]
script-clock-drift [new file with mode: 0644]
script-lat-lon [new file with mode: 0755]
script-sat [new file with mode: 0755]
script-system-clock [new file with mode: 0755]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..33d41dd
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,4 @@
+CFLAGS += -Wall
+LDLIBS += -lgps
+
+default: mygps
diff --git a/mygps.c b/mygps.c
new file mode 100644 (file)
index 0000000..f72f582
--- /dev/null
+++ b/mygps.c
@@ -0,0 +1,239 @@
+/*
+  Author : Benoit Papillault <benoit.papillault@free.fr>
+  Creation : 2011-06-08
+  Goal : sample example to use libgps
+
+  In order to compile :
+  # aptitude install libgps-dev
+*/
+
+#include <stdio.h>
+#include <gps.h>
+#include <errno.h>
+
+struct timeval tv_now;
+
+double gps_lat_ori = 48.82408;
+double gps_long_ori = 2.261415;
+
+void gps_time_set(struct gps_data_t *gps) {
+       static double prev_fix_time = 0.0;
+       double cur_diff;
+       static double pre_diff = 0.0;
+
+       if (gps->fix.time > prev_fix_time) {
+               double now = (double)tv_now.tv_sec +
+                       (double)tv_now.tv_usec * 0.000001;
+               cur_diff = gps->fix.time - now;
+               printf("Fix time: %f s (diff: %f, vdiff: %f)\n", gps->fix.time,
+                      cur_diff, cur_diff - pre_diff);
+               pre_diff = cur_diff;
+               prev_fix_time = gps->fix.time;
+       } else {
+               printf("Fix time: %f s\n", gps->fix.time);
+       }
+}
+
+void gps_timerr_set(struct gps_data_t *gps) {
+       /* Apparently, this is set to 0.005 s by default, so we probably
+        * cannot rely on it*/
+       printf("Fix time error: %f s\n", gps->fix.ept);
+}
+
+void gps_latlon_set(struct gps_data_t *gps) {
+       double d;
+
+       d = earth_distance(gps_lat_ori, gps_long_ori,
+                          gps->fix.latitude, gps->fix.longitude);
+       printf("Latitude: %f °, Longitude: %f ° (Distance: %f m)\n",
+              gps->fix.latitude, gps->fix.longitude, d);
+}
+
+void gps_altitude_set(struct gps_data_t *gps) {
+       printf("Altitude: %f m\n", gps->fix.altitude);
+}
+
+void gps_speed_set(struct gps_data_t *gps) {
+       printf("Horizontal speed: %f m/s\n", gps->fix.speed);
+}
+
+void gps_track_set(struct gps_data_t *gps) {
+       /* what does this mean ? */
+       printf("Track: %f °\n", gps->fix.track);
+}
+
+void gps_climb_set(struct gps_data_t *gps) {
+       printf("Vertical speed: %f m/s\n", gps->fix.climb);
+}
+
+void gps_status_set(struct gps_data_t *gps) {
+       const char *status_values[] = {
+               [STATUS_NO_FIX]         = "No",
+               [STATUS_FIX]            = "Yes (without DGPS)",
+               [STATUS_DGPS_FIX]       = "Yes (with DGPS)" };
+       printf("Fix:%s\n", status_values[gps->status]);
+}
+
+void gps_mode_set(struct gps_data_t *gps) {
+
+       const char *mode_values[] = {
+               [MODE_NOT_SEEN] = "Not seen",
+               [MODE_NO_FIX]   = "No FIX ",
+               [MODE_2D]       = "2D FIX",
+               [MODE_3D]       = "3D FIX" };
+
+       printf("Mode: %s\n", mode_values[gps->fix.mode]);
+}
+
+void gps_version_set(struct gps_data_t *gps) {
+
+       printf("Running libgps API version %d.%d (%s %s)\n",
+              gps->version.proto_major,
+              gps->version.proto_minor,
+              gps->version.release, gps->version.rev);
+}
+
+void gps_herr_set(struct gps_data_t *gps) {
+       printf("Latitude error: %f m, Longitude error: %f m\n",
+              gps->fix.epy, gps->fix.epx);
+}
+
+void gps_verr_set(struct gps_data_t *gps) {
+       printf("Altitude error: %f m\n", gps->fix.epv);
+}
+
+void gps_policy_set(struct gps_data_t *gps) {
+       printf("watcher:%d json:%d nmea:%d timing:%d devpath:%s\n",
+              gps->policy.watcher, gps->policy.json,
+              gps->policy.nmea, gps->policy.timing,
+              gps->policy.devpath);
+}
+
+void gps_satellite_set(struct gps_data_t *gps) {
+       int i;
+
+       printf("Satellites in view: %d\n",
+              gps->satellites_visible);
+       for (i = 0; i < gps->satellites_visible; i++) {
+               printf("    %2.2d: %2.2d %3.3d %3.0f dB, %c\n",
+                      gps->PRN[i], gps->elevation[i],
+                      gps->azimuth[i], gps->ss[i],
+                      gps->used[i] ? 'Y' : 'N');
+       }
+}
+
+void gps_speederr_set(struct gps_data_t *gps) {
+       printf("Horizontal speed error: %f m/s\n", gps->fix.eps);
+}
+
+void gps_trackerr_set(struct gps_data_t *gps) {
+       printf("Track error: %f °\n", gps->fix.epd);
+}
+
+void gps_climberr_set(struct gps_data_t *gps) {
+       printf("Vertical speed error: %f m/s\n", gps->fix.epc);
+}
+
+void gps_device_set(struct gps_data_t *gps) {
+       printf("Device is %s, driver is %s\n",
+              gps->dev.path, gps->dev.driver);
+}
+
+void gps_devicelist_set(struct gps_data_t *gps) {
+
+       int i;
+                       
+       printf("Time: %f s, %d device(s) :",
+              gps->devices.time, gps->devices.ndevices);
+       for (i=0; i<gps->devices.ndevices; i++) {
+               printf("\t%s\n", gps->devices.list[i].path);
+       }
+}
+
+void gps_packet_set(struct gps_data_t *gps) {
+       /* display nothing on purpose */
+}
+
+struct gps_handler_t {
+       gps_mask_t set;
+       void (*handler)(struct gps_data_t *gps);
+} gps_handlers[] = {
+       { ONLINE_SET,           NULL },
+       { TIME_SET,             gps_time_set },
+       { TIMERR_SET,           gps_timerr_set },
+       { LATLON_SET,           gps_latlon_set },
+       { ALTITUDE_SET,         gps_altitude_set },
+       { SPEED_SET,            gps_speed_set },
+       { TRACK_SET,            gps_track_set },
+       { CLIMB_SET,            gps_climb_set },
+       { STATUS_SET,           gps_status_set },
+       { MODE_SET,             gps_mode_set },
+       { DOP_SET,              NULL },
+       { VERSION_SET,          gps_version_set },
+       { HERR_SET,             gps_herr_set },
+       { VERR_SET,             gps_verr_set },
+       { ATTITUDE_SET,         NULL },
+       { POLICY_SET,           gps_policy_set },
+       { SATELLITE_SET,        gps_satellite_set },
+       { RAW_SET,              NULL },
+       { USED_SET,             NULL },
+       { SPEEDERR_SET,         gps_speederr_set },
+       { TRACKERR_SET,         gps_trackerr_set },
+       { CLIMBERR_SET,         gps_climberr_set },
+       { DEVICE_SET,           gps_device_set },
+       { DEVICELIST_SET,       gps_devicelist_set },
+       { DEVICEID_SET,         NULL },
+       { ERROR_SET,            NULL },
+       { RTCM2_SET,            NULL },
+       { RTCM3_SET,            NULL },
+       { AIS_SET,              NULL },
+       { PACKET_SET,           gps_packet_set },
+};
+
+#define sizetab(tab) (sizeof(tab)/sizeof(tab[0]))
+
+int main() {
+       struct gps_data_t *gps;
+
+       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 */
+       if ((gps = gps_open(NULL, NULL)) == NULL) {
+               fprintf(stderr, "gps_open: %s\n", gps_errstr(errno));
+               return -1;
+       }
+
+       gps_stream(gps, WATCH_ENABLE, NULL);
+
+       for (;;) {
+               int i;
+
+               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);
+
+               for (i=0; i<sizetab(gps_handlers); i++) {
+                       if (gps->set & gps_handlers[i].set &&
+                           gps_handlers[i].handler) {
+                               gps->set &= ~gps_handlers[i].set;
+                               gps_handlers[i].handler(gps);
+                       }
+               }
+               fflush(stdout);
+
+               if (gps->set) {
+                       printf("=> gps->set not decoded : 0x%x\n", gps->set);
+               }
+       }
+
+
+       gps_close(gps);
+
+       return 0;
+}
diff --git a/script-clock-drift b/script-clock-drift
new file mode 100644 (file)
index 0000000..e9403cc
--- /dev/null
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+grep vdiff /tmp/mygps.txt  | awk '{print $3" "$6" "$8}' | sed -e 's|[),]||g' > x
+
+nb=0; sum=0; while read x y z; do nb=$(( $nb + 1 )) ; sum=$(echo "${sum} + ${y}" |bc); avg=$(echo "scale=6; ${sum} / ${nb}.0" | bc); echo "nb=${nb} sum=${sum} avg=${av^C" ; done < x
+
+gnuplot <<EOF
+plot "x" using ($1-1307581737.000000):(-$2) with line, 4.153258 with line
+EOF
+
diff --git a/script-lat-lon b/script-lat-lon
new file mode 100755 (executable)
index 0000000..996c4f1
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+GPS=mygps-2.txt
+
+grep "^Latitude:" ${GPS} > lat-lon.txt
+grep "^Latitude error:" ${GPS} > lat-lon-error.txt
+gnuplot <<EOF
+set terminal png size 800,600
+set output "positions.png"
+plot "lat-lon.txt" using 2:5 with lines title "My position over time"
+EOF
+
+gnuplot <<EOF
+set terminal png size 800,600
+set output "distances.png"
+set yrange [0:100]
+plot "lat-lon.txt" using 8 with lines title "distance (m)" , \
+  "lat-lon-error.txt" using 3 with lines title "latitude error (m)", \
+  "lat-lon-error.txt" using 7 with lines title "longitude error (m)"
+EOF
\ No newline at end of file
diff --git a/script-sat b/script-sat
new file mode 100755 (executable)
index 0000000..498501a
--- /dev/null
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+GPS=mygps-2.txt
+
+# get data for each satellite number 00..99
+for id in $(seq 0 99); do
+    x=$(printf %02d $id)
+    echo $x
+    grep "^    ${x}: " ${GPS} > "sat-${x}.txt"
+done
+
+# remove empty files
+find . -size 0 -exec rm {} \;
+
+# create graph of signal over time
+(
+    cat <<EOF
+set terminal png size 800,600
+set output "sat-all-signals.png"
+plot 0 with lines \\
+EOF
+    for id in $(seq 0 99); do
+       x=$(printf %02d $id)
+       if [ -e "sat-${x}.txt" ]; then
+
+           gnuplot <<EOF
+set terminal png size 800,600
+set output "sat-${x}-signal.png"
+plot "sat-${x}.txt" using 4 with lines title "Satellite ${x} signal" 
+EOF
+
+           gnuplot <<EOF
+set terminal png size 800,600
+set output "sat-${x}-position.png"
+set xrange [0:360]
+set yrange [0:360]
+plot "sat-${x}.txt" using 2:3 with lines title "Satellite ${x} position"
+EOF
+
+           cat <<EOF
+  , "sat-${x}.txt" using 4 with lines title "Satellite ${x}" \\
+EOF
+       fi
+    done
+) | gnuplot
\ No newline at end of file
diff --git a/script-system-clock b/script-system-clock
new file mode 100755 (executable)
index 0000000..46ca13e
--- /dev/null
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+grep "^---" /tmp/mygps.txt | awk '{print $2}' | while read x; do
+    echo "x=${x} prev=${prev} last=${last} ..."
+    if [ "${prev}" == "" ]; then
+       last="${x}"
+       echo "${last}"
+    else
+       diff=$(echo "${x} - ${last}" | bc)
+       #echo " last=${last} x=${x} => diff=${diff}"
+       greater=$(echo "${diff} >= 1.0"|bc)
+       if [ "${greater}" -eq 1 ]; then
+           # we need to choose between ${prev} and ${x}
+           diff1=$(echo "1.0 - (${prev} - ${last})"|bc)
+           diff2=$(echo "(${x} - ${last}) - 1.0"|bc)
+           #echo "diff1=${diff1} diff2=${diff2}"
+           lower=$(echo "${diff1} < ${diff2}"|bc)
+           if [ "${lower}" -eq 1 ]; then
+               echo "${prev} diff1=${diff1}"
+               last="${prev}"
+           else
+               echo "${x} diff2=${diff2}"
+               last="${x}"
+           fi
+       fi
+    fi
+    prev="${x}"
+done
\ No newline at end of file