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