Equip Fitlet With A GPS Time Source

Application notes using fitlet. Credit goes to user Hassellbear for publishing more than enough cool application notes to justify a dedicated sub-forum.

Moderator: hassellbear

Post Reply
hassellbear
Posts: 106
Joined: Mon May 28, 2012 12:25 pm

Equip Fitlet With A GPS Time Source

Post by hassellbear »

Overview

Typically, fitlet is connected to a network and, as a result, has access to standard network time sources. However, there may be fitlet applications where network connectivity and standard time sources are unavailable. For those cases, it is possible to provide fitlet with a time source that should be superior to the free running system clock. An inexpensive, USB connected GPS receiver can be utillized as a time source which will provide relatively decent accuracy.
Clock.gif
Clock.gif (751 KiB) Viewed 11762 times



GPS Satellites as a Source of Accurate Time

The earth is enveloped in a baseline constellation of 24 GPS satellites. Each of these satellites carries an onboard complement of 4 atomic clocks which are either cesium or rubidium based. The typical GPS atomic clock has an accuracy on the order of 20 - 30 nanoseconds.

In addition to broadcasting position data, GPS satellites also broadcast accurate time data. It is this broadcast time data we can use as a time source for fitlet.

GPS Constellation
ConstellationGPS.gif
ConstellationGPS.gif (485.5 KiB) Viewed 11762 times



Hardware

The hardware required for this application is:

1. fitlet
2. GPS Receiver - USB type (I am using GlobalSat BU-353)

The GPS unit receives highly accurate time broadcasts from GPS satellites and in turn provides this time information to Fitlet.
Hardware.jpg
Hardware.jpg (40.75 KiB) Viewed 11739 times

About GPS Receiver Output Data

Most USB based GPS receivers have the ability to output serial data which adheres to the NMEA 0183 protocol. The output data consists of a repeating series of sentences. Each of those sentences is in comma delimited form and consists of a $"Header" followed by data. A general description of each sentence type is included in the following key.


NMEA 0183 Sentence Key
$GPBOD - Bearing, origin to destination
$GPBWC - Bearing and distance to waypoint, great circle
$GPGGA - Global Positioning System Fix Data
$GPGLL - Geographic position, latitude / longitude
$GPGSA - GPS DOP and active satellites
$GPGSV - GPS Satellites in view
$GPHDT - Heading, True
$GPR00 - List of waypoints in currently active route
$GPRMA - Recommended minimum specific Loran-C data
$GPRMB - Recommended minimum navigation info
$GPRMC - Recommended minimum specific GPS/Transit data
$GPRTE - Routes
$GPTRF - Transit Fix Data
$GPSTN - Multiple Data ID
$GPVBW - Dual Ground / Water Speed
$GPVTG - Track made good and ground speed
$GPWPL - Waypoint location
$GPXTE - Cross-track error, Measured
$GPZDA - Date & Time




In the sentences listed below, those beginning with $GPRMC contain the time and date data we are interested in. The sentences beginning with $GPGGA also contain time data. The time is shown using red letters and the date is shown with green letters.

Sample NMEA 0183 Output FROM GPS Receiver
$GPRMC,230541.380,V,,,,,,,300116,,,N*42
$GPGGA,230542.380,,,,,0,00,,,M,0.0,M,,0000*5F
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,3,1,12,03,62,228,35,16,61,048,29,23,47,340,,26,32,039,*7C
$GPGSV,3,2,12,27,25,131,,09,18,318,,08,17,162,,07,15,269,*75
$GPGSV,3,3,12,01,06,207,,31,02,059,,11,02,193,,22,02,154,*7F
$GPRMC,230542.380,V,,,,,,,300116,,,N*41
$GPGGA,230543.380,,,,,0,00,,,M,0.0,M,,0000*5E
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPRMC,230543.380,V,,,,,,,300116,,,N*40
$GPGGA,230544.380,,,,,0,00,,,M,0.0,M,,0000*59
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPRMC,230544.380,V,,,,,,,300116,,,N*47


About GPS Receiver Time Source Accuracy

The most accurate terrestrial GPS based time sources feature a Pulse Per Second (PPS) output. While it is likely possible to interface a PPS output to fitlet using fitlet's GPIO capability, this would require an extra bit of interfacing and programming. A simpler, non-PPS, USB based solution is featured in this application note. In initial tests of the non-PPS solution featured here, fitlet's clock is running less than 1 second different from the official time provided by WWV. Such accuracy should be adequate for all but the most demanding applications.

hassellbear
Posts: 106
Joined: Mon May 28, 2012 12:25 pm

Equip Fitlet With A GPS Time Source

Post by hassellbear »

Software

The following software is required for the GPS Time Source application. It is all available in the Linux package repositories.

1. ntp
2. ntpdate
3. gpsd
4. gpsd-clients
5. python-gps


About GPSD

GPSD is a service dameon which monitors the GPS units connected to your system and makes the data they provide available to other applications. It is more or less an indispensable application for working with GPS systems in Linux.




Software and System Configuration

1. Connect gps to fitlet
2. Navigate to /dev directory and find GPS port (my case port = ttyUSB0)
3. Connect to GPS using a terminal program such as minicom. Look for NMEA
output from GPS - will resemble the following:

$GPGGA,020834.000,3027.3429,N,09101.4821,W,1,07,1.1,13.7,M,-25.9,M,,0000*5E
$GPGSA,A,3,32,31,01,14,20,16,23,,,,,,2.0,1.1,1.7*35
$GPRMC,020834.000,A,3027.3429,N,09101.4821,W,0.14,326.64,181213,,,A*75
$GPGGA,020835.000,3027.3427,N,09101.4822,W,1,07,1.1,13.4,M,-25.9,M,,0000*51
$GPGSA,A,3,32,31,01,14,20,16,23,,,,,,2.0,1.1,1.7*35
$GPRMC,020835.000,A,3027.3427,N,09101.4822,W,0.79,188.87,181213,,,A*79

4. Configure gpsd using the terminal command sudo dpkg-reconfigure gpsd as follows:
...A. Start gpsd automatically? yes
...B. Handle gps receivers automatically? yes
...C.Device gps receiver is attached to: /dev/ttyUSB0 in my case
...D. Options to gpsd: -b --n
...E. gpsd control socket path: /var/run/gpsd.sock

5. Run cgps to confirm operation of gpsd. Pay special attention to the parameter Time Offset. It is typically less than 1. If it is a huge number, there is a problem.
cgps.png
cgps.png (100.31 KiB) Viewed 11752 times

6. Using an editor such as nano, edit the NTP configuration file /etc/ntp.conf as follows:

# gps ntp
server 127.127.28.0 minpoll 4 maxpoll 4
fudge 127.127.28.0 time1 0.183 refid NMEA


7. Execute the following commands as su:

...A. killall -9 gpsd ntpd
...B. ntpd -gN
...C. gpsd -b -n /dev/ttyUSB0
...D. cgps

8. If cgps returns active, normal output then querry ntp using the command ntpq -p
You should get a listing similar to:
ntpq -p.png
ntpq -p.png (50.07 KiB) Viewed 11752 times

The above screenshot shows all of the time sources available to fitlet at the time of the query. The line

SHM(0) .NMEA. 0 l 11 16 77 0.000 -7.112 4.471

indicates the GPS receiver is providing time data to the NTP dameon.


An Important Note

Shortly after bootup, Fitlet uses ntpdate to initially set the system clock. Thereafter, the ntp dameon keeps the clock synchronized to ntp servers including your GPS. Unfortunately, ntpdate will not accept time data from a gps - it only uses ntp servers. Therefore, if you operating where internet service is unavailable, you must manually set the clock to within 4 hours of actual time after bootup. After you initially set the linux system clock, the ntp dameon takes over and uses the GPS time to synchronize the system clock. Please note that if your initial time setting is 4 hours or more different from the actual time, the ntp dameon will not correct the linux system clock to GPS time. The initial setting must be less than 4 hours different form the actual time.

hassellbear
Posts: 106
Joined: Mon May 28, 2012 12:25 pm

Equip Fitlet With A GPS Time Source

Post by hassellbear »

Fitlet Not Connected To A Network

Without a GPS Time Source

The following screenshot shows the time sources available when fitlet is not connected to a network and is also not connected to a GPS receiver. Even thought the GPS source is listed, no data is available due to the absence of a GPS receiver- all values are 0. This means that fitlet's system clock runs free and is not corrected by an external source. Over time, the internal clock will drift.
ntpq -p no sources.png
ntpq -p no sources.png (45.04 KiB) Viewed 11750 times


With a GPS Time Source

The following screenshot shows the time sources available when fitlet is not connected to a network, but is connected to a GPS receiver. The single GPS time source is listed and is supplying data. In this case, fitlet's internal clock is corrected to GPS time on a repetitive basis. System clock drift is eliminated.
ntpq -p gps only.png
ntpq -p gps only.png (46.53 KiB) Viewed 11752 times



Results

1. The results of using an inexpensive GPS receiver as an external time source for fitlet were good. In general, fitlet's system clock was maintained to within 1 second of the official U.S. time provided by WWV.
Time Comparison.png
Time Comparison.png (408.44 KiB) Viewed 11743 times



Conclusions

1. A simple GPS receiver can serve as an accurate and effective time source for those cases where fitlet must operate without a network connection or without access to other time sources.

2. Fitlet again demonstrates is adaptability and versatility.

Post Reply

Return to “Cool stuff with fitlet”