5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonでGPSモジュールから10Hzでデータを取得する

Last updated at Posted at 2017-03-21

gpsdを使わない、シリアル接続でのGPS10Hz化です。
コードは以下の通りで、一番下のメイン部分に受け取ったデータを処理するコードを書きます。

mtk3339_set_10Hz.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-

from time import sleep
import serial

# シリアル接続baudrate初期値:9600
ser=serial.Serial('/dev/ttyS0', baudrate=9600, timeout=30)

class GPS: 
    def __init__(self):
        UPDATE_100msec=  r"$PMTK220,100*2F\r\n"        # 更新間隔 100msec(10Hz)
        UPDATE_200msec=  r"$PMTK220,200*2C\r\n"        # 更新間隔 200msec(5Hz)
        UPDATE_250msec=  r"$PMTK220,250*29\r\n"        # 更新間隔 250msec(4Hz)
        UPDATE_500msec=  r"$PMTK220,500*2B\r\n"        # 更新間隔 500msec(2Hz)
        UPDATE_1000msec=  r"$PMTK220,1000*1F\r\n"      # 更新間隔 1sec(1Hz)
        MEAS_100msec= r"$PMTK300,100,0,0,0,0*2C\r\n"   # 測定間隔 100msec(10Hz)
        MEAS_200msec= r"$PMTK300,200,0,0,0,0*2F\r\n"   # 測定間隔 200msec(5Hz)
        MEAS_250msec= r"$PMTK300,250,0,0,0,0*2A\r\n"   # 測定間隔 250msec(4Hz)
        MEAS_500msec= r"$PMTK300,500,0,0,0,0*28\r\n"   # 測定間隔 500msec(2Hz)
        MEAS_1000msec= r"$PMTK300,1000,0,0,0,0*1C\r\n" # 測定間隔 1sec(1Hz)
        BAUDRATE_9600 = r"$PMTK251,9600*17\r\n"        # ttyのbaudrate 9600bps
        BAUDRATE_19200 = r"$PMTK251,19200*22\r\n"      # ttyのbaudrate 19200bps
        BAUDRATE_38400 = r"$PMTK251,38400*27\r\n"      # ttyのbaudrate 38400bps
        BAUDRATE_57600 = r"$PMTK251,57600*2C\r\n"      # ttyのbaudrate 57600bps
        BAUDRATE_115200 = r"$PMTK251,115200*1F\r\n"    # ttyのbaudrate 115200bps

        #Commands for which NMEA Sentences are sent
        GPRMC_ONLY= "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n" # Send only the GPRMC Sentence
        GPRMC_GPGGA="$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" # Send GPRMC AND GPGGA Sentences
        SEND_ALL ="$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n"  # Send All Sentences
        SEND_NOTHING="$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" # Send Nothing

        ser.write(BAUDRATE_115200.encode('utf-8')) # Set Baudrate to 115200
        sleep(1)
        ser.baudrate=115200
        ser.write(UPDATE_100msec.encode('utf-8'))  # Set update interval 100ms
        sleep(1)
        ser.write(MEAS_100msec.encode('utf-8'))    # Set measurement interval 100ms
        sleep(1)
        ser.write(GPRMC_ONLY.encode('utf-8'))      # Set report GPRMC message only
        sleep(1)
        ser.flushInput()
        ser.flushOutput()
        print("GPS is Initialized\n")

gps=GPS() # インスタンス初期化時にGPSの設定を行っている
while(1):
    while ser.inWaiting()==0:
        pass
    data=ser.readline()
    # ここに取得データの処理を書く
    print(data)

updateとmeasurementの設定が分かれているのはなぜだろう?連動しないと意味が無いような気がしますが。

5
6
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?