Custom cltfactor.inc generator

For discussing how to choose sensors and create a wiring harness for all Bowling and Grippo versions of the MegaSquirt® EFI controller.
Forum rules
Read the manual to see if your question is answered there before posting. Many users will not reply if the answer is already available in the manual.

If your question is about troubleshooting, configuration, or tuning, you MUST include your processor type (MS-I or MS-II) and code version in your post. If your question is about PCB assembly or modifications, you must also include the main board version number (1.01, 2.2 or 3.0).

If you have questions about MS1/Extra or MS2/Extra code configuration or tuning, please post them at www.msextra.com Such questions posted here will be moved to: a temporary MSextra sub-forum, where they will be removed after 7 days

The full forum rules are here: Forum Rules, be sure to read them all regularly.
Post Reply
msoultan
Helpful Squirter
Posts: 58
Joined: Sun Jun 27, 2004 11:04 am
Location: Long Beach, CA - USA

Custom cltfactor.inc generator

Post by msoultan »

For those of you trying to generate custom coolant include files but want more flexibility over the output, this may be of help to you. Please see the USAGE section inside the VBS file for instructions on how to use the generator.

Take the following code and save it in a filed called generatecltfactor.vbs:

Code: Select all

'    Custom Coolant Factor Include File Generator
'    Copyright (C) 2008 - Mike Soultanian - msoultan@csulb.edu
'
'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>.

'***************************************************************
'This file was created with the algorithm used in Megatune 2.25
'***************************************************************

'CHANGELOG
'v1.0 - Release
'v1.1 - Code cleanup and documentation added

'USAGE
'To use this script, you will need to edit the script and change a few values
'in the section below between the BEGIN/END EDITABLE VALUES makers
'
'First thing to do is  select whether or not you are using Celsious.  This is
'pretty straightforward
' 
'The next thing to do is select your failsafe value.  This is the value that is
'used by the MegaSquirt when the sensor fails.  Pick a value that will keep
'the car running so you can "limp" home.  20F, for example, would not be a
'good choice because the MegaSquirt would still think it's in warmup mode.
'
'The next two variables select your temperature range.  The Megasquirt
'defaults to -40F/300F for most engines and -40F/600F for engines using
'the expanded temperature range.  You will want to select values inside
'that range, but a little bit outside of the range of temperatures that you
'think your sensor will encounter.  This will allow for the most resolution
'while also giving you a little headroom should your engine get really hot
'or cold.  It's better to see to what temperature extremes your engine
'reaches instead of railing at a particular value.
'
'To build your table, you need to have three pairs of measured
'temperature/resistance data.   You can get this data by measuring your
'sensor at room temperature, in warm water, and in boiling water.  Try and
'get three values that are as spread apart as possible as that will yield the
'most accurate table.  
'
'The last setting is the bias resistor value.  You will want to pick a value
'for which a known resistor value exists.  Standard values can be found
'here:
'  http://www.elexp.com/t_eia.htm
'or just do a Google search for "standard resistor values".  When I did this,
'I looked at what resistors were available in my assortment pack that I
'purchased from Radio Shack.  Chances are if you're doing this mod, this
'should be pretty straightforward.
'
'Continuing on, pick a resistor value and then run the generator to see what
'the table looks like.  If everything is scrunched up at the top, lower the
'value until you start to see an even number of failsafe values at the top
'and bottom of your table.  Once you find a bias value that works and
'represents a real resistor value, you can generate your final table and
'you're done editing this file.
'
'Now you will want to take the cltfactor.inc file that was generated by this
'script and overwrite the default GM lookup table that is included with the
'MegaSquirt source code.  Recompile the source code and the new coolant
'lookup table will be included in the S19 package.  Upload that file to your
'MegaSquirt and then install a resistor matching the value you selected for
'the bias resistor on your MegaSquirt circuit board and enjoy your new
'setup!
'
'Should you run into any problems with this script, please feel free to email
'me.


'***************************************************************
'**********BEGIN EDITABLE VALUES*****************************
'***************************************************************

'change the following values as you see fit
blnDegressInCelsius = FALSE ' change this to TRUE if you want to use Celsius values


const conFailsafeValue = 350 'failsafe temperature that should be used when the sensor is faulty


const conMinTemp = -40 'minimum temperature
const conMaxTemp = 400 'maximum temperature


'here is where you specify your three measured temp/resistance pairs
'pair 1
const conTemp1 = 7 'Temp 1
const conRes1 = 11600 'Resistance 1

'pair 2
const conTemp2 = 129 'Temp 2
const conRes2 = 703 'Resistance 2

'pair 3
const conTemp3 = 204 'Temp 3
const conRes3 = 207 'Resistance 3


'The value below is what you will be using as your bias resistor, R7.  Larger values
'shifts everything up, smaller value shifts everything down.  Try and get your table
' to be evenly centered in between the failsafe sensor values that you selected above
const conBiasResistorValue = 1000 'R7 coolant bias resistor value


'don't change anything else below here

'***************************************************************
'**********END EDITABLE VALUES*******************************
'***************************************************************


const intADCResolution = 1023 'ADC resolution - 1 - DON'T TOUCH - this should be 1023 for the MSII

'let's convert the temperatures to degrees kelvin
dim t(3)
t(0) = conTemp1
t(1) = conTemp2
t(2) = conTemp3

for i = 0 to 2
	if NOT blnDegressInCelsius then
		t(i) = (t(i)-32.0)*5.0/9.0
	end if
		t(i) = t(i) + 273.15
next

'let's figure out our coefficients
c11 = log(conRes1)
c12 = c11^3
c13 = 1.0 / t(0)

c21 = log(conRes2)
c22 = c21^3
c23 = 1.0 / t(1)

c31 = log(conRes3)
c32 = c31^3.0
c33 = 1.0 / t(2)

C = ((c23-c13) - (c33-c13)*(c21-c11)/(c31-c11)) / ((c22-c12) - (c32-c12)*(c21-c11)/(c31-c11))
B = (c33-c13 - C*(c32-c12)) / (c31-c11)
A = c13 - B*c11 - C*c12


Const ForReading = 1, ForWriting = 2, ForAppending = 8

dim objFS, objFile
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile("cltfactor.inc", ForWriting, True, 0)

'let's output the file header
objFile.WriteLine "//------------------------------------------------------------------------------"
objFile.WriteLine "//--  Custom coolant factor include file generated by"
objFile.WriteLine "//--  Mike Soultanian's Custom CLT VBScript - " & Now() 
objFile.WriteLine "//--  This file was created with the algorithm used in Megatune 2.25"
objFile.WriteLine "//--"
objFile.WriteLine "//--  Bias = " & Round(conBiasResistorValue, 1)
objFile.WriteLine "//--  ADC Resolution = " & intADCResolution + 1
objFile.WriteLine "//--  Temperature range: " & conMinTemp & "F to " & conMaxTemp & "F"
objFile.WriteLine "//--"
objFile.WriteLine "//--  Resistance" & vbTab & vbTab & "tInput"
objFile.WriteLine "//--  ------------" & vbTab & "-----------------"
objFile.WriteLine "//--  " & conRes1 & " ohm" & vbTab & vbTab & Round(t(0),1) & "K" & vbTab & "(" & conTemp1 & IIF(blnDegressInCelsius, "C", "F") & ") " & vbTab & Round(Tu(Tf(conRes1), blnDegressInCelsius), 1)
objFile.WriteLine "//--  " & conRes2 & " ohm" & vbTab & vbTab & Round(t(1),1) & "K" & vbTab & "(" & conTemp2 & IIF(blnDegressInCelsius, "C", "F") & ") " & vbTab & Round(Tu(Tf(conRes2), blnDegressInCelsius), 1)
objFile.WriteLine "//--  " & conRes3 & " ohm" & vbTab & vbTab & Round(t(2),1) & "K" & vbTab & "(" & conTemp3 & IIF(blnDegressInCelsius, "C", "F") & ") " & vbTab & Round(Tu(Tf(conRes3), blnDegressInCelsius), 1)
objFile.WriteLine "//------------------------------------------------------------------------------"
objFile.WriteLine 
objFile.WriteLine "#ifndef GCC_BUILD"
objFile.WriteLine "#pragma ROM_VAR CLT_ROM"
objFile.WriteLine "#endif"
objFile.WriteLine "const int cltfactor_table[1024] LOOKUP_ATTR  = {"
objFile.WriteLine vbTab & vbTab & "//" & vbTab & vbTab & "ADC" & vbTab & vbTab & "Volts" & vbTab & vbTab & "Temp" & vbTab & vbTab & "Ohms"

'this is where the table code starts
for adcCount = 0 to intADCResolution
	res  = conBiasResistorValue / ((intADCResolution / IIf(adcCount = 0 OR adcCount = intADCResolution, 0.01, adcCount)) - 1.0)
	temp = Tf(res)
	if temp <= conMinTemp then
		temp = conFailsafeValue
	elseif temp >= conMaxTemp then
		temp = conFailsafeValue
	end if

	tt = temp*10
	objFile.WriteLine Round(tt) & IIf(adcCount <> intADCResolution, ",", "};") & vbTab & vbTab & "//" & vbTab & vbTab & adcCount & vbTab & vbTab & Round(5.0*adcCount/intADCResolution,2) & vbTab & vbTab & Round(Tu(tt/10.0, blnDegressInCelsius),1) & vbTab & vbTab & Round(res,1)
next

'output the table footer and we're done!
objFile.WriteLine "#ifndef GCC_BUILD"
objFile.WriteLine "#pragma ROM_VAR DEFAULT"
objFile.WriteLine "#endif"

objFile.Close


'function declarations
function iif(psdStr, trueStr, falseStr)
  if psdStr then
    iif = trueStr
  else 
    iif = falseStr
  end if
end function

function k2f(t)
	k2f = (t * 9.0/5.0) - 459.67
end function

function Tk (R)
	Tk = 1.0 / (A + B*log(R) + C*(log(R))^3)
end function

function Tf (R)
	Tf = k2f(Tk(R))
end function

function Tu (f, blnDegressInCelsius)
	if blnDegressInCelsius then
		Tu = ((f-32.0)*5.0/9.0)
	else
		Tu = f
	end if
end function

Create a batch filed called generatecltfactor.bat with the following contents and save it in the same folder as the vbs file:

Code: Select all

cscript generatecltfactor.vbs
After you run the bat file, it'll spit out a new cltfactor.inc file.

Let me know if you have any questions.

enjoy!
Mike
msoultan
Helpful Squirter
Posts: 58
Joined: Sun Jun 27, 2004 11:04 am
Location: Long Beach, CA - USA

Re: Custom cltfactor.inc generator

Post by msoultan »

Sounds great. I will keep you posted if I make updates to the generator.

thanks!
Mike
msoultan
Helpful Squirter
Posts: 58
Joined: Sun Jun 27, 2004 11:04 am
Location: Long Beach, CA - USA

Re: Custom cltfactor.inc generator

Post by msoultan »

Hey Lance,
Is there any reason that this script can't also be used to generate the matfactor lookup table as well? These types of sensors all use a similar curve, right?

Someone asked about getting the bias resistor value for the air temp sensor and I was going to point him in this direction. Heck, I could even tweak the script to spit out the matfactor file as well...

Thanks!
Mike
teng169
MegaSquirt Newbie
Posts: 6
Joined: Wed Aug 06, 2008 3:41 am

Re: Custom cltfactor.inc generator

Post by teng169 »

Hi, This could be a stupid question, but I do want to know:
What is the difference between this generator and the Thermistor Talbe generator in the Megatune 2.25?
Currently, I use that thermistor to generate the table, but the readout of the Coolant sensor doesn't reflect the real temperature.
When the Coolant is 21 oC, the read out is showing 13 oC.
What I enter intor the Table generator is
Bias Resistor: 2000 ohms and my board do have 2000 ohms Resistor on.
1.1 oC / 5260 ohms
27.3 oC / 1980 ohms
79.3 oC/ 380 ohms
then click "OK" to burn into the flash.

But when I found the readout is wrong, I swap the CLT sensor with a 5K Variable resistor.
I got below data.
104 oC/ resistor 161 ohms / Vref: 4.87v / V input on pin 29 of MSII process: 0.37V / it is nearly matched the calculation: 4.87/(2000+161)*161=0.363V

79 oC/ resistor 292 ohms / Vref: 4.87v / V input on pin 29 of MSII process: 0.62V / it is exactly matched the calculation: 4.87/(2000+292)*292=0.62V

Did above data show that the circuit is right but the Thermistor Table Generator is wrong?
msoultan
Helpful Squirter
Posts: 58
Joined: Sun Jun 27, 2004 11:04 am
Location: Long Beach, CA - USA

Re: Custom cltfactor.inc generator

Post by msoultan »

I haven't had a chance to look at this. The only difference with my script are the limits that are used. That might be why you're getting a different table. If you graph the two, they should look the same.

Mike
Post Reply