======================================================================
Installing ntp and gpsd for PPS input on Ubunto 8.04 LTS

Revision History:
2008 11 28 LLW Created and Written
2008 11 30 LLW Revised, still not ready for prime time.
               Need to get runtime libs to execute (ldconfig?)
               properly after make install.
======================================================================


======================================================================
Installing ntp from ubunto repositories
======================================================================
sudo apt-get install ntp
sudo apt-get install ntp-doc 

ntp config file is /etc/ntp.conf
ntp options file is /etc/defaults/ntp

commands:
ntpq -p
watch -d -n 1 ntpq -p
sudo /etc/init.d/ntp stop
sudo /etc/init.d/ntp start
sudo /etc/init.d/ntp restart 

when on a local net without DHCP and name resolution,
ntpq -pn
watch -d -n 1 ntpq -pn


======================================================================
Installing latest versoin of gpsd from gpsd svn 

Do this if you want to use a garmin GPS 
Do a manual build and install of gpsd 2.37 or higher
Gpsd 2.36 does not work with garmin nmea gps units because of a bug in
gpsd which causes two pps monitor threads to be launched.

As of 2008 11 28, gpsd 2.37 is the latest gpsd release
======================================================================

FYI here how to get the bleeding edge latest gpsd revision.
For reference, do not do this.
svn checkout svn://svn.berlios.de/gpsd/trunk  gpsd_svn

Get release-2.37
svn checkout svn://svn.berlios.de/gpsd/tags/release-2.37  release-2.37


sudo apt-get build-dep gpsd
sudo apt-get install automake
sudo apt-get install libtool

cd gpsd_svn
./autogen.sh
make

test gpsd on command line


make install



sudo mv /usr/sbin/gpsd /usr/sbin/gpsd.orig
sudo cp .lib/gpsd /usr/sbin/gpsd
sudo cp contrib/etc_init.d_gpsd /etc/init.d/gpsd


edit this file as necessary
sudo cp contrib/etc_init.d_gpsd /etc/init.d/gpsd

edit gpsd default file /etc/defaults/gpsd

----------------------------------------
# Default settings for gpsd. This file is sourced from
# /etc/init.d/gpsd.
START_DAEMON="true"
DAEMON_OPTS="-n -D 5"
DEVICES="/dev/ttyUSB0"
----------------------------------------

Setup /etc/ntp.conf


Edit ntp default file /etc/defaults/ntp

----------------------------------------
# 2008 11 29 LLW use "-g"
# NTPD_OPTS='-g' 
----------------------------------------

Explanation of ntpd -g and -x options

-g  Normally, ntpd exits with a message to the system log if the
    offset exceeds the panic threshold, which is 1000 s by
    default. This option allows the time to be set to any value
    without restriction; however, this can happen only once. If the
    threshold is exceeded after that, ntpd will exit with a message to
    the system log. This option can be used with the -q and -x
    options. See the tinker command for other options.

- x Normally, the time is slewed if the offset is less than the step
    threshold, which is 128 ms by default, and stepped if above the
    threshold. This option sets the threshold to 600 s, which is well
    within the accuracy window to set the clock manually. Note: Since
    the slew rate of typical Unix kernels is limited to 0.5 ms/s, each
    second of adjustment requires an amortization interval of 2000
    s. Thus, an adjustment as much as 600 s will take almost 14 days
    to complete. This option can be used with the -g and -q
    options. See the tinker command for other options. Note: The
    kernel time discipline is disabled with this option.


----------------------------------------------------------------------
# uncomment to add this server
server louie.udel.edu minpoll 4 maxpoll 4

# 2008 11 16 LLW Added for gps
# Use gpsd version 2.37 or higher.
# Gpsd version 2.36 and lower has a bug with garmin GPS units
# This tells NTP use the shared memory device (28) unit 0
# this clock unit is driven by the  NEMA gps string 
server 127.127.28.0 minpoll 4 maxpoll 4
fudge  127.127.28.0 time1 0.420 stratum 1 refid GPS0

# This tells NTP use the shared memory device (28) unit 1
# this clock unit is driven by the gps PPS signal on the
# DCD pin of the serial port
server 127.127.28.1 minpoll 4 maxpoll 4 prefer
fudge  127.127.28.1 refid GPS1

# this tells NTP to use the local computer clock as a last resort
# (stratum 10) when all other clocks fail
server 127.127.1.0
fudge  127.127.1.0 stratum 10 
----------------------------------------------------------------------

======================================================================
Setting up a machine as an NTP client from the command line
======================================================================
sudo apt-get install ntp ntp-doc ntpdate ntp

setup /etc/defaults/ntp same as server, above

set computer clock to use UTC by editing 
/etc/defaults/rcS 
sudo emacs /etc/defaults/rcS 
and editing this line:
UTC=yes 

set computer local time zone to UTC by the following command:
sudo cp /usr/share/zoneinfo/UTC /etc/localtime

Ubuntu ntp package installation normally sets up NTP to 
automatically run on boot.  To manually set this up:
Set ntpd to run every time machine boots with this command:
sudo undate-rc.d ntp default

setup ntp.conf for client as below

sudo reboot now

check with 

watch -n 1 -d ntpq -p
or
watch -n 1 -d ntpq -pn

======================================================================
ntp.conf file for a local NTP client 
======================================================================

----------------------------------------------------------------------
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Enable this if you want statistics to be logged.
statsdir /var/log/ntpstats/

# statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# use server ntp.ubuntu.com 
server ntp.ubuntu.com
# use server louie.udel.edu, poll every 2^4 seconds
server louie.udel.edu minpoll 4 maxpoll 4
# use local server 192.168.1.107, poll every 2^4 seconds
server 192.168.1.107 minpoll 4 maxpoll 4

----------------------------------------------------------------------


======================================================================
Installing gpsd from ubuntu repositories
======================================================================

As of 2008 11 28, this installs gpsd 2.36, which does not support
the garmin GPS due to a bug which causes two pps monitor threads
to be launched.

Do not use this if you want to use a garmin GPS, do a manual
build and install of gpsd 2.37 or higher

As of 2008 11 28, gpsd 2.37 is the latest gpsd release

----------------------------------------------------------------------
sudo apt-get install gpsd 
sudo apt-get install gpsd-clients 

To get source stuff:
sudo apt-get build-dep gpsd
sudo apt-get source gpsd

sudo apt-file search gpsd
downloads into current directory
cd into directory
./configure
make gpsd

Optional packages recommended by apt-get:
sudo apt-get install python-gps
sudo apt-get install libglib2.0-data

use dmesg to identify device entry of USB serial port

run in foreground:
sudo gpsd /dev/ttyUSB0 -N  -n

run in foreground with verbose output:
sudo gpsd /dev/ttyUSB0 -N  -n -D 5

run as daemon:
sudo gpsd /dev/ttyUSB0 -n

Check to see if it has opened port 2947
sudo netstat -tulpen |grep gpsd

Sample fixes and plot graph: 
gpsprof | gnuplot -persist

Edit ntp.conf (sudo emacs /etc/ntp.conf)
add following lines:

----------------------------------------------------------------------
# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
# disable auth
# broadcastclient

# uncomment to add this server
# server louie.udel.edu minpoll 4 maxpoll 4

# 2008 11 16 LLW Added for gps
# Use gpsd version 2.37 or higher.
# Gpsd version 2.36 and lower has a bug with garmin GPS units
# This tells NTP use the shared memory device (28) unit 0
# this clock unit is driven by the  NEMA gps string 
server 127.127.28.0 minpoll 4 maxpoll 4
# fudge  127.127.28.0 time1 0.420 stratum 1 
fudge  127.127.28.0 time1 0.420 stratum 1 refid GPS0

# This tells NTP use the shared memory device (28) unit 1
# this clock unit is driven by the gps PPS signal on the
# DCD pin of the serial port
server 127.127.28.1 minpoll 4 maxpoll 4 prefer
# fudge  127.127.28.1 
fudge  127.127.28.1 refid GPS1

# this tells NTP to use the local computer clock as a last resort
# (stratum 10) when all other clocks fail
server 127.127.1.0
fudge 127.127.1.0 stratum 10 

----------------------------------------------------------------------


Other stuff
sudo apt-get install gpsdrive
sudo apt-get install gpsdrive-scripts
sudo apt-get install gpsababel


======================================================================
