gpsd-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [gpsd-dev] Minimum working example for libgps


From: Michael J. Tubby B.Sc. MIET
Subject: Re: [gpsd-dev] Minimum working example for libgps
Date: Fri, 3 Nov 2017 10:46:45 +0000
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0

Hi David,

Below is the test harness I wrote a couple of weeks ago when I first started using GPSD on one of our products:

#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <errno.h>
#include <math.h>

#include "/usr/local/include/gps.h"

void main(void)
{
        int ret;
        struct gps_data_t gps_data;

        ret = gps_open("localhost", "2947", &gps_data);
        (void)gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);

        for (;;) {

                /* Put this in a loop with a call to a high resolution sleep () in it. */
                if (gps_waiting(&gps_data, 500)) {
                        errno = 0;
                        if (gps_read(&gps_data) == -1) {
                                printf("Read error\n");
                                exit(1);
                        } else {
                                if (gps_data.set) {
                                        printf("\n--- GPS ---\n");
                                        printf("gps_data.online:           %10.0f\n", gps_data.online);
                                        printf("gps_data.status:           %d\n", gps_data.status);
                                        printf("gps_data.satellites_used:  %d\n", gps_data.satellites_used);
                                        printf("gps_data.fix.mode:         %d\n", gps_data.fix.mode);
                                        printf("gps_data.fix.time:         %10.0f\n", gps_data.fix.time);
                                        printf("gps_data.fix.latitude:     %f\n", gps_data.fix.latitude);
                                        printf("gps_data.fix.longitude:    %f\n", gps_data.fix.longitude);
                                        printf("gps_data.fix.altitude:     %f\n", gps_data.fix.altitude);
                                        printf("gps_data.fix.speed:        %f\n", gps_data.fix.speed);
                                        printf("gps_data.fix.track:        %f\n", gps_data.fix.track);
                                        printf("gps_data.dop.pdop:         %f\n", gps_data.dop.pdop);
                                }
                        }
                }
        }
}


the code is a life from the GPSD documentation and "just works"(tm) for me - YMMV.

Platform is ARM dual-core iMX6; Ubuntu 14.04.5 LTS Server, gcc 5

I'm using GPDS 3.19 built from source and installed in /usr/local/...

My test harness compiles:

   cc -o test test.c -l:/usr/local/lib/libgps.so

and runs, thus:

address@hidden:~/vr2200/avld-ng/src/tests$ ./test

--- GPS ---
gps_data.online:           1509705893
gps_data.status:           0
gps_data.satellites_used:  0
gps_data.fix.mode:         0
gps_data.fix.time:                nan
gps_data.fix.latitude:     nan
gps_data.fix.longitude:    nan
gps_data.fix.altitude:     nan
gps_data.fix.speed:        nan
gps_data.fix.track:        nan
gps_data.dop.pdop:         nan

--- GPS ---
gps_data.online:           1509705893
gps_data.status:           0
gps_data.satellites_used:  0
gps_data.fix.mode:         0
gps_data.fix.time:                nan
gps_data.fix.latitude:     nan
gps_data.fix.longitude:    nan
gps_data.fix.altitude:     nan
gps_data.fix.speed:        nan
gps_data.fix.track:        nan
gps_data.dop.pdop:         nan

--- GPS ---
gps_data.online:           1509705893
gps_data.status:           0
gps_data.satellites_used:  0
gps_data.fix.mode:         0
gps_data.fix.time:                nan
gps_data.fix.latitude:     nan
gps_data.fix.longitude:    nan
gps_data.fix.altitude:     nan
gps_data.fix.speed:        nan
gps_data.fix.track:        nan
gps_data.dop.pdop:         nan

--- GPS ---
gps_data.online:           1509705894
gps_data.status:           1
gps_data.satellites_used:  0
gps_data.fix.mode:         3
gps_data.fix.time:         1509705894
gps_data.fix.latitude:     52.230025
gps_data.fix.longitude:    -2.188018
gps_data.fix.altitude:     43.450000
gps_data.fix.speed:        0.043000
gps_data.fix.track:        280.369300
gps_data.dop.pdop:         nan

--- GPS ---
gps_data.online:           1509705895
gps_data.status:           1
gps_data.satellites_used:  0
gps_data.fix.mode:         3
gps_data.fix.time:         1509705895
gps_data.fix.latitude:     52.230026
gps_data.fix.longitude:    -2.188018
gps_data.fix.altitude:     43.468000
gps_data.fix.speed:        0.040000
gps_data.fix.track:        307.944700
gps_data.dop.pdop:         nan

--- GPS ---
gps_data.online:           1509705896
gps_data.status:           1
gps_data.satellites_used:  0
gps_data.fix.mode:         3
gps_data.fix.time:         1509705896
gps_data.fix.latitude:     52.230026
gps_data.fix.longitude:    -2.188017
gps_data.fix.altitude:     43.568000
gps_data.fix.speed:        0.017000
gps_data.fix.track:        1.317500
gps_data.dop.pdop:         nan

--- GPS ---
gps_data.online:           1509705896
gps_data.status:           1
gps_data.satellites_used:  14
gps_data.fix.mode:         3
gps_data.fix.time:         1509705896
gps_data.fix.latitude:     52.230026
gps_data.fix.longitude:    -2.188017
gps_data.fix.altitude:     43.568000
gps_data.fix.speed:        0.017000
gps_data.fix.track:        1.317500
gps_data.dop.pdop:         1.380000

--- GPS ---
gps_data.online:           1509705897
gps_data.status:           1
gps_data.satellites_used:  14
gps_data.fix.mode:         3
gps_data.fix.time:         1509705897
gps_data.fix.latitude:     52.230026
gps_data.fix.longitude:    -2.188016
gps_data.fix.altitude:     43.645000
gps_data.fix.speed:        0.030000
gps_data.fix.track:        43.908300
gps_data.dop.pdop:         1.380000

--- GPS ---
gps_data.online:           1509705898
gps_data.status:           1
gps_data.satellites_used:  14
gps_data.fix.mode:         3
gps_data.fix.time:         1509705898
gps_data.fix.latitude:     52.230026
gps_data.fix.longitude:    -2.188016
gps_data.fix.altitude:     43.603000
gps_data.fix.speed:        0.008000
gps_data.fix.track:        177.232800
gps_data.dop.pdop:         1.380000


Hope this helps.

Regards

Mike



On 11/3/2017 5:10 AM, David Huichen Dai wrote:
Hi all,

I have gpsd and libgps installed on my Ubuntu 14.04 using the apt-get install command. The gpsd version is 3.9 (Ubuntu software center does not have gpsd updated to the latest version).

I use the following code (from stackoverflow) to get the coordinates (latitude and longitude). This code uses socket to communicate with the gpsd daemon process, generally it works well.
But I often find that the gps_waiting function returns false, and after that even I re-run this code, the coordinates can no longer be obtained (still fail at gps_waiting). I tried several methods, like killing the gpsd process and restart it, removing the /var/run/gpsd.sock, all didn't work. The only thing I can do is to reboot the computer. I have got two questions:

1. How to avoid this problem.
2. This is only a toy program. In my project, I need to have the latest coordinates immediately whenever I want, I dont want to wait for any time before I can have the coordinates, also not in a loop as in this toy program. How could I achieve this?

I am very happy to see some sample code in real projects. Your help are very much appreciated. Thank you!



Best wishes,
David


#include <gps.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

int main() {
int rc;
struct timeval tv;

struct gps_data_t gps_data;
if ((rc = gps_open("localhost", "2947", &gps_data)) == -1) {
    printf("code: %d, reason: %s\n", rc, gps_errstr(rc));
    return EXIT_FAILURE;
}
gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);

while (1) {
    /* wait for 2 seconds to receive data */
    if (gps_waiting (&gps_data, 2000000)) {
        /* read data */
        if ((rc = gps_read(&gps_data)) == -1) {
            printf("error occured reading gps data. code: %d, reason: %s\n", rc, gps_errstr(rc));
        } else {
            /* Display data from the GPS receiver. */
            if ((gps_data.status == STATUS_FIX) && 
                (gps_data.fix.mode == MODE_2D || gps_data.fix.mode == MODE_3D) &&
                !isnan(gps_data.fix.latitude) && 
                !isnan(gps_data.fix.longitude)) {
                    //gettimeofday(&tv, NULL); EDIT: tv.tv_sec isn't actually the timestamp!
                    printf("latitude: %f, longitude: %f, speed: %f, timestamp: %ld\n", gps_data.fix.latitude, gps_data.fix.longitude, gps_data.fix.speed, gps_data.fix.time); //EDIT: Replaced tv.tv_sec with gps_data.fix.time
            } else {
                printf("no GPS data available\n");
            }
        }
    }
    else printf("waiting failed.\n"); 
    
}

/* When you are done... */
gps_stream(&gps_data, WATCH_DISABLE, NULL);
gps_close (&gps_data);

return EXIT_SUCCESS;
}


--
==================================================
David Huichen Dai, PhD
Research assistant
Lab of Broadband Network Switching Technology and Communication
Dept. of Computer Science and Technology
Tsinghua University, Beijing, P.R. China, 100084
Tel: 86-010-6277-3441
Email: address@hidden; address@hidden
==================================================


--

Michael J Tubby B.Sc MIET / Technical Director / +44 (0)7973 225144

Thorcom Systems Ltd
Office: +44 (0)1905 756 700 / Fax: +44 (0)1905 755 777
Unit 4, 96B Blackpole Trading Estate West, Worcester, WR3 8TJ Registered in England & Wales 2704696 / VAT number GB 487 9256 81
www.thorcom.co.uk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]