目次
RaspbianでGPSデータを使用する手順。
GPSレシーバとして、GLOBALSAT BU-353S4を使用する。
USB接続型のGPSレシーバであるため取り回しが良く、アンテナの底面に磁石が埋め込まれているため金属面へ貼り付けることができる。
# lsusb Bus 001 Device 006: ID 0c45:7403 Microdia Foot Switch Bus 001 Device 005: ID 1a40:0101 Terminus Technology Inc. Hub Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
GPSレシーバが受信した情報から位置情報を読み取るためにはgpsdとpythonの導入を行う。
# apt install gpsd gpsd-clients
# vi /etc/default/gpsd
DEVICES="/dev/ttyUSB0" GPSD_OPTIONS="-F /var/run/gpsd.sock -b -n"
# systemctl enable gpsd # systemctl start gpsd
# apt-get install python-gps # pip3 install gps3
GPSレシーバから情報を受信できることを確認する。
gpsmonは専用のインターフェイスにより受信状況を確認することができる。
# gpsmon
gpsdはAPIを経由してデータを再利用できる構造を提供している。
以下の様なPythonスクリプトでデータの取得が可能。
#!/usr/bin/python3
from gps3 import gps3
import codecs
gps_socket = gps3.GPSDSocket()
data_stream = gps3.DataStream()
gps_socket.connect()
gps_socket.watch()
for new_data in gps_socket:
if new_data:
data_stream.unpack(new_data)
print('time : ', data_stream.TPV['time'], file=codecs.open('/tmp/geoinfo.log', 'a', 'utf-8'))
print('lat : ', data_stream.TPV['lat'], file=codecs.open('/tmp/geoinfo.log', 'a', 'utf-8'))
print('lon : ', data_stream.TPV['lon'], file=codecs.open('/tmp/geoinfo.log', 'a', 'utf-8'))
print('alt : ', data_stream.TPV['alt'], file=codecs.open('/tmp/geoinfo.log', 'a', 'utf-8'))
print('speed : ', data_stream.TPV['speed'], file=codecs.open('/tmp/geoinfo.log', 'a', 'utf-8'))
time : 2020-09-20T10:29:11.000Z lat : 35.682083333 lon : 139.631108333 alt : 37.2 speed : 0.0
GPSからは時刻情報が取得できるため、NTPサービスのデータソースとして使用することができる。
ntpdを導入し、時刻ソースとしてGPSを指定する。
# apt-get install ntp # systemctl start ntp
# vi /etc/ntp.conf 【以下を追記する】 # gps ntp server 127.127.28.0 minpoll 4 fudge 127.127.28.0 time1 0.183 refid NMEA server 127.127.28.1 minpoll 4 prefer fudge 127.127.28.1 time1 0.183 refid PPS
# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*SHM(0) .NMEA. 0 l 13 16 377 0.000 0.073 0.410
SHM(1) .PPS. 0 l - 16 0 0.000 0.000 0.000