演示案例 - 使用昉·星光 2 的UART读取GPS数据

产品简介

本应用指南提供了使用昉·星光 2的UART,通过Python示例程序读取GPS数据的步骤。

下图显示了40-pin GPIO header的位置:

40-Pin GPIO Header定义

准备

1. 运行环境要求

2. 准备硬件

在执行演示程序之前,请务必准备以下硬件:

硬件准备

注:*:M:必须;O:可选。

以下表格和图片描述了如何将NEO-6M GPS连接到40-Pin GPIO Header上:

将NEO-6M GPS连接到40-Pin GPIO Header上

3. 准备软件

确认按照以下步骤进行操作:

注:该Python应用VisionFive.gpio适用于昉·星光单板计算机、昉·星光 2和昉·惊鸿7110 EVB。

3.1 按照《昉·星光 2单板计算机快速参考手册》(http://doc.rvspace.org/VisionFive2/PDF/VisionFive2_QSG.pdf)中的“将OS烧录到Micro-SD”章节,将Debian OS烧录到Micro-SD卡上。

3.2 登录Debian并确保昉·星光 2已联网。有关详细说明,请参阅《昉·星光 2单板计算机快速参考手册》中“通过以太网使用SSH”或“使用USB转串口转换器”章节。

3.3 在Debian上扩展分区,请参见《昉·星光 2单板计算机快速参考手册》中“扩展分区”章节。

3.4 执行以下命令,在Debian系统上安装PIP:

apt-get install python3-pip

3.5 在昉·星光 2 Debian上执行pip命令,以安装VisionFive.gpio包:

sudo pip install VisionFive.gpio

或者,您也可以执行以下命令:

sudo pip3 install VisionFive.gpio

3.6 (可选)如果您将源代码复制到昉·星光 2 Debian下的本地目录,请在源代码目录下执行以下命令:

提示:点击以下链接可下载源代码:VisionFive.gpio。

sudo apt-get install python3-dev sudo python setup.py install

或者,您也可以执行以下命令:

sudo python3 setup.py install

执行演示代码

执行以下操作,以在昉·星光 2的Debian系统上运行演示代码:

1. 找到测试代码uart_gps_demo.py所在的目录:

1.1 执行以下命令以获取VisionFive.gpio所在的目录:

pip show VisionFive.gpio

示例结果:

Location: /usr/local/lib64/python3.9/site-packages

注:实际输出取决于应用的安装方式。

1.2 如前一步输出中所示,执行以下操作进入目录/usr/local/lib64/python3.9/site-packages:

cd /usr/local/lib64/python3.9/site-packages

1.3 执行以下命令进入sample-code目录:

cd ./VisionFive/sample-code/

2. 在执行演示代码前,在您的终端上执行以下命令:

sudo systemctl stop serial-getty@ttyS0.service

3. 在sample-code目录下,执行以下命令以运行演示代码:

sudo python uart_gps_demo.py

或者,您也可以执行以下命令:

sudo python3 uart_gps_demo.py

结果:

如果GPS信号弱,终端输出如下:
*****The GGA info is as follows: *****  
msg_id: $GPGGA  
NorS:   
EorW:   
pos_indi: 0  
total_Satellite: 00  
!!!!!!Positioning is invalid!!!!!!
如果GPS信号强,几秒后终端输出如下:
*****The GGA info is as follows: *****  
msg_id: print(" utc time: 2:54:47.0  
utc time: 025447.00 (format: hhmmss.sss)  
latitude: 30 degree 33.29251 minute  latitude: 3033.29251 (format: dddmm.mmmmm)  
NorS: N 
longitude: 104 degree 3.45523 minute  
longitude: 10403.45523 (format: dddmm.mmmmm)  
EorW: E  
pos_indi: 1  
total_Satellite: 08 

 *****The positioning type is 3D ***** 
The Satellite ID of channel {} : {}                            
                            ch1 : 14
                            ch2 : 01
                            ch3 : 03
                            ch4 : 06
                            ch5 : 30 
                            ch6 : 21
                            ch7 : 19
                            ch8 : 17

演示源代码

本演示中的资源代码仅作为参考。

uart_gps_demo.py:

'''
Please make sure the NEO-6M is connected to the correct pins.
The following table describes how to connect NEO-6M to the 40-pin header
-----------------------------------------
Passive Buzzer___Pin Number_____Pin Name
    VCC             4           5 V Power
    GND             6             GND
    TXD             10          UART RX
    RXD             8           UART TX
----------------------------------------
'''

import sys
import serial
import time

#Reference information of the GPGSA format.
'''
Example 1 (GPS only): 

$GPGSA,M,3,17,02,30,04,05,10,09,06,31,12,,,1.2,0.8,0.9*35

Example 2 (Combined GPS and GLONASS): 

$GNGSA,M,3,17,02,30,04,05,10,09,06,31,12,,,1.2,0.8,0.9*2B

$GNGSA,M,3,87,70,,,,,,,,,,,1.2,0.8,0.9*2A

----------------------------------------------------------
SN      Field   
                Description   
                                Symbol
                                            Example
----------------------------------------------------------
1       $GPGSA 
                Log header.For information about the log headers, see ASCII, Abbreviated ASCII or Binary.
                                N/A
                                            $GPGSA
2       mode MA      
                Mode: 1 = Fix not available; 2 = 2D; 3 = 3D
                                x
                                            3
3       mode 123     
                Latitude (DDmm.mm)
                                llll.ll     
                                            5106.9847
4-15   prn
                PRN numbers of satellites used in solution (null for unused fields), total of 12 fields
                GPS = 1 to 32
                SBAS = 33 to 64 (add 87 for PRN number)
                GLO = 65 to 96
                                xx,xx,.....
                                            18,03,13,25,16,24,12,20,,,,

The detail info, please see GPGSA
            
'''

GPGSA_dict = {
"msg_id":  	0,
"mode1":  	1,
"mode2":  	2,
"ch1":          3,
"ch2":          4,
"ch3":          5,
"ch4":          6,
"ch5":          7,
"ch6":          8,
"ch7":          9,
"ch8":          10,
"ch9":          11,
"ch10":         12,
"ch11":         13,
"ch12":         14,
}

#Reference information of the GPGGA format.
'''
Example 1 (GPS only): 

$GPGSA,M,3,17,02,30,04,05,10,09,06,31,12,,,1.2,0.8,0.9*35

Example 2 (Combined GPS and GLONASS): 

$GNGSA,M,3,17,02,30,04,05,10,09,06,31,12,,,1.2,0.8,0.9*2B

$GNGSA,M,3,87,70,,,,,,,,,,,1.2,0.8,0.9*2A

---------------------------------------------------------
SN   Field   
                Description   
                                Symbol
                                            Example
---------------------------------------------------------
1   $GPGGA 
                Log header.For information about the log headers, see ASCII, Abbreviated ASCII or Binary.
                                N/A
                                            $GPGGA
2   utc      
                UTC time status of position (hours/minutes/seconds/ decimal seconds)
                                hhmmss.ss
                                            202134.00
3   lat     
                Latitude (DDmm.mm)
                                llll.ll     
                                            5106.9847
4   lat dir
                Latitude direction (N = North, S = South)
                                a
                                            N
5   lon
                Latitude direction (N = North, S = South)
                                yyyyy.yy
                                            11402.2986
6   lon dir
                Longitude direction (E = East, W = West)
                                a
                                            W
7   quality
                refer to Table: GPS Quality Indicators
                                x
                                            1
8   # sats
                Number of satellites in use.May be different to the number in view
                                xx
                                            10
The detail info, please see GPGGA
            
'''
GPGGA_dict = {
"msg_id":  		0,
"utc":  		        1,
"latitude":             2,
"NorS":                 3,
"longitude":            4,
"EorW":                 5,
"pos_indi":             6,
"total_Satellite":      7,
}

uart_port = "/dev/ttyS0"

def IsValidGpsinfo(gps): 
    data = gps.readline()
    #Convert the data to string. 
    msg_str = str(data, encoding="utf-8")
    #Split string with ",".
    #GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30
    msg_list = msg_str.split(",")
    
    #Parse the GPGSA message.
    if (msg_list[GPGSA_dict['msg_id']] == "$GPGSA"): 
            print()
            #Check if the positioning is valid.
            if msg_list[GPGSA_dict['mode2']] == "1": 
                print("!!!!!!Positioning is invalid!!!!!!")
            else: 
                print("*****The positioning type is {}D *****".format(msg_list[GPGSA_dict['mode2']]))
                print("The Satellite ID of channel {} : {}")
                #Parse the channel information of the GPGSA message.
                for id in range(0, 12): 
                    key_name = list(GPGSA_dict.keys())[id + 3]
                    value_id =  GPGSA_dict[key_name]
                    if  not (msg_list[value_id] == ''): 
                        print("                           {} : {}".format(key_name, msg_list[value_id]))

    #Parse the GPGGA message. 
    if msg_list[GPGGA_dict['msg_id']] == "$GPGGA": 
        print()
        print("*****The GGA info is as follows: *****")
        for key, value in GPGGA_dict.items(): 
            #Parse the utc information.
            if key == "utc": 
                utc_str =  msg_list[GPGGA_dict[key]]
                if not utc_str == '': 
                    h = int(utc_str[0:2])
                    m = int(utc_str[2:4])
                    s = float(utc_str[4:])
                    print(" utc time: {}:{}:{}".format(h,m,s))
                    print(" {} time: {} (format: hhmmss.sss)".format(key, msg_list[GPGGA_dict[key]]))
            #Parse the latitude information.
            elif key == "latitude": 
                lat_str = msg_list[GPGGA_dict[key]]
                if not lat_str == '': 
                    Len = len(lat_str.split(".")[0])
                    d = int(lat_str[0:Len-2])
                    m = float(lat_str[Len-2:])
                    print(" latitude: {} degree {} minute".format(d, m))
                    print(" {}: {} (format: dddmm.mmmmm)".format(key, msg_list[GPGGA_dict[key]]))
            #Parse the longitude information.
            elif key == "longitude": 
                lon_str = msg_list[GPGGA_dict[key]]
                if not lon_str == '': 
                    Len = len(lon_str.split(".")[0])
                    d = int(lon_str[0:Len-2])
                    m = float(lon_str[Len-2:])
                    print(" longitude: {} degree {} minute".format(d, m))
                    print(" {}: {} (format: dddmm.mmmmm)".format(key, msg_list[GPGGA_dict[key]]))
            else: 
                print(" {}: {}".format(key, msg_list[GPGGA_dict[key]]))

def main(): 
    gps = serial.Serial(uart_port, baudrate=9600, timeout=0.5)
    while True: 
        IsValidGpsinfo(gps)
        time.sleep(1)
    
    gps.close()
                
if __name__ == "__main__": 
    sys.exit(main())
展开阅读全文

页面更新:2024-03-12

标签:演示   星光   单板计算机   终端   惊鸿   源代码   命令   案例   代码   硬件   目录   数据

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top