Flat Shift with LUA
Posted: Sat Dec 30, 2023 11:23 pm
Hi again, I have made a LUA script for flat shift, which uses a configurable timer retard/cut ignition (with three stages!) when a shift is initiated.
Code is as follows:
Variables are:
clutchState = the current state of the clutch (usually up, since best results are achieved when you detect the moment the clutch should start disengaging
flatShiftTimer = a timer that counts from the moment the clutch is pressed up to ->
flatShiftTotalTimeMs = the total time a shift should take; i do math to convert secs to ms since my brain thinks in ms when doing these kinds of things
flatShiftFlag = lets us know that we should be doing flat shift stuff, and monitors if the users has taken longer than the expected time to restart full ignition regardless of clutch state (without this, it would begin the timer again if the shift took longer)
RPMread = current RPM
the rest is generic Lua. I use three stages to make the transition into/out of flat shift smoother, and it works wonders.
In this pic we can see when the shift is initiated, timing is reduced by 15 degrees for 50ms, by 20 for 150ms, and 15 again for 50 more ms, which is a usual entry->shift->exit flat shift cycle. Tested on real car and works good!
Code is as follows:
Code: Select all
flatShiftTimer = Timer.new()
flatShiftFlag = false
flatShiftTotalTimeMs = 250
flatShiftTotalTimeMs = flatShiftTotalTimeMs / 1000
-- flat shift
clutchState = readPin("PF10")
if (RPMread) > 2000 and clutchState == 0 and flatShiftFlag == false and driverD > 50 then
flatShiftFlag = true
flatShiftTimer : reset()
end
flatShiftTime = flatShiftTimer : getElapsedSeconds()
-- print(flatShiftTime)
if flatShiftTime < flatShiftTotalTimeMs / 5 and clutchState == 0 and flatShiftFlag then
setSparkHardSkipRatio(.7)
setTimingAdd(-15)
elseif flatShiftTime >= flatShiftTotalTimeMs / 5 and flatShiftTime < (flatShiftTotalTimeMs / 5) * 4 and clutchState == 0 and flatShiftFlag then
setSparkHardSkipRatio(.95)
setTimingAdd(-20)
elseif flatShiftTime >= (flatShiftTotalTimeMs / 5) * 4 and flatShiftTime < (flatShiftTotalTimeMs / 5) * 5 and clutchState == 0 and flatShiftFlag then
setSparkHardSkipRatio(.75)
setTimingAdd(-15)
elseif flatShiftTime > (flatShiftTotalTimeMs / 5) * 5 and clutchState == 0 then
setSparkHardSkipRatio(0)
setTimingAdd(0)
else
setSparkHardSkipRatio(0)
setTimingAdd(0)
flatShiftFlag = false
end
clutchState = the current state of the clutch (usually up, since best results are achieved when you detect the moment the clutch should start disengaging
flatShiftTimer = a timer that counts from the moment the clutch is pressed up to ->
flatShiftTotalTimeMs = the total time a shift should take; i do math to convert secs to ms since my brain thinks in ms when doing these kinds of things
flatShiftFlag = lets us know that we should be doing flat shift stuff, and monitors if the users has taken longer than the expected time to restart full ignition regardless of clutch state (without this, it would begin the timer again if the shift took longer)
RPMread = current RPM
the rest is generic Lua. I use three stages to make the transition into/out of flat shift smoother, and it works wonders.
In this pic we can see when the shift is initiated, timing is reduced by 15 degrees for 50ms, by 20 for 150ms, and 15 again for 50 more ms, which is a usual entry->shift->exit flat shift cycle. Tested on real car and works good!