'How to plot data from UDP stream coming from F1 2019 game, my lineplot comes up blank
I have this below code in which I am getting data packets from UDP from the game. However, when running this code I am getting a blank plot, I want to plot Speed Vs Time here and I want to see what interesting visualization comes up while I drive in corners. Any help or pointer is much appreciated.
Code -
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)
Solution 1:[1]
Hi you may have solved the problem on your own, but if not i hope this can help. I ran your code and the problem seems to be, that you are not saving the data and the plot just has one data point. Try creating a list (L=[]
) and then L.append(speed)
. Do the same with time and plot the list(one list for each axis).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Bene |