added send to arduino
This commit is contained in:
parent
81faf3bd45
commit
883d3fe98a
1 changed files with 53 additions and 1 deletions
|
|
@ -1,11 +1,63 @@
|
||||||
import socket
|
import socket
|
||||||
|
import struct
|
||||||
|
import serial
|
||||||
|
|
||||||
|
toPi=serial.Serial('COM5',115200)
|
||||||
|
|
||||||
|
def decodeFlag(flag):
|
||||||
|
flagBin = str(bin(flag))
|
||||||
|
flags = {"showTurbo":newBool(flagBin[2]),"showKM":not newBool(flagBin[1]),"showBAR":not newBool(flagBin[0])}
|
||||||
|
return flags
|
||||||
|
|
||||||
|
def newBool(string):
|
||||||
|
if(string == "0"):
|
||||||
|
return False
|
||||||
|
if(string == "1"):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def decodeLights(lightsAvailable,lightsActive):
|
||||||
|
lightsAvBin = str(bin(lightsAvailable))[2:][::-1]
|
||||||
|
lightsActBin = str(bin(lightsActive))[2:][::-1]
|
||||||
|
lights = {}
|
||||||
|
totalLights = ["shift_light","full_beam","handbrake","pit_limiter","tc","left_turn","right_turn","both_turns","oil_warn","battery_warn","abs","spare_light"]
|
||||||
|
for i in range(0,12):
|
||||||
|
try:
|
||||||
|
lights[totalLights[i]] = newBool(lightsActBin[i])
|
||||||
|
except Exception:
|
||||||
|
lights[totalLights[i]] = False
|
||||||
|
return lights
|
||||||
|
|
||||||
UDP_IP = "127.0.0.1"
|
UDP_IP = "127.0.0.1"
|
||||||
UDP_PORT = 4444
|
UDP_PORT = 4444
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
||||||
sock.bind((UDP_IP,UDP_PORT))
|
sock.bind((UDP_IP,UDP_PORT))
|
||||||
|
print("waiting for data:\n")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
data, addr = sock.recvfrom(1024)
|
data, addr = sock.recvfrom(1024)
|
||||||
print("received mesage: %s" % data)
|
unpackedData = struct.unpack("I4sHBBfffffffIIfff16s16sxxxx",data)
|
||||||
|
carData = {"time":unpackedData[0],
|
||||||
|
"carName":unpackedData[1].decode("utf-8"),
|
||||||
|
"flags": decodeFlag(unpackedData[2]),
|
||||||
|
"gear": unpackedData[3],
|
||||||
|
"PLID": unpackedData[4],
|
||||||
|
"speed": unpackedData[5],
|
||||||
|
"rpm": unpackedData[6],
|
||||||
|
"turboPressure":unpackedData[7],
|
||||||
|
"engTemp":unpackedData[8],
|
||||||
|
"fuel":unpackedData[9],
|
||||||
|
"oilPressure":unpackedData[10],
|
||||||
|
"oilTemp":unpackedData[11],
|
||||||
|
"lights":decodeLights(unpackedData[12],unpackedData[13]),
|
||||||
|
"throttle": unpackedData[14],
|
||||||
|
"brake": unpackedData[15],
|
||||||
|
"clutch": unpackedData[16],
|
||||||
|
"misc1": unpackedData[17],
|
||||||
|
"misc2": unpackedData[18]
|
||||||
|
}
|
||||||
|
#print("speed: %s" % (carData["speed"]*3.6))
|
||||||
|
kmh=carData["speed"]*3.6
|
||||||
|
toPi.write(str(kmh).encode())
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue