如何绘制来自 F1 2019 游戏的 UDP 流数据,我的线图空白
How to plot data from UDP stream coming from F1 2019 game, my lineplot comes up blank
我有下面的代码,我在其中从游戏的 UDP 获取数据包。但是,当 运行 此代码出现空白图时,我想在此处绘制速度与时间的关系图,并且想看看在转弯时会出现什么有趣的可视化效果。非常感谢任何帮助或指示。
代码-
import time
import socket
import rawutil
import matplotlib.pyplot as plt
plt.ion()
import seaborn as sns
from f1_2019_telemetry.packets import unpack_udp_packet
# udp_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# udp_socket.bind(('', 20777))
# while True:
# udp_packet = udp_socket.recv(2048)
# packet = unpack_udp_packet(udp_packet)
# print("Received:", packet)
listen_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
listen_socket.bind(('', 20777))
while True:
x = int(round(time.time()*1000))
print(x)
# Receiving data
data, address = listen_socket.recvfrom(2048)
header = data[:20]
telemetry = data[20:]
# decode the header
packetFormat, = rawutil.unpack('<H', header[:2])
gameMajorVersion, = rawutil.unpack('<B', header[2:3])
gameMinorVersion, = rawutil.unpack('<B', header[3:4])
packetVersion, = rawutil.unpack('<B', header[4:5])
packetId, = rawutil.unpack('<B', header[5:6])
sessionUID, = rawutil.unpack('<Q', header[6:14])
sessionTime, = rawutil.unpack('<f', header[14:18])
frameIdentifier, = rawutil.unpack('<B', header[18:19])
playerCarIndex, = rawutil.unpack('<B', header[19:20])
# print all info (just for now)
## print('Packet Format : ',packetFormat)
## print('Game Major Version : ',gameMajorVersion)
## print('Game Minor Version : ',gameMinorVersion)
## print('Packet Version : ',packetVersion)
## print('Packet ID : ', packetId)
## print('Unique Session ID : ',sessionUID)
## print('Session Time : ',sessionTime)
## print('Frame Number : ',frameIdentifier)
## print('Player Car Index : ',playerCarIndex)
## print('\n\n')
#start getting the packet data for each packet starting with telemetry data
if (packetId == 6):
speed, = rawutil.unpack('<H' , telemetry[3:5])
throttle, = rawutil.unpack('<f' , telemetry[4:8])
steer, = rawutil.unpack('<f' , telemetry[8:12])
brake, = rawutil.unpack('<f' , telemetry[12:16])
gear, = rawutil.unpack('<b' , telemetry[17:18])
rpm, = rawutil.unpack('<H' , telemetry[18:20])
print (speed)
y = speed
plt.xlabel("Nanos")
plt.ylabel("Speed")
# sns.lineplot(x,int(y))
# plt.scatter(x,y)
lines = plt.plot(x,y)
plt.draw()
plt.setp(lines, color='r', linewidth=2.0)
plt.pause(0.1)
plt.show(block=True)
您好,您可能已经自行解决了问题,但如果没有,我希望这能对您有所帮助。我 运行 你的代码和问题似乎是,你没有保存数据并且绘图只有一个数据点。尝试创建一个列表 (L=[]
),然后是 L.append(speed)
。对时间做同样的事情并绘制列表(每个轴一个列表)。
我有下面的代码,我在其中从游戏的 UDP 获取数据包。但是,当 运行 此代码出现空白图时,我想在此处绘制速度与时间的关系图,并且想看看在转弯时会出现什么有趣的可视化效果。非常感谢任何帮助或指示。
代码-
import time
import socket
import rawutil
import matplotlib.pyplot as plt
plt.ion()
import seaborn as sns
from f1_2019_telemetry.packets import unpack_udp_packet
# udp_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# udp_socket.bind(('', 20777))
# while True:
# udp_packet = udp_socket.recv(2048)
# packet = unpack_udp_packet(udp_packet)
# print("Received:", packet)
listen_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
listen_socket.bind(('', 20777))
while True:
x = int(round(time.time()*1000))
print(x)
# Receiving data
data, address = listen_socket.recvfrom(2048)
header = data[:20]
telemetry = data[20:]
# decode the header
packetFormat, = rawutil.unpack('<H', header[:2])
gameMajorVersion, = rawutil.unpack('<B', header[2:3])
gameMinorVersion, = rawutil.unpack('<B', header[3:4])
packetVersion, = rawutil.unpack('<B', header[4:5])
packetId, = rawutil.unpack('<B', header[5:6])
sessionUID, = rawutil.unpack('<Q', header[6:14])
sessionTime, = rawutil.unpack('<f', header[14:18])
frameIdentifier, = rawutil.unpack('<B', header[18:19])
playerCarIndex, = rawutil.unpack('<B', header[19:20])
# print all info (just for now)
## print('Packet Format : ',packetFormat)
## print('Game Major Version : ',gameMajorVersion)
## print('Game Minor Version : ',gameMinorVersion)
## print('Packet Version : ',packetVersion)
## print('Packet ID : ', packetId)
## print('Unique Session ID : ',sessionUID)
## print('Session Time : ',sessionTime)
## print('Frame Number : ',frameIdentifier)
## print('Player Car Index : ',playerCarIndex)
## print('\n\n')
#start getting the packet data for each packet starting with telemetry data
if (packetId == 6):
speed, = rawutil.unpack('<H' , telemetry[3:5])
throttle, = rawutil.unpack('<f' , telemetry[4:8])
steer, = rawutil.unpack('<f' , telemetry[8:12])
brake, = rawutil.unpack('<f' , telemetry[12:16])
gear, = rawutil.unpack('<b' , telemetry[17:18])
rpm, = rawutil.unpack('<H' , telemetry[18:20])
print (speed)
y = speed
plt.xlabel("Nanos")
plt.ylabel("Speed")
# sns.lineplot(x,int(y))
# plt.scatter(x,y)
lines = plt.plot(x,y)
plt.draw()
plt.setp(lines, color='r', linewidth=2.0)
plt.pause(0.1)
plt.show(block=True)
您好,您可能已经自行解决了问题,但如果没有,我希望这能对您有所帮助。我 运行 你的代码和问题似乎是,你没有保存数据并且绘图只有一个数据点。尝试创建一个列表 (L=[]
),然后是 L.append(speed)
。对时间做同样的事情并绘制列表(每个轴一个列表)。