added option toggle to write data to csv file
This commit is contained in:
parent
ec71dd1094
commit
eb76c18efc
1 changed files with 29 additions and 17 deletions
|
|
@ -13,6 +13,7 @@ UDP_IP = "127.0.0.1"
|
|||
BEAMNG_UDP_PORT = 4444
|
||||
FORZA_UDP_PORT= 4843
|
||||
SERIAL_PORT = ""
|
||||
firstRun= True #used to do the headers for csv files
|
||||
|
||||
class GameType(Enum):
|
||||
NONE=0,
|
||||
|
|
@ -25,6 +26,19 @@ gameSelected = False
|
|||
portToConnect= 0
|
||||
gameType= GameType.NONE
|
||||
carData = 0
|
||||
csvOut = False
|
||||
|
||||
#Functions <-------------------------------------------------------------------------------------------
|
||||
def csvWriteOut(firstRun,carData,csvFile):
|
||||
if firstRun == True:
|
||||
out=""
|
||||
for im,nm in carData.items():
|
||||
out = out + im + ","
|
||||
csvFile.write(out + "\n")
|
||||
outString=""
|
||||
for itm,num in carData.items():
|
||||
outString = outString + str(num) +","
|
||||
csvFile.write(outString+"\n")
|
||||
|
||||
#code runs from here<-----------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
|
@ -40,7 +54,7 @@ else:
|
|||
|
||||
#select game
|
||||
while gameSelected == False:
|
||||
gameNo = input("1:BEAMNG\n2:FORZA\n\n\n9:SET SERIAL PORT ("+SERIAL_PORT+")\n")
|
||||
gameNo = input("1:BEAMNG\n2:FORZA\n\n7:Toggle CSV out ("+str(csvOut)+")\n9:SET SERIAL PORT ("+SERIAL_PORT+")\n")
|
||||
if gameNo == "1":
|
||||
portToConnect = BEAMNG_UDP_PORT
|
||||
gameSelected = True
|
||||
|
|
@ -51,6 +65,11 @@ while gameSelected == False:
|
|||
gameType=GameType.FORZA
|
||||
gameSelected=True
|
||||
print("Forza Selected")
|
||||
elif gameNo == "7":
|
||||
if csvOut == True:
|
||||
csvOut=False
|
||||
elif csvOut ==False:
|
||||
csvOut =True
|
||||
elif gameNo == "9":
|
||||
SERIAL_PORT = input("set serial port: ")
|
||||
else:
|
||||
|
|
@ -74,12 +93,11 @@ while connectedArduino == False:
|
|||
print("please check connection to arduino and verify the correct serial port")
|
||||
time.sleep(1)
|
||||
|
||||
csvOut = False
|
||||
csvFile = open('dataOut.csv',"a")
|
||||
##csvWriter = csv.writer(csvFile)
|
||||
firstRun= True
|
||||
if csvOut == True:
|
||||
csvFile = open(str(int(time.time())+'.csv',"a"))
|
||||
|
||||
|
||||
|
||||
#print(struct.calcsize(FORZA_METHODS.FORZA_DATA_FORMAT))
|
||||
print("ready:\n")
|
||||
while True:
|
||||
data, addr = sock.recvfrom(1024)
|
||||
|
|
@ -87,22 +105,16 @@ while True:
|
|||
unpackedData = struct.unpack(BEAMNG_METHODS.BEAMNG_DATA_FORMAT,data)
|
||||
carData = BEAMNG_METHODS.unpackData(unpackedData)
|
||||
kmh=carData["speed"]*3.6
|
||||
if csvOut == True:
|
||||
csvWriteOut(firstRun,carData,csvFile)
|
||||
firstRun=False
|
||||
elif gameType == GameType.FORZA:
|
||||
unpackedData = struct.unpack(FORZA_METHODS.FORZA_DATA_FORMAT,data)
|
||||
carData = FORZA_METHODS.unpackData(unpackedData)
|
||||
#carData = FORZA_METHODS.get_data(data)
|
||||
kmh = carData["Speed"]*3.6
|
||||
if csvOut == True:
|
||||
if firstRun == True:
|
||||
out=""
|
||||
for im,nm in carData.items():
|
||||
out = out + im + ","
|
||||
csvFile.write(out + "\n")
|
||||
firstRun=False
|
||||
outString=""
|
||||
for itm,num in carData.items():
|
||||
outString = outString + str(num) +","
|
||||
csvFile.write(outString+"\n")
|
||||
csvWriteOut(firstRun,carData,csvFile)
|
||||
firstRun=False
|
||||
|
||||
toPi.write(str(kmh).encode()+":".encode())
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue