I have GPSD
running on a Linux system (specifically SkyTraq Venus 6
on a Raspberry Pi 3
, but that shouldn't matter). Is there a way to trigger a command when the GPS first acquires or loses the 3D fix, almost like the scripts in /etc/network/if-up.d
and /etc/network/if-down.d
?
Asked
Active
Viewed 1,734 times
2

rudolfbyker
- 2,034
- 1
- 20
- 25
1 Answers
4
I found a solution:
Step 1: With GPSD
running, gpspipe -w
outputs JSON
data, documented here. The TPV
class has a mode
value, which can take one of these values:
- 0=unknown mode
- 1=no fix
- 2=2D fix
- 3=3D fix
Step 2: Write a little program called gpsfix.py
:
#!/usr/bin/env python
import sys
import errno
import json
modes = {
0: 'unknown',
1: 'nofix',
2: '2D',
3: '3D',
}
try:
while True:
line = sys.stdin.readline()
if not line: break # EOF
sentence = json.loads(line)
if sentence['class'] == 'TPV':
sys.stdout.write(modes[sentence['mode']] + '\n')
sys.stdout.flush()
except IOError as e:
if e.errno == errno.EPIPE:
pass
else:
raise e
For every TPV
sentence, gpspipe -w | ./gpsfix.py
will print the mode.
Step 3: Use grep 3D -m 1
to wait for the first fix, and then quit (which sends SIGPIPE
to all other processes in the pipe).
gpspipe -w | ./gpsfix.py | grep 3D -m 1
will print 3D
on the first fix.
Step 4: Put in in a bash script:
#!/usr/bin/env bash
# Wait for first 3D fix
gpspipe -w | ./gpsfix.py | grep 3D -m 1
# Do something nice
cowsay "TARGET LOCATED"
And run it:
$ ./act_on_gps_fix.sh
3D
________________
< TARGET LOCATED >
----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

rudolfbyker
- 2,034
- 1
- 20
- 25
-
1I think you went a step too far. There is no need to use another pipe and grep the output, when you can simply exit your script if you find that `sentence['mode'] == 3`. So `if sentence['class'] == 'TPV' and sentence['mode'] == 3: sys.exit(0)` shoudl do it. – solsTiCe Feb 10 '20 at 21:49
-
Indeed! If I recall correctly, I piped the output of `gpsfix.py` to other things, too. Maybe a log file. – rudolfbyker Feb 11 '20 at 09:03