chore(exec/temp):Fix CSV fields in stdout output
[EVA-2020-02.git] / doc / time / README.org
CommitLineData
88b5ae54
SBS
1#+TITLE: Ninfacyzga-1 Time Tracking
2#+AUTHOR: Steven Baltakatei Sandoval
3#+EMAIL: baltakatei@gmail.com
d50056c2 4* Time Tracking
88b5ae54 5** About
6bdc2473 6This document was created by Steven Baltakatei Sandoval on
fffd9f66
SBS
7~2020-07-23T22:27Z~ under a [[https://creativecommons.org/licenses/by-sa/4.0/][Creative Commons BY-SA 4.0 license]]. It was
8updated by Steven Baltakatei Sandoval on ~2020-10-18T00:11Z~
1b813272 9
6bdc2473
SBS
10** Narrative
11The ~ninfacyzga-01~ device is equipped with an Ozzmaker BerryGPS-IMU
1b0ddef8 12module which provides time and location data to ~gpsd~ and ~chrony~. The
d50056c2
SBS
13time is provided by GPS satellites which themselves are
14equipped [fn:nasa_20020408_atomicclock] with atomic clocks. This
15extremely accurate set of clocks are needed since a GPS receiver
16calculates its position in space using a General Relativity
6bdc2473
SBS
17calculation that uses the small variations in the time stamps received
18from each satellite. This means that ~gpsd~ may be used to set the
19system clock without a need for an internet connection to a default
20Debian time server; ~ninfacyzga-01~ can be its own time server.
d50056c2
SBS
21
22[fn:nasa_20020408_atomicclock] Title:[[https://science.nasa.gov/science-news/science-at-nasa/2002/08apr_atomicclock/][Tick-Tock Atomic Clock]];
23Date:2002-04-08; Website:NASA.gov; [[https://web.archive.org/web/20100429141752/http://science.nasa.gov/science-news/science-at-nasa/2002/08apr_atomicclock/][Archive-link]]; Archive-date:
242010-04-29
25
6bdc2473
SBS
26** Description
27*** Hardware
88b5ae54 28Ozzmaker BerryGPS-IMU, Version 3 (see [[https://ozzmaker.com/berrygps-berrygps-imu-quick-start-guide/][ref]]).
6bdc2473 29*** Software
1b0ddef8
SBS
30- [[https://www.raspberrypi.org/downloads/raspberry-pi-os/][Raspberry Pi OS]] : A GNU/Linux operating system derived from
31 Debian 10. This procedure was developed with version ~August 2020~.
32
88b5ae54
SBS
33- [[https://tracker.debian.org/pkg/gpsd][~gpsd~]] : A background daemon app capable of interfacing with the
34 [[https://ozzmaker.com/berrygps-berrygps-imu-quick-start-guide/][Ozzmaker BerryGPS-IMU]]'s GPS submodule. Installed and initialized by
35 ~apt~. Should be installed along with the ~gpsd-clients~
36 package. This procedure was developed with ~gpsd~ version
37
1b0ddef8
SBS
38- [[https://chrony.tuxfamily.org/][~chrony~]] : A set of programs capable of continuously adjusting the
39 system clock until it is synchronized with configurable time sources
40 such as GPS and PPS data provided by ~gpsd~. ~chrony~ may be
41 configured to act as an NTP time client or server. It uses the same
42 protocol as ~ntp~ but is a GPLv2 implementation. This procedure was
43 developed with ~chrony~ version ~3.4-4~.
88b5ae54 44
6bdc2473
SBS
45** Operating Procedures
46*** Initial Startup
88b5ae54
SBS
47**** Perform initial setup.
48See [[file:../setup/README.org][Main Setup]] procedure.
49**** Install Hardware for time tracking
6bdc2473
SBS
50See [[https://ozzmaker.com/forums/topic/connecting-gps-pps-pin/][this]] Ozzmaker forum topic about connecting the BerryGPS-IMU
51~T_PULSE~ pin to GPIO 18.
52
88b5ae54
SBS
53#+CAPTION: An image showing how to connect the PPS signal from an Ozzmaker BerryGPS-IMU board to a Raspberry Pi Zero W.
54#+NAME: fig:PPS_BERRYGPS_RASPIZW
55[[../../img/Compact_Stratum_1_NTP_time_server_hardware,_October_2020.jpg]]
56
6bdc2473 57Connect the ~T_PULSE~ connection on the BerryGPS-IMU-3 to GPIO pin 18
aafe11cf
SBS
58(ex: with solder and wire) in order to provide the PPS data signal
59generated by the BerryGPS-IMU to the Raspberry Pi. Processing of this
60data signal is handled by adding a line to ~/boot/config.txt~ in the
61next section ("Install Software").
6bdc2473 62
88b5ae54
SBS
63Note: If it is desired to specify a custom GPIO pin besides the one
64recommended, see this [[https://raspberryautomation.com/connect-multiple-ds18b20-temperature-sensors-to-a-raspberry-pi/][Raspberry Autom]] article.
65
66**** Install Software for time tracking
67The time tracking function can be performed by two programs: ~gpsd~
1b0ddef8 68and ~chrony~.
88b5ae54
SBS
69
70Basically, two things need to happen:
71
721. ~gpsd~ needs to be pointed towards the correct device files for
73 incoming GPS data (in NMEA format) and the PPS signal ("pulse per
74 second"; a high precision time signal).
75
1b0ddef8 762. ~chrony~ needs to be pointed towards the correct local IP addresses
88b5ae54
SBS
77 where ~gpsd~ provides GPS data and the PPS signal.
78
1b0ddef8 79~gpsd~ then will provide GPS and PPS data to ~chrony~ via a "shared
88b5ae54 80memory driver".
6bdc2473 81
aafe11cf
SBS
82***** Install packages via ~apt~
83Run the following command to install the required packages.
1b0ddef8 84: $ sudo apt install gpsd gpsd-clients python-gps pps-tools chrony
aafe11cf 85
88b5ae54
SBS
86***** Enable PPS device
87Modify the ~/boot/config.txt~ file in order to tell the Raspberry Pi
88to expect PPS data on ~BCM 18~ (pin number 12; see [[https://pinout.xyz/][link]]). This is done
89by adding the following line to ~/boot/config.txt~ as described on
90[[https://ozzmaker.com/forums/topic/problems-with-pps-on-a-pi0w-running-raspian-and-attached-to-a-berrygps-imuv3/][this Ozzmaker page]]:
aafe11cf 91
fffd9f66 92: # Enable PPS on GPIO 18
aafe11cf
SBS
93: dtoverlay=pps-gpio,gpiopin=18
94
88b5ae54
SBS
95The ~/boot/config.txt~ file can be modified via:
96
97: $ sudo nano /boot/config.txt
98
aafe11cf
SBS
99PPS data can be confirmed by running:
100
101#+BEGIN_EXAMPLE
102$ sudo su -
103# ppstest /dev/pps0
104trying PPS source "/dev/pps0"
105found PPS source "/dev/pps0"
106ok, found 1 source(s), now start fetching data...
107source 0 - assert 1595708074.003644641, sequence: 219 - clear 0.000000000, sequence: 0
108source 0 - assert 1595708075.003709620, sequence: 220 - clear 0.000000000, sequence: 0
109source 0 - assert 1595708076.003779580, sequence: 221 - clear 0.000000000, sequence: 0
110source 0 - assert 1595708077.003850580, sequence: 222 - clear 0.000000000, sequence: 0
111#+END_EXAMPLE
112
88b5ae54
SBS
113Note: For older Raspberry Pi models, it may be necessary to enable
114~pps-gpio~ via modifications to ~/etc/modules~ (see [[https://www.raspberrypi.org/forums/viewtopic.php?p=757747#p757747][link]]).
115
116***** Enable GPS device
117The Ozzmaker BerryGPS-IMU makes NMEA sentences available via the
118serial "UART" device ~/dev/ttyAMA0~. If bluetooth has not been
119disabled, the Raspberry Pi OS automatically creates a software "UART"
fffd9f66 120device at ~/dev/serial0~. See the "[[file:../setup/README.org::*Disable%20Bluetooth][Disable Bluetooth]]" section in the
88b5ae54
SBS
121[[file:../setup/README.org][Main Setup]] Initial Startup procedure for instructions on how to
122disable bluetooth to free up ~/dev/ttyAMA0~ for use by ~gpsd~.
123
88b5ae54 124***** Setup ~gpsd~
fffd9f66
SBS
125See the "[[file:../location/README.org::*Setup%20~gpsd~][Setup ~gpsd~]]" subsection within the "Initial Startup" section
126of the Location Logging [[file:../location/README.org][~README.org~]] file. There is one additional
88b5ae54
SBS
127change that must be made which is to add a ~/dev/pps0~ item to the
128~DEVICES=~ line in ~/etc/default/gpsd~ like so:
129
130: DEVICES="/dev/ttyAMA0 /dev/pps0"
131
132~/dev/ttyAMA0~ is where ~gpsd~ can get NMEA data from the GPS unit.
133
134~/dev/pps0~ is where ~gpsd~ can get a PPS signal.
135
136As an example, the following lines will be present in
137~/etc/default/gpsd~ if both location and time tracking are set up:
138
139#+BEGIN_EXAMPLE
140START_DAEMON="true"
141USBAUTO="false"
142DEVICES="/dev/ttyAMA0 /dev/pps0"
143GPSD_OPTIONS="-n"
144#+END_EXAMPLE
145
1b0ddef8 146Make sure to enable ~gpsd~ to automatically start as a system service.
88b5ae54 147
1b0ddef8
SBS
148: $ sudo systemctl enable gpsd
149: $ sudo systemctl start gpsd
88b5ae54 150
1b0ddef8
SBS
151***** Setup ~chrony~
152Modify the configuration file for ~chrony~ at ~/etc/chrony/chrony.conf~.
88b5ae54 153
1b0ddef8
SBS
154: $ sudo nano /etc/chrony/chrony.conf
155
156Add the following lines:
88b5ae54
SBS
157
158#+BEGIN_EXAMPLE
1b0ddef8
SBS
159# Get time from GPS (/dev/XXXX) and PPS (/dev/YYYY)
160#refclock SOCK /run/chrony.XXXX.sock refid GPS precision 1e-1 offset 0.0000
161#refclock SOCK /run/chrony.YYYY.sock refid PP precision 1e-7
162refclock SHM 0 refid GPS precision 1e-1 offset 0.0000 delay 0.2 stratum 1
163refclock SHM 1 refid PPS precision 1e-7 stratum 1
164#+END_EXAMPLE
88b5ae54 165
1b0ddef8
SBS
166Where
167- ~XXXX~ : the basename of the GPS device's serial port. In this guide
168 it should be ~ttyAMA0~; other setups may use ~ttyS0~, ~ttyACM0~, or
169 ~serial0~.
88b5ae54 170
1b0ddef8
SBS
171- ~YYYY~ : the basename of the PPS device's serial port. In this guide
172 it should be ~pps0~.
88b5ae54 173
1b0ddef8
SBS
174Note: The ~refclock SOCK~ lines are left as comments in case ~gpsd~
175incorrectly maps the GPS and PPS data.
88b5ae54 176
1b0ddef8
SBS
177The following commands may be useful for testing ~gpsd~ and ~chrony~
178configurations.
fffd9f66 179- ~chronyc sources -v~ : Shows time sources and associated accuracy
1b0ddef8 180 information.
88b5ae54 181
fffd9f66 182- ~chronyc tracking~ : Shows the current time difference between
1b0ddef8
SBS
183 the reference clock and the system clock. Note: ~chrony~ gradually
184 attempts to reduce the difference by changing the system clock.
185
186- ~sudo chronyc makestep~ : Force ~chrony~ to set the system clock to
187 match the reference clock immediately.
188
189- ~sudo systemctl enable chrony~ : Enable automatic startup of
190 ~chrony~ (Note: This command shouldn't be necessary since the act of
191 installing ~chrony~ via ~sudo apt install chrony~ should
192 automatically enable it).
193
194- ~sudo systemctl stop chrony~ : Stop ~chrony~.
88b5ae54 195
1b0ddef8 196- ~sudo systemctl restart chrony~ : Restart ~chrony~.
88b5ae54 197
fffd9f66 198- ~systemctl status chrony~ : Check status of ~chrony~ service.
88b5ae54
SBS
199
200- ~sudo ntpshmmon~ : Shows live output of data using the shared memory
1b0ddef8 201 driver filled by ~gpsd~. ([[https://gpsd.gitlab.io/gpsd/gpsd-time-service-howto.html][ref]])
88b5ae54
SBS
202
203- ~sudo ipcs -m~ : Show live segments of the shared memory. ([[https://gpsd.gitlab.io/gpsd/gpsd-time-service-howto.html][ref]])
204
1b0ddef8
SBS
205- ~sudo date -s '2020-07-07T00:00+0000'~ : Manually sets time to a
206 string.
207
208An example output of ~sudo chronyc sources -v~ will show something
209similar to this:
aafe11cf 210
88b5ae54 211#+BEGIN_EXAMPLE
1b0ddef8
SBS
212pi@ninfacyzga-1-x:~ $ sudo chronyc sources -v
213210 Number of sources = 6
214
215 .-- Source mode '^' = server, '=' = peer, '#' = local clock.
216 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
217| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
218|| .- xxxx [ yyyy ] +/- zzzz
219|| Reachability register (octal) -. | xxxx = adjusted offset,
220|| Log2(Polling interval) --. | | yyyy = measured offset,
221|| \ | | zzzz = estimated error.
222|| | | \
223MS Name/IP address Stratum Poll Reach LastRx Last sample
224===============================================================================
225#- GPS 1 4 377 21 +110ms[ +110ms] +/- 200ms
226#* PPS 1 4 377 22 +2496ns[+3045ns] +/- 1000ns
227^- vps-2d3ddab6.vps.ovh.ca 2 6 277 57 +1302us[+1304us] +/- 151ms
228^? time.richiemcintosh.com 2 6 1 59 +2626us[+2628us] +/- 92ms
229^- varuna.ga-group.nl 3 6 377 55 -3962us[-3960us] +/- 151ms
230^- ntp3.junkemailfilter.com 2 6 377 58 -4561us[-4558us] +/- 80ms
88b5ae54 231#+END_EXAMPLE
aafe11cf 232
1b0ddef8 233General references for the ~chrony.conf~ file are:
aafe11cf 234
1b0ddef8
SBS
235- The ~chrony~ ~4.0~ documentation. ([[https://chrony.tuxfamily.org/doc/4.0/chrony.conf.html][ref]])
236
237- The ~gpsd~ documentation for communicating with ~chrony~. ([[https://gpsd.gitlab.io/gpsd/gpsd-time-service-howto.html#_feeding_chrony_from_gpsd][ref]])
238
239- Setup guide for a USB GPS with ~gpsd~ and ~chrony~. ([[https://photobyte.org/raspberry-pi-stretch-gps-dongle-as-a-time-source-with-chrony-timedatectl/][ref]])
aafe11cf 240
88b5ae54
SBS
241***** Disable CPU power saving
242Power saving featurs of the Raspberry Pi Zero W may also be disabled
243in order to improve accuracy.
244
245****** Configure CPU ~scaling_governor~
246If additional precision is required, the PPS signal may be made more
247reliable at the cost of increasing CPU power by configuring the CPU to
248always run at maximum frequency.[fn:se_20180320_raspicpugov] This
249change can be performed by modifying the following file as root:
250
251: /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
252
253The file should consist of one line. Change
aafe11cf 254
88b5ae54 255: ondemand
aafe11cf 256
88b5ae54 257to
aafe11cf 258
88b5ae54 259: performance
aafe11cf 260
88b5ae54 261.
aafe11cf 262
88b5ae54
SBS
263This change can be performed via the ~nano~ text editor by running the
264following commands:
aafe11cf 265
88b5ae54
SBS
266: $ sudo su -
267: # nano /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
aafe11cf 268
88b5ae54
SBS
269Additionally, in order to prevent the ~raspi-config~ init script from
270reverting this text file back to ~ondemand~ after a reboot, this
271script must be disabled via:
aafe11cf 272
88b5ae54
SBS
273: $ sudo systemctl disable raspi-config
274
275****** Configure ~/boot/config.txt~
276Modify ~/boot/config.txt~ so that it contains these lines in order to
277disable power saving functions:
aafe11cf
SBS
278
279#+BEGIN_EXAMPLE
88b5ae54
SBS
280# Disable power saving
281nohz=off
aafe11cf
SBS
282#+END_EXAMPLE
283
88b5ae54
SBS
284[fn:se_20180320_raspicpugov] Title:[[https://raspberrypi.stackexchange.com/questions/9034/how-to-change-the-default-governor]["How to change the default governor?"]]; Author:[[https://raspberrypi.stackexchange.com/users/5538/goldilocks][goldilocks]]; Date: 2018-03-20; Website:stackexchange.com;
285
6bdc2473
SBS
286*** Normal Startup
287*** Normal Operation
288*** Normal Shutdown
289*** Unscheduled Shutdown
88b5ae54 290** Appendix A
1b0ddef8
SBS
291*** Example ~chrony.conf~ for ~chrony~
292For Raspberry Pi OS, the configuration file should be installed at
293~/etc/chrony/chrony.conf~.
88b5ae54
SBS
294
295#+BEGIN_EXAMPLE
1b0ddef8
SBS
296# Welcome to the chrony configuration file. See chrony.conf(5) for more
297# information about usuable directives.
298pool 2.debian.pool.ntp.org iburst
88b5ae54 299
1b0ddef8
SBS
300# This directive specify the location of the file containing ID/key pairs for
301# NTP authentication.
302keyfile /etc/chrony/chrony.keys
88b5ae54 303
1b0ddef8
SBS
304# This directive specify the file into which chronyd will store the rate
305# information.
306driftfile /var/lib/chrony/chrony.drift
88b5ae54 307
1b0ddef8
SBS
308# Uncomment the following line to turn logging on.
309#log tracking measurements statistics
88b5ae54 310
1b0ddef8
SBS
311# Log files location.
312logdir /var/log/chrony
88b5ae54 313
1b0ddef8
SBS
314# Stop bad estimates upsetting machine clock.
315maxupdateskew 100.0
88b5ae54 316
1b0ddef8
SBS
317# This directive enables kernel synchronisation (every 11 minutes) of the
318# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
319rtcsync
88b5ae54 320
1b0ddef8
SBS
321# Step the system clock instead of slewing it if the adjustment is larger than
322# one second, but only in the first three clock updates.
323makestep 1 3
88b5ae54 324
1b0ddef8
SBS
325# Get time from GPS (/dev/ttyAMA0) and PPS (/dev/pps0)
326#refclock SOCK /run/chrony.ttyAMA0.sock refid GPS precision 1e-1 offset 0.0000
327#refclock SOCK /run/chrony.pps0.sock refid PP precision 1e-7
328refclock SHM 0 refid GPS precision 1e-1 offset 0.0000 delay 0.2 stratum 1
329refclock SHM 1 refid PPS precision 1e-7 stratum 1
88b5ae54 330#+END_EXAMPLE
fffd9f66 331