2016年2月20日 星期六

在Raspberry 上面使用GPS module


好一陣子沒有在玩 Raspberry Pi 了,由於網友詢問這類問題剛好手上有GPS模組所以找了空檔寫了這篇希望能有所幫助。
一般GPS大概都是透過非同步式串列介面(UART)作為訊號傳遞連結,模組與Raspberry Pi連結就是透過高低電位的TTL方式使用UART介面來傳遞,通常運用到模組上的RX,TX 兩條訊號線及VCC/GND (這裡的VCC是供電給GPS模組使用的)。
模組上你可能會看到還有VBAT這是外接電池使用或者每家開發商會有不同的定義其他腳位,這個部分就得要看所買到的GPS模組Datasheet了。

至於GPS模組中還要注意的是協議部分,常見的GPS模組內建協議包括NMEA-0183、 SiRF binary、RTCM SC-104等格式,這些協議格是可以透過AT命令進行切換,本篇文章則以常見的NMEA為主,好處是讀取道的資訊是一般的明碼,我們取來應用比較方便。


實作Raspberry Pi 整合 GPS 模組有兩種方式,一種是透過TTL方式直接跟IO接腳的第8腳UART TX及第10腳UART RX連結(方案一),另外就是GPS模組透過 USB to TTL Adapter Cable連接到Raspberry Pi 的 USB上面(方案二)。詳細接線圖如下:

注意  (usb2TTL)TX -> 對方的RX...
         (usb2TTL)RX -> 對方的TX...

USB2TTL 的綠色為TX...
                   白色為RX...



1. Python
在寫程式的前置動作必須先安裝 python 的 serial 函式庫,執行下列指令:
sudo apt-get install python-serial

接著用編輯器編輯一個檔案名稱為 gps.py 的程式,其內容如下:
import serial

def getdata(port):
# open the serial port
    port.open()
# check that the port is open
    if port.isOpen():
# read 16 lines
        line = []
        for i in range(1,16):
            line.append(port.readline())
# close the serial port
    port.close()
# discard the first line (sometimes it contains rubbish, so just always discard it)
    del line[0]
# return the list of lines
    return line

def outputdata(data):
# print the list of lines
    for i in range(0,len(data)):
        print data[i]

def initialise():
# initialise serial port settings
    Port = serial.Serial()
    Port.baudrate = 9600              //鮑率(Baud Rate)
    Port.port = '/dev/ttyAMA0'       //如接USB的話改成dev/ttyUSB0
    Port.xonxoff = 1
# return the port as an object we can use
    return Port

# main program starts here
sPort = initialise()
data = getdata(sPort)
outputdata(data)
# end

執行結果:
使用指令:
sudo python gps.py
結果如下:



接下來就是要研究這些資訊室做甚麼的.....

This is the raw GPS "NMEA sentence" output from the GPS module. There are a few different kinds of NMEA sentences, the most common ones people use are the $GPRMC (Global Positioning Recommended Minimum Coordinates or something like that) and the $GPGGA sentences. These two provide the time, date, latitude, longitude, altitude, estimated land speed, and fix type. Fix type indicates whether the GPS has locked onto the satellite data and received enough data to determine the location (2D fix) or location+altitude (3D fix).

For more details about NMEA sentences and what data they contain, check out this site


Something that starts with $GPRMC like:
Copy Code
  1. $GPRMC,194509.000,A,4042.6142,N,07400.4168,W,2.03,221.11,160412,,,A*77
This line is called the RMC (Recommended Minimum) sentence and has pretty much all of the most useful data. Each chunk of data is separated by a comma.
The first part 194509.000 is the current time GMT (Greenwich Mean Time). The first two numbers 19 indicate the hour (1900h, otherwise known as 7pm) the next two are the minute, the next two are the seconds and finally the milliseconds. So the time when this screenshot was taken is 7:45 pm and 9 seconds. The GPS does not know what time zone you are in, or about "daylight savings" so you will have to do the calculation to turn GMT into your timezone

The second part is the 'status code', if it is a V that means the data is Void (invalid). If it is an A that means its Active (the GPS could get a lock/fix)
The next 4 pieces of data are the geolocation data. According to the GPS, my location is 4042.6142,N (Latitude 40 degrees, 42.6142 decimal minutes North) & 07400.4168,W. (Longitude 74 degrees, 0.4168 decimal minutes West) To look at this location in Google maps, type +40° 42.6142', -74° 00.4168' into the google maps search box . Unfortunately gmaps requires you to use +/- instead of NSWE notation. N and E are positive, S and W are negative.

People often get confused because the GPS is working but is "5 miles off" - this is because they are not parsing the lat/long data correctly. Despite appearances, the geolocation data is NOT in decimal degrees. It is in degrees and minutes in the following format: Latitude: DDMM.MMMM (The first two characters are the degrees.) Longitude: DDDMM.MMMM (The first three characters are the degrees.)
The next data is the ground speed in knots. We're going 2.03 knots
After that is the tracking angle, this is meant to approximate what 'compass' direction we're heading at based on our past travel
The one after that is 160412 which is the current date (this sentence was first 'grabbed' on 16th of April, 2012).
Finally there is the *XX data which is used as a data transfer checksum
Once you get a fix using your GPS module, verify your location with google maps (or some other mapping software). Remember that GPS is often only accurate to 5-10 meters and worse if you're indoors or surrounded by tall buildings.

Reference : http://cheng-min-i-taiwan.blogspot.tw/2014/03/raspberry-pi-gps.html

Reference : https://github.com/mcauser/Raspberry-Pi-ITead-Studio-GPS-NEO-6M

Reference : https://learn.adafruit.com/adafruit-ultimate-gps-hat-for-raspberry-pi/pi-setup