Last active
April 7, 2016 03:28
-
-
Save kosenko-max/758af0ddb0f8e7f5a69b to your computer and use it in GitHub Desktop.
Evo X RaceCapture Pro logging (requires rewrite to consume less memory and be configurable for different demands)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Copyright 2016 Mash at boostedforums.net | |
-- Minify before use by https://mothereff.in/lua-minifier | |
-- Next version: https://gist.github.com/kosenko-max/c5cb086292c287e45004 | |
--TODO: ======================== TEPHRA X MOD V3 LOGGING ==================================== | |
--requestCAN(2016,0x805100,0x1E) | |
--3.2x Higher Precision, larger scale: logs[number_of_logs].load = ((uint)Data[0] * 256 + Data[1]) * 10.0f / 32.0f; | |
--2 times higher precision: logs[number_of_logs].rpm = ((uint)Data[2] * 256 + Data[3]) * 1000.0f / 256.0f; | |
--2.5 times higher precision: if (Omni4BAR) { | |
-- logs[number_of_logs].boost = (((uint)Data[4] * 256 + Data[5]) / 4.0f) * 0.2343f - 14.196f; | |
--} else { | |
-- logs[number_of_logs].boost = (((uint)Data[4] * 256 + Data[5]) / 4.0f) * 0.19347f - 14.5f; | |
--} | |
--8 times higher precision: logs[number_of_logs].mivec_intake = current_mivec_intake = ((int)(4096 - (uint)(Data[6] * 256 + Data[7]))) * 0.01953125f; | |
--8 times higher precision: logs[number_of_logs].mivec_exhaust = current_mivec_exhaust = ((int)(4096 - (uint)(Data[0x8] * 256 + Data[0x9]))) * 0.01953125f; | |
--100 times higher precision: logs[number_of_logs].ipw = ((uint)Data[0xa] * 256 + Data[0xb]) / 1000.0f; | |
--logs[number_of_logs].afr = (Data[0xc] / 255.0f) * AFR_Calc_Multi + AFR_Calc_Adder; | |
--Limited to 255kph?: logs[number_of_logs].speed = Data[0xd]; | |
--logs[number_of_logs].timing = (int)Data[0xe] - 20; | |
--logs[number_of_logs].knocksum = (uint)Data[0xf]; | |
--logs[number_of_logs].tps = Data[0x10] * 100.0f / 255.0f; | |
--logs[number_of_logs].active_wgdc = Data[0x11] / 2.0f; | |
--logs[number_of_logs].ect = (int)Data[0x12] - 40; | |
--logs[number_of_logs].mat = (int)Data[0x13] - 40; | |
--logs[number_of_logs].maf_iat = (int)Data[0x14] - 40; | |
--logs[number_of_logs].stft = (float)(0.1953125f * Data[0x15]) - 25.0f; | |
--logs[number_of_logs].ltft_inuse = (float)(0.1953125f * Data[0x16]) - 25.0f; | |
--logs[number_of_logs].maf_volts = (float)(5.0f * Data[0x17] / 255.0f); | |
--256 times higher resolution, no limits: logs[number_of_logs].map_calcs = ((uint)Data[0x18] * 256 + Data[0x19]) * 100.0f / 16384.0f; | |
--256 times higher resolution, no limits: logs[number_of_logs].maf_calcs = ((uint)Data[0x1a] * 256 + Data[0x1b]) * 100.0f / 16384.0f; | |
--NEW: logs[number_of_logs].knock_base = (uint)Data[0x1c]; | |
--NEW: logs[number_of_logs].knock_filter_adc = (uint)Data[0x1d]; | |
--Lack from RAX: LTFT Idle, LTFT Cruise, AFR Map, LoadTiming, Barometer, Mivec Intake/Exhaust Targets, | |
-- APP, WGDC Correction, ECU Voltage, Load IMAP, Load Chosen, Front O2 Voltage | |
--======================== SST DIAGNOSTICS ==================================== | |
local chSSTTempCl1,chSSTTempCl2,chSSTSlip1,chSSTSlip2,chSSTShaft1,chSSTShaft2 = 0,0,0,0,0,0 | |
local chSSTDamper,chSSTGear,chSSTTarGear,chSSTPress1,chSSTPress2 = 0,0,0,0,0 | |
local chSSTSolPress,STSolCool,chSSTSolShift1,chSSTSolShift2 = 0,0,0,0 | |
local chSSTFork1,chSSTFork2,chSSTFork3,chSSTFork4 = 0,0,0,0 | |
function getSSTDiag() | |
if chSSTTempCl1 == 0 then | |
chSSTTempCl1 = addChannel("SSTTempCl1", 100, 1, -40, 150, "C") | |
chSSTTempCl2 = addChannel("SSTTempCl2", 100, 1, -40, 150, "C") | |
chSSTSlip1 = addChannel("SSTSlip1", 100, 0, -8500, 8500, "RPM") | |
chSSTSlip2 = addChannel("SSTSlip2", 100, 0, -8500, 8500, "RPM") | |
chSSTShaft1 = addChannel("SSTShaft1", 100, 0, 0, 8500, "RPM") | |
chSSTShaft2 = addChannel("SSTShaft2", 100, 0, 0, 8500, "RPM") | |
chSSTDamper = addChannel("SSTDamper", 100, 0, 0, 8500, "RPM") | |
chSSTGear = addChannel("Gear", 100, 0, -1, 6, "Gear") | |
chSSTTarGear = addChannel("SSTTarGear", 100, 0, -1, 6, "Gear") | |
chSSTPress1 = addChannel("SSTPress1", 100, 2, 0, 30, "Bar") | |
chSSTPress2 = addChannel("SSTPress2", 100, 2, 0, 30, "Bar") | |
chSSTSolPress = addChannel("SSTSolPress", 100, 0, 0, 10000, "mA") | |
chSSTSolCool = addChannel("SSTSolCool", 100, 0, 0, 10000, "mA") | |
chSSTSolShift1 = addChannel("SSTSolShift1", 100, 0, 0, 10000, "mA") | |
chSSTSolShift2 = addChannel("SSTSolShift2", 100, 0, 0, 10000, "mA") | |
chSSTFork1 = addChannel("SSTFork1", 100, 0, -50, 50, "mm") | |
chSSTFork2 = addChannel("SSTFork2", 100, 0, -50, 50, "mm") | |
chSSTFork3 = addChannel("SSTFork3", 100, 0, -50, 50, "mm") | |
chSSTFork4 = addChannel("SSTFork4", 100, 0, -50, 50, "mm") | |
end | |
d1 = requestCAN(2017,166) | |
if d1~=nil then | |
setChannel(chSSTSlip1,mBytes(d1[4],d1[5])/2) | |
setChannel(chSSTSlip2,mBytes(d1[6],d1[7])/2) | |
end | |
d1 = requestCAN(2017,170) | |
if d1~=nil then | |
setChannel(chSSTPress1,mBytes(d1[4],d1[5])/500) | |
setChannel(chSSTPress2,mBytes(d1[6],d1[7])/500) | |
end | |
d1 = requestCAN(2017,165) | |
if d1~=nil then | |
setChannel(chSSTTempCl1,mBytes(d1[4],d1[5])/4) | |
setChannel(chSSTTempCl2,mBytes(d1[6],d1[7])/4) | |
end | |
d1 = requestCAN(2017,176) | |
if d1~=nil then | |
setChannel(chSSTShaft1,mBytes(d1[4],d1[5])/2) | |
setChannel(chSSTShaft2,mBytes(d1[6],d1[7])/2) | |
end | |
d1 = requestCAN(2017,178) | |
if d1 ~= nil then setChannel(chSSTDamper,mBytes(d1[4],d1[5])/2) end | |
d1 = requestCAN(2017,54) | |
if d1~=nil then | |
if d1[4]==11 then d1[4] = -1 end | |
setChannel(chSSTGear,d1[4]) | |
end | |
d1 = requestCAN(2017,56) | |
if d1~=nil then | |
if d1[4]==11 then d1[4] = -1 end | |
setChannel(chSSTTarGear,d1[4]) | |
end | |
d1 = requestCAN(2017,175) | |
if d1 ~= nil then setChannel(chSSTSolPress,mBytes(d1[4],d1[5])/16) end | |
d1 = requestCAN(2017,174) | |
if d1 ~= nil then setChannel(chSSTSolCool,mBytes(d1[4],d1[5])/16) end | |
d1 = requestCAN(2017,173) | |
if d1 ~= nil then | |
setChannel(chSSTSolShift1,mBytes(d1[4],d1[5])/16) | |
setChannel(chSSTSolShift1,mBytes(d1[6],d1[7])/16) | |
end | |
d1 = requestCAN(2017,171) | |
if d1 ~= nil then | |
setChannel(chSSTFork1,mBytes(d1[4],d1[5])/256) | |
setChannel(chSSTFork2,mBytes(d1[6],d1[7])/256) | |
end | |
d1 = requestCAN(2017,172) | |
if d1 ~= nil then | |
setChannel(chSSTFork3,mBytes(d1[4],d1[5])/256) | |
setChannel(chSSTFork4,mBytes(d1[6],d1[7])/256) | |
end | |
--TODO: More codes from CBF https://drive.google.com/file/d/0B2hhlN7v68k8dnN6bnVUUkUxeTQ/view | |
-- 04: Engine torque | |
-- 06: Dynamic engine torque | |
-- 07: Expected engine torque | |
-- 08: Maximum possible engine torque | |
-- 09: Minimum engine torque | |
-- 12: Ambient temperature | |
-- 2F: Transmission loss torque | |
-- 2C: Output shaft speed | |
-- 42: Clutch torque: #1, #2, #1 desired, #2 desired | |
-- 43: Clutch pressure: #1, #2, #1 desired, #2 desired | |
-- 44: Acceleration and Deceleration m/sec2 | |
-- B1: Output shaft speed | |
-- B3: Transmission oil temperature | |
-- B4: Mechatronic unit temperature | |
-- B5: Line pressure solenoid | |
-- B6: Clutch cooling flow solenoid | |
-- B7: Clutch shift pressure solenoid CSPS1 | |
-- B8: Clutch shift pressure solenoid CSPS2 | |
-- BA: Clutch 1 boost pressures | |
-- BB: Clutch 2 boost pressures | |
-- BC: Clutches stroke: 1 pressure, 2 pressure, 1 stroke delta pressures (1,2,3), 2 stroke delta pressures (1,2,3) | |
-- C3: Valve CSPS1 and CSPS2 : #1 (0-12), #2 (0-12) | |
-- C4: Valve LPS - part 1: values 0-19 | |
-- C5: Valve LPS - part 2: values 20-32 | |
-- C6: Valve CCFS: Clutch cooling flow solenoid valve #1 (0-8), #2 (0-8), #3(0-2) | |
-- C7: Clutches gain: 1,2 | |
end | |
--======================== SST ================================================ | |
local chSSTTemp, chSSTMode = 0,0 | |
function getSST() | |
--TODO: Make Channels of general slip vs load vs clutch pressure on a current gear | |
if chSSTTemp == 0 then | |
chSSTTemp = addChannel("SSTTemp", 100, 1, -40, 150, "C") | |
chSSTMode = addChannel("SSTMode", 1, 0, 0, 2, "Mode") | |
end | |
d1 = requestCAN(2017,52) | |
if d1~=nil then | |
if d1[4]==3 then setChannel(chSSTMode,0) | |
elseif d1[4]==2 then setChannel(chSSTMode,10) | |
else setChannel(chSSTMode,100) end | |
end | |
d1 = requestCAN(2017,51) | |
--d1[5] is Transmission temperature: Transmission temperature warning | |
if d1~=nil then setChannel(chSSTTemp,d1[4]-50) end | |
end | |
--======================== WHEELS ============================================= | |
local chWheelFL, chWheelFR, chWheelRL, chWheelRR = 0,0,0,0 | |
function getWheels() | |
if chWheelFL == 0 then | |
chWheelFL = addChannel("WheelFL", 100, 1, 0, 3000, "RPM") | |
chWheelFR = addChannel("WheelFR", 100, 1, 0, 3000, "RPM") | |
chWheelRL = addChannel("WheelRL", 100, 1, 0, 3000, "RPM") | |
chWheelRR = addChannel("WheelRR", 100, 1, 0, 3000, "RPM") | |
end | |
d1,d2 = requestCAN(2017,27) | |
if d1==nil or d2==nil then return end | |
setChannel(chRLWheel, d2[4]*128+d2[5]/2) | |
setChannel(chFLWheel, d2[2]*128+d2[3]/2) | |
setChannel(chRRWheel, d1[7]*128+d1[8]/2) | |
setChannel(chFRWheel, d1[5]*128+d1[6]/2) | |
end | |
--======================== STEERING AND BRAKES ================================ | |
local chSteering, chBrakeMC, chBrakeFL, chBrakeFR, chBrakeRL, chBrakeRR = 0,0,0,0,0,0 | |
function getBrakes() | |
if chSteering == 0 then | |
chSteering = addChannel("Steering", 100, 1, -420, 420, "deg") | |
chBrakeMC = addChannel("BrakeMC", 100, 2, 0, 120, "Bar") | |
chBrakeFL = addChannel("BrakeFL", 100, 2, 0, 120, "Bar") | |
chBrakeFR = addChannel("BrakeFR", 100, 2, 0, 120, "Bar") | |
chBrakeRL = addChannel("BrakeRL", 100, 2, 0, 120, "Bar") | |
chBrakeRR = addChannel("BrakeRR", 100, 2, 0, 120, "Bar") | |
end | |
d1,d2,d3 = requestCAN(1924,2) | |
if d1==nil or d2==nil or d3==nil then return end | |
setChannel(chSteering, mBytes(d1[5],d1[6]) / 22.885 ) | |
setChannel(chBrakeMC, mBytes(d1[8],d2[2]) / 100.0 ) | |
setChannel(chBrakeFL, mBytes(d2[6],d2[7]) / 100.0 ) | |
setChannel(chBrakeFR, mBytes(d2[8],d3[2]) / 100.0 ) | |
setChannel(chBrakeRL, mBytes(d3[3],d3[4]) / 100.0 ) | |
setChannel(chBrakeRR, mBytes(d3[5],d3[6]) / 100.0 ) | |
end | |
--======================== RAX MAIN DATA ===================================== | |
local chBarometer,chMAP,chBoost,chWGDC,chMAFVolt = 0,0,0,0,0 | |
local chMIVInTar,chMIVInAct,chMIVExTar,chMIVExAct = 0,0,0,0 | |
local chMIVInErr,chMIVExErr,chMIVActOver,chMIVTarOver = 0,0,0,0 | |
local chTPS,chAPP,chMAFAirTemp,chWGDCCorr = 0,0,0,0 | |
local chSpeed,chECUVolt,chWaterTemp,chInAirTemp,chInAirDiff = 0,0,0,0,0 | |
local chLoadMAF,chLoadMAP,chLoadI_MAP,chLoadChosen = 0,0,0,0 | |
local chFrontO2 = 0 | |
local chF_LTFTIdle,chF_LTFTCur,chF_LTFTCruise,chF_STFT,chF_STFTSum = 0,0,0,0,0 | |
local chAFR,chAFRMap,chAFRDiff,chLoad,chIPW,chIDC = 0,0,0,0,0,0 | |
local chLoadTiming,chTiming,chKnockSum,chRPM = 0,0,0,0 | |
d1,d2,d3,res,id,ext = 0,0,0,0,0 | |
function getRAX() | |
if chF_LTFTIdle == 0 then | |
--RAX A | |
chF_LTFTIdle = addChannel("F_LTFTIdle", 100, 2, -30, 30, "%") | |
chF_LTFTCur = addChannel("F_LTFTCur", 100, 2, -30, 30, "%") | |
chF_LTFTCruise = addChannel("F_LTFTCruise", 100, 2, -30, 30, "%") | |
chF_STFT = addChannel("F_STFT", 100, 2, -30, 30, "%") | |
chF_STFTSum = addChannel("F_STFTSum", 100, 2, -60, 60, "%") | |
--RAX B | |
chAFR = addChannel("AFR", 100, 2, 8, 20, "A/FR") | |
chAFRMap = addChannel("AFRMap", 100, 2, 8, 20, "A/FR") | |
chAFRDiff = addChannel("AFRDiff", 100, 1, -12, 12, "A/FR") | |
chLoad = addChannel("Load", 100, 0, 0, 500, "Load") | |
chIPW = addChannel("IPW", 100, 1, 0, 25.5, "ms") | |
chIDC = addChannel("IDC", 100, 0, 0, 120, "%") | |
--RAX C | |
chLoadTiming = addChannel("LoadTiming", 100, 0, 0, 398, "Load") | |
chTiming = addChannel("Timing", 100, 0, -20, 107, "deg") | |
chKnockSum = addChannel("KnockSum", 100, 0, 0, 63, "counts") | |
chRPM = addChannel("RPM", 100, 0, 0, 8500, "RPM") | |
--RAX D | |
chBarometer = addChannel("Barometer", 100, 2, 6.52, 15.7, "PSIA") | |
chMAP = addChannel("MAP", 100, 2, 0, 58, "PSIA") | |
chBoost = addChannel("Boost", 100, 1, -14.5, 43.5, "PSI") | |
chWGDC = addChannel("WGDC", 100, 1, 0, 100, "%") | |
chMAFVolt = addChannel("MAFVolt", 100, 2, 0, 5, "V") | |
chFuelPressErr = addChannel("FuelPressErr", 100, 1, -30, 30, "PSI") | |
--RAX E | |
chMIVInTar = addChannel("MIVInTar", 100, 1, -2.5, 37.5, "deg") | |
chMIVInAct = addChannel("MIVInAct", 100, 1, -2.5, 37.5, "deg") | |
chMIVExTar = addChannel("MIVExTar", 100, 1, -37.5, 2.5, "deg") | |
chMIVExAct = addChannel("MIVExAct", 100, 1, -37.5, 2.5, "deg") | |
chMIVInErr = addChannel("MIVInErr", 100, 1, -40, 40, "deg") | |
chMIVExErr = addChannel("MIVExErr", 100, 1, -40, 40, "deg") | |
--RAX F | |
chTPS = addChannel("TPS", 100, 1, 0, 100, "%") | |
chAPP = addChannel("APP", 100, 1, 0, 100, "%") | |
chMAFAirTemp = addChannel("MAFAirTemp", 100, 0, -40, 100, "C") | |
chWGDCCorr = addChannel("WGDCCorr", 100, 1, -64, 64, "%") | |
--RAX G | |
chSpeed = addChannel("Speed", 100, 0, 0, 300, "Km/h") | |
chECUVolt = addChannel("ECUVolt", 100, 2, 0, 18.6, "V") | |
chWaterTemp = addChannel("WaterTemp", 100, 0, -40, 150, "C") | |
chInAirTemp = addChannel("InAirTemp", 100, 0, -40, 210, "C") | |
chInAirDiff = addChannel("InAirDiff", 100, 0, -100, 100, "C") | |
--RAX H d | |
chLoadMAF = addChannel("LoadMAF", 100, 0, 0, 398, "Load") | |
chLoadMAP = addChannel("LoadMAP", 100, 0, 0, 398, "Load") | |
chLoadI_MAP = addChannel("LoadI_MAP", 100, 0, 0, 398, "Load") | |
chLoadChosen = addChannel("LoadChosen", 100, 0, 0, 398, "Load") | |
chFrontO2 = addChannel("FrontO2", 100, 2, 0, 1, "V") | |
end | |
local ipw = 0 | |
d1 = requestCAN(2016,0x8051A8,6) | |
if d1 ~= nil then | |
--load*1.5625 | |
setChannel(chLoad,d1[3]*1.5625) | |
--ipw*0.1 | |
ipw = d1[4]*0.1 | |
setChannel(chIPW,ipw) | |
--14.7*128/afrmap | |
local afrMap = 14.7*128/d1[5] | |
setChannel(chAFRMap,afrMap) | |
--afr*0.046872+7.3125 | |
local afr = d1[6]*0.046872+7.3125 | |
setChannel(chAFR,afr) | |
setChannel(chAFRDiff,afr-afrMap) | |
--stft*0.1953125-25 | |
local stft = d1[7]*0.1953125-25 | |
setChannel(chF_STFT,stft) | |
--ltftcurrent*0.1953125-25 | |
local ltft = d1[8]*0.1953125-25 | |
setChannel(chF_LTFTCur,ltft) | |
setChannel(chF_STFTSum,stft+ltft) | |
end | |
d1 = requestCAN(2016,0x8051AE,6) | |
if d1 ~= nil then | |
--ltftidle*0.1953125-25 | |
setChannel(chF_LTFTIdle,d1[3]*0.1953125-25) | |
--ltftcruise*0.1953125-25 | |
setChannel(chF_LTFTCruise,d1[4]*0.1953125-25) | |
--loadtiming*1.5625 | |
setChannel(chLoadTiming,d1[5]*1.5625) | |
--7 bit - timing-20 | |
setChannel(chTiming,m23Bits(d1,25,7)-20) | |
--6 bit - knocksum | |
setChannel(chKnockSum,m23Bits(d1,32,6)) | |
--11 bit - rpm*7.8125 | |
local rpm = m23Bits(d1,38,11)*7.8125 | |
setChannel(chRPM,rpm) | |
setChannel(chIDC,ipw*rpm/1200) | |
end | |
--TODO: Check that Mode23 can pull all data by single request | |
d1 = requestCAN(2016,0x8051B4,6) | |
local mivecInTar = 0 | |
local mivecExTar = 0 | |
if d1 ~= nil then | |
--9bit - map*0.12267775508 | |
local map = m23Bits(d1,17,9)*0.12267775508 | |
setChannel(chMAP,map) | |
--7bit - (barometer+90)*0.07251887 | |
local baro = (m23Bits(d1,26,7)+90)*0.07251887 | |
setChannel(chBarometer,baro) | |
setChannel(chBoost,map-baro) | |
setChannel(chFuelPressErr,getAnalog(fuelPressureChannel)-map+baro-targetFuelPressure) | |
--wgdc/2 | |
setChannel(chWGDC,d1[5]/2.0) | |
--mafvoltage*0.019608 | |
setChannel(chMAFVolt,d1[6]*0.019608) | |
--(MivecInTar-16)*0.15625 | |
mivecInTar = (d1[7]-16)*0.15625 | |
setChannel(chMIVInTar,mivecInTar) | |
--(16-mivecExTar)*0.15625 | |
mivecExTar = (16-d1[8])*0.15625 | |
setChannel(chMIVExTar,mivecExTar) | |
end | |
d1 = requestCAN(2016,0x8051BA,6) | |
local mafAirTemp = 0 | |
if d1 ~= nil then | |
local mivecInAct, mivecExAct | |
--(MivecInAct-16)*0.15625 | |
mivecInAct = (d1[3]-16)*0.15625 | |
setChannel(chMIVInAct, mivecInAct) | |
setChannel(chMIVInErr, mivecInAct - mivecInTar) | |
--(16-mivecExAct)*0.15625 | |
mivecExAct = (16-d1[4])*0.15625 | |
setChannel(chMIVExAct, mivecExAct) | |
setChannel(chMIVExErr, mivecExAct - mivecExTar) | |
--tps*100/255 - TODO: CALIBRATE by MUT3 | |
setChannel(chTPS,d1[5]*100.0/255.0) | |
--(app-32)*129/255 - TODO: CALIBRATE by MUT3 | |
setChannel(chAPP,(d1[6]-32.0)*129.0/255.0) | |
--mafAirTemp-40 | |
mafAirTemp = d1[7]-40 | |
setChannel(chMAFAirTemp,mafAirTemp) | |
--wgdccorr/2-64 | |
setChannel(chWGDCCorr,d1[8]/2-64) | |
end | |
d1 = requestCAN(2016,0x8051C0,6) | |
if d1 ~= nil then | |
--Speed*2 | |
setChannel(chSpeed,d1[3]*2) | |
--ECUVolt*0.07333 | |
setChannel(chECUVolt,d1[4]*0.07333) | |
--WaterTemp-40 | |
setChannel(chWaterTemp,d1[5]-40) | |
--IntakeTemp-40 | |
local inAirTemp = d1[6]-40 | |
setChannel(chInAirTemp,inAirTemp) | |
setChannel(chInAirDiff,inAirTemp-mafAirTemp) | |
--LoadMAP*1.5625 | |
setChannel(chLoadMAP,d1[7]*1.5625) | |
--LoadI_MAP*1.5625 | |
setChannel(chLoadI_MAP,d1[8]*1.5625) | |
end | |
d1 = requestCAN(2016,0x8051C6,2) | |
if d1 ~= nil then | |
--LoadMAF*1.5625 | |
setChannel(chLoadMAF,d1[3]*1.5625) | |
--LoadChosen*1.5625 | |
setChannel(chLoadChosen,d1[4]*1.5625) | |
end | |
d1 = requestCAN(2016,0x808661,1) | |
if d1 ~= nil then | |
--frontO2*0.01952 | |
setChannel(chFrontO2,d1[3]*0.01952) | |
end | |
end | |
-- =========== Byte xtraction from Mode23 CAN ================================= | |
local bits = {0x1,0x3,0x7,0xF,0x1F,0x3F,0x7F, | |
0xFF,0x1FF,0x3FF,0x7FF,0xFFF,0x1FFF, | |
0x3FFF,0x7FFF,0xFFFF,0x1FFFF,0x3FFFF,0x7FFFF, | |
0xFFFFF,0x1FFFFF,0x3FFFFF,0x7FFFFF,0xFFFFFF} | |
local band, bxor, bnot = bit.band, bit.bxor, bit.bnot | |
local lshift, rshift = bit.lshift, bit.rshift | |
function m23Bits(d,startBit,length) | |
local shift = (8 - ((startBit+length-1) % 8)) % 8 | |
local startByte = 3 + (startBit - 1 - ((startBit-1) % 8)) / 8 -- TODO: change to // floor division | |
local bnumb = d[startByte] | |
if (length+shift) > 8 and startByte < 8 then | |
bnumb = d[startByte+1] + lshift(bnumb,8) | |
if (length+shift) > 16 and startByte < 7 then | |
bnumb = d[startByte+2] + lshift(bnumb,8) | |
end | |
end | |
bnumb = rshift(bnumb,shift) | |
bnumb = band(bnumb,bits[length]) | |
return bnumb | |
end | |
--============================ 2 BYTES SIGNED CONVERSION ====================== | |
function mBytes(byte1,byte2) | |
if byte1 > 127 then | |
--TODO:Verify it works proper | |
return 0 - lshift(band(-bnot(0x1000+byte1),0x00FF),8) - band(-bxor(0x1000+byte2,0x0000),0x00FF) - 1 | |
end | |
return lshift(byte1,8) + byte2 | |
end | |
--============================ CAN MESSAGING ================================== | |
function requestCAN(address, pid, bytes) | |
local reqStandard = {2,33,0,255,255,255,255,255} | |
local reqContinue = {48,8,0,255,255,255,255,255} | |
local reqMode23 = {5,35,0,0,0,0,255,255} | |
local to = 100 | |
-- Mode23/Standard reading | |
if pid>255 then | |
reqMode23[3]=rshift(pid,16) | |
reqMode23[4]=band(rshift(pid,8),bits[8]) | |
reqMode23[5]=band(pid,bits[8]) | |
reqMode23[6]=bytes | |
res = txCAN(0, address, 0, reqMode23,to) | |
if res ~= 1 then return nil end | |
else | |
reqStandard[3] = pid | |
res = txCAN(0, address, 0, reqStandard,to) | |
if res ~= 1 then return nil end | |
end | |
-- Single row answer | |
id, ext, d1 = rxCAN(0,to) | |
if id == nil then return nil end | |
if d1[1] < 16 then return d1,nil,nil end | |
-- 2-3 row first answer | |
res = txCAN(0, address, 0, reqContinue,to) | |
if res ~= 1 then return nil end | |
id, ext, d2 = rxCAN(0,to) | |
if id == nil then return nil end | |
if(d1[2]<14) then return d1, d2, nil end | |
-- 3 row answer | |
id, ext, d3 = rxCAN(0,to) | |
if id == nil then return nil end | |
return d1, d2, d3 | |
--TODO: Rewrite to the number of answers based on number of requested bytes and return single array. | |
-- Check how it's done in TephraXMod v3 responses | |
end | |
-- ========================= MAIN CYCLE ======================================= | |
local tickRate = 30 | |
local fuelPressureChannel = 3 | |
local targetFuelPressure = 43.5 | |
local chCANHz = addChannel("CANSpeed", 1, 0, 0, 300, "Hz") | |
initCAN(0, 500000) | |
setTickRate(tickRate) | |
function onTick() | |
-- Release Tick every second to allow refresh of scripts | |
local started = getUptime() | |
local mc = 0 | |
--getSST() | |
repeat | |
--getBrakes() | |
--getWheels() | |
--getSSTDiag() | |
getRAX() | |
mc = mc + 1 | |
until getUptime() > started + 1000 | |
setChannel(chCANHz, mc) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment