connected main program

This commit is contained in:
Seth Samuel 2024-12-02 12:35:16 +13:00
parent f54e0d2fdb
commit e1474b162c

View file

@ -6,6 +6,7 @@ import serial
import time
import platform
import sys
import threading
from enum import Enum
import tkinter
from GAME_METHODS import *
@ -27,6 +28,7 @@ FORZA_UDP_PORT= 4843
SERIAL_PORT = ""
firstRun= True #used to do the headers for csv files
pushingToArduino = False
stopThread = False
class GameType(Enum):
NONE=0
@ -37,12 +39,12 @@ class ProgramState(Enum):
PUSHING_DATA = 1
WAITNG = 2
ERROR = 3
appState =ProgramState.WAITNG
connectedArduino = False
connectedWebSocket = False
gameSelected = False
portToConnect= 0
gameType= GameType.NONE
carData = 0
csvOut = False
@ -58,6 +60,46 @@ def csvWriteOut(firstRun,carData,csvFile):
outString = outString + str(num) +","
csvFile.write(outString+"\n")
def runningThread():
global stopThread
#check everything is connected
try:
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((UDP_IP,portToConnect))
except:
print("please check you are able to open the socket on this system\ntired to open port: "+portToConnect)
exit()
try:
toPi=serial.Serial(SERIAL_PORT,115200,timeout=2) #connect to arduino
except:
print("please check connection to arduino and verify the correct serial port")
exit()
gameType= tkGametype.get()
while not stopThread:
data, addr = sock.recvfrom(1024)
if gameType == GameType.BEAMNG.value:
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.value:
unpackedData = struct.unpack(FORZA_METHODS.FORZA_DATA_FORMAT,data)
carData = FORZA_METHODS.unpackData(unpackedData)
kmh = carData["Speed"]*3.6
if csvOut == True:
csvWriteOut(firstRun,carData,csvFile)
firstRun=False
try:
toPi.write(str(kmh).encode()+":".encode())
except:
print("arduino disconnected please check connection\n")
#code runs from here<-----------------------------------------------------------------------------------------------------------
if runningOs == 'Windows':
@ -71,13 +113,20 @@ else:
SERIAL_PORT = 'COM5'
def startStopButtonFunc():
global pushingToArduino
if pushingToArduino == True:
global appState
if appState == ProgramState.WAITNG:
startStopButton.config(text="start")
pushingToArduino = False
elif pushingToArduino == False:
appState = ProgramState.PUSHING_DATA
stopThread=False
workThread = threading.Thread(target=runningThread)
workThread.start()
statusTextVar.set("Waiting...")
elif appState == ProgramState.PUSHING_DATA:
startStopButton.config(text="stop")
pushingToArduino = True
appState = ProgramState.WAITNG
statusTextVar.set("Running...")
stopThread = True
##GUI init
rootFrameGui= tkinter.Tk()
@ -159,23 +208,7 @@ while gameSelected == False:
else:
print("please select a number from the list")
#check everything is connected
while connectedWebSocket == False:
try:
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((UDP_IP,portToConnect))
connectedWebSocket = True
except:
print("please check you are able to open the socket on this system\ntired to open port: "+portToConnect)
exit()
while connectedArduino == False:
try:
toPi=serial.Serial(SERIAL_PORT,115200,timeout=2) #connect to arduino
connectedArduino = True
except:
print("please check connection to arduino and verify the correct serial port")
time.sleep(1)
if csvOut == True:
csvFile = open(gameType.value+"_"+str(int(time.time()))+'.csv',"a")
@ -183,24 +216,5 @@ if csvOut == True:
print("ready:\n")
while True:
data, addr = sock.recvfrom(1024)
if gameType == GameType.BEAMNG:
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)
kmh = carData["Speed"]*3.6
if csvOut == True:
csvWriteOut(firstRun,carData,csvFile)
firstRun=False
try:
toPi.write(str(kmh).encode()+":".encode())
except:
print("arduino disconnected please check connection\n")