ESP-NOW数据吞吐量记录GPS坐标

发布于 2025-01-26 08:03:43 字数 3058 浏览 3 评论 0 原文

我正在使用两个ESP32板进行ESP-NOW通信,其中一个ESP正在发送数据包,而另一个ESP则在将这些数据与GPS一起记录这些数据时接收到它们,以在某个坐标处测量吞吐量,以便我在移动和更改时获得吞吐量率发件人和接收器之间的速率。

ESP32接收器和GPS模块已连接到RPI,该RPI正在运行从串行COM端口读取数据,解析并将其写入文件的脚本。

import serial
import time
import pandas as pd

# Receiver
esp_com = '/dev/ttyUSB1'
esp_baud = 115200
ser = serial.Serial(port=esp_com, baudrate=esp_baud, timeout=0.1)

# GPS
gps_com = '/dev/ttyUSB0'
gps_baud = 4800
sergps = serial.Serial(port=gps_com, baudrate=gps_baud, timeout=0.1)

# title of the file to write data in
title = 'ESP-NOW LoRa'

# time step at which data is logged
timestep = 1

# create lists
XCV = []

# initial dropped/received packets data
drp = 0
rcv = 0
counter = 0

waiting_for_recv = False

# outputs empty data table
df = pd.DataFrame()

# outputs time in seconds since the timer started
then = time.time()
then1 = time.time()

# clear ESP buffers
ser.flushInput()
ser.flushOutput()

# initial data
RECEIVED = 0
RECEIVED = []
LAT = []
LON = []
T = []
t = 0
gpsdata = ''

# loop
while True:
    try:
        xcv = ser.readline().decode()  # decodes string
        if xcv != '':
            xcv = xcv.split("\r\n")
            for x in xcv:
                if x != '':
                    print(x)
                    XCV.append(x)
                    if 'RECV' in x:
                        RECEIVED = int(x[4:])  # sliced first 4 characters

        while True:
            cvb = sergps.readline()
            if cvb != b'':
                if 'GNGGA' in cvb.decode():
                    gpsdata = cvb.decode()
            else:
                break

        now = time.time()

        if now > then+timestep:
            print(now)
            df.at[counter, 'timestamp'] = now
            df.at[counter, 'received'] = RECEIVED
            drp = 0
            rcv = 0

            RECEIVED = 0
            then = now

            counter += 1

            vbn = gpsdata.split(',')
            if('GNGGA' in vbn[0]) & (vbn[2] != "") & (len(vbn) >= 5):
                print('GPS has a fix')
                tt = vbn[1]
                lat = vbn[2]
                lon = vbn[4]
                lat = int(lat[0:2])+float(lat[2:])/60
                lon = int(lon[0:3])+float(lon[3:])/60
                LAT.append(lat)
                LON.append(lon)
                T.append(t)
                t += 1
                print('LAT: {}, LON: {}'.format(lat, lon))

            else:
                print('GPS does not have a fix')
                LAT.append(0)
                LON.append(0)

            # write data to file every 30 seconds
            if now > then1+30:
                then1 = now
                # print GPS dataframe to columns
                df['lat'] = LAT
                df['lon'] = LON
                df.to_excel(title+'.xlsx')  # writes data to excel file

    except Exception as e:
        print(e)

我遇到了一个问题,一旦GPS有修复程序,并开始与坐标发送完整的NMEA消息,接收/丢弃的数据包数据丢失了,整个脚本都会挂起。我可以在变量中看到GPS NMEA数据正确解析,并且当GPS没有修复时,一切都起作用,但是一旦它开始发送完整的NMEA消息,脚本会断开脚本。

我自己真的无法弄清楚为什么,我认为代码本身需要优化。

I'm using two ESP32 boards for ESP-Now communication, where one ESP is sending packets and while the other is receiving them while logging this data together with GPS to measure throughput at a certain coordinate for me to get throughput rate while moving and changing rate between sender and receiver.

ESP32 receiver and GPS module is connected to Rpi that is running a script of reading the data from serial com ports, parsing it, and writing it to a file.

import serial
import time
import pandas as pd

# Receiver
esp_com = '/dev/ttyUSB1'
esp_baud = 115200
ser = serial.Serial(port=esp_com, baudrate=esp_baud, timeout=0.1)

# GPS
gps_com = '/dev/ttyUSB0'
gps_baud = 4800
sergps = serial.Serial(port=gps_com, baudrate=gps_baud, timeout=0.1)

# title of the file to write data in
title = 'ESP-NOW LoRa'

# time step at which data is logged
timestep = 1

# create lists
XCV = []

# initial dropped/received packets data
drp = 0
rcv = 0
counter = 0

waiting_for_recv = False

# outputs empty data table
df = pd.DataFrame()

# outputs time in seconds since the timer started
then = time.time()
then1 = time.time()

# clear ESP buffers
ser.flushInput()
ser.flushOutput()

# initial data
RECEIVED = 0
RECEIVED = []
LAT = []
LON = []
T = []
t = 0
gpsdata = ''

# loop
while True:
    try:
        xcv = ser.readline().decode()  # decodes string
        if xcv != '':
            xcv = xcv.split("\r\n")
            for x in xcv:
                if x != '':
                    print(x)
                    XCV.append(x)
                    if 'RECV' in x:
                        RECEIVED = int(x[4:])  # sliced first 4 characters

        while True:
            cvb = sergps.readline()
            if cvb != b'':
                if 'GNGGA' in cvb.decode():
                    gpsdata = cvb.decode()
            else:
                break

        now = time.time()

        if now > then+timestep:
            print(now)
            df.at[counter, 'timestamp'] = now
            df.at[counter, 'received'] = RECEIVED
            drp = 0
            rcv = 0

            RECEIVED = 0
            then = now

            counter += 1

            vbn = gpsdata.split(',')
            if('GNGGA' in vbn[0]) & (vbn[2] != "") & (len(vbn) >= 5):
                print('GPS has a fix')
                tt = vbn[1]
                lat = vbn[2]
                lon = vbn[4]
                lat = int(lat[0:2])+float(lat[2:])/60
                lon = int(lon[0:3])+float(lon[3:])/60
                LAT.append(lat)
                LON.append(lon)
                T.append(t)
                t += 1
                print('LAT: {}, LON: {}'.format(lat, lon))

            else:
                print('GPS does not have a fix')
                LAT.append(0)
                LON.append(0)

            # write data to file every 30 seconds
            if now > then1+30:
                then1 = now
                # print GPS dataframe to columns
                df['lat'] = LAT
                df['lon'] = LON
                df.to_excel(title+'.xlsx')  # writes data to excel file

    except Exception as e:
        print(e)

I ran into a problem, where once GPS has a fix, and starts sending full NMEA messages with coordinates, received/dropped packet data gets lost, and the whole script hangs. I can see in my variables that GPS NMEA data is parsed correctly and when GPS does not have a fix, everything works, but once it starts sending full NMEA messages with coordinates the script breaks.

I can't really figure it out myself why, and I think that the code itself needs optimisation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2025-02-02 08:03:43

我看到已经有一段时间了,但是我遇到了这篇文章。

因此,我看到的一个问题:

while True:
    cvb = sergps.readline()
    if cvb != b'':
         if 'GNGGA' in cvb.decode():
             gpsdata = cvb.decode()
    else:
        break

在这里,您将在任何数据来自GPS模块时陷入困境。获得 GPSDATA 后,您可能还需要休息。

另一件事,您有布尔值,因此请使用(逻辑)而不是& (bitwise):

vbn = gpsdata.split(',')
    if('GNGGA' in vbn[0]) & (vbn[2] != "") & (len(vbn) >= 5):
        print('GPS has a fix')

在此处阅读更多:'>'and'(boolean)vs'&'' (位)

I see it's been a while, but I have bumped in to this post.

So one issue I see:

while True:
    cvb = sergps.readline()
    if cvb != b'':
         if 'GNGGA' in cvb.decode():
             gpsdata = cvb.decode()
    else:
        break

Here you will stuck in the loop while the ANY data is coming from GPS module. You might want to break also after you get gpsdata.

One other thing here you have boolean values so use and (logical) and not & (bitwise):

vbn = gpsdata.split(',')
    if('GNGGA' in vbn[0]) & (vbn[2] != "") & (len(vbn) >= 5):
        print('GPS has a fix')

Read more here: 'and' (boolean) vs '&' (bitwise)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文