Flat Shift with LUA

It's all about the code!
Post Reply
User avatar
NormanAlphaspeed
Posts: 64
Joined: Fri Jan 13, 2017 7:15 am
Location: Puerto Rico
Contact:

Flat Shift with LUA

Post by NormanAlphaspeed »

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:

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
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!
Attachments
image.png
image.png (13.46 KiB) Viewed 10351 times
User avatar
AndreyB
Site Admin
Posts: 14334
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Flat Shift with LUA

Post by AndreyB »

This is golden stuff! Noted on https://github.com/rusefi/rusefi/issues/2467
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
NormanAlphaspeed
Posts: 64
Joined: Fri Jan 13, 2017 7:15 am
Location: Puerto Rico
Contact:

Re: Flat Shift with LUA

Post by NormanAlphaspeed »

I had to combine launch control and flat shift since they both manage ignition and ignition cut

video at https://youtu.be/0HKLx0XIM6s

code:

Code: Select all

flatShiftFlag = false
flatShiftTimer = Timer.new()

launchSwitch = true
launchMode = false
launchRPM = 5000
launchRPMstartWindow = 500
launchStartTimingOffset = 15
launchEndTimingFinal = 35
launchStartIgnCutOffset = 30
launchEndIgnCutFinal = 65
launchStartIgnCutOffset = launchStartIgnCutOffset/100
launchEndIgnCutFinal = launchEndIgnCutFinal/100
launchFuelAdd = 1.2

-- flat shift
	clutchState = readPin("PF10")

	if RPMread / 4 > 6000 and clutchState == 0 and flatShiftFlag == false and driverD > 50 then
		flatShiftFlag = true
		flatShiftTimer : reset()
	end
	-- launch control
	if rvss < 15 and driverD > 15 and launchSwitch == true then
		launchMode = true
	else
		launchMode = false
	end
	flatShiftTime = flatShiftTimer : getElapsedSeconds()
	--print(launchRPM-launchRPMstartWindow)
	if RPMread/4 > (launchRPM - launchRPMstartWindow) and RPMread/4 < launchRPM and launchMode == true then
		distanceToLimitPct = (launchRPM - RPMread/4) / launchRPMstartWindow
		print(launchEndIgnCutFinal -(launchStartIgnCutOffset * distanceToLimitPct))
		setTimingAdd(- launchEndTimingFinal +(launchStartTimingOffset * distanceToLimitPct))
		setSparkHardSkipRatio(launchEndIgnCutFinal -(launchStartIgnCutOffset * distanceToLimitPct))
		setFuelMult(launchFuelAdd)
	elseif RPMread/4 > launchRPM and launchMode == true then
		setTimingAdd(- launchEndTimingFinal)
		setSparkHardSkipRatio(1)
		setFuelMult(1)
	elseif flatShiftTime < flatShiftTotalTimeMs / 5 and clutchState == 0 and flatShiftFlag then
		setSparkHardSkipRatio(.7)
		setTimingAdd(-15)
		setFuelMult(1)
	elseif flatShiftTime >= flatShiftTotalTimeMs / 5 and flatShiftTime < (flatShiftTotalTimeMs / 5) * 4 and clutchState == 0 and flatShiftFlag then
		setSparkHardSkipRatio(.95)
		setTimingAdd(-20)
		setFuelMult(1)
	elseif flatShiftTime >= (flatShiftTotalTimeMs / 5) * 4 and flatShiftTime < (flatShiftTotalTimeMs / 5) * 5 and clutchState == 0 and flatShiftFlag then
		setSparkHardSkipRatio(.75)
		setTimingAdd(-15)
		setFuelMult(1)
	elseif flatShiftTime > (flatShiftTotalTimeMs / 5) * 5 and clutchState == 0 then
		setSparkHardSkipRatio(0)
		setTimingAdd(0)
		setFuelMult(1)
	else
		setSparkHardSkipRatio(0)
		setTimingAdd(0)
		setFuelMult(1)
		flatShiftFlag = false
	end
We now have a distance to cut RPM (distanceToLimitPct ) that changes the amount of retard and cut depending on how far we are from the target launch rpm (linearly only for now :( )

launchSwitch = tthis would ideally be used by a launch switch, since i'm using vss as the switch, this is hardcoded to true
launchMode = this is a compilation of all check to see if we are ready to be in launch mode
launchRPM = come on
launchRPMstartWindow = launch control, erm, controlling starts at launchRPM MINUS this window. etc 3000-500=2500
launchStartTimingOffset = when we enter the launch window, this is ADDED to the timing below, so as timing varies towards negative linearly as RPM goes up
launchEndTimingFinal = final timing REMOVED FROM ACTUAL TIMING TABLE (at 3k rpm, removes 35 degrees in example code)
launchStartIgnCutOffset = when we enter the launch window, this is ADDED to the %cut below, so as cut% varies towards more linearly as RPM goes up
launchEndIgnCutFinal = final percent of cut applied to ignition timing before we run into hard cut
launchStartIgnCutOffset = launchStartIgnCutOffset/100 --changes big numbers to the project leader's favorite decimals
launchEndIgnCutFinal = launchEndIgnCutFinal/100 --changes big numbers to the project leader's favorite decimals
launchFuelAdd = 1.2
Post Reply